exec_outcome stringclasses 1 value | code_uid stringlengths 32 32 | file_name stringclasses 111 values | prob_desc_created_at stringlengths 10 10 | prob_desc_description stringlengths 63 3.8k | prob_desc_memory_limit stringclasses 18 values | source_code stringlengths 117 65.5k | lang_cluster stringclasses 1 value | prob_desc_sample_inputs stringlengths 2 802 | prob_desc_time_limit stringclasses 27 values | prob_desc_sample_outputs stringlengths 2 796 | prob_desc_notes stringlengths 4 3k ⌀ | lang stringclasses 5 values | prob_desc_input_from stringclasses 3 values | tags listlengths 0 11 | src_uid stringlengths 32 32 | prob_desc_input_spec stringlengths 28 2.37k ⌀ | difficulty int64 -1 3.5k ⌀ | prob_desc_output_spec stringlengths 17 1.47k ⌀ | prob_desc_output_to stringclasses 3 values | hidden_unit_tests stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | b7827e040105187f94a78490e798c444 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static PrintWriter out;
static Reader in;
public static void main(String[] args) throws Exception {
input_output();
Main solver = new Main();
solver.solve();
out.close();
out.flush();
}
static int INF = (int)1e9;
static int MAXN = (int)2e6 + 5;
static int q, t, n, m, k;
static boolean[][] vis;
void solve() throws Exception {
t = in.nextInt();
while (t --> 0) {
n = in.nextInt();
m = in.nextInt();
vis = new boolean[n][m];
char[][] move = new char[n][m];
for (int i = 0; i < n; i++) {
String s = in.next();
for (int j = 0; j < m; j++) {
move[i][j] = s.charAt(j);
}
}
int[][] cnt = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!vis[i][j]) {
simulate(i, j, move, cnt);
}
}
}
int x = 1, y = 1, max = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
//out.println(i+" "+j+" "+cnt[i][j]);
if (cnt[i][j] > max) {
x = i+1;
y = j+1;
max = cnt[i][j];
}
}
}
out.println(x+" "+y+" "+max);
}
}
static void simulate(int x, int y, char[][] move, int[][] cnt) {
List<Point> arr = new ArrayList<>();
int curX = x, curY = y;
boolean wall = false; // hit a visited cell before crash.
while (curX >= 0 && curY >= 0 && curX < n && curY < m) {
arr.add(new Point(curX, curY));
if (vis[curX][curY]) {
wall = true;
break;
}
vis[curX][curY] = true;
if (move[curX][curY] == 'U') curX--;
else if (move[curX][curY] == 'D') curX++;
else if (move[curX][curY] == 'R') curY++;
else curY--;
}
//out.println("hey "+arr.size());
boolean cycle = checkIfCycle(arr);
//out.println(cycle+" "+wall);
if (cycle) {
int count = countCycle(arr);
boolean cons = false;
for (int i = 0; i < arr.size()-1-count; i++) {
Point p = arr.get(i);
cnt[p.x][p.y] = arr.size()-1-i;
}
for (int i = arr.size()-1-count; i < arr.size()-1; i++) {
Point p = arr.get(i);
cnt[p.x][p.y] = count;
}
} else {
if (wall) {
Point last = arr.get(arr.size()-1);
int add = cnt[last.x][last.y];
for (int i = 0; i < arr.size()-1; i++) {
Point p = arr.get(i);
cnt[p.x][p.y] = arr.size()-1-i+add;
}
} else {
for (int i = 0; i < arr.size(); i++) {
Point p = arr.get(i);
cnt[p.x][p.y] = arr.size()-i;
}
}
}
}
static boolean checkIfCycle(List<Point> arr) {
Point a = arr.get(arr.size()-1);
for (int i = 0; i < arr.size()-1; i++) {
if (arr.get(i).x == a.x && arr.get(i).y == a.y) return true;
}
return false;
}
static int countCycle(List<Point> arr) {
Point a = arr.get(arr.size()-1);
for (int i = 0; i < arr.size()-1; i++) {
if (arr.get(i).x == a.x && arr.get(i).y == a.y) {
return arr.size()-1-i;
}
}
return -1;
}
public static class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public Reader() {
this(System.in);
}
public Reader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String next() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
static void input_output() throws IOException {
File f = new File("in.txt");
if (f.exists() && !f.isDirectory()) {
in = new Reader(new FileInputStream("in.txt"));
} else in = new Reader();
f = new File("out.txt");
if (f.exists() && !f.isDirectory()) {
out = new PrintWriter(new File("out.txt"));
} else out = new PrintWriter(System.out);
}
}
| Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | c5626ac10b74a2e62e79e94dacb48853 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.io.*;
import java.util.*;
public class BMain {
public static void main(String[] args) {
QuickReader in = new QuickReader(System.in);
try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));)
{
new BMain().solve(in, out);
}
}
int n,m;
public void solve(QuickReader in, PrintWriter out)
{
int T = in.nextInt();
while(T-->0)
{
n = in.nextInt();
m = in.nextInt();
char[][] b = new char[n][];
for(int i=0;i<n;i++)
b[i] = in.next().toCharArray();
int last = n*m;
int[] to = new int[n*m+1];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(b[i][j] == 'L')
to[getId(i,j)] = (j>0)? getId(i,j-1) : last;
else if(b[i][j] == 'R')
to[getId(i,j)] = (j<m-1)? getId(i,j+1) : last;
else if(b[i][j] == 'U')
to[getId(i,j)] = (i>0)? getId(i-1,j) : last;
else
to[getId(i,j)] = (i<n-1)? getId(i+1,j) : last;
}
to[last] = last;
int[] used = new int[n*m+1];
int[] resLen = new int[n*m+1];
for(int i=0;i<=n*m;i++)
resLen[i] = -1;
for(int i=0;i<=n*m;i++)
if(used[i] == 0)
{
int cur = i;
while(used[cur] == 0)
{
used[cur] = 1;
cur = to[cur];
}
if( used[cur] == 1 )
{
int start = cur;
int len = 0;
do
{
cur = to[cur];
len++;
}
while(cur != start);
do
{
resLen[cur] = len;
cur = to[cur];
}
while(cur != start);
}
cur = i;
while(used[cur] == 1)
{
used[cur] = 2;
cur = to[cur];
}
}
resLen[last] = 0;
int[] seq = new int[n*m+1];
for(int i=0;i<n*m;i++)
{
if(resLen[i] == -1)
{
int cur = i;
int pos = 0;
while(resLen[cur] == -1)
{
seq[pos++] = cur;
cur = to[cur];
}
for(int j=pos-1;j>=0;j--)
resLen[seq[j]] = resLen[to[seq[j]]]+1;
}
}
int res = 1;
int resI = 1;
int resJ = 1;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(res < resLen[getId(i,j)])
{
res = resLen[getId(i, j)];
resI = i+1;
resJ = j+1;
}
out.println(resI + " " + resJ + " " + res);
}
}
public int getId(int r, int c)
{
return r*m + c;
}
}
class QuickReader
{
BufferedReader in;
StringTokenizer token;
public QuickReader(InputStream ins)
{
in=new BufferedReader(new InputStreamReader(ins));
token=new StringTokenizer("");
}
public boolean hasNext()
{
while (!token.hasMoreTokens())
{
try
{
String s = in.readLine();
if (s == null) return false;
token = new StringTokenizer(s);
} catch (IOException e)
{
throw new InputMismatchException();
}
}
return true;
}
public String next()
{
hasNext();
return token.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public int[] nextInts(int n)
{
int[] res = new int[n];
for (int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
public long nextLong() {
return Long.parseLong(next());
}
}
| Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 1c3ebc97c9e7271ad538b9adfe8057d2 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.io.*;
import java.util.*;
public class CF1607F extends PrintWriter {
CF1607F() { super(System.out); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1607F o = new CF1607F(); o.main(); o.flush();
}
void main() {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
byte[][] cc = new byte[n][];
for (int i = 0; i < n; i++)
cc[i] = sc.next().getBytes();
int[][] dd = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (dd[i][j] > 0)
continue;
int i_ = i, j_ = j, k = 0;
while (i_ >= 0 && i_ < n && j_ >= 0 && j_ < m && dd[i_][j_] == 0) {
dd[i_][j_] = -1;
k++;
if (cc[i_][j_] == 'L')
j_--;
else if (cc[i_][j_] == 'R')
j_++;
else if (cc[i_][j_] == 'U')
i_--;
else
i_++;
}
if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < m && dd[i_][j_] != -1)
k += dd[i_][j_];
int io = i_, jo = j_;
i_ = i; j_ = j;
while (i_ != io || j_ != jo) {
dd[i_][j_] = k--;
if (cc[i_][j_] == 'L')
j_--;
else if (cc[i_][j_] == 'R')
j_++;
else if (cc[i_][j_] == 'U')
i_--;
else
i_++;
}
if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < m)
while (dd[i_][j_] == -1) {
dd[i_][j_] = k;
if (cc[i_][j_] == 'L')
j_--;
else if (cc[i_][j_] == 'R')
j_++;
else if (cc[i_][j_] == 'U')
i_--;
else
i_++;
}
}
int d_ = 0, i_ = -1, j_ = -1;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
int d = dd[i][j];
if (d_ < d) {
d_ = d;
i_ = i;
j_ = j;
}
}
i_++;
j_++;
println(i_ + " " + j_ + " " + d_);
}
}
}
| Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 6abdd0302899fa71e54246161ed68bec | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.util.*;
import java.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class Round753F {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
// the code for the deep recursion (more than 100k or 3~400k or so)
// Thread t = new Thread(null, new RoundEdu130F(), "threadName", 1<<28);
// t.start();
// t.join();
Round753F sol = new Round753F();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = true;
initIO(isFileIO);
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
if(isDebug){
out.printf("Test %d\n", i);
}
getInput();
solve();
printOutput();
}
in.close();
out.close();
}
// use suitable one between matrix(2, n), matrix(n, 2), transposedMatrix(2, n) for graph edges, pairs, ...
int n, m;
String[] board;
void getInput() {
n = in.nextInt();
m = in.nextInt();
board = in.nextStringArray(n);
}
void printOutput() {
out.print(r+1);
out.print(' ');
out.print(c+1);
out.print(' ');
out.println(d);
}
int[][] dp;
int r, c, d;
void solve(){
dp = new int[n][m];
d = 0;
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(dp[i][j] > 0)
continue;
ArrayList<Pair> trace = new ArrayList<>();
int currI = i;
int currJ = j;
while(true) {
if(dp[currI][currJ] == -1) {
int len = trace.size();
int base = 0;
int to = -1;
Pair curr = new Pair(currI, currJ);
for(int k=len-1; k>=0; k--) {
if(trace.get(k).equals(curr)) {
base = len-k;
to = k;
break;
}
}
for(int k=len-1; k>=to; k--)
dp[trace.get(k).first][trace.get(k).second] = base;
for(int k=to-1; k>=0; k--)
dp[trace.get(k).first][trace.get(k).second] = ++base;
break;
}
else if(dp[currI][currJ] > 0) {
int base = dp[currI][currJ];
int len = trace.size();
for(int k=len-1; k>=0; k--)
dp[trace.get(k).first][trace.get(k).second] = len-k + base;
break;
}
dp[currI][currJ] = -1;
trace.add(new Pair(currI, currJ));
int nextI = currI;
int nextJ = currJ;
switch(board[currI].charAt(currJ)){
case 'L':
nextJ--;
break;
case 'R':
nextJ++;
break;
case 'U':
nextI--;
break;
case 'D':
nextI++;
break;
}
if(0 <= nextI && nextI < n && 0 <= nextJ && nextJ < m) {
currI = nextI;
currJ = nextJ;
}
else {
int len = trace.size();
for(int k=len-1; k>=0; k--)
dp[trace.get(k).first][trace.get(k).second] = len-k;
break;
}
}
if(dp[i][j] > d) {
d = dp[i][j];
r = i;
c = j;
}
}
}
}
// Optional<T> solve()
// return Optional.empty();
static class Pair implements Comparable<Pair>{
final static long FIXED_RANDOM = System.currentTimeMillis();
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int hashCode() {
// http://xorshift.di.unimi.it/splitmix64.c
long x = first;
x <<= 32;
x += second;
x += FIXED_RANDOM;
x += 0x9e3779b97f4a7c15l;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9l;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebl;
return (int)(x ^ (x >> 31));
}
@Override
public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
Pair other = (Pair) obj;
return first == other.first && second == other.second;
}
@Override
public String toString() {
return "[" + first + "," + second + "]";
}
@Override
public int compareTo(Pair o) {
int cmp = Integer.compare(first, o.first);
return cmp != 0? cmp: Integer.compare(second, o.second);
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[][] nextTransposedMatrix(int n, int m){
return nextTransposedMatrix(n, m, 0);
}
int[][] nextTransposedMatrix(int n, int m, int offset){
int[][] mat = new int[m][n];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[j][i] = nextInt()+offset;
}
}
return mat;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
String[] nextStringArray(int len) {
String[] s = new String[len];
for(int i=0; i<len; i++)
s[i] = next();
return s;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
// public <T> void printlnAns(Optional<T> ans) {
// if(ans.isEmpty())
// println(-1);
// else
// printlnAns(ans.get());
// }
public void printlnAns(OptionalInt ans) {
println(ans.orElse(-1));
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean[] ans) {
for(boolean b: ans)
printlnAns(b);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][--outDegree[u]] = v;
inNeighbors[v][--inDegree[v]] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][--degree[u]] = v;
neighbors[v][--degree[v]] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
| Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 17 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 19ab44a149b047505153fe6c5f2a0980 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.io.*;
public class robot_board_2 {
static final int MAX = 2000;
static int N, M;
static int visited[][] = new int[MAX][MAX];
static int time[][] = new int[MAX][MAX];
static char map[][] = new char[MAX][MAX];
static int path_row[] = new int[MAX * MAX + 1];
static int path_col[] = new int[MAX * MAX + 1];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
while (T-- > 0) {
br.readLine();
String[] line1 = br.readLine().split(" ");
N = Integer.parseInt(line1[0]);
M = Integer.parseInt(line1[1]);
for (int i = 0; i < N; i++) {
String line = br.readLine();
for (int j = 0; j < M; j++) {
visited[i][j] = 0;
time[i][j] = 0;
map[i][j] = line.charAt(j);
}
}
int best_row = 0;
int best_col = 0;
for (int y = 0; y < N; y++) {
for (int x = 0; x < M; x++) {
if (time[y][x] > 0) { continue; }
calc(y, x);
if (time[y][x] > time[best_row][best_col]) { best_row = y; best_col = x; }
}
}
System.out.println((best_row + 1) + " " + (best_col + 1) + " " + time[best_row][best_col]);
}
}
static void calc(int row, int col) {
int counter = 1;
path_calc:
while (true) {
path_row[counter] = row;
path_col[counter] = col;
if (time[row][col] > 0) { counter--; break path_calc; }
else { visited[row][col] = counter++; }
int nxt_row = row;
int nxt_col = col;
switch (map[row][col]) {
case 'U': nxt_row--; break;
case 'D': nxt_row++; break;
case 'L': nxt_col--; break;
case 'R': nxt_col++; break;
}
if (nxt_row < 0 || nxt_col < 0 || nxt_row >= N || nxt_col >= M) {
time[row][col] = 1;
visited[row][col] = 0;
counter -= 2;
break path_calc;
} else if (visited[nxt_row][nxt_col] > 0) {
time[row][col] = visited[row][col] - visited[nxt_row][nxt_col] + 1;
int cycle_start = visited[nxt_row][nxt_col];
while (--counter >= cycle_start) {
time[path_row[counter]][path_col[counter]] = time[row][col];
visited[path_row[counter]][path_col[counter]] = 0;
}
break path_calc;
}
row = nxt_row;
col = nxt_col;
}
for (int i = counter; i >= 1; i--) {
time[path_row[i]][path_col[i]] = 1 + time[path_row[i + 1]][path_col[i + 1]];
visited[path_row[i]][path_col[i]] = 0;
}
}
} | Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 8 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 2fead3d34d17b47e3ac51bbb8e30a61c | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class TaskB {
static long mod = 1000000007;
static FastScanner scanner;
static final StringBuilder result = new StringBuilder();
static char[][] matrix = new char[2000][2000];
static int[][] path = new int[2000][2000];
public static void main(String[] args) {
// 2 : 1000000000
scanner = new FastScanner();
int T = scanner.nextInt();
for (int t = 0; t < T; t++) {
solve(t + 1);
result.append("\n");
}
System.out.println(result);
}
static void solve(int t) {
int n = scanner.nextInt();
int m = scanner.nextInt();
// char[][] matrix = new char[n][m];
// int[][] path = new int[n][m];
for (int i = 0; i < n; i++) {
String s = scanner.nextToken();
for (int j = 0; j < m; j++) {
matrix[i][j] = s.charAt(j);
path[i][j] = 0;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (path[i][j] == 0) {
walk2(i, j, n, m);
}
}
}
int max = 0;
int r = 0;
int c = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (path[i][j] > max) {
max = path[i][j];
r = i +1;
c = j+1;
}
}
}
result.append(r + " " + c + " " +max);
}
static int[] stack = new int[8000500];
static int stackIdx = 0;
static void walk2(int startI, int startJ, int n, int m) {
stackIdx = 0;
stack[stackIdx++] = startI * 10000 + startJ;
while (stackIdx != 0) {
Integer crt = stack[--stackIdx];
int i = (crt / 10000) % 10000;
int j = crt % 10000;
int idx = crt / 100000000;
int nxt = nextStep(i, j, matrix[i][j]);
int nxtI = nxt / 10000;
int nxtJ = nxt % 10000;
if (idx == 0) {
if (path[i][j] > 0) {
continue;
}
if (path[i][j] == -1) {
// detect cycle
int steps = detectCycle(nxtI, nxtJ, i, j, matrix);
markCycle(nxtI, nxtJ, i, j, matrix, path, steps);
continue;
}
path[i][j] = -1;
if (nxtI < 0 || nxtI >= n || nxtJ < 0 || nxtJ >= m) {
path[i][j] = 1;
continue;
}
stack[stackIdx++] = crt + 100000000;
stack[stackIdx++] = (nxt);
}
if (idx == 1) {
if (path[i][j] == -1) {
path[i][j] = path[nxtI][nxtJ] + 1;
}
}
}
}
static int detectCycle(int i, int j, int targetI, int targetJ, char[][] matrix) {
int steps = 1;
while (!(i == targetI && j == targetJ)) {
int nxt = nextStep(i, j, matrix[i][j]);
i = nxt / 10000;
j = nxt % 10000;
steps++;
}
return steps;
}
static void markCycle(int i, int j, int targetI, int targetJ, char[][] matrix, int[][] path, int steps) {
while (!(i == targetI && j == targetJ)) {
path[i][j] = steps;
int nxt = nextStep(i, j, matrix[i][j]);
i = nxt / 10000;
j = nxt % 10000;
}
path[i][j] = steps;
}
static int nextStep(int i, int j, char c) {
switch (c) {
case 'L':
return i * 10000 + j - 1;
case 'R':
return i * 10000 + j + 1;
case 'U':
return (i - 1) * 10000 + j;
case 'D':
return (i + 1) * 10000 + j;
}
return -1;
}
static class WithIdx implements Comparable<WithIdx> {
int val, idx;
public WithIdx(int val, int idx) {
this.val = val;
this.idx = idx;
}
@Override
public int compareTo(WithIdx o) {
if (val == o.val) {
return Integer.compare(idx, o.idx);
}
return Integer.compare(val, o.val);
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
int[] nextIntArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
long[] nextLongArray(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) res[i] = nextLong();
return res;
}
String[] nextStringArray(int n) {
String[] res = new String[n];
for (int i = 0; i < n; i++) res[i] = nextToken();
return res;
}
}
static class PrefixSums {
long[] sums;
public PrefixSums(long[] sums) {
this.sums = sums;
}
public long sum(int fromInclusive, int toExclusive) {
if (fromInclusive > toExclusive) throw new IllegalArgumentException("Wrong value");
return sums[toExclusive] - sums[fromInclusive];
}
public static PrefixSums of(int[] ar) {
long[] sums = new long[ar.length + 1];
for (int i = 1; i <= ar.length; i++) {
sums[i] = sums[i - 1] + ar[i - 1];
}
return new PrefixSums(sums);
}
public static PrefixSums of(long[] ar) {
long[] sums = new long[ar.length + 1];
for (int i = 1; i <= ar.length; i++) {
sums[i] = sums[i - 1] + ar[i - 1];
}
return new PrefixSums(sums);
}
}
static class ADUtils {
static void sort(int[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
Arrays.sort(ar);
}
static void reverse(int[] arr) {
int last = arr.length / 2;
for (int i = 0; i < last; i++) {
int tmp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = tmp;
}
}
static void sort(long[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
long a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
Arrays.sort(ar);
}
}
static class MathUtils {
static long[] FIRST_PRIMES = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013,
1019, 1021, 1031, 1033, 1039, 1049, 1051};
static long[] primes(int to) {
long[] all = new long[to + 1];
long[] primes = new long[to + 1];
all[1] = 1;
int primesLength = 0;
for (int i = 2; i <= to; i++) {
if (all[i] == 0) {
primes[primesLength++] = i;
all[i] = i;
}
for (int j = 0; j < primesLength && i * primes[j] <= to && all[i] >= primes[j];
j++) {
all[(int) (i * primes[j])] = primes[j];
}
}
return Arrays.copyOf(primes, primesLength);
}
static long modpow(long b, long e, long m) {
long result = 1;
while (e > 0) {
if ((e & 1) == 1) {
/* multiply in this bit's contribution while using modulus to keep
* result small */
result = (result * b) % m;
}
b = (b * b) % m;
e >>= 1;
}
return result;
}
static long submod(long x, long y, long m) {
return (x - y + m) % m;
}
static long modInverse(long a, long m) {
long g = gcdF(a, m);
if (g != 1) {
throw new IllegalArgumentException("Inverse doesn't exist");
} else {
// If a and m are relatively prime, then modulo
// inverse is a^(m-2) mode m
return modpow(a, m - 2, m);
}
}
static public long gcdF(long a, long b) {
while (b != 0) {
long na = b;
long nb = a % b;
a = na;
b = nb;
}
return a;
}
}
}
/*
5
3 2 3 8 8
2 8 5 10 1
*/ | Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 8 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | d096497acdefb165a27154c4d7881571 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.util.Scanner;
import java.util.Stack;
public class RobotOnTheBoard2Stack {
public static class Node {
int d = -1;
int turnVisited;
public Node (int turnVisited){
this.turnVisited = turnVisited;
}
}
public static void calculateD(Node[][] d, char[][] dir, int y, int x){
Stack<Node> stack = new Stack<>();
int turn = 0;
int loopLeft = 0;
int distance = 0;
// push
while(true){
// base case 1: off the board
if(y >= d.length || x >= d[0].length || y<0 || x<0) {
stack.peek().d = 1; // optional
break;
}
Node node = d[y][x];
if(node == null) {
// recursive case
Node newNode = new Node(turn);
d[y][x] = newNode;
stack.push(newNode);
char c = dir[y][x];
if(c == 'R') x += 1;
else if (c == 'L') x -= 1;
else if (c == 'U') y -= 1;
else if (c == 'D') y += 1;
turn++;
} else {
// base case 2: loop found
if(node.d == -1){
distance = turn - node.turnVisited;
loopLeft = distance;
}
// base case 3: value already calculated
else {
distance = node.d;
}
break;
}
}
// pop
while(!stack.isEmpty()){
Node node = stack.pop();
if(loopLeft == 0){
distance++;
} else {
loopLeft--;
}
node.d = distance;
}
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int N = s.nextInt();
s.nextLine();
for(int w=0; w<N; w++){
s.nextLine();
int n = s.nextInt(); // rows
int m = s.nextInt(); // cols
s.nextLine();
char[][] dir = new char[n][m];
Node[][] d = new Node[n][m];
for(int t=0; t<n; t++){
dir[t] = s.nextLine().toCharArray();
d[t] = new Node[m];
}
for(int y=0; y<n; y++){
for(int x=0; x<m; x++){
calculateD(d, dir, y, x);
}
}
int maxLength = -1;
int resX = -1;
int resY = -1;
for(int y=0; y<n; y++){
for(int x=0; x<m; x++){
Node node = d[y][x];
if(node.d > maxLength) {
maxLength = node.d;
resX = x;
resY = y;
}
// System.out.print(node.d+" ");
}
// System.out.println();
}
System.out.println((resY+1)+" "+(resX+1)+" "+(maxLength));
// System.out.println();
}
}
} | Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 8 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | a903640d7a4da54ac1b905b1b7515c19 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | // Problem: F. Robot on the Board 2
// Contest: Codeforces - Codeforces Round #753 (Div. 3)
// URL: https://codeforces.com/contest/1607/problem/F
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
import java.io.*;
import java.util.*;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
public class Main {
private void preparation() {
mpi['L'] = 2;
mpi['R'] = 0;
mpi['D'] = 1;
mpi['U'] = 3;
}
private void clear() {
}
int [] stepx = {0,1,0,-1};
int [] stepy = {1,0,-1,0};
int [] mpi = new int[266];
int dfs(int x, int y, int F){
ArrayList<int []> load = new ArrayList<>();
load.add(new int[]{x,y});
vis[x][y] = load.size();
boolean cycle = false;
int base = 0;
// print("%d %d", x,y);
for(int i = 0; i < load.size(); i++){
int curx = load.get(i)[0];
int cury = load.get(i)[1];
int xx = curx + stepx[mpi[mp[curx][cury]]];
int yy = cury + stepy[mpi[mp[curx][cury]]];
if(xx >= 0 && xx < n && yy >= 0 && yy < m){
// print("%d %d-> %d %d, %c", curx,cury,xx,yy, mp[curx][cury]);
if(vis[xx][yy] == 0){
//keep going
load.add(new int[]{xx,yy});
vis[xx][yy] = load.size();
}else if(vis[xx][yy] > 0){
//cycle
cycle = true;
base = vis[xx][yy];
break;
}else{
//meet a cell visted
base = dp[xx][yy];
break;
}
}else{
break;
}
}
if(cycle){
int cnt = load.size() - base + 1;
for(int j = base - 1; j < load.size(); j++){
dp[load.get(j)[0]][load.get(j)[1]] = cnt;
}
// print("base = %d, load.size() = %d", base, load.size());
for(int i = 0; i < base - 1; i ++){
dp[load.get(i)[0]][load.get(i)[1]] = load.size() - i;
}
}else{
for(int i = 0; i < load.size(); i ++){
dp[load.get(i)[0]][load.get(i)[1]] = load.size() - i + base;
}
}
for(int [] t : load){
vis[t[0]][t[1]] = -1;
}
return dp[x][y];
}
int n,m;
char [][] mp = new char[2000][2000];
int [][] vis = new int[2000][2000];
int [][] dp = new int[2000][2000];
private void solve() throws Exception {
rf();rf();
n = ri();
m = ri();
// print("n = %d, m = %d", n,m);
for(int i = 0; i < n; i++){
rf();
char [] c= rs2c();
for(int j = 0; j < m; j++){
mp[i][j] = c[j];
vis[i][j] = 0;
dp[i][j] = 0;
}
}
int ans = 0;
int cnt = 1;
int an = 0, am = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(vis[i][j] == 0){
// print("");
int t = dfs(i,j, cnt++);
if(t > ans){
ans = t;
an = i + 1;
am = j + 1;
}
}
}
}
addAns(an + " " + am + " " + ans);
// print("end");
}
private void run() throws Exception {
int T = 1;
rf();
T = ri();
preparation();
while (T-- > 0) {
solve();
if (T != 0) {
clear();
}
}
printAns();
}
public static void main(String[] args) throws Exception {
new Main().run();
}
StringBuilder sb = new StringBuilder();
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer strT;
private void addAns(int a){
sb.append(a + "\n");
}
private void addAns(long a){
sb.append(a + "\n");
}
private void addAns(String s){
sb.append(s + "\n");
}
private void rf() throws IOException {
strT = new StringTokenizer(infile.readLine());
}
private int ri() {
return Integer.parseInt(strT.nextToken());
}
private long rl() {
return Long.parseLong(strT.nextToken());
}
private char [] rs2c(){
return strT.nextToken().toCharArray();
}
private String rs(){
return strT.nextToken();
}
private void printAns() {
System.out.println(sb);
}
private int[] readArr(int N) throws Exception {
int[] arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(strT.nextToken());
}
return arr;
}
private long[] readArr2(int N) throws Exception {
long[] arr = new long[N];
for (int i = 0; i < N; i++) {
arr[i] = Long.parseLong(strT.nextToken());
}
return arr;
}
private void print(String format, Object ... args){
System.out.println(String.format(format,args));
}
private void print(int[] arr, int x) {
//for debugging only
for (int i = 0;i < min(arr.length, x); i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
private void print(long[] arr, int x) {
//for debugging only
for (int i = 0;i < min(arr.length, x); i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
| Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 8 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | d794a96520c4ab8dffa92f3bfb5568c6 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.Math;
public class Main {
public class MainSolution extends MainSolutionT {
// global vars
public void init(int tests_count){}
public class TestCase extends TestCaseT
{
int n, m;
int[][] Mx;
int[][] My;
int[][] P;
int[][] C;
int[][] T;
int[][] stack;
int stackSize;
public void walk(int X, int Y, int c)
{
int x = X;
int y = Y;
int x1, y1;
int k = 0;
stackSize = 0;
while (true)
{
C[x][y] = c;
T[x][y] = k++;
x1 = x + Mx[x][y];
y1 = y + My[x][y];
if ((x1<0)||(y1<0)||(x1>=n)||(y1>=m))
{
walk_back_1(x,y,X,Y,1);
return;
}
if (C[x1][y1]==c)
{
int cycle_size = T[x][y] - T[x1][y1]+1;
walk_back_2(x,y,x1,y1,cycle_size);
walk_back_1(x1,y1,X,Y, cycle_size);
return;
}
if (C[x1][y1]!=-1)
{
walk_back_1(x,y,X,Y,P[x1][y1]+1);
return;
}
stack[stackSize][0] = x;
stack[stackSize][1] = y;
stackSize ++;
x = x1;
y = y1;
}
}
public void walk_back_1(int x,int y, int X, int Y, int k)
{
int x1, y1;
while (true)
{
P[x][y] = k++;
if (stackSize==0)
{
return;
}
stackSize--;
x1 = stack[stackSize][0];
y1 = stack[stackSize][1];
x = x1;
y = y1;
}
}
public void walk_back_2(int x,int y, int X, int Y, int k)
{
int x1, y1;
while (true)
{
if ((x==X)&&(y==Y))
{
return;
}
P[x][y] = k;
stackSize--;
x1 = stack[stackSize][0];
y1 = stack[stackSize][1];
x = x1;
y = y1;
}
}
public Object solve()
{
n = readInt();
m = readInt();
Mx = new int[n][m];
My = new int[n][m];
int i,j;
for (i=0; i<n; i++)
Arrays.fill(Mx[i], 0);
for (i=0; i<n; i++)
Arrays.fill(My[i], 0);
String s;
for (i=0; i<n; i++)
{
s = readLn();
for (j=0; j<m; j++)
{
switch (s.charAt(j))
{
case 'L':
My[i][j]--;
break;
case 'R':
My[i][j]++;
break;
case 'U':
Mx[i][j]--;
break;
case 'D':
Mx[i][j]++;
break;
}
}
}
P = new int[n][m];
for (i=0; i<n; i++)
Arrays.fill(P[i], -1);
C = new int[n][m];
for (i=0; i<n; i++)
Arrays.fill(C[i], -1);
T = new int[n][m];
for (i=0; i<n; i++)
Arrays.fill(T[i], -1);
stack = new int[n*m][2];
int c= 1;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
if (C[i][j]==-1)
{
walk(i, j, c++);
}
}
}
int maxx = 0;
int maxy = 0;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
if (P[i][j] > P[maxx][maxy])
{
maxx = i;
maxy = j;
}
}
}
return strf("%d %d %d", maxx+1, maxy+1, P[maxx][maxy]);
}
}
public void run()
{
int t = multiply_test ? readInt() : 1;
this.init(t);
for (int i = 0; i < t; i++) {
TestCase T = new TestCase();
T.run(i + 1);
}
}
public void loc_params()
{
this.log_enabled = false;
}
public void params(){
this.multiply_test = true;
}
}
public class MainSolutionT extends MainSolutionBase
{
public class TestCaseT extends TestCaseBase
{
}
}
public class MainSolutionBase
{
public boolean is_local = false;
public MainSolutionBase()
{
}
public class TestCaseBase
{
public Object solve() {
return null;
}
public int caseNumber;
public TestCaseBase() {
}
public void run(int cn){
this.caseNumber = cn;
Object r = this.solve();
if ((r != null))
{
out.println(r);
}
}
}
public String impossible(){
return "IMPOSSIBLE";
}
public String strf(String format, Object... args)
{
return String.format(format, args);
}
public BufferedReader in;
public PrintStream out;
public boolean log_enabled = false;
public boolean multiply_test = true;
public void params()
{
}
public void loc_params()
{
}
private StringTokenizer tokenizer = null;
public int readInt() {
return Integer.parseInt(readToken());
}
public long readLong() {
return Long.parseLong(readToken());
}
public double readDouble() {
return Double.parseDouble(readToken());
}
public String readLn() {
try {
String s;
while ((s = in.readLine()).length() == 0);
return s;
} catch (Exception e) {
return "";
}
}
public String readToken() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(in.readLine());
}
return tokenizer.nextToken();
} catch (Exception e) {
return "";
}
}
public int[] readIntArray(int n) {
int[] x = new int[n];
readIntArray(x, n);
return x;
}
public void readIntArray(int[] x, int n) {
for (int i = 0; i < n; i++) {
x[i] = readInt();
}
}
public long[] readLongArray(int n) {
long[] x = new long[n];
readLongArray(x, n);
return x;
}
public void readLongArray(long[] x, int n) {
for (int i = 0; i < n; i++) {
x[i] = readLong();
}
}
public void readLongArrayRev(long[] x, int n) {
for (int i = 0; i < n; i++) {
x[n-i-1] = readLong();
}
}
public void logWrite(String format, Object... args) {
if (!log_enabled) {
return;
}
out.printf(format, args);
}
public void readLongArrayBuf(long[] x, int n) {
char[]buf = new char[1000000];
long r = -1;
int k= 0, l = 0;
long d;
while (true)
{
try{
l = in.read(buf, 0, 1000000);
}
catch(Exception E){};
for (int i=0; i<l; i++)
{
if (('0'<=buf[i])&&(buf[i]<='9'))
{
if (r == -1)
{
r = 0;
}
d = buf[i] - '0';
r = 10 * r + d;
}
else
{
if (r != -1)
{
x[k++] = r;
}
r = -1;
}
}
if (l<1000000)
return;
}
}
public void readIntArrayBuf(int[] x, int n) {
char[]buf = new char[1000000];
int r = -1;
int k= 0, l = 0;
int d;
while (true)
{
try{
l = in.read(buf, 0, 1000000);
}
catch(Exception E){};
for (int i=0; i<l; i++)
{
if (('0'<=buf[i])&&(buf[i]<='9'))
{
if (r == -1)
{
r = 0;
}
d = buf[i] - '0';
r = 10 * r + d;
}
else
{
if (r != -1)
{
x[k++] = r;
}
r = -1;
}
}
if (l<1000000)
return;
}
}
public void printArray(long[] a, int n)
{
printArray(a, n, ' ');
}
public void printArray(int[] a, int n)
{
printArray(a, n, ' ');
}
public void printArray(long[] a, int n, char dl)
{
long x;
int i, l = 0;
for (i=0; i<n; i++)
{
x = a[i];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
x = a[i];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (i>0)
{
s[l--] = dl;
}
}
out.println(new String(s));
}
public void printArray(double[] a, int n, char dl)
{
long x;
double y;
int i, l = 0;
for (i=0; i<n; i++)
{
x = (long)a[i];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += n-1 + 10*n;
char[] s = new char[l];
l--;
boolean z;
int j;
for (i=n-1; i>=0; i--)
{
x = (long)a[i];
y = (long)(1000000000*(a[i]-x));
z = false;
if (x<0)
{
x = -x;
z = true;
}
for (j=0; j<9; j++)
{
s[l--] = (char)('0' + (y % 10));
y /= 10;
}
s[l--] = '.';
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (i>0)
{
s[l--] = dl;
}
}
out.println(new String(s));
}
public void printArray(int[] a, int n, char dl)
{
int x;
int i, l = 0;
for (i=0; i<n; i++)
{
x = a[i];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
x = a[i];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (i>0)
{
s[l--] = dl;
}
}
out.println(new String(s));
}
public void printMatrix(int[][] a, int n, int m)
{
int x;
int i,j, l = 0;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
x = a[i][j];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += m-1;
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
for (j=m-1; j>=0; j--)
{
x = a[i][j];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (j>0)
{
s[l--] = ' ';
}
}
if (i>0)
{
s[l--] = '\n';
}
}
out.println(new String(s));
}
public void printMatrix(long[][] a, int n, int m)
{
long x;
int i,j, l = 0;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
x = a[i][j];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += m-1;
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
for (j=m-1; j>=0; j--)
{
x = a[i][j];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (j>0)
{
s[l--] = ' ';
}
}
if (i>0)
{
s[l--] = '\n';
}
}
out.println(new String(s));
}
}
public void run()
{
MainSolution S;
try {
S = new MainSolution();
S.in = new BufferedReader(new InputStreamReader(System.in));
//S.out = System.out;
S.out = new PrintStream(new BufferedOutputStream( System.out ));
} catch (Exception e) {
return;
}
S.params();
S.run();
S.out.flush();
}
public static void main(String args[]) {
Locale.setDefault(Locale.US);
Main M = new Main();
M.run();
}
} | Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 8 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | d067d0f021a3dce494f653b361fe7340 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | // Main Code at the Bottom
import java.util.*;
import java.io.*;
import java.sql.Time;
public class Main implements Runnable{
//Fast IO class
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
boolean env=System.getProperty("ONLINE_JUDGE") != null;
//env=true;
if(!env) {
try {
br=new BufferedReader(new FileReader("src\\input.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
else br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static long MOD=(long)1e9+7;
//debug
static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
static FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
//Global variables and functions
char graph[][];
int dp[][];
boolean visited[][];
HashMap<Integer, Integer> st;
ArrayList<int[]> path;
int getId(int i, int j) {
return i * m + j;
}
int n, m;
//Main function(The main code starts from here)
public void run() {
int test=1;
test=sc.nextInt();
while(test-->0) {
sc.nextLine();
n = sc.nextInt();
m = sc.nextInt();
char[][] b = new char[n][];
for(int i=0;i<n;i++)
b[i] = sc.nextLine().toCharArray();
int last = n*m;
int[] to = new int[n*m+1];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(b[i][j] == 'L')
to[getId(i,j)] = (j>0)? getId(i,j-1) : last;
else if(b[i][j] == 'R')
to[getId(i,j)] = (j<m-1)? getId(i,j+1) : last;
else if(b[i][j] == 'U')
to[getId(i,j)] = (i>0)? getId(i-1,j) : last;
else
to[getId(i,j)] = (i<n-1)? getId(i+1,j) : last;
}
to[last] = last;
int[] used = new int[n*m+1];
int[] resLen = new int[n*m+1];
for(int i=0;i<=n*m;i++)
resLen[i] = -1;
for(int i=0;i<=n*m;i++)
if(used[i] == 0)
{
int cur = i;
while(used[cur] == 0)
{
used[cur] = 1;
cur = to[cur];
}
if( used[cur] == 1 )
{
int start = cur;
int len = 0;
do
{
cur = to[cur];
len++;
}
while(cur != start);
do
{
resLen[cur] = len;
cur = to[cur];
}
while(cur != start);
}
cur = i;
while(used[cur] == 1)
{
used[cur] = 2;
cur = to[cur];
}
}
resLen[last] = 0;
int[] seq = new int[n*m+1];
for(int i=0;i<n*m;i++)
{
if(resLen[i] == -1)
{
int cur = i;
int pos = 0;
while(resLen[cur] == -1)
{
seq[pos++] = cur;
cur = to[cur];
}
for(int j=pos-1;j>=0;j--)
resLen[seq[j]] = resLen[to[seq[j]]]+1;
}
}
int res = 1;
int resI = 1;
int resJ = 1;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(res < resLen[getId(i,j)])
{
res = resLen[getId(i, j)];
resI = i+1;
resJ = j+1;
}
out.println(resI + " " + resJ + " " + res);
}
out.flush();
out.close();
}
public static void main (String[] args) throws java.lang.Exception {
new Thread(null, new Main(), "anything", (1<<28)).start();
}
} | Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 8 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 4df96400beba248aa1bc861e87bbaec8 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {new Main().run();}
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
void run() {
for(int q=ni();q>0;q--){
work();
}
out.flush();
}
long mod=1000000007;
long gcd(long a,long b) {
return a==0?b:gcd(b%a,a);
}
long inf=Long.MAX_VALUE/3;
void work() {
int n=ni(),m=ni();
int[][] dir=new int[][]{{-1,0},{1,0},{0,-1},{0,1}};
int[][] A=new int[n][m];
for(int i=0;i<n;i++){
char[] chs=ns().toCharArray();
for(int j=0;j<m;j++){
if(chs[j]=='L'){
A[i][j]=2;
}else if(chs[j]=='R'){
A[i][j]=3;
}else if(chs[j]=='D'){
A[i][j]=1;
}else{
A[i][j]=0;
}
}
}
int[][] vis=new int[n][m];
int[][] ret=new int[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(vis[i][j]==0){
int x=i,y=j;
int d=0;
int cx=-1,cy=-1;
while(x>=0&&x<n&&y>=0&&y<m){
if(vis[x][y]==1){
cx=x;
cy=y;
break;
}else if(vis[x][y]==2){
d+=ret[x][y];
break;
}else{
vis[x][y]=1;
int nx=x+dir[A[x][y]][0];
int ny=y+dir[A[x][y]][1];
x=nx;
y=ny;
}
d++;
}
int d2=0;
x=i;y=j;
while(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==1){
vis[x][y]=2;
ret[x][y]=d-d2;
d2++;
int nx=x+dir[A[x][y]][0];
int ny=y+dir[A[x][y]][1];
x=nx;
y=ny;
}
if(cx!=-1){
int x1=cx,y1=cy;
do{
ret[x1][y1]=ret[cx][cy];
int nx=x1+dir[A[x1][y1]][0];
int ny=y1+dir[A[x1][y1]][1];
x1=nx;
y1=ny;
}while (x1!=cx||y1!=cy);
}
}
}
}
int x=0,y=0,d=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(ret[i][j]>d){
d=ret[i][j];
x=i;
y=j;
}
}
}
out.println((x+1)+" "+(y+1)+" "+d);
}
@SuppressWarnings("unused")
private ArrayList<Integer>[] ng(int n, int m) {
ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
int s=in.nextInt()-1,e=in.nextInt()-1;
graph[s].add(e);
graph[e].add(s);
}
return graph;
}
private ArrayList<long[]>[] ngw(int n, int m) {
ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong();
graph[(int)s].add(new long[] {e,w});
graph[(int)e].add(new long[] {s,w});
}
return graph;
}
private int ni() {
return in.nextInt();
}
private long nl() {
return in.nextLong();
}
private double nd() {
return in.nextDouble();
}
private String ns() {
return in.next();
}
private long[] na(int n) {
long[] A=new long[n];
for(int i=0;i<n;i++) {
A[i]=in.nextLong();
}
return A;
}
private int[] nia(int n) {
int[] A=new int[n];
for(int i=0;i<n;i++) {
A[i]=in.nextInt();
}
return A;
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
InputStreamReader input;//no buffer
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(boolean isBuffer)
{
if(!isBuffer){
input=new InputStreamReader(System.in);
}else{
br=new BufferedReader(new InputStreamReader(System.in));
}
}
public boolean hasNext(){
try{
String s=br.readLine();
if(s==null){
return false;
}
st=new StringTokenizer(s);
}catch(IOException e){
e.printStackTrace();
}
return true;
}
public String next()
{
if(input!=null){
try {
StringBuilder sb=new StringBuilder();
int ch=input.read();
while(ch=='\n'||ch=='\r'||ch==32){
ch=input.read();
}
while(ch!='\n'&&ch!='\r'&&ch!=32){
sb.append((char)ch);
ch=input.read();
}
return sb.toString();
}catch (Exception e){
e.printStackTrace();
}
}
while(st==null || !st.hasMoreElements())//回车,空行情况
{
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt()
{
return (int)nextLong();
}
public long nextLong() {
try {
if(input!=null){
long ret=0;
int b=input.read();
while(b<'0'||b>'9'){
b=input.read();
}
while(b>='0'&&b<='9'){
ret=ret*10+(b-'0');
b=input.read();
}
return ret;
}
}catch (Exception e){
e.printStackTrace();
}
return Long.parseLong(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
} | Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 8 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 9b1ceb39fe6ce9c4b97e959365c9b8ff | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastReader input=new FastReader();
PrintWriter out=new PrintWriter(System.out);
int T=input.nextInt();
while(T-->0)
{
int n=input.nextInt();
int m=input.nextInt();
char ch[][]=new char[n][m];
for(int i=0;i<n;i++)
{
String s=input.next();
ch[i]=s.toCharArray();
}
int arr[]=dfs(ch,n,m);
int r=arr[0]+1;
int c=arr[1]+1;
int max=arr[2];
out.println(r+" "+c+" "+max);
}
out.close();
}
public static int [] dfs(char ch[][],int n,int m)
{
int visit[][]=new int[n][m];
int arr[][]=new int[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(visit[i][j]==0)
{
dfsVisit(ch,i,j,visit,arr,n,m);
}
}
}
int max=0;
int r=0,c=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(arr[i][j]>max)
{
max=arr[i][j];
r=i;c=j;
}
}
}
return new int[]{r,c,max};
}
public static void dfsVisit(char ch[][],int i,int j,int visit[][],int arr[][],int n,int m)
{
ArrayList<int []> list=new ArrayList<>();
while(true)
{
list.add(new int[]{i,j});
visit[i][j]=1;
if(ch[i][j]=='U') i--;
else if(ch[i][j]=='D') i++;
else if(ch[i][j]=='R') j++;
else j--;
if(i>=n || j>=m || i<0 || j<0) break;
else if(visit[i][j]==1) break;
}
if(i>=n || j>=m || i<0 || j<0)
{
int x=1;
for(int l=list.size()-1;l>=0;l--)
{
int u=list.get(l)[0];
int v=list.get(l)[1];
arr[u][v]=x;
x++;
}
}
else
{
if(arr[i][j]==0)
{
int in=-1;
for(int l=list.size()-1;l>=0;l--)
{
int u=list.get(l)[0];
int v=list.get(l)[1];
if(u==i && v==j)
{
in=l;
break;
}
}
int c=list.size()-in;
for(int l=in;l<list.size();l++)
{
int u=list.get(l)[0];
int v=list.get(l)[1];
arr[u][v]=c;
}
for(int l=in-1;l>=0;l--)
{
c++;
int u=list.get(l)[0];
int v=list.get(l)[1];
arr[u][v]=c;
}
}
else
{
int c=arr[i][j];
for(int l=list.size()-1;l>=0;l--)
{
c++;
int u=list.get(l)[0];
int v=list.get(l)[1];
arr[u][v]=c;
}
}
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while(st==null || !st.hasMoreElements())
{
try
{
st=new StringTokenizer(br.readLine());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch(IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 8 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 42888a2c72352cf12e5f853d9ad8cbe6 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
public class cf {
static FastReader in = new FastReader();
public static void main(String[] args) throws IOException {
int t = i();
while (t-- > 0) {
s();
int n = i(), m = i();
String[] mat = new String[n];
for (int i = 0; i < n; i++) {
mat[i] = s();
}
int[][] visited = new int[n][m];
int[][] vals = new int[n][m];
int big = 0;
Pair loc = null;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (visited[i][j] == 0) {
List<Pair> path = new ArrayList<>();
int r = i;
int c = j;
while (0 <= r && r < n && 0 <= c && c < m && visited[r][c] == 0) {
visited[r][c] = 1;
path.add(new Pair(r, c));
char dir = mat[r].charAt(c);
if (dir == 'U') { r -= 1;
} else if (dir == 'D') { r += 1;
} else if (dir == 'L') { c -= 1;
} else { c += 1; }
}
if (r < 0 || r >= n || c < 0 || c >= m) {
int k = 0;
if (path.size() > big) {
big = path.size();
loc = path.get(0);
}
for (Pair p : path) {
vals[p.i][p.j] = path.size() - k;
k++;
}
} else if (vals[r][c] == 0) {
int k = 0;
int l = 0;
if (path.size() > big) {
big = path.size();
loc = path.get(0);
}
for (Pair p : path) {
if (p.i == r && p.j == c) {
l = path.size() - k;
}
vals[p.i][p.j] = Math.max(l, path.size() - k);
k++;
}
} else {
int v = vals[r][c];
int k = 0;
if (v + path.size() > big) {
big = v + path.size();
loc = path.get(0);
}
for (Pair p : path) {
vals[p.i][p.j] = v + path.size() - k;
k++;
}
}
}
}
}
String out = Integer.toString(loc.i + 1);
out += " ";
out += loc.j + 1;
out += " ";
out += big;
System.out.println(out);
}
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s() {
return in.nextLine();
}
}
class Pair implements Comparable<Pair> {
public int i, j;
Pair(int i, int j) {
this.i = i;
this.j = j;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pair pair = (Pair) o;
return i == pair.i && j == pair.j;
}
@Override
public int hashCode() {
return Objects.hash(i, j);
}
@Override
public int compareTo(Pair p) {
if (this.i == p.i) {
return this.j - p.j;
}
return this.i - p.i;
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
} | Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 8 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 751ae070b2b44457685bd920a9c9cf83 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution{
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}
catch(IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
}
static class xyz{
int x;
int y;
int z;
public xyz(int x,int y,int z){
this.x = x;
this.y = y;
this.z = z;
}
}
static List<xyz> ret;
public static void main(String[] args){
FastReader in = new FastReader();
int t = in.nextInt();
while(t-->0){
int n = in.nextInt();
int[] arr = new int[n];
Arrays.setAll(arr,i->in.nextInt());
ret=new ArrayList<>();
int[] copy = Arrays.copyOf(arr,n);
Arrays.sort(copy);
if(Arrays.equals(arr,copy)){
System.out.println(0);
continue;
}
differentialSorting(arr,n);
}
}
public static void differentialSorting(int[] arr,int n){
if(arr[n-1]<arr[n-2]) {
System.out.println(-1);
return;
}
arr[n-3]= arr[n-2]-arr[n-1];
if(arr[n-3]<=arr[n-2] && arr[n-3]<=arr[n-1] && arr[n-2]<=arr[n-1]){
System.out.println(n-2);
for(int i=0;i<n-2;i++){
System.out.println(i+1+" "+(n-1)+" "+n);
}
}
else System.out.println(-1);
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | b9094d0db183e6f1aba9e8c9123f6d7a | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in); // 1 10 11
int t = in.nextInt();
in.nextLine();
// List<String> input = readFileStrings();
// int t = Integer.parseInt(input.get(0));
int lineIndex = 1;
for (int i = 0; i < t; i++) {
// int countNumber = Integer.parseInt(input.get(lineIndex));
int countNumber = in.nextInt();
in.nextLine();
// lineIndex++;
int res = 0;
// List<Integer> numbers = Arrays.asList(input.get(lineIndex).split(" ")).stream()
// .map((s) -> Integer.parseInt(s)).collect(Collectors.toList());
List<Integer> numbers = new ArrayList<>();
for (int ir = 0; ir < countNumber; ir++)
numbers.add(in.nextInt());
in.nextLine();
if (numbers.get(countNumber - 1) < numbers.get(countNumber - 2)) {
System.out.println(-1);
// lineIndex++;
continue;
}
if (numbers.get(countNumber - 1)<0){
List<Integer> sortArray = new ArrayList<>(numbers);
sortArray.sort(Comparator.naturalOrder());
boolean flag = true;
for (int indexAr=0;indexAr<sortArray.size();indexAr++){
if (!numbers.get(indexAr).equals(sortArray.get(indexAr))){
flag = false;
}
}
if (flag){
System.out.println(0);
}else{
System.out.println(-1);
}
// lineIndex++;
continue;
}
StringBuilder builder = new StringBuilder();
int maxInt = numbers.get(countNumber - 1), minInt = numbers.get(countNumber - 2),
maxIndex = countNumber - 1, minIndex = countNumber - 2;
System.out.println(countNumber -2);
for (int j = 0; j < countNumber - 2; j++) {
// numbers.set(j, numbers.get(minIndex) - numbers.get(maxIndex));
builder.append(j+1).append(" ").append(minIndex+1).append(" ").append(maxIndex+1).append("\n");
// minInt = numbers.get(j);
// minIndex = j;
res++;
}
System.out.print(builder.toString());
// lineIndex++;
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | f6bb4e78c5b62011f2b99790a4956eec | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes |
import java.awt.datatransfer.FlavorListener;
import java.util.*;
import java.util.regex.Pattern;
public class Main {
boolean[] visited = new boolean[4];
Stack<Integer> stack = new Stack<Integer>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] nums = new int[n];
boolean isSort = true;
nums[0] = sc.nextInt();
for (int i = 1; i < n; i++) {
nums[i] = sc.nextInt();
if (nums[i] < nums[i - 1]) isSort = false;
}
// 5 -4 2 -1 2
if (isSort) System.out.println(0);
else if (nums[n - 2] > nums[n - 1] || nums[n - 1] < 0) System.out.println(-1);
else {
System.out.println(n - 2);
for (int i = 0; i < n - 2; i++)
System.out.println(i + 1 + " " + (n - 1) + " " + n);
}
}
}
static boolean isDigit(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
/**
* 组合代码
*
* @param start 开始的位置
* @param current 装载每一种组合情况的数组
* @param n 组合数的数量
* @param k 需要取的数的个数
* @param ans 结果列表,所有结果将呈现在此
*/
private static void backtrack(int start, LinkedList<Integer> current, int n, int k, List<List<Integer>> ans) {
if (current.size() == k) {
ans.add(new LinkedList<Integer>(current));
}
for (int i = start; i <= n && current.size() < k; i++) {
current.add(i);
backtrack(i + 1, current, n, k, ans);
current.removeLast();
}
}
/**
* 1-n的组合情况
*
* @param n 组合范围
* @param k 选取的个数
* @return 所有组合的情况
*/
public static List<List<Integer>> combine(int n, int k) {
List<List<Integer>> ans = new LinkedList<>();
if (k == 0) {
ans.add(new LinkedList<Integer>());
return ans;
}
backtrack(1, new LinkedList<Integer>(), n, k, ans);
return ans;
}
public void dfs(int[] nums) {
if (nums.length == stack.size()) {
System.out.println(stack);
return;
}
for (int i = 0; i < nums.length; i++) {
if (!visited[i]) {
visited[i] = true;
stack.push(nums[i]);
dfs(nums);
visited[i] = false;
stack.pop();
}
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 4c71611a425648ed1e1a711b08193c8f | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes |
import java.awt.datatransfer.FlavorListener;
import java.util.*;
import java.util.regex.Pattern;
public class Main {
boolean[] visited = new boolean[4];
Stack<Integer> stack = new Stack<Integer>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] nums = new int[n];
boolean isSort = true;
nums[0] = sc.nextInt();
for (int i = 1; i < n; i++) {
nums[i] = sc.nextInt();
if (nums[i] < nums[i - 1]) isSort = false;
}
// 5 -4 2 -1 2
if (isSort) System.out.println("0");
else if (nums[n - 2] > nums[n - 1] || nums[n - 1] < 0) System.out.println("-1");
else {
System.out.println(n - 2);
for (int i = 1; i < n - 1; i++)
System.out.println(i + " " + (n - 1) + " " + n);
}
}
}
static boolean isDigit(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
/**
* 组合代码
*
* @param start 开始的位置
* @param current 装载每一种组合情况的数组
* @param n 组合数的数量
* @param k 需要取的数的个数
* @param ans 结果列表,所有结果将呈现在此
*/
private static void backtrack(int start, LinkedList<Integer> current, int n, int k, List<List<Integer>> ans) {
if (current.size() == k) {
ans.add(new LinkedList<Integer>(current));
}
for (int i = start; i <= n && current.size() < k; i++) {
current.add(i);
backtrack(i + 1, current, n, k, ans);
current.removeLast();
}
}
/**
* 1-n的组合情况
*
* @param n 组合范围
* @param k 选取的个数
* @return 所有组合的情况
*/
public static List<List<Integer>> combine(int n, int k) {
List<List<Integer>> ans = new LinkedList<>();
if (k == 0) {
ans.add(new LinkedList<Integer>());
return ans;
}
backtrack(1, new LinkedList<Integer>(), n, k, ans);
return ans;
}
public void dfs(int[] nums) {
if (nums.length == stack.size()) {
System.out.println(stack);
return;
}
for (int i = 0; i < nums.length; i++) {
if (!visited[i]) {
visited[i] = true;
stack.push(nums[i]);
dfs(nums);
visited[i] = false;
stack.pop();
}
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 7bb9399ed4814f0524a16b2f05fd550d | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes |
import java.awt.datatransfer.FlavorListener;
import java.util.*;
import java.util.regex.Pattern;
public class Main {
boolean[] visited = new boolean[4];
Stack<Integer> stack = new Stack<Integer>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] arr = new int[n];
boolean sort = true;
arr[0] = sc.nextInt();
for (int i = 1; i < n; i++) {
arr[i] = sc.nextInt();
if (arr[i] < arr[i - 1]) sort = false;
}
if (sort == true) System.out.println("0");
else if (arr[n - 1] < arr[n - 2] || arr[n - 1] < 0) {
System.out.println("-1");
} else {
System.out.println(n - 2);
for (int i = 1; i < n - 1; i++) System.out.println(i + " " + (n - 1) + " " + n);
}
}
// Scanner sc = new Scanner(System.in);
// int t = sc.nextInt();
// while (t-- > 0) {
// int n = sc.nextInt();
// int[] nums = new int[n];
// boolean isSort = true;
// nums[0] = sc.nextInt();
// for (int i = 1; i < n; i++) {
// nums[i] = sc.nextInt();
// if (nums[i] < nums[i - 1]) isSort = false;
// }
// // 5 -4 2 -1 2
// if (isSort) System.out.println(0);
// else if (nums[n - 2] > nums[n - 1] || nums[n - 1] < 0) System.out.println(-1);
// else {
// System.out.println(n - 2);
// for (int i = 0; i < n - 2; i++)
// System.out.printf("%d %d %d\n", i + 1, n - 1, n);
// }
// }
}
static boolean isDigit(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
/**
* 组合代码
*
* @param start 开始的位置
* @param current 装载每一种组合情况的数组
* @param n 组合数的数量
* @param k 需要取的数的个数
* @param ans 结果列表,所有结果将呈现在此
*/
private static void backtrack(int start, LinkedList<Integer> current, int n, int k, List<List<Integer>> ans) {
if (current.size() == k) {
ans.add(new LinkedList<Integer>(current));
}
for (int i = start; i <= n && current.size() < k; i++) {
current.add(i);
backtrack(i + 1, current, n, k, ans);
current.removeLast();
}
}
/**
* 1-n的组合情况
*
* @param n 组合范围
* @param k 选取的个数
* @return 所有组合的情况
*/
public static List<List<Integer>> combine(int n, int k) {
List<List<Integer>> ans = new LinkedList<>();
if (k == 0) {
ans.add(new LinkedList<Integer>());
return ans;
}
backtrack(1, new LinkedList<Integer>(), n, k, ans);
return ans;
}
public void dfs(int[] nums) {
if (nums.length == stack.size()) {
System.out.println(stack);
return;
}
for (int i = 0; i < nums.length; i++) {
if (!visited[i]) {
visited[i] = true;
stack.push(nums[i]);
dfs(nums);
visited[i] = false;
stack.pop();
}
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | f06be744f158ba3036ec3788edc18e80 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.Math.sqrt;
import static java.lang.Math.pow;
import static java.lang.System.out;
import static java.lang.System.err;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
static FastReader sc;
static long mod=((long)1e9)+7;
// static FastWriter out;
public static void main(String hi[]){
initializeIO();
sc=new FastReader();
int t=sc.nextInt();
// boolean[] seave=sieveOfEratosthenes((int)(1e7));
// int[] seave2=sieveOfEratosthenesInt((int)(1e4));
// int tp=1;
while(t--!=0){
int n=sc.nextInt();
int[] arr=readIntArray(n);
List<int[]> li=new ArrayList<>();
List<int[]> res=new ArrayList<>();
if(arr[n-2]>arr[n-1]){
out.println(-1);
continue;
}
boolean sorted=true;
for(int i=n-1;i>=0;i--){
int val=arr[i];
if(i<n-2){
if(arr[i]>arr[i+1])sorted=false;
res.add(new int[]{i,n-2,n-1});
}
}
if(arr[n-1]<0){
if(sorted){
print(0);
}else{
print(-1);
}
continue;
}
print(res.size());
for(int[] x:res){
out.println((x[0]+1)+" "+(x[1]+1)+" "+(x[2]+1));
}
}
// System.out.println(String.format("%.9f", max));
}
private static int bs(List<int[]> nums,long val){
int i=0,l=0,r=nums.size()-1;
while(l<=r){
int mid=l+((r-l)/2);
if(nums.get(mid)[0]<=val){
i=mid;
l=mid+1;
}else{
r=mid-1;
}
}
return i;
}
private static int lessThen(long[] nums,long val){
int i=0,l=0,r=nums.length-1;
while(l<=r){
int mid=l+((r-l)/2);
if(nums[mid]<=val){
i=mid;
l=mid+1;
}else{
r=mid-1;
}
}
return i;
}
private static int lessThen(List<Long> nums,long val){
int i=0,l=0,r=nums.size()-1;
while(l<=r){
int mid=l+((r-l)/2);
if(nums.get(mid)<=val){
i=mid;
l=mid+1;
}else{
r=mid-1;
}
}
return i;
}
private static int greaterThen(long[] nums,long val){
int i=nums.length-1,l=0,r=nums.length-1;
while(l<=r){
int mid=l+((r-l)/2);
if(nums[mid]>=val){
i=mid;
r=mid-1;
}else{
l=mid+1;
}
}
return i;
}
private static long sumOfAp(long a,long n,long d){
long val=(n*( 2*a+((n-1)*d)));
return val/2;
}
//geometrics
private static double areaOftriangle(double x1,double y1,double x2,double y2,double x3,double y3){
double[] mid_point=midOfaLine(x1,y1,x2,y2);
// debug(Arrays.toString(mid_point)+" "+x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+" "+y3);
double height=distanceBetweenPoints(mid_point[0],mid_point[1],x3,y3);
double wight=distanceBetweenPoints(x1,y1,x2,y2);
// debug(height+" "+wight);
return (height*wight)/2;
}
private static double distanceBetweenPoints(double x1,double y1,double x2,double y2){
double x=x2-x1;
double y=y2-y1;
return sqrt(Math.pow(x,2)+Math.pow(y,2));
}
private static double[] midOfaLine(double x1,double y1,double x2,double y2){
double[] mid=new double[2];
mid[0]=(x1+x2)/2;
mid[1]=(y1+y2)/2;
return mid;
}
private static long sumOfN(long n){
return (n*(n+1))/2;
}
private static long power(long x,long y,long p){
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0){
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
/* Function to calculate x raised to the power y in O(logn)*/
static long power(long x, long y){
long temp;
if( y == 0)
return 1l;
temp = power(x, y / 2);
if (y % 2 == 0)
return (temp*temp);
else
return (x*temp*temp);
}
private static StringBuilder reverseString(String s){
StringBuilder sb=new StringBuilder(s);
int l=0,r=sb.length()-1;
while(l<=r){
char ch=sb.charAt(l);
sb.setCharAt(l,sb.charAt(r));
sb.setCharAt(r,ch);
l++;
r--;
}
return sb;
}
private static void swap(long arr[],int i,int j){
long t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
private static void swap(int arr[],int i,int j){
int t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
private static void swap(double arr[],int i,int j){
double t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
private static void swap(float arr[],int i,int j){
float t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static long lcm(long a, long b){
return (a / gcd(a, b)) * b;
}
private static String decimalToString(int x){
return Integer.toBinaryString(x);
}
private static boolean isPallindrome(String s){
int l=0,r=s.length()-1;
while(l<r){
if(s.charAt(l)!=s.charAt(r))return false;
l++;
r--;
}
return true;
}
private static StringBuilder removeLeadingZero(StringBuilder sb){
int i=0;
while(i<sb.length()&&sb.charAt(i)=='0')i++;
// debug("remove "+i);
if(i==sb.length())return new StringBuilder();
return new StringBuilder(sb.substring(i,sb.length()));
}
private static int stringToDecimal(String binaryString){
// debug(decimalToString(n<<1));
return Integer.parseInt(binaryString,2);
}
private static int stringToInt(String s){
return Integer.parseInt(s);
}
private static String toString(int val){
return String.valueOf(val);
}
private static void print(String s){
out.println(s);
}
private static void debug(String s){
err.println(s);
}
private static int charToInt(char c){
return ((((int)(c-'0'))%48));
}
private static void print(double s){
out.println(s);
}
private static void print(float s){
out.println(s);
}
private static void print(long s){
out.println(s);
}
private static void print(int s){
out.println(s);
}
private static void debug(double s){
err.println(s);
}
private static void debug(float s){
err.println(s);
}
private static void debug(long s){
err.println(s);
}
private static void debug(int s){
err.println(s);
}
private static boolean isPrime(int n){
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
//read graph
private static List<List<Integer>> readUndirectedGraph(int n){
List<List<Integer>> graph=new ArrayList<>();
for(int i=0;i<=n;i++){
graph.add(new ArrayList<>());
}
for(int i=0;i<n;i++){
int x=sc.nextInt();
int y=sc.nextInt();
graph.get(x).add(y);
graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readUndirectedGraph(int[][] intervals,int n){
List<List<Integer>> graph=new ArrayList<>();
for(int i=0;i<=n;i++){
graph.add(new ArrayList<>());
}
for(int i=0;i<intervals.length;i++){
int x=intervals[i][0];
int y=intervals[i][1];
graph.get(x).add(y);
graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readDirectedGraph(int[][] intervals,int n){
List<List<Integer>> graph=new ArrayList<>();
for(int i=0;i<=n;i++){
graph.add(new ArrayList<>());
}
for(int i=0;i<intervals.length;i++){
int x=intervals[i][0];
int y=intervals[i][1];
graph.get(x).add(y);
// graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readDirectedGraph(int n){
List<List<Integer>> graph=new ArrayList<>();
for(int i=0;i<=n;i++){
graph.add(new ArrayList<>());
}
for(int i=0;i<n;i++){
int x=sc.nextInt();
int y=sc.nextInt();
graph.get(x).add(y);
// graph.get(y).add(x);
}
return graph;
}
static String[] readStringArray(int n){
String[] arr=new String[n];
for(int i=0;i<n;i++){
arr[i]=sc.next();
}
return arr;
}
private static Map<Character,Integer> freq(String s){
Map<Character,Integer> map=new HashMap<>();
for(char c:s.toCharArray()){
map.put(c,map.getOrDefault(c,0)+1);
}
return map;
}
static boolean[] sieveOfEratosthenes(long n){
boolean prime[] = new boolean[(int)n + 1];
for (int i = 2; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true){
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
static int[] sieveOfEratosthenesInt(long n){
boolean prime[] = new boolean[(int)n + 1];
Set<Integer> li=new HashSet<>();
for (int i = 1; i <= n; i++){
prime[i] = true;
li.add(i);
}
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true){
for (int i = p * p; i <= n; i += p){
li.remove(i);
prime[i] = false;
}
}
}
int[] arr=new int[li.size()+1];
int i=0;
for(int x:li){
arr[i++]=x;
}
return arr;
}
public static long Kadens(List<Long> prices) {
long sofar=0;
long max_v=0;
for(int i=0;i<prices.size();i++){
sofar+=prices.get(i);
if (sofar<0) {
sofar=0;
}
max_v=Math.max(max_v,sofar);
}
return max_v;
}
static boolean isMemberAC(int a, int d, int x){
// If difference is 0, then x must
// be same as a.
if (d == 0)
return (x == a);
// Else difference between x and a
// must be divisible by d.
return ((x - a) % d == 0 && (x - a) / d >= 0);
}
private static void sort(int[] arr){
int n=arr.length;
List<Integer> li=new ArrayList<>();
for(int x:arr){
li.add(x);
}
Collections.sort(li);
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sortReverse(int[] arr){
int n=arr.length;
List<Integer> li=new ArrayList<>();
for(int x:arr){
li.add(x);
}
Collections.sort(li,Collections.reverseOrder());
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sort(double[] arr){
int n=arr.length;
List<Double> li=new ArrayList<>();
for(double x:arr){
li.add(x);
}
Collections.sort(li);
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sortReverse(double[] arr){
int n=arr.length;
List<Double> li=new ArrayList<>();
for(double x:arr){
li.add(x);
}
Collections.sort(li,Collections.reverseOrder());
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sortReverse(long[] arr){
int n=arr.length;
List<Long> li=new ArrayList<>();
for(long x:arr){
li.add(x);
}
Collections.sort(li,Collections.reverseOrder());
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sort(long[] arr){
int n=arr.length;
List<Long> li=new ArrayList<>();
for(long x:arr){
li.add(x);
}
Collections.sort(li);
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static long sum(int[] arr){
long sum=0;
for(int x:arr){
sum+=x;
}
return sum;
}
private static long evenSumFibo(long n){
long l1=0,l2=2;
long sum=0;
while (l2<n) {
long l3=(4*l2)+l1;
sum+=l2;
if(l3>n)break;
l1=l2;
l2=l3;
}
return sum;
}
private static void initializeIO(){
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
System.setErr(new PrintStream(new FileOutputStream("error.txt")));
} catch (Exception e) {
// System.err.println(e.getMessage());
}
}
private static int maxOfArray(int[] arr){
int max=Integer.MIN_VALUE;
for(int x:arr){
max=Math.max(max,x);
}
return max;
}
private static long maxOfArray(long[] arr){
long max=Long.MIN_VALUE;
for(long x:arr){
max=Math.max(max,x);
}
return max;
}
private static int[][] readIntIntervals(int n,int m){
int[][] arr=new int[n][m];
for(int j=0;j<n;j++){
for(int i=0;i<m;i++){
arr[j][i]=sc.nextInt();
}
}
return arr;
}
private static long gcd(long a,long b){
if(b==0)return a;
return gcd(b,a%b);
}
private static int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
private static int[] readIntArray(int n){
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
return arr;
}
private static double[] readDoubleArray(int n){
double[] arr=new double[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextDouble();
}
return arr;
}
private static long[] readLongArray(int n){
long[] arr=new long[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextLong();
}
return arr;
}
private static void print(int[] arr){
out.println(Arrays.toString(arr));
}
private static void print(long[] arr){
out.println(Arrays.toString(arr));
}
private static void print(String[] arr){
out.println(Arrays.toString(arr));
}
private static void print(double[] arr){
out.println(Arrays.toString(arr));
}
private static void debug(String[] arr){
err.println(Arrays.toString(arr));
}
private static void debug(int[] arr){
err.println(Arrays.toString(arr));
}
private static void debug(long[] arr){
err.println(Arrays.toString(arr));
}
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
static class Dsu {
int[] parent, size;
Dsu(int n) {
parent = new int[n + 1];
size = new int[n + 1];
for (int i = 0; i <= n; i++) {
parent[i] = i;
size[i] = 1;
}
}
private int findParent(int u) {
if (parent[u] == u) return u;
return parent[u] = findParent(parent[u]);
}
private boolean union(int u, int v) {
// System.out.println("uf "+u+" "+v);
int pu = findParent(u);
// System.out.println("uf2 "+pu+" "+v);
int pv = findParent(v);
// System.out.println("uf3 " + u + " " + pv);
if (pu == pv) return false;
if (size[pu] <= size[pv]) {
parent[pu] = pv;
size[pv] += size[pu];
} else {
parent[pv] = pu;
size[pu] += size[pv];
}
return true;
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 983290ac2cf8a282514c5e99cb2146ff | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes |
import java.util.*;
import java.math.*;
import java.io.*;
public class Solution {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void sort(int a[]){ // int -> long
ArrayList<Integer> arr=new ArrayList<>(); // Integer -> Long
for(int i=0;i<a.length;i++)
arr.add(a[i]);
Collections.sort(arr);
for(int i=0;i<a.length;i++)
a[i]=arr.get(i);
}
private static long gcd(long a, long b){
if(b==0)return a;
return gcd(b,a%b);
}
private static long pow(long x,long y){
if(y==0)return 1;
long temp = pow(x, y/2);
if(y%2==1){
return x*temp*temp;
}
else{
return temp*temp;
}
}
static long powM(long a, long b) {
if (b == 0) return 1;
long res = pow(a, b / 2);
res = (res%mod * res%mod) % 1_000_000_007;
if (b % 2 == 1) {
res = (res%mod * a%mod) % 1_000_000_007;
}
return res%mod;
}
static int log(long n){
int res = 0;
while(n>0){
res++;
n/=2;
}
return res;
}
static int mod = (int)1e9+7;
static PrintWriter out;
static FastReader sc ;
public static void main(String[] args) throws IOException {
sc = new FastReader();
out = new PrintWriter(System.out);
// primes();
// ________________________________
// int test = 1;
StringBuilder output = new StringBuilder();
int test =sc.nextInt();
while (test-- > 0) {
//System.out.println(mdh+" "+nhc);
int n = sc.nextInt();
long a [] = new long[n];
for(int i =0;i<n;i++)
{
a[i] = sc.nextLong();
;
}
solver(n,a);
}
// solver(s);
// int n = sc.nextInt();
// out.println(solver());
// ________________________________
out.flush();
}
static class Edge
{
int u, v;
Edge(int u, int v)
{
this.u = u;
this.v = v;
}
}
static class Pair implements Comparable <Pair>{
int l, r;
Pair(int l, int r)
{
this.l = l;
this.r = r;
}
public int compareTo(Pair o)
{
return this.l-o.l;
}
}
static ArrayList<Integer>adj [] ;
static boolean [] vst ;
public static void solver( int n, long a []) {
boolean f1 = true;
for(int i =1;i<n;i++)
{
if(a[i]<a[i-1])
{
f1 = false;
}
}
if(f1||n==1)
{
System.out.println(0);
return ;
}
if(a[n-1]<a[n-2])
{
System.out.println(-1);
return ;
}
boolean flag =true;
ArrayList<Integer>ans = new ArrayList<>();
for(int i =n-3;i>=0;i--)
{
if((a[i+1]-a[n-1])<a[i])
{
a[i] = a[i+1]-a[n-1];
ans.add(i);
}
if(a[i]>a[i+1])
flag = false;
}
if(!flag)
System.out.println(-1);
else
{
System.out.println(ans.size());
for(int i :ans)
{
System.out.println(i+1+" "+(i+2)+" "+n);
}
}
}
static void dfs(int cur) {
if(vst[cur]) return ;
vst[cur]=true;
for(int c:adj[cur]) dfs(c);
}
public static int Mex(int []a, int i, int j)
{
boolean [] vis = new boolean[a.length+2];
int mex =0;
for(int k=i;k<j;k++)
{
if(a[k]<vis.length)
{
vis[a[k]] = true;
}
}
while(vis[mex]==true)
{
mex++;
}
return mex;
}
public static long log2(long N)
{
// calculate log2 N indirectly
// using log() method
long result = (long)(Math.log(N) / Math.log(2));
return result;
}
static long highestPowerof2(long x)
{
// check for the set bits
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
// Then we remove all but the top bit by xor'ing the
// string of 1's with that string of 1's shifted one to
// the left, and we end up with just the one top bit
// followed by 0's.
return x ^ (x >> 1);
}
public
static void rotate(int [] arr, int s, int n)
{
int x = arr[n], i;
for (i = n; i > s; i--)
arr[i] = arr[i-1];
arr[s] = x;
// for(int j=s;j<=n;j++)
// System.out.print(arr[j]+" ");
// System.out.println();
}
static int lower_bound(int[] a , long x)
{
int i = 0;
int j = a.length-1;
//if(arr[i] > key)return -1;
if(a[j] < x)return a.length;
while(i<j)
{
int mid = (i+j)/2;
if(a[mid] == x)
{
j = mid;
}
else if(a[mid] < x)
{
i = mid+1;
}
else
j = mid-1;
}
return i;
}
int upper_bound(int [] arr , int key)
{
int i = 0;
int j = arr.length-1;
if(arr[j] <= key)return j+1;
while(i<j)
{
int mid = (i+j)/2;
if(arr[mid] <= key)
{
i = mid+1;
}
else
j = mid;
}
return i;
}
static void reverseArray(int arr[], int start,
int end)
{
while (start < end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 7285155ed81445e67d0b6a4a6214bddd | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes |
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
static FastReader sc;
static StringBuilder sb = new StringBuilder();
public static void main(String args[]) {
sc=new FastReader();
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
long[]a=readArrayLong(n);
if(a[n-2]>a[n-1]){
sb.append(-1+"\n");
continue;
}
boolean check=true;
List<String>ans=new ArrayList<>();
for(int i=n-3;i>=0;i--){
if(a[i]>a[i+1]){
long k=a[i+1]-a[n-1];
if(k>a[i+1]){
check=false;
break;
}
a[i]=k;
int x=i,y=i+1,z=n-1;
ans.add(++x+ " "+ ++y+ " "+ ++z+ "\n");
}
}
if(check==false){
sb.append(-1+"\n");
continue;
}
sb.append(ans.size()+"\n");
for(String s:ans){
sb.append(s);
}
}
out.println(sb.toString());
}
public static class Pair{
public int a;
public int b;
public String s;
public long l;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return a == pair.a && b == pair.b;
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
Pair(int a , int b){
this.a = a;
this.b = b;
}
Pair(long a , int b){
this.l = a;
this.b = b;
}
Pair(int a , String b){
this.a = a;
this.s = b;
}
@Override
public String toString(){
return a + " -> " + b;
}
}
static void swap(int[]a,int i,int j){
int k=a[i];
a[i]=a[j];
a[j]=k;
}
static void printArray(int[]a){
int n=a.length;
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static boolean isVowel(char c)
{
return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U');
}
static int[] readArray(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) sc.nextInt();
}
return res;
}
static long[] readArrayLong(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = (int) sc.nextLong();
}
return res;
}
public static long gcd(long a, long b){
if(b == 0)
return a;
return gcd(b , a%b);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 89620ee12949bbb3e580ada260cbcb3c | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
public class Main {
// Graph
// prefix sums
//inputs
public static void main(String args[])throws Exception{
Input sc=new Input();
precalculates p=new precalculates();
StringBuilder sb=new StringBuilder();
int t=sc.readInt();
for(int f=0;f<t;f++){
int n=sc.readInt();
long a[]=sc.readArrayLong();
if(a[n-1]<a[n-2]){
sb.append("-1\n");
continue;
}
boolean b=true;
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1]){
b=false;
break;
}
}
if(b){
sb.append("0\n");
continue;
}
int count=0;
StringBuilder s=new StringBuilder();
for(int i=0;i<n-2;i++){
count++;
s.append((i+1)+" "+(n-1)+" "+(n)+"\n");
a[i]=a[n-2]-a[n-1];
}
long sum=a[n-2]+a[n-1];
if(Math.abs(sum)<=Math.pow(10,18)){
b=false;
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1]){
b=true;
break;
}
}
if(b){
sb.append("-1\n");
continue;
}
sb.append(count+"\n"+s);
}else
sb.append("-1\n");
}
System.out.print(sb);
}
}
class Input{
BufferedReader br;
StringTokenizer st;
Input(){
br=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
}
public int[] readArray() throws Exception{
st=new StringTokenizer(br.readLine());
int a[]=new int[st.countTokens()];
for(int i=0;i<a.length;i++){
a[i]=Integer.parseInt(st.nextToken());
}
return a;
}
public long[] readArrayLong() throws Exception{
st=new StringTokenizer(br.readLine());
long a[]=new long[st.countTokens()];
for(int i=0;i<a.length;i++){
a[i]=Long.parseLong(st.nextToken());
}
return a;
}
public int readInt() throws Exception{
st=new StringTokenizer(br.readLine());
return Integer.parseInt(st.nextToken());
}
public long readLong() throws Exception{
st=new StringTokenizer(br.readLine());
return Long.parseLong(st.nextToken());
}
public String readString() throws Exception{
return br.readLine();
}
public int[][] read2dArray(int n,int m)throws Exception{
int a[][]=new int[n][m];
for(int i=0;i<n;i++){
st=new StringTokenizer(br.readLine());
for(int j=0;j<m;j++){
a[i][j]=Integer.parseInt(st.nextToken());
}
}
return a;
}
}
class precalculates{
public long gcd(long p, long q) {
if (q == 0) return p;
else return gcd(q, p % q);
}
public int[] prefixSumOneDimentional(int a[]){
int n=a.length;
int dp[]=new int[n];
for(int i=0;i<n;i++){
if(i==0)
dp[i]=a[i];
else
dp[i]=dp[i-1]+a[i];
}
return dp;
}
public int[] postSumOneDimentional(int a[]) {
int n = a.length;
int dp[] = new int[n];
for (int i = n - 1; i >= 0; i--) {
if (i == n - 1)
dp[i] = a[i];
else
dp[i] = dp[i + 1] + a[i];
}
return dp;
}
public int[][] prefixSum2d(int a[][]){
int n=a.length;int m=a[0].length;
int dp[][]=new int[n+1][m+1];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j]=a[i-1][j-1]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1];
}
}
return dp;
}
public long pow(long a,long b){
long mod=998244353;
long ans=1;
if(b<=0)
return 1;
if(b%2==0){
ans=pow(a,b/2)%mod;
return ((ans%mod)*(ans%mod))%mod;
}else{
ans=pow(a,b-1)%mod;
return ((a%mod)*(ans%mod))%mod;
}
}
}
class GraphInteger{
HashMap<Integer,vertex> vtces;
class vertex{
HashMap<Integer,Integer> children;
public vertex(){
children=new HashMap<>();
}
}
public GraphInteger(){
vtces=new HashMap<>();
}
public void addVertex(int a){
vtces.put(a,new vertex());
}
public void addEdge(int a,int b,int cost){
if(!vtces.containsKey(a)){
vtces.put(a,new vertex());
}
if(!vtces.containsKey(b)){
vtces.put(b,new vertex());
}
vtces.get(a).children.put(b,cost);
// vtces.get(b).children.put(a,cost);
}
public boolean isCyclicDirected(){
boolean isdone[]=new boolean[vtces.size()+1];
boolean check[]=new boolean[vtces.size()+1];
for(int i=1;i<=vtces.size();i++) {
if (!isdone[i] && isCyclicDirected(i,isdone, check)) {
return true;
}
}
return false;
}
private boolean isCyclicDirected(int i,boolean isdone[],boolean check[]){
if(check[i])
return true;
if(isdone[i])
return false;
check[i]=true;
isdone[i]=true;
Set<Integer> set=vtces.get(i).children.keySet();
for(Integer ii:set){
if(isCyclicDirected(ii,isdone,check))
return true;
}
check[i]=false;
return false;
}
}
class union_find {
int n;
int[] sz;
int[] par;
union_find(int nval) {
n = nval;
sz = new int[n + 1];
par = new int[n + 1];
for (int i = 0; i <= n; i++) {
par[i] = i;
sz[i] = 1;
}
}
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
boolean find(int a, int b) {
return root(a) == root(b);
}
int union(int a, int b) {
int ra = root(a);
int rb = root(b);
if (ra == rb)
return 0;
if(a==b)
return 0;
if (sz[a] > sz[b]) {
int temp = ra;
ra = rb;
rb = temp;
}
par[ra] = rb;
sz[rb] += sz[ra];
return 1;
}
}
/*
static int mod=998244353;
private static int add(int x, int y) {
x += y;
return x % MOD;
}
private static int mul(int x, int y) {
int res = (int) (((long) x * y) % MOD);
return res;
}
private static int binpow(int x, int y) {
int z = 1;
while (y > 0) {
if (y % 2 != 0)
z = mul(z, x);
x = mul(x, x);
y >>= 1;
}
return z;
}
private static int inv(int x) {
return binpow(x, MOD - 2);
}
private static int devide(int x, int y) {
return mul(x, inv(y));
}
private static int C(int n, int k, int[] fact) {
return devide(fact[n], mul(fact[k], fact[n-k]));
}
*/
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 46d906eea925b1d42433e8b275bc259e | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* @author 邶风
* @data 2022/2/20 22:33
*/
public class Main {
static class FastReader{
StringTokenizer st;
BufferedReader br;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st==null||!st.hasMoreElements()){
try {
st=new StringTokenizer(br.readLine());
}catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static PrintWriter out = new PrintWriter(System.out);
static FastReader in = new FastReader();
static class Pll{
int x,y,z;
public Pll(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
static void solve(){
int n=in.nextInt();
int a[]=new int[n+10];
boolean flag=true;
for(int i=1;i<=n;i++) {
a[i] = in.nextInt();
if(i!=1&&(a[i]<a[i-1])){
flag=false;
}
}
if(flag){
out.println(0);
return;
}
if(a[n-1]>=0){
if(a[n-1]>a[n]){
out.println(-1);
return;
}
}else{
if(a[n]<0){
out.println(-1);
return;
}
}
out.println(n-2);
for(int i=1;i<=n-2;i++){
out.println(i+" "+(n-1)+" "+(n));
}
}
public static void main(String[] args) {
int t=in.nextInt();
while (t-->0){
solve();
out.flush();
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 51dd0cbc820456298d9ffafdb07badb3 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes |
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Solution {
/* -------------------------------------- Main Start ----------------------------------- */
public static void main(String[] args) throws IOException {
FastReader fs = new FastReader();
StringBuilder sb = new StringBuilder();
int t = Integer.parseInt(fs.nextLine());
while(t-- > 0){
int n = fs.nextInt();
long[] arr = fs.IL(n);
if(arr[n - 2] > arr[n - 1]){
System.out.println("-1");
continue;
}
long l1 = arr[n - 1], l2 = arr[n - 2];
boolean dec = true;
for(int i = 0; i < n - 1; i++){
if(arr[i] > arr[i + 1]){
dec = false;
break;
}
}
if(dec){
System.out.println("0");
continue;
}
if(l2 - l1 <= l2 && l2 <= l1){
System.out.println(n - 2);
for(int i = 0; i < n - 2; i++){
System.out.println((i + 1) + " " + (n - 1) + " " + n);
}
continue;
}
System.out.println("-1");
}
}
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
StringBuilder sb = new StringBuilder();
sb.append(x + "\n");
Collections.sort(arr, (a, b) -> Integer.compare(a[0], b[0]))
arr.toArray(new int[arr.size()][])
List<List<Integer>> pair = new ArrayList<>();
pair.add(Arrays.asList(nums[i], nums[j],-sum));
*/
private static final int MOD_1 = 1000000000 + 7;
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
String nextLine() throws IOException {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
/* --------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
void printArray(int[] arr) {
System.out.println(Arrays.toString(arr));
}
void printArray(String[] arr) {
System.out.println(Arrays.toString(arr));
}
void printArray(long[] arr) {
System.out.println(Arrays.toString(arr));
}
void print(int data) {
System.out.println(data);
}
void print(String data) {
System.out.println(data);
}
void print(long data) {
System.out.println(data);
}
int[] II(int n) throws IOException {
int[] d = new int[n];
String[] arr = nextLine().split(" ");
for (int i = 0; i < n; i++) {
d[i] = Integer.parseInt(arr[i]);
}
return d;
}
String[] IS(int n) throws IOException {
return nextLine().split(" ");
}
long[] IL(int n) throws IOException {
long[] d = new long[n];
String[] arr = nextLine().split(" ");
for (int i = 0; i < n; i++) {
d[i] = Long.parseLong(arr[i]);
}
return d;
}
public long gcd(long a, long b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
public long power(long x, long y, long p) {
long res = 1;
x = x % p;
if (x == 0) {
return 0;
}
while (y > 0) {
if ((y & 1) == 1) {
res = (res * x) % p;
}
y = y >> 1;
x = (x * x) % p;
}
return res;
}
void sieveOfEratosthenes(boolean prime[], int size) {
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
prime[2] = true;
for (int p = 2; p * p < size; p++) {
if (prime[p] == true) {
for (int i = p * p; i < size; i += p) {
prime[i] = false;
}
}
}
}
public long fact(long n) {
long ans = 1;
for (int i = 2; i <= n; i++) {
ans = (ans * i) % MOD_1;
}
return ans;
}
public long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
public int LowerBound(int[] nums, int target){
int result = -1;
int low = 0;
int high = nums.length - 1;
while(low <= high){
int mid = low + ((high-low)/2);
if (nums[mid] < target){
low = mid +1;
} else if (nums[mid] > target){
high = mid - 1;
} else {
// nums[mid] == target
// Set (result = mid), we want lower want so we move towards left
result = mid;
high = mid - 1;
}
}
return result;
}
public int UpperBound(int[] nums, int target){
int result = -1;
int low = 0;
int high = nums.length - 1;
while(low <= high){
int mid = low + (high-low)/2;
if (nums[mid] < target){
low = mid +1;
} else if (nums[mid] > target){
high = mid - 1;
} else {
// nums[mid] == target
// Set (result = mid), we want upper bound so we move towards right
result = mid;
low = mid + 1;
}
}
return result;
}
// complexity O(r)
public long ncr(long n, long r){
// 10C3 = (10*9*8) / (3*2*1)
// 6C4 = (6*5*4*3) / (4*3*2*1)
double result = 1;
for(int i = 1; i <= r; i++){
result *= ((double)(n - r + i) / i);
}
return (long) result;
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | b8c693b6ea32f17e9f14e8765738a52d | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.IOException;
public class buildthepermutation {
public static void main(String[] args) throws IOException {
class Tripl {
int ind1;
int ind2;
int ind3;
public Tripl(int ind1, int ind2, int ind3) {
this.ind1 = ind1;
this.ind2 = ind2;
this.ind3 = ind3;
}
}
Scanner in = new Scanner(System.in);
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
int tt = in.nextInt();
for(int w = 0; w< tt; w++) {
int n = in.nextInt();
long[] a =new long[n];
Queue<Tripl> kj = new LinkedList<>();
long trenmin = Long.MAX_VALUE;
long trenmax = Long.MIN_VALUE;
int indmin = 0;
int indmax = 0;
int cnt = 0;
int flag = 0;
for(int i = 0; i<n; i++) {
a[i] = in.nextLong();
}
if(a[n-2]>a[n-1]) {
log.write(-1+"\n");
log.flush();
continue;
}
else {
for(int i = n-1; i>=1; i--) {
if(a[i]<=trenmin) {
trenmin = a[i];
indmin = i;
}
if(a[i]>trenmax) {
trenmax = a[i];
indmax = i;
}
if(a[i]<a[i-1]) {
if(trenmin-trenmax>trenmin) {
log.write(-1+"\n");
log.flush();
flag = 1;
break;
}
if(a[i-1]<trenmin-trenmax) {
log.write(-1+"\n");
log.flush();
flag = 1;
break;
}
else {
cnt++;
kj.add(new Tripl(i,indmin+1, indmax+1));
a[i-1] = trenmin-trenmax;
}
}
}
}
if(flag==0) {
log.write(cnt+"\n");
for(int i = 0; i<cnt; i++) {
log.write(kj.peek().ind1+" "+kj.peek().ind2+" "+kj.peek().ind3+"\n");
kj.poll();
}
log.flush();
}
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | a1482c3d4755801117739eeb30567ef6 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.io.*;
public class MergeKSorted {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]) {
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t, n, countOp = 0;
t = sc.nextInt();
loop1: for (int i = 0; i < t; i++) {
n = sc.nextInt();
int lastPosIndex = -1;
long arr[] = new long[n];
String sol = "";
for (int j = 0; j < n; j++)
arr[j] = sc.nextLong();
if(arr[n-1] >= 0)
lastPosIndex = n;
if(arr[n-2]>arr[n-1]) {
System.out.println(-1);
continue;
}
if(arr[n-1] < 0) {
for (int j = n - 3; j >= 0; j--) {
if (arr[j] > arr[j + 1]) {
System.out.println(-1);
continue loop1;
}
}
out.println(0);
}
else {
out.println(n-2);
for(int j=n-3;j>=0;j--) {
out.println((j+1)+" "+(j+2)+" "+lastPosIndex);
}
}
out.flush();
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 3da8dd6dd11ec016eb53908f223b8900 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.BufferedReader;
import java.lang.*;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.util.*;
import java.util.LinkedList;
public class CEdu{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void sort(long[] arr) {
ArrayList<Long> a = new ArrayList<>();
for (long i : arr) {
a.add(i);
}
Collections.sort(a);
for (int i = 0; i < a.size(); i++) {
arr[i] = a.get(i);
}
}
static boolean get(char[] ch, int i, long k) {
long ope = 0;
for(int j = i; j >=0; j--) {
long ele = ((ch[j]-'0') + ope)%10;
long req = 10 - ele;
if(req == 10) {
req = 0;
}
if(ope+req <= k) {
ope = ope + req;
if(j == 0) {
return true;
}
}else {
return false;
}
}
return false;
}
static void solve(long a, long b, long c) {
System.out.println((a|b) & (b|c) & (c|a));
}
static long get(long[] arr , int low, int high) {
long len = high-low+1;
for(int i = low; i <= high; i++) {
if(arr[i] == 0) {
len++;
}
}
return len;
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
int t = sc.nextInt();
test: while(t-- > 0) {
int n = sc.nextInt();
long[] arr = new long[n];
long[] brr = new long[n];
for(int i = 0; i < n; i++){
arr[i] = sc.nextLong();
brr[i] = arr[i];
}
boolean sor = true;
sort(brr);
for(int i = 0; i < n; i++){
if(arr[i] != brr[i]) {
sor = false;
break;
}
}
if(sor){
System.out.println(0);
continue test;
}
if(arr[n-1] < 0){
System.out.println(-1);
continue test;
}
if(arr[n-2] <= arr[n-1]) {
System.out.println(n-2);
for(int i = 0; i < n-2; i++){
System.out.println((i+1)+" "+(n-1)+" "+(n));
}
}else {
System.out.println(-1);
}
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | f3d09e4326971c66a1bbdba7f09c1557 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.lang.*;
import java.util.*;
import java.io.*;
import java.awt.Point;
public class A
{
static class Ans {
int x, y, z;
Ans(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
void solve_test() {
int n = ri();
long[] a = rla(n);
ArrayList<Ans> answer = new ArrayList<>();
long[] b = Arrays.copyOf(a, n);
Arrays.sort(b);
if(Arrays.equals(a, b)) {
out.println(0);
return;
}
if(a[n - 2] > a[n - 1]) {
out.println("-1");
return;
}
for(int i = n - 3; i >= 0; i--) {
if(a[i] < a[i + 1] && a[i] < a[i + 1] - a[n - 1]) continue;
a[i] = Math.min(a[i + 1], a[n - 1]) - Math.max(a[i + 1], a[n - 1]);
answer.add(new Ans(i + 1, i + 2, n));
if(a[i] > a[i + 1]) {
out.println("-1");
return;
}
}
out.println(answer.size());
for(var el : answer) {
out.printf("%d %d %d\n", el.x, el.y, el.z);
}
}
long[] rla(int n) {
long[] a = new long[n];
for(int i = 0; i < n; ++i) a[i] = rl();
return a;
}
void solve() {
int t = ri();
while(t-- > 0) solve_test();
}
int gcd(int a, int b) {
return a == 0 ? b : gcd(b % a, a);
}
public static void main(String... args) {
new A().run();
}
void run() {
try {
solve();
out.close();
} catch(Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
String readLine() {
try {
return in.readLine();
} catch(Exception e) {
throw new RuntimeException(e);
}
}
String rs() {
while(!tok.hasMoreTokens()) {
tok = new StringTokenizer(readLine());
}
return tok.nextToken();
}
int ri() {
return Integer.parseInt(rs());
}
long rl() {
return Long.parseLong(rs());
}
int[] ria(int n) {
int[] a = new int[n];
for(int i = 0; i < n; i++) a[i] = ri();
return a;
}
double rd() {
return Double.parseDouble(rs());
}
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer tok = new StringTokenizer("");
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 4796df47bb39565063024a0346a3ba3a | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.security.cert.X509CRL;
import java.util.*;
import java.lang.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
@SuppressWarnings("unused")
public class Main {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static String OUTPUT = "";
//global
private final static long BASE = 998244353L;
private final static int ALPHABET = (int)('z') - (int)('a') + 1;
//private final static int BASE = 1000000007;
private final static int INF_I = (1<<31)-1;
private final static long INF_L = (1l<<63)-1;
private final static int MAXN = 200100;
private final static int MAXK = 31;
private final static int[] DX = {-1,0,1,0};
private final static int[] DY = {0,1,0,-1};
private static long pw(int a,int n) {
if (n==0) return 1l;
if (n==1) return 1l*a;
long tmp = pw(a, n/2);
tmp = tmp * tmp %BASE;
if (n%2==0) return tmp;
return tmp*a%BASE;
}
static void solve() {
int ntest = readInt();
for (int test=0;test<ntest;test++) {
int N = readInt();
long[] A = readLongArray(N);
boolean isOk = (A[N-2]<=A[N-1]);
if (!isOk) out.println(-1);
else if (A[N-1]<0) {
isOk = true;
for (int i=0;i+1<N;i++) isOk &= (A[i]<=A[i+1]);
if (isOk) out.println(0);
else out.println(-1);
}
else {
List<Integer> ans = new ArrayList<>();
for (int i=N-3; i>=0;i--) {
A[i] = A[N-2] - A[N-1];
ans.add(i);
}
out.println(ans.size());
for (int i:ans) out.format("%d %d %d\n", i+1, N-1, N);
}
}
}
public static void main(String[] args) throws Exception
{
long S = System.currentTimeMillis();
if (INPUT=="") {
is = System.in;
} else {
File file = new File(INPUT);
is = new FileInputStream(file);
}
if (OUTPUT == "") out = new PrintWriter(System.out);
else out = new PrintWriter(OUTPUT);
solve();
out.flush();
long G = System.currentTimeMillis();
}
private static class Point<T extends Number & Comparable<T>> implements Comparable<Point<T>> {
private T x;
private T y;
public Point(T x, T y) {
this.x = x;
this.y = y;
}
public T getX() {return x;}
public T getY() {return y;}
@Override
public int compareTo(Point<T> o) {
int cmp = x.compareTo(o.getX());
if (cmp==0) return y.compareTo(o.getY());
return cmp;
}
}
private static class ClassComparator<T extends Comparable<T>> implements Comparator<T> {
public ClassComparator() {}
@Override
public int compare(T a, T b) {
return a.compareTo(b);
}
}
private static class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> {
public ListComparator() {}
@Override
public int compare(List<T> o1, List<T> o2) {
for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) {
int c = o1.get(i).compareTo(o2.get(i));
if (c != 0) {
return c;
}
}
return Integer.compare(o1.size(), o2.size());
}
}
private static boolean eof()
{
if(lenbuf == -1)return true;
int lptr = ptrbuf;
while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
try {
is.mark(1000);
while(true){
int b = is.read();
if(b == -1){
is.reset();
return true;
}else if(!isSpaceChar(b)){
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inbuf = new byte[1024];
static int lenbuf = 0, ptrbuf = 0;
private static int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
// private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); }
private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private static double readDouble() { return Double.parseDouble(readString()); }
private static char readChar() { return (char)skip(); }
private static String readString()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private static char[] readChar(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] readTable(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = readChar(m);
return map;
}
private static int[] readIntArray(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = readInt();
return a;
}
private static long[] readLongArray(int n) {
long[] a = new long[n];
for (int i=0;i<n;i++) a[i] = readLong();
return a;
}
private static int readInt()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static long readLong()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 60b3268617a8f110879bb37455ab9399 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.*;
import java.util.*;
public class codeforces {
static class in {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static String next() throws IOException {
while (!tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
//System.out.println(" I WAS CALLED");
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws IOException {
int t= in.nextInt();
StringBuilder st = new StringBuilder();
while (t>0){
int n=in.nextInt();
int[] arr = new int[n];
boolean flag= true;
for(int i=0;i<n;i++){
arr[i]=in.nextInt();
if(i!=0&&arr[i]<arr[i-1])flag=false;
}
if(flag)st.append(0).append('\n');
else if(arr[n-2]>arr[n-1])st.append(-1).append('\n');
else {
if(arr[n-2]-arr[n-1]<=arr[n-2]){
st.append(n-2).append('\n');
for(int i=0;i<n-2;i++)st.append(i+1).append(' ').append(n-1).append(' ').append(n).append('\n');
}
else{
st.append(-1).append('\n');
}
}
t--;
}
System.out.println(st);
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 71b281d5e86d96ebe43d69fa00b6b19f | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | // package div_2_772;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class c{
public static void main(String[] args){
FastReader sc = new FastReader();
int t=sc.nextInt();
while(t-->0){
int n =sc.nextInt();
int a[]=sc.fastArray(n);
int b[]=a.clone();
Arrays.sort(b);
if(check(a,b))System.out.println(0);
else if(a[n-2]>a[n-1]) System.out.println(-1);
else if(a[n-2]-a[n-1]>a[n-2]) System.out.println(-1);
else {
System.out.println(n-2);
for(int i=0;i<n-2;i++) {
System.out.println((i+1)+" "+(n-1)+" "+n);
}
}
}
}
static boolean check(int a[],int b[]) {
for(int i=0;i<a.length;i++) {
if(a[i]!=b[i])return false;
}
return true;
}
static int pow(int a,int b) {
if(b==0)return 1;
if(b==1)return a;
return a*pow(a,b-1);
}
static class pair {
int x,y, z,val;
pair(int x,int y,int z,int val){
this.x=x;
this.y=y;
this.z=z;
this.val=val;
}
void update(int x,int y,int z,int val){
this.x=x;
this.y=y;
this.z=z;
this.val=val;
}
}
static ArrayList<Integer> primeFac(int n){
ArrayList<Integer>ans = new ArrayList<Integer>();
int lp[]=new int [n+1];
Arrays.fill(lp, 0); //0-prime
for(int i=2;i<=n;i++) {
if(lp[i]==0) {
for(int j=i;j<=n;j+=i) {
if(lp[j]==0) lp[j]=i;
}
}
}
int fac=n;
while(fac>1) {
ans.add(lp[fac]);
fac=fac/lp[fac];
}
print(ans);
return ans;
}
static ArrayList<Long> prime_in_given_range(long l,long r){
ArrayList<Long> ans= new ArrayList<>();
int n=(int)Math.sqrt(r)+1;
int prime[]=sieve_of_Eratosthenes(n);
long res[]=new long [(int)(r-l)+1];
for(int i=0;i<=r-l;i++) {
res[i]=i+l;
}
for(int i=0;i<prime.length;i++) {
if(prime[i]==1) {
System.out.println(2);
for(int j=Math.max((int)i*i, (int)(l+i-1)/i*i);j<=r;j+=i) {
res[j-(int)l]=0;
}
}
}
for(long i:res) if(i!=0)ans.add(i);
return ans;
}
static int [] sieve_of_Eratosthenes(int n) {
int prime[]=new int [n];
Arrays.fill(prime, 1); // 1-prime | 0-not prime
prime[0]=prime[1]=0;
for(int i=2;i<n;i++) {
if(prime[i]==1) {
for(int j=i*i;j<n;j+=i) {
prime[j]=0;
}
}
}
return prime;
}
static long binpow(long a,long b) {
long res=1;
if(b==0)return 1;
if(a==0)return 0;
while(b>0) {
if((b&1)==1) {
res*=a;
}
a*=a;
b>>=1;
}
return res;
}
static void print(int a[]) {
System.out.println(a.length);
for(int i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long a[]) {
System.out.println(a.length);
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static long rolling_hashcode(String s ,int st,int end,long Hashcode,int n) {
if(end>=s.length()) return -1;
int mod=1000000007;
Hashcode=Hashcode-(s.charAt(st-1)*(long)Math.pow(27,n-1));
Hashcode*=10;
Hashcode=(Hashcode+(long)s.charAt(end))%mod;
return Hashcode;
}
static long hashcode(String s,int n) {
long code=0;
for(int i=0;i<n;i++) {
code+=((long)s.charAt(i)*(long)Math.pow(27, n-i-1)%1000000007);
}
return code;
}
static void print(ArrayList<Integer> a) {
System.out.println(a.size());
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
int [] fastArray(int n) {
int a[]=new int [n];
for(int i=0;i<n;i++) {
a[i]=nextInt();
}
return a;
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 4ac10d12fa494b46ac1514a2cf340aa4 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | //Siddhartha Bose
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class hacker49 {
//protected h() {
//}
public static int r1=0;
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static long[][] f=new long[501][501];
public static void main(String[] args) {
OutputStream outputStream =System.out;
PrintWriter out =new PrintWriter(outputStream);
FastReader s=new FastReader();
//TreeNode root= new TreeNode(1);
//root.left=new TreeNode(3);
//List<List<Integer>> t=verticalTraversal(root);
int t=s.nextInt();
while(t>0) {
int n=s.nextInt();
int[] a=new int[n+1];
int ans=0;
for(int i=1;i<=n;i++) {
a[i]=s.nextInt();
}
int[] pre=new int[n+1];
if(a[n]<a[n-1]) {
out.println(-1);
}else {
int i=n;
while(i>0 && a[i]<0) {
i--;
}
ArrayList<pair3> e=new ArrayList<>();
int d=i-1;
int g=i;
for(int j=1;j<i-1;j++) {
e.add(new pair3(j,i-1,i));
a[j]=a[i-1]-a[i];
}
boolean p=true;
for(int j=1;j<n;j++) {
if(a[j]>a[j+1]) {p=false;}
}
if(!p) {out.println(-1);}else {
out.println(e.size());
for( i=0;i<e.size();i++) {
out.println(e.get(i).a+" "+e.get(i).b+" "+e.get(i).c);
}
}
}
t--;
}
out.close();
}
static class pair3{
int a;
int b;
int c;
pair3(int a,int b,int c){
this.a=a;
this.b=b;
this.c=c;
}
}
// class MinStack {
// static Stack<Integer> st= st=new Stack<>();
// static int min=Integer.MAX_VALUE;
//// public MinStack() {
//
//
//// }
//
// static public void push(int val) {
// if(st.isEmpty()){
// st.add(val);
// min=val;
// return;
// }
// if(val>=min){
// st.add(val);
// }else{
// st.add(2*val-min);
// min=val;
// }
// }
//
// static public void pop() {
// if(st.peek()<=min){
// min=2*min-st.peek();
//// st.pop();
// }
// st.pop();
// }
//
// static public int top() {
// if(st.peek()<=min){
// return min;
// }
// return st.peek();
// }
//
// static public int getMin() {
// return min;
// }
//
// static int[] ans=new int[100001];
// static int[] deg=new int[100001];
// static void dfs(int node,int v) {
// vis[node]=1;
//// deg[node]++;
//
// for(int i=0;i<f1[node].size();i++) {
// if(vis[f1[node].get(i).a]==0) {
// int g=2;
// if(v==2) {
// g=5;
// }
// ans[f1[node].get(i).b]=v;
// dfs(f1[node].get(i).a,g);
//// deg[node]++;
//
// }
//
// }
// }
//// static long ans(int[] a,int[] b,long till,long[] post,int n,int k,int i) {
//// if(k==0 || i==n) {
//// return till+post[i];
//// }
//// if(f[i][k]!=-1) {
//// return f[i][k];
//// }
////
//// }
//// static class pair
// static int[] vis=new int[100001];
// static class pair1 implements Comparable<pair1>{
// private int a;
// private int b;
// pair1(int a,int b){
// this.a=a;
// this.b=b;
// }
// @Override
// public int compareTo(pair1 o) {
// // TODO Auto-generated method stub
// return Integer.compare(this.a, o.a);
// }
//
//
//
// }
// static class pair3 implements Comparable<pair3>{
// private int a;
// private int b;
// private int d;
// private int c;
// pair3(int a,int b,int c,int d){
// this.a=a;
// this.d=d;
// this.b=b;
// this.c=c;
// }
// @Override
// public int compareTo(pair3 c) {
// if(c.b-c.a!=this.b-this.a) {
// return Integer.compare(c.b-c.a, this.b-this.a);
// }else {
// return Integer.compare(this.c, c.c);
// }
//
// }
// }
// static class pair2 implements Comparable<pair2>{
// private int a;
// private int b;
// private int d;
// private int c;
// pair2(int a,int b,int c,int d){
// this.a=a;
// this.d=d;
// this.b=b;
// this.c=c;
// }
// public int compareTo(pair2 c) {
// if(c.b!=this.b) {
// return Integer.compare(c.b, this.b);
// }else {
// if(c.c!=this.c) {
// return Integer.compare(this.c, c.c);
// }else {
// return Integer.compare(this.a, c.a);
// }
//
// }
//
// }
// }
// static boolean r(int k) {
// for(int i=2;i<=k/2;i++) {
// if(k%i==0) {
// return false;
// }
// }
// return true;
// }
// public static int[] is_prime=new int[1000001];
// public static int[] pre=new int[1000001];
// public static ArrayList<Integer> primes=new ArrayList<>();
// public static void sieve() {
// long maxN=1000000;
// for(int i=1;i<=maxN;i++) {
// is_prime[i]=1;
// }
// is_prime[0]=0;
// is_prime[1]=0;
// for(long i=2;i*i<=maxN;i++) {
// if(is_prime[(int) i]==1) {
//// primes.add((int) i);
// for(long j=i*i;j<=maxN;j+=i) {
// is_prime[(int) j]=0;
// }
// }
// }
// for(int i=1;i<=1000000;i++) {
// pre[i]=pre[i-1]+is_prime[i];
// }
// for(int i=0;i<=maxN;i++) {
// if(is_prime[i]==1) {
// primes.add(i);
// }
// }
//// int count=0;
//// for(long i=1;i<=maxN;i++) {
//// if(is_prime[(int) i]==1) {
//// count++;
//// }
//// if(is_prime[count]==1) {
//// arr[(int) i]=1;
//// }else {
//// arr[(int)i]=0;
//// }
//// }
// }
// public static long[] merge_sort(long[] A, int start, int end) {
// if (end > start) {
// int mid = (end + start) / 2;
// long[] v = merge_sort(A, start, mid);
// long[] o = merge_sort(A, mid + 1, end);
// return (merge(v, o));
// } else {
// long[] y = new long[1];
// y[0] = A[start];
// return y;
// }
// }
// static ArrayList<pair1>[] f1=new ArrayList[100001];
//
// public static long[] merge(long a[], long b[]) {
//// int count=0;
// long[] temp = new long[a.length + b.length];
// int m = a.length;
// int n = b.length;
// int i = 0;
// int j = 0;
// int c = 0;
// while (i < m && j < n) {
// if (a[i] < b[j]) {
// temp[c++] = a[i++];
//
// } else {
// temp[c++] = b[j++];
// }
// }
// while (i < m) {
// temp[c++] = a[i++];
// }
// while (j < n) {
// temp[c++] = b[j++];
// }
// return temp;
// }
//
// static int MAX = (int) 1000000;
//
// //Initialize global divisor vector
// //array of sequence container
// static ArrayList<Integer> []divisor = new ArrayList[MAX + 1];
//
// //Calculate all
// //divisors of number
// static void sieve1()
// {
// for (int i = 1; i <= MAX; ++i)
// {
// for (int j = i; j <= MAX; j += i)
// divisor[j].add(i);
// }
// }
// public static int getMedian(ArrayList<ArrayList<Integer>> matrix)
// {
// int n=matrix.size();
// int m=matrix.get(0).size();
// int mid=(n*m)/2;
// int l=0;
// int h=(int)1e5;
// while(l<h){
// int count=0;
// int val=(l+h)/2;
//// System.out.println(l+" "+h);
// for(int i=0;i<n;i++){
// count+=getcount(matrix.get(i),val,m);
// }
// if(count<=mid){
// l=val+1;
// }else{
// h=val;
// }
// }
// return h;
// }
// static int getcount(ArrayList<Integer> mat,int val,int m){
// int count=0;
// int l=0;
// int h=m-1;
//// System.out.println(val);
// while(l<=h){
// int mid=(l+h)/2;
//// if(val>3)
//// System.out.println(l+" "+h+" "+mid+" "+val);
// if(mat.get(mid)>val){
// h=mid-1;
// }else{
// l=mid+1;
// }
// }
// return h;
// }
// static int books(int[] a, int c,int n) {
// int l=0;
// int r=(int)1e10;
// for(int i=0;i<1000;i++){
// int mid=(l+r)/2;
//// System.out.println(mid);
// if(isGood(a,c,n,mid)){
// r=mid;
//// System.out.println(mid+" "+1);
// }else{
// l=mid;
// }
// }
// return r;
// }
// static boolean isGood(int[] a ,int c,int n,int target){
//
// int count=1;
//// long diff=0;
// int curr=0;
// for(int i=0;i<n;i++) {
// if(i==0) {
// curr=a[0];
// }else {
// if(a[i]-curr>=target) {
// curr=a[i];
// count++;
// }
// }
// }
//
// return (((count)>=c));
// }
// public static int[] nextGreater(int[] arr, int n) {
// Stack<Integer> s=new Stack<>();
// int[] ans=new int[n];
// for(int i=2*n-1;i>=0;i--){
// while(!s.isEmpty() && s.peek()<=arr[i%n]) {
// s.pop();
// }
// if(i<n) {
// if(s.isEmpty()) {
// ans[i]=-1;
// }else {
// ans[i]=s.peek();
// }
// }
// s.add(arr[i%n]);
// }
// return ans;
// }
// static void insert(int x){
// if(st.isEmpty() || st.peek()>=x) {
// st.add(x);
// }else {
// int t=st.pop();
// insert(x);
// st.push(t);
// }
//
// }
// static HashSet<Long> h2=new HashSet<>();
// static HashSet<Long> h1=new HashSet<>();
// static void get(long[] a,long d,int i,int n) {
// if(i==n+1) {
// h1.add(d);
// return;
// }
// d+=a[i];
// get(a,d,i+1,n);
// d-=a[i];
// d^=a[i];
// get(a,d,i+1,n);
// d^=a[i];
// }
//// static Stack<Integer> st=new Stack<>();
// static void reverse() {
// if(!st.isEmpty()) {
// int k=st.pop();
// reverse();
// insert(k);
// }
// }
// static int[] z_algo(String str){
// int n=str.length();
// int[] z=new int[n];
// int l = 0,r=0;
// for(int i=1;i<n;i++) {
// if(i<=r) z[i]=Math.min(z[i-l], r-i+1);
// while(i+z[i]<n && str.charAt(i+z[i])==str.charAt(z[i])) {
// z[i]++;
// }
// if(i+z[i]-1>r) {
// l=i;
// r=i+z[i]-1;
// }
// }
//
// return z;
//
// }
// static int[] KMP(String str) {
// int n=str.length();
// int[] kmp=new int[n];
// int j=-1;
// for(int i=1;i<n;i++) {
// j=kmp[i-1];
// while(j>0 && str.charAt(j)!=str.charAt(i))j=kmp[j-1];
// if(str.charAt(j)==str.charAt(i))j++;
// kmp[i]=j;
// }
// return kmp;
// }
// public ArrayList <Integer> bottomView(Node root)
// {
// ArrayList<Integer> e=new ArrayList<>();
// HashSet<Integer> f=new HashSet<>();
// Queue<pair> q=new LinkedList<>();
// ans(root,f,q,0);
// while(!q.isEmpty()){
// pair d=((LinkedList<pair>) q).pollFirst();
// if(f.contains(d.a)){
// e.add(d.b);
// f.remove(d.a);
// }
// }
// return e;
// }
// static class pair{
// int a;
// int b;
// pair(int a,int b){
// this.a=a;
// this.b=b;
// }
// }
// static void ans(Node root,HashSet<Integer> f,Queue<pair> k,int val){
// if(root==null)return;
// ans(root.left,f,k,val+1);
// f.add(val);
// k.add(new pair(val,root.data));
// ans(root.right,f,k,val-1);
// }
// public static List<List<Integer>> getTreeTraversal(BinaryTreeNode<Integer> root) {
// ArrayList<Integer> in=new ArrayList<>();
// ArrayList<Integer> pre=new ArrayList<>();
// ArrayList<Integer> post=new ArrayList<>();
// Stack<pair11> e=new Stack<>();
// e.add(new pair11(root,1));
// while(!e.isEmpty()){
// pair11 d=e.pop();
// if(d.b==1){
// pre.add((Integer) d.a.data);
// if(d.a.left!=null){
// d.b++;
// e.add(d);}
// }else if(d.b==2){
// in.add((Integer) d.a.data);
// if(d.a.right!=null){
// d.b++;
// e.add(d);}
// }else{
// post.add((Integer) d.a.data);
// }
// }
// ArrayList<List<Integer>> ans=new ArrayList<>();
// ans.add(in);
// ans.add(pre);
// ans.add(post);
// return ans;
// }
static class pair{
TreeNode a;
int b;
pair(TreeNode a,int b){
this.a=a;
this.b=b;
}
}
// class BinaryTreeNode<T> {
// T data;
// BinaryTreeNode<T> left;
// BinaryTreeNode<T> right;
//
// public BinaryTreeNode(T data) {
// this.data = data;
// }
// }
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public static List<List<Integer>> verticalTraversal(TreeNode root) {
TreeMap<Integer,PriorityQueue<Integer>> e=new TreeMap<>();
Stack<pair> st=new Stack<>();
// st.add(new pair(root,0));
while(!st.isEmpty()) {
pair d=st.pop();
if(!e.containsKey(d.b)){
e.put(d.b,new PriorityQueue<>());
e.get(d.b).add(d.a.val);
}else{
e.get(d.b).add(d.a.val);
}
st.add(new pair(d.a.left,d.b-1));
st.add(new pair(d.a.right,d.b+1));
}
List<List<Integer>> ans=new ArrayList<>();
for(Map.Entry<Integer, PriorityQueue<Integer>> um:e.entrySet()) {
List<Integer> f=new ArrayList<>();
while(!um.getValue().isEmpty()) {
f.add(um.getValue().poll());
}
}
return ans;
}
}
//
// 3
// 1 4
// 0 2 2
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | bd6bb62f3d91c047acfe0ec4b5fc06de | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.Vector;
public class Main {
static int mod = (int) (1e9) + 7;
/* ======================DSU===================== */
static class dsu {
static int parent[], n;// min[],value[];
static long size[];
dsu(int n) {
parent = new int[n + 1];
size = new long[n + 1];
// min=new int[n+1];
// value=new int[n+1];
Main.dsu.n = n;
makeSet();
}
static void makeSet() {
for (int i = 1; i <= n; i++) {
parent[i] = i;
size[i] = 1;
// min[i]=i;
}
}
static int find(int a) {
if (parent[a] == a)
return a;
else {
return parent[a] = find(parent[a]);// Path Compression
}
}
static void union(int a, int b) {
int setA = find(a);
int setB = find(b);
if (setA == setB)
return;
if (size[setA] >= size[setB]) {
parent[setB] = setA;
size[setA] += size[setB];
} else {
parent[setA] = setB;
size[setB] += size[setA];
}
}
}
/* ======================================================== */
static class Pair<X extends Number, Y extends Number> implements Comparator<Pair> {
X x;
Y y;
// Constructor
public Pair(X x, Y y) {
this.x = x;
this.y = y;
}
public Pair() {
}
@Override
public int compare(Main.Pair o1, Main.Pair o2) {
return ((int) (o1.y.intValue() - o2.y.intValue()));// Ascending Order based on 'y'
}
}
/* ===============================Tries================================= */
static class TrieNode {
private HashMap<Character, TrieNode> children = new HashMap<>();
public int size;
boolean endOfWord;
public void putChildIfAbsent(char ch) {
children.putIfAbsent(ch, new TrieNode());
}
public TrieNode getChild(char ch) {
return children.get(ch);
}
}
static private TrieNode root;
public static void insert(String str) {
TrieNode curr = root;
for (char ch : str.toCharArray()) {
curr.putChildIfAbsent(ch);
curr = curr.getChild(ch);
curr.size++;
}
// mark the current nodes endOfWord as true
curr.endOfWord = true;
}
public static int search(String word) {
TrieNode curr = root;
for (char ch : word.toCharArray()) {
curr = curr.getChild(ch);
if (curr == null) {
return 0;
}
}
// size contains words starting with prefix- word
return curr.size;
}
public boolean delete(TrieNode current, String word, int index) {
if (index == word.length()) {
// when end of word is reached only delete if currrent.endOfWord is true.
if (!current.endOfWord) {
return false;
}
current.endOfWord = false;
// if current has no other mapping then return true
return current.children.size() == 0;
}
char ch = word.charAt(index);
TrieNode node = current.children.get(ch);
if (node == null) {
return false;
}
boolean shouldDeleteCurrentNode = delete(node, word, index + 1);
// if true is returned then delete the mapping of character and trienode
// reference from map.
if (shouldDeleteCurrentNode) {
current.children.remove(ch);
// return true if no mappings are left in the map.
return current.children.size() == 0;
}
return false;
}
/* ================================================================= */
static boolean isPrime(long n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static Pair<Integer, Integer> lowerBound(long[] a, long x) { // x is the target, returns lowerBound. If not found
// return -1
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] == x) {
return new Pair<>(m, 1);
}
if (a[m] >= x)
r = m;
else
l = m;
}
return new Pair<>(r, 0);
}
static Pair<Integer, Integer> upperBound(long[] a, long x) {// x is the target, returns upperBound. If not found
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] == x) {
return new Pair<>(m, 1);
}
if (a[m] <= x)
l = m;
else
r = m;
}
return new Pair<>(l + 1, 0);
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
static long power(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y >>= 1;
x = (x * x);
}
return res;
}
public double binPow(double x, int n) {// binary exponentiation with negative power as well
if (n == 0)
return 1.0;
double binPow = binPow(x, n / 2);
if (n % 2 == 0) {
return binPow * binPow;
} else {
return n > 0 ? (binPow * binPow * x) : (binPow * binPow / x);
}
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
/*
* ===========Modular Operations==================
*/
static long modPower(long x, long y, long p) {
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p) {
return modPower(n, p - 2, p);
}
static long modAdd(long a, long b) {
return ((a + b + mod) % mod);
}
static long modMul(long a, long b) {
return ((a % mod) * (b % mod)) % mod;
}
static long[] fac = new long[200000 + 5];
static long[] invFac = new long[200000 + 5];
static long nCrModPFermat(int n, int r) {
if (r == 0)
return 1;
// return (fac[n] * modInverse(fac[r], mod) % mod * modInverse(fac[n - r], mod)
// % mod) % mod;
return (fac[n] * invFac[r] % mod * invFac[n - r] % mod) % mod;
}
/*
* ===============================================
*/
static void ruffleSort(long[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n);
long temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(int[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n);
int temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
static boolean isVowel(char ch) {
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
static String binString32(int n) {
StringBuilder str = new StringBuilder("");
String bin = Integer.toBinaryString(n);
if (bin.length() != 32) {
for (int k = 0; k < 32 - bin.length(); k++) {
str.append("0");
}
str.append(bin);
}
return str.toString();
}
static class sparseTable {
public static int st[][];
public static int log = 4;
static int func(int a, int b) {// make func as per question(here min range query)
return (int) gcd(a, b);
}
void makeTable(int n, int a[]) {
st = new int[n][log];
for (int i = 0; i < n; i++) {
st[i][0] = a[i];
}
for (int j = 1; j < log; j++) {
for (int i = 0; i + (1 << j) <= n; i++) {
st[i][j] = func(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
}
static int query(int l, int r) {
int length = r - l + 1;
int k = 0;
while ((1 << (k + 1)) <= length) {
k++;
}
return func(st[l][k], st[r - (1 << k) + 1][k]);
}
static void printTable(int n) {
for (int j = 0; j < log; j++) {
for (int i = 0; i < n; i++) {
System.out.print(st[i][j] + " ");
}
System.out.println();
}
}
}
/*
* ====================================Main=================================
*/
// static int st[], a[];
// static void buildTree(int treeIndex, int lo, int hi) {
// if (hi == lo) {
// st[treeIndex] = a[lo];
// return;
// }
// int mid = (lo + hi) / 2;
// buildTree(treeIndex * 2 + 1, lo, mid);
// buildTree(treeIndex * 2 + 2, mid + 1, hi);
// st[treeIndex] = merge(st[treeIndex * 2 + 1], st[treeIndex * 2 + 2]);
// }
// static void update(int treeIndex, int lo, int hi, int arrIndex, int val) {
// if (hi == lo) {
// st[treeIndex] = val;
// a[arrIndex] = val;
// return;
// }
// int mid = (hi + lo) / 2;
// if (mid < arrIndex) {
// update(treeIndex * 2 + 2, mid + 1, hi, arrIndex, val);
// } else {
// update(treeIndex * 2 + 1, lo, mid, arrIndex, val);
// }
// st[treeIndex] = merge(st[treeIndex * 2 + 1], st[treeIndex * 2 + 2]);
// }
// static int query(int treeIndex, int lo, int hi, int l, int r) {
// if (l <= lo && r >= hi) {
// return st[treeIndex];
// }
// if (l > hi || r < lo) {
// return 0;
// }
// int mid = (hi + lo) / 2;
// return query(treeIndex * 2 + 1, lo, mid, l, Math.min(mid, r));
// }
// static int merge(int a, int b) {
// return a + b;
// }
public static long findKthPositive(long[] A, long k) {
int l = 0, r = A.length, m;
while (l < r) {
m = (l + r) / 2;
if (A[m] - 1 - m < k)
l = m + 1;
else
r = m;
}
return l + k;
}
static int[] z_function(char ar[]) {
int[] z = new int[ar.length];
z[0] = ar.length;
int l = 0;
int r = 0;
for (int i = 1; i < ar.length; i++) {
if (r < i) {
l = i;
r = i;
while (r < ar.length && ar[r - l] == ar[r])
r++;
z[i] = r - l;
r--;
} else {
int k = i - l;
if (z[k] < r - i + 1) {
z[i] = z[k];
} else {
l = i;
while (r < ar.length && ar[r - l] == ar[r])
r++;
z[i] = r - l;
r--;
}
}
}
return z;
}
static void mergeSort(int a[]) {
int n = a.length;
if (n >= 2) {
int mid = n / 2;
int left[] = new int[mid];
int right[] = new int[n - mid];
for (int i = 0; i < mid; i++) {
left[i] = a[i];
}
for (int i = mid; i < n; i++) {
right[i - mid] = a[i];
}
mergeSort(left);
mergeSort(right);
mergeSortedArray(left, right, a);
}
}
static void mergeSortedArray(int left[], int right[], int a[]) {
int i = 0, j = 0, k = 0, n = left.length, m = right.length;
while (i != n && j != m) {
if (left[i] < right[j]) {
a[k++] = left[i++];
} else {
a[k++] = right[j++];
}
}
while (i != n) {
a[k++] = left[i++];
}
while (j != m) {
a[k++] = right[j++];
}
}
// BINARY SEARCH
// count of set bits in a particular position
// suffix max array/ suffix sum array/ prefix
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] intArr(int n) {
int res[] = new int[n];
for (int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
long[] longArr(int n) {
long res[] = new long[n];
for (int i = 0; i < n; i++)
res[i] = nextLong();
return res;
}
}
static FastReader f = new FastReader();
static BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
static long dp[][];
public static void main(String args[]) throws Exception {
int t = 1;
t = f.nextInt();
int tc = 1;
// fac[0] = 1;
// for (int i = 1; i <= 200000; i++) {
// fac[i] = fac[i - 1] * i % mod;
// invFac[i] = modInverse(fac[i], mod);
// }
while (t-- != 0) {
int n = f.nextInt();
int a[] = new int[n];
a = f.intArr(n);
if (a[n - 1] < a[n - 2]) {
w.write("-1\n");
continue;
}
ArrayList<Data> ans = new ArrayList<>();
int flag = -1;
for (int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
flag = 1;
break;
}
}
if (flag == -1) {
w.write("0\n");
} else {
for (int i = 0; i < n - 2; i++) {
a[i] = a[n - 2] - a[n - 1];
ans.add(new Data(i + 1, n - 1, n));
}
for (int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
flag = 0;
break;
}
}
if (flag == 1) {
w.write(ans.size() + "\n");
for (Data i : ans) {
w.write(i.x + " " + i.y + " " + i.z + "\n");
}
} else {
w.write("-1\n");
}
}
}
w.flush();
}
static class Data {
int x, y, z;
Data(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
}
/*
* dp[i][j] -> time when I have traversed till ith element and excluded j of
* them. j<=i
* 8 120 4
* 0 3 4 8 9 15 22 100
* 5 8 3 6 2 14 75 27
* 5 4 3 4 2
* 1 4 3 4 2
* 1 1 3 4 2
* 1 1
*/ | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | f50a74d191c908199f1494e89f4e92c3 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.Vector;
public class Main {
static int mod = (int) (1e9) + 7;
/* ======================DSU===================== */
static class dsu {
static int parent[], n;// min[],value[];
static long size[];
dsu(int n) {
parent = new int[n + 1];
size = new long[n + 1];
// min=new int[n+1];
// value=new int[n+1];
Main.dsu.n = n;
makeSet();
}
static void makeSet() {
for (int i = 1; i <= n; i++) {
parent[i] = i;
size[i] = 1;
// min[i]=i;
}
}
static int find(int a) {
if (parent[a] == a)
return a;
else {
return parent[a] = find(parent[a]);// Path Compression
}
}
static void union(int a, int b) {
int setA = find(a);
int setB = find(b);
if (setA == setB)
return;
if (size[setA] >= size[setB]) {
parent[setB] = setA;
size[setA] += size[setB];
} else {
parent[setA] = setB;
size[setB] += size[setA];
}
}
}
/* ======================================================== */
static class Pair<X extends Number, Y extends Number> implements Comparator<Pair> {
X x;
Y y;
// Constructor
public Pair(X x, Y y) {
this.x = x;
this.y = y;
}
public Pair() {
}
@Override
public int compare(Main.Pair o1, Main.Pair o2) {
return ((int) (o1.y.intValue() - o2.y.intValue()));// Ascending Order based on 'y'
}
}
/* ===============================Tries================================= */
static class TrieNode {
private HashMap<Character, TrieNode> children = new HashMap<>();
public int size;
boolean endOfWord;
public void putChildIfAbsent(char ch) {
children.putIfAbsent(ch, new TrieNode());
}
public TrieNode getChild(char ch) {
return children.get(ch);
}
}
static private TrieNode root;
public static void insert(String str) {
TrieNode curr = root;
for (char ch : str.toCharArray()) {
curr.putChildIfAbsent(ch);
curr = curr.getChild(ch);
curr.size++;
}
// mark the current nodes endOfWord as true
curr.endOfWord = true;
}
public static int search(String word) {
TrieNode curr = root;
for (char ch : word.toCharArray()) {
curr = curr.getChild(ch);
if (curr == null) {
return 0;
}
}
// size contains words starting with prefix- word
return curr.size;
}
public boolean delete(TrieNode current, String word, int index) {
if (index == word.length()) {
// when end of word is reached only delete if currrent.endOfWord is true.
if (!current.endOfWord) {
return false;
}
current.endOfWord = false;
// if current has no other mapping then return true
return current.children.size() == 0;
}
char ch = word.charAt(index);
TrieNode node = current.children.get(ch);
if (node == null) {
return false;
}
boolean shouldDeleteCurrentNode = delete(node, word, index + 1);
// if true is returned then delete the mapping of character and trienode
// reference from map.
if (shouldDeleteCurrentNode) {
current.children.remove(ch);
// return true if no mappings are left in the map.
return current.children.size() == 0;
}
return false;
}
/* ================================================================= */
static boolean isPrime(long n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static Pair<Integer, Integer> lowerBound(long[] a, long x) { // x is the target, returns lowerBound. If not found
// return -1
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] == x) {
return new Pair<>(m, 1);
}
if (a[m] >= x)
r = m;
else
l = m;
}
return new Pair<>(r, 0);
}
static Pair<Integer, Integer> upperBound(long[] a, long x) {// x is the target, returns upperBound. If not found
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] == x) {
return new Pair<>(m, 1);
}
if (a[m] <= x)
l = m;
else
r = m;
}
return new Pair<>(l + 1, 0);
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
static long power(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y >>= 1;
x = (x * x);
}
return res;
}
public double binPow(double x, int n) {// binary exponentiation with negative power as well
if (n == 0)
return 1.0;
double binPow = binPow(x, n / 2);
if (n % 2 == 0) {
return binPow * binPow;
} else {
return n > 0 ? (binPow * binPow * x) : (binPow * binPow / x);
}
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
/*
* ===========Modular Operations==================
*/
static long modPower(long x, long y, long p) {
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p) {
return modPower(n, p - 2, p);
}
static long modAdd(long a, long b) {
return ((a + b + mod) % mod);
}
static long modMul(long a, long b) {
return ((a % mod) * (b % mod)) % mod;
}
static long[] fac = new long[200000 + 5];
static long[] invFac = new long[200000 + 5];
static long nCrModPFermat(int n, int r) {
if (r == 0)
return 1;
// return (fac[n] * modInverse(fac[r], mod) % mod * modInverse(fac[n - r], mod)
// % mod) % mod;
return (fac[n] * invFac[r] % mod * invFac[n - r] % mod) % mod;
}
/*
* ===============================================
*/
static void ruffleSort(long[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n);
long temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(int[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n);
int temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
static boolean isVowel(char ch) {
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
static String binString32(int n) {
StringBuilder str = new StringBuilder("");
String bin = Integer.toBinaryString(n);
if (bin.length() != 32) {
for (int k = 0; k < 32 - bin.length(); k++) {
str.append("0");
}
str.append(bin);
}
return str.toString();
}
static class sparseTable {
public static int st[][];
public static int log = 4;
static int func(int a, int b) {// make func as per question(here min range query)
return (int) gcd(a, b);
}
void makeTable(int n, int a[]) {
st = new int[n][log];
for (int i = 0; i < n; i++) {
st[i][0] = a[i];
}
for (int j = 1; j < log; j++) {
for (int i = 0; i + (1 << j) <= n; i++) {
st[i][j] = func(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
}
static int query(int l, int r) {
int length = r - l + 1;
int k = 0;
while ((1 << (k + 1)) <= length) {
k++;
}
return func(st[l][k], st[r - (1 << k) + 1][k]);
}
static void printTable(int n) {
for (int j = 0; j < log; j++) {
for (int i = 0; i < n; i++) {
System.out.print(st[i][j] + " ");
}
System.out.println();
}
}
}
/*
* ====================================Main=================================
*/
// static int st[], a[];
// static void buildTree(int treeIndex, int lo, int hi) {
// if (hi == lo) {
// st[treeIndex] = a[lo];
// return;
// }
// int mid = (lo + hi) / 2;
// buildTree(treeIndex * 2 + 1, lo, mid);
// buildTree(treeIndex * 2 + 2, mid + 1, hi);
// st[treeIndex] = merge(st[treeIndex * 2 + 1], st[treeIndex * 2 + 2]);
// }
// static void update(int treeIndex, int lo, int hi, int arrIndex, int val) {
// if (hi == lo) {
// st[treeIndex] = val;
// a[arrIndex] = val;
// return;
// }
// int mid = (hi + lo) / 2;
// if (mid < arrIndex) {
// update(treeIndex * 2 + 2, mid + 1, hi, arrIndex, val);
// } else {
// update(treeIndex * 2 + 1, lo, mid, arrIndex, val);
// }
// st[treeIndex] = merge(st[treeIndex * 2 + 1], st[treeIndex * 2 + 2]);
// }
// static int query(int treeIndex, int lo, int hi, int l, int r) {
// if (l <= lo && r >= hi) {
// return st[treeIndex];
// }
// if (l > hi || r < lo) {
// return 0;
// }
// int mid = (hi + lo) / 2;
// return query(treeIndex * 2 + 1, lo, mid, l, Math.min(mid, r));
// }
// static int merge(int a, int b) {
// return a + b;
// }
public static long findKthPositive(long[] A, long k) {
int l = 0, r = A.length, m;
while (l < r) {
m = (l + r) / 2;
if (A[m] - 1 - m < k)
l = m + 1;
else
r = m;
}
return l + k;
}
static int[] z_function(char ar[]) {
int[] z = new int[ar.length];
z[0] = ar.length;
int l = 0;
int r = 0;
for (int i = 1; i < ar.length; i++) {
if (r < i) {
l = i;
r = i;
while (r < ar.length && ar[r - l] == ar[r])
r++;
z[i] = r - l;
r--;
} else {
int k = i - l;
if (z[k] < r - i + 1) {
z[i] = z[k];
} else {
l = i;
while (r < ar.length && ar[r - l] == ar[r])
r++;
z[i] = r - l;
r--;
}
}
}
return z;
}
static void mergeSort(int a[]) {
int n = a.length;
if (n >= 2) {
int mid = n / 2;
int left[] = new int[mid];
int right[] = new int[n - mid];
for (int i = 0; i < mid; i++) {
left[i] = a[i];
}
for (int i = mid; i < n; i++) {
right[i - mid] = a[i];
}
mergeSort(left);
mergeSort(right);
mergeSortedArray(left, right, a);
}
}
static void mergeSortedArray(int left[], int right[], int a[]) {
int i = 0, j = 0, k = 0, n = left.length, m = right.length;
while (i != n && j != m) {
if (left[i] < right[j]) {
a[k++] = left[i++];
} else {
a[k++] = right[j++];
}
}
while (i != n) {
a[k++] = left[i++];
}
while (j != m) {
a[k++] = right[j++];
}
}
// BINARY SEARCH
// count of set bits in a particular position
// suffix max array/ suffix sum array/ prefix
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] intArr(int n) {
int res[] = new int[n];
for (int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
long[] longArr(int n) {
long res[] = new long[n];
for (int i = 0; i < n; i++)
res[i] = nextLong();
return res;
}
}
static FastReader f = new FastReader();
static BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
static long dp[][];
public static void main(String args[]) throws Exception {
int t = 1;
t = f.nextInt();
int tc = 1;
// fac[0] = 1;
// for (int i = 1; i <= 200000; i++) {
// fac[i] = fac[i - 1] * i % mod;
// invFac[i] = modInverse(fac[i], mod);
// }
while (t-- != 0) {
int n = f.nextInt();
int a[] = new int[n];
a = f.intArr(n);
if (a[n - 1] < a[n - 2]) {
w.write("-1\n");
continue;
}
ArrayList<Data> ans = new ArrayList<>();
// for (Pair<Integer, Integer> i : smallestPosToRight) {
// System.out.print(i.x + " ");
// }
// System.out.println();
int flag = -1;
for (int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
flag = 1;
break;
}
}
if (flag == -1) {
w.write("0\n");
} else {
for (int i = 0; i < n - 2; i++) {
a[i] = a[n - 2] - a[n - 1];
ans.add(new Data(i + 1, n - 1, n));
}
// for(int i:a){
// w.write(i+" ");
// }
// w.write("\n");
if (a[n - 1] < a[n - 2]) {
flag = 0;
}
for (int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
flag = 0;
break;
}
}
if (flag == 1) {
w.write(ans.size() + "\n");
for (Data i : ans) {
w.write(i.x + " " + i.y + " " + i.z + "\n");
}
} else {
w.write("-1\n");
}
}
}
w.flush();
}
static class Data {
int x, y, z;
Data(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
}
/*
* dp[i][j] -> time when I have traversed till ith element and excluded j of
* them. j<=i
* 8 120 4
* 0 3 4 8 9 15 22 100
* 5 8 3 6 2 14 75 27
* 5 4 3 4 2
* 1 4 3 4 2
* 1 1 3 4 2
* 1 1
*/ | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 38e0c3c53e0498b4bc9b5668fb2142a6 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes |
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class EG {
public static void main(String[] args) throws ArithmeticException{
FastScanner fs=new FastScanner();
int t=fs.nextInt();
while(t-->0) {
int n=fs.nextInt();
Long a[]=new Long[n];
for(int i=0;i<n;i++) {
a[i]=fs.nextLong();
}
int count=0;
List<int []>al=new ArrayList<>();
int min=n-1;
for(int i=n-3;i>=0;i--) {
if(a[i]>a[i+1]) {
count++;
a[i]=a[i+1]-a[n-1];
al.add(new int[] {i+1,i+2,min});
}
if(a[i+1]>=0 && a[min]>a[i+1])min=i+1;
}
if(a[n-1]<a[n-2])System.out.println(-1);
else if((a[n-1]<0)){
if(count==0)System.out.println(0);
else System.out.println(-1);
}
else {
System.out.println(count);
for(int x[]:al) {
System.out.println(x[0]+" "+(x[1])+" "+(x[2]+1));
}
}
}
}
}
class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next(){
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().charAt(0);
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 2ee0e570bd11910d40b1b3e575bf8424 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static int mod = (int) 1e9 + 7;
// **** -----> Disjoint Set Union(DSU) Start **********
public static int findPar(int node, int[] parent) {
if (parent[node] == node)
return node;
return parent[node] = findPar(parent[node], parent);
}
public static boolean union(int u, int v, int[] rank, int[] parent) {
u = findPar(u, parent);
v = findPar(v, parent);
if(u == v) return false;
if (rank[u] < rank[v])
parent[u] = v;
else if (rank[u] > rank[v])
parent[v] = u;
else {
parent[u] = v;
rank[v]++;
}
return true;
}
// **** DSU Ends ***********
public static String toBinary(int decimal) {StringBuilder sb = new StringBuilder();while (decimal > 0) {sb.append(decimal % 2);decimal = decimal / 2;}return sb.reverse().toString();}
public static boolean isPalindrome(String s) {int i = 0, j = s.length() - 1;while (i < j) {if (s.charAt(i) != s.charAt(j))return false;i++;j--;}return true;}
public static boolean isPalindrome(int[] arr) {int i = 0, j = arr.length - 1;while (i < j) {if (arr[i] != arr[j])return false;}return true;}
public static int pow(int x, int y) {int res = 1;x = x % mod;if (x == 0)return 0;while (y > 0) {if ((y & 1) != 0)res = (res * x) % mod;y = y >> 1;x = (x * x) % mod;}return res;}
public static int gcd(int a, int b) {if (b == 0)return a;return gcd(b, a % b);}
public static long gcd(long a, long b) {if (b == 0)return a;return gcd(b, a % b);}
public static void sort(long[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);long temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);}
public static void reverse(int a[]) {int i, k, t, n = a.length;for (i = 0; i < n / 2; i++) {t = a[i];a[i] = a[n - i - 1];a[n - i - 1] = t;}}
public static void sort(int[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);int temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);}
public static void revSort(int[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);int temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);reverse(a);}
public static long LCM(long a, long b) {if (a > b) {long t = a;a = b;b = t;}a /= gcd(a, b);return (a * b);}
public static int findMax(int[] a, int left, int right) {int res = left;int max = a[left];for (int i = left + 1; i <= right; i++) {if (a[i] > max) {max = a[i];res = i;}}return res;}
public static long findClosest(long arr[], long target) {int n = arr.length;if (target <= arr[0])return arr[0];if (target >= arr[n - 1])return arr[n - 1];int i = 0, j = n, mid = 0;while (i < j) {mid = (i + j) / 2;if (arr[mid] == target)return arr[mid];if (target < arr[mid]) {if (mid > 0 && target > arr[mid - 1])return getClosest(arr[mid - 1], arr[mid], target);j = mid;} else {if (mid < n - 1 && target < arr[mid + 1])return getClosest(arr[mid], arr[mid + 1], target);i = mid + 1;}}return arr[mid];}
public static long getClosest(long val1, long val2, long target) {if (target - val1 >= val2 - target)return val2;else return val1;}
public static int findClosest(int arr[], int target) { int n = arr.length; if (target <= arr[0]) return arr[0]; if (target >= arr[n - 1]) return arr[n - 1]; int i = 0, j = n, mid = 0; while (i < j) { mid = (i + j) / 2; if (arr[mid] == target) return arr[mid];if (target < arr[mid]) {if (mid > 0 && target > arr[mid - 1])return getClosest(arr[mid - 1], arr[mid], target);j = mid;} else {if (mid < n - 1 && target < arr[mid + 1])return getClosest(arr[mid], arr[mid + 1], target);i = mid + 1;}}return arr[mid];}
public static int getClosest(int val1, int val2, int target) {if (target - val1 >= val2 - target)return val2;else return val1;}
public static String reverse(String str) {String nstr = "";char ch;for (int i = 0; i < str.length(); i++) {ch = str.charAt(i);nstr = ch + nstr;}return nstr;}
public static boolean isPrime(int n){if (n <= 1)return false;if (n <= 3)return true;if (n % 2 == 0 || n % 3 == 0)return false;for (int i = 5; i * i <= n; i = i + 6)if (n % i == 0 || n % (i + 2) == 0)return false;return true;}
public static int xorSum(int arr[], int n){int bits = 0;for (int i = 0; i < n; ++i)bits |= arr[i];int ans = bits * (int)Math.pow(2, n-1);return ans;}
public static ArrayList<Integer> primeFactors(int n) { ArrayList<Integer> res = new ArrayList<>();while (n%2 == 0) { res.add(2); n = n/2; } for (int i = 3; i <= Math.sqrt(n); i = i+2) { while (n%i == 0) { res.add(i); n = n/i; } } if (n > 2) res.add(n);return res;}
public static ArrayList<Long> primeFactors(long n) { ArrayList<Long> res = new ArrayList<>();while (n%2 == 0) { res.add(2L); n = n/2; } for (long i = 3; i <= Math.sqrt(n); i = i+2) { while (n%i == 0) { res.add(i); n = n/i; } } if (n > 2) res.add(n);return res;}
static int lower_bound(int array[], int low, int high, int key){
int mid;
while (low < high) {
mid = low + (high - low) / 2;
if (key <= array[mid]) high = mid;
else low = mid + 1;
}
if (low < array.length && array[low] < key) low++;
return low;
}
/********************************* Start Here ***********************************/
// int mod = 1000000007;
// static HashMap<Integer, HashMap<Long, Integer>> map;
public static void main(String[] args) throws java.lang.Exception {
if (System.getProperty("ONLINE_JUDGE") == null) {
PrintStream ps = new PrintStream(new File("output.txt"));
System.setOut(ps);
}
FastScanner sc = new FastScanner("input.txt");
int T = sc.nextInt();
// int T = 1;
while (T-- > 0) {
int n = sc.nextInt();
long[] arr = sc.readLongArray(n);
long limit = (long)1e18;
// System.out.println(Math.abs(arr[n-2]-arr[n-1]) > limit);
if(arr[n-2] > arr[n-1] || Math.abs(arr[n-2]-arr[n-1]) >= limit){
System.out.println(-1);
}else{
int res = n-2;
boolean sort = true;
for(int i = 1; i < n; i++){
if(arr[i] < arr[i-1]){
sort = false;
break;
}
}
if(sort){
System.out.println(0);
continue;
}else if(arr[n-1] < 0){
System.out.println(-1);
continue;
}
StringBuilder sb = new StringBuilder();
for(int i = 0; i < n-2; i++){
sb.append((i+1)+" " + (n-1)+" "+n+"\n");
}
System.out.println(res);
System.out.print(sb);
}
}
System.out.close();
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
if (st == null || !st.hasMoreTokens()) {
try {
return br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken("\n");
}
public String[] readStringArray(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++) {
a[i] = next();
}
return a;
}
int nextInt() {
return Integer.parseInt(nextToken());
}
String next() {
return nextToken();
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
int[][] read2dArray(int n, int m) {
int[][] a = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = nextInt();
}
}
return a;
}
long[][] read2dlongArray(int n, int m) {
long[][] a = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = nextLong();
}
}
return a;
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 2297048ffcd7b174ff5e139f6c046d0d | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.Scanner;
public class practice {
public static long fun(long ar[],int n){
long c=0;
for(int i=0;i<n-1;i++){
if(ar[i]<=ar[i+1]){
c++;
}
}
if(c==n-1){
return 0;
}
return 1;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long t=sc.nextLong();
for(int i=0;i<t;i++){
long n=sc.nextLong();
long ar[]=new long[(int)n];
for(int j=0;j<n;j++){
ar[j]=sc.nextLong();
}
if(fun(ar,(int) n)==0){
System.out.println(0);
}
else if(fun(ar,(int) n)==1){
if(ar[(int)n-1]<ar[(int)n-2] || n<3){
System.out.println(-1);
}
else if(ar[(int)n-1]>=0 && ar[(int)n-1]>=ar[(int)n-2]){
System.out.println(n-2);
int a=3,b=2;
while(n-a>=0){
System.out.println((n-a+1)+" "+(n-b+1)+" "+(n-1+1));
a++;b++;
}
}
else if(ar[(int)n-1]>=ar[(int)n-2] && ar[(int)n-1]<=0){
if(fun(ar,(int)n-1)==0){
System.out.println(0);
}
else{
System.out.println(-1);
}
}
}
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 9df6b0e502964060ea93cf8b5602f853 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | // Problem: C. Differential Sorting
// Contest: Codeforces - Codeforces Round #772 (Div. 2)
// URL: https://codeforces.com/contest/1635/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
import java.io.BufferedReader;
import java.io.*;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.*;
public class Yoo
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args)
{
FastReader sc=new FastReader();
int i,j=0;
int t =sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int m=n+1;
int a[]=new int[m];
boolean flag=true;
for(i=1;i<m;i++)
{
a[i]=sc.nextInt();
}
for(i=1;i<m-1;i++)
{
if(a[i]>a[i+1])
{
flag=false;
}
}
if(flag==true)
{
System.out.println(0);
}
else
{
if(a[m-1]<a[m-2] || a[m-1]<0)
{
System.out.println(-1);
continue;
}
System.out.println(m-3);
if(a[m-1]>=0)
{
for(i=1;i<m-2;i++)
{
System.out.print(i+" "+(m-2)+" "+(m-1));
System.out.println();
}
}
}
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 77d56c40257824a5afc2f792d45b2972 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
public static void main (String[] args) throws java.lang.Exception
{
try{
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
long a[] = new long[n];
for(int i=0;i<n;i++){
a[i] = sc.nextLong();
}
if(a[n-2]>a[n-1])
{
System.out.println(-1);
continue;
}
long z = a[n-1];
ArrayList<ArrayList<Long>> list = new ArrayList<ArrayList<Long>>();
int flag =0;
for(int i = n-3;i>=0;i--){
// if(a[i]>a[i+1]&&a[n-1]<0){
// System.out.println(-1);
// flag++;
// break;
// }
if(a[i]>a[i+1]){
a[i] = a[i+1] - z;
ArrayList<Long> temp = new ArrayList<Long>();
temp.add((long)(i+1));
temp.add((long)(i+2));
temp.add((long)n);
list.add(temp);
}
}
if(list.size()>0 && a[n-2]-a[n-1]>a[n-2]){
System.out.println(-1);
}
else{
System.out.println(list.size());
if(list.size()>0)
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).get(0)+" "+list.get(i).get(1)+" "+list.get(i).get(2));
}
}
}
}
catch(Exception e){
return;
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | df16a23e76ae1131f4de5321726e870f | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.lang.*;
public class Main{
public static PrintWriter out;
public static FastReader in;
public static int mod = 1000003;
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
try {
in = new FastReader();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = in.nextInt();
while(t-->0){
int n = in.nextInt();
long[] a = new long[n];
input(a);
if(a[n-2]>a[n-1]){
out.println(-1);
continue;
}
ArrayList<Integer> b = new ArrayList<>();
int ct = 0;
for (int i=n-3;i>=0;i--){
if(a[i]>a[i+1]){
a[i] = a[i+1] - a[n-1];
ct++;
b.add(i);
if(a[i]>a[i+1]){
ct = -1;
break;
}
}
}
if(ct==-1){
out.println(-1);
continue;
}
out.println(ct);
for (int i=0;i<ct;i++){
int x = b.get(i);
out.println(x+1+ " "+(x+2)+" "+(n));
}
}
out.flush();
out.close();
} catch (Exception e) {
System.out.println("Exception");
return;
}
}
static boolean isPrime(int a){
if (a == 1) return false;
if (a == 2 || a == 3) return true;
if (a%2==0 || a%3==0) return false;
for (int i=5;i*i<=a;i+=6){
if (a%i==0 || a%(i+2)==0) return false;
}
return true;
}
static void printAllPrime(int n){
// Sieve of Eratosthenes algorithm
if(n <= 1) return;
boolean[] arr = new boolean[n+1];
Arrays.fill(arr,true);
for (int i=2;i*i<=n;i++){
if (arr[i]){
for (int j=i*i;j<=n;j+=i){
arr[j] = false;
}
}
}
for (int i=2;i<=n;i++){
if (arr[i]){
System.out.printf(i+" ");
}
}
}
static int highestPowerOf2(int x)
{
// check for the set bits
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
// Then we remove all but the top bit by xor'ing the
// string of 1's with that string of 1's shifted one to
// the left, and we end up with just the one top bit
// followed by 0's.
return x ^ (x >> 1);
}
static long pow(long a,long b){
long ans = b;
long res =1;
if(b<0){
ans = -1*b;
}
while(ans>0){
if((ans&1)==1){
res = (res*a)%mod;
}
a = (a*a)%mod;
ans = ans>>1;
}
if(b<0){
res = 1/res;
}
return res;
}
static double pow(double a,long b){
long ans = b;
double res =1;
if(b<0){
ans = -1*b;
}
while(ans>0){
if((ans&1)==1){
res = (res*a)%mod;
}
a = (a*a)%mod;
ans = ans>>1;
}
if(b<0){
res = 1/res;
}
return res;
}
static void sort(int[] arr)
{
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
static void sort(long[] arr)
{
ArrayList<Long> ls = new ArrayList<Long>();
for(long x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
static HashMap<Integer, Integer> sortValue(HashMap<Integer,Integer> h){
List<Map.Entry<Integer, Integer> > list = new ArrayList<>(h.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2){
int fp = o2.getValue().compareTo(o1.getValue());
int sp = o2.getKey().compareTo(o1.getKey());
return fp==0 ? sp : fp;
}
});
//clear the hashmap
// h.clear();
HashMap<Integer, Integer> hm = new LinkedHashMap<>();
for(Map.Entry<Integer, Integer> mp : list){
hm.put(mp.getKey(),mp.getValue());
}
return hm;
}
static HashMap<Integer, Integer> sortKey(HashMap<Integer,Integer> h){
List<Map.Entry<Integer, Integer> > list = new ArrayList<>(h.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2){
int fp = o2.getValue().compareTo(o1.getValue());
int sp = o2.getKey().compareTo(o1.getKey());
return fp==0 ? fp : sp;
}
});
//clear the hashmap
// h.clear();
HashMap<Integer, Integer> hm = new LinkedHashMap<>();
for(Map.Entry<Integer, Integer> mp : list){
hm.put(mp.getKey(),mp.getValue());
}
return hm;
}
static long totient(long n)
{
long result = n;
for (int p = 2; p*p <= n; ++p)
if (n % p == 0)
{
while(n%p == 0)
n /= p;
result -= result/p;
}
if (n > 1)
result -= result/n;
return result;
}
static int pow(int x,int y){
return (int)Math.pow(x,y);
}
static void allDivisor(int a){
int i=0;
for (i=1;i*i<=a;i++){
if (a%i==0){
System.out.printf(i+" ");
}
}
for (;i>=1;i--){
if (a%i==0){
System.out.printf(a/i+" ");
}
}
}
static int binaryToInt(String s){
return Integer.parseInt(s,2);
}
static String toBinaryString(int s){
return Integer.toBinaryString(s);
}
static void primeFactors(int a){
if (a<=1) return;
while (a%2==0) {System.out.printf(2+" "); a=a/2;}
while (a%3==0) {System.out.printf(3+" "); a=a/3;}
for (int i=5;i*i<=a;i+=6){
while (a%i==0){
System.out.printf(i+" ");
a = a/i;
}
while (a%(i+2)==0){
System.out.printf((i+2)+" ");
a = a / (i+2);
}
}
if (a>3){
System.out.printf(a+" ");
}
System.out.println();
}
static int lcm(int a,int b){
return a*b/gcd(a,b);
}
static int gcd(int a, int b){
if (a==0) return b;
return gcd(b%a,a);
}
static int countZeros(int f){
int x = 0;
for (int i=5;i<=f;i=i*5){
x += f/i;
}
return x;
}
static int ExtendedGcd(int a, int b,int x,int y){
if(b == 0){
x = 1;
y = 0;
return a;
}
int x1=1,y1=1;
int ans = ExtendedGcd(b, a%b,x1,y1);
x = y1;
y = x1 - (a/b)*y1;
System.out.println(x+" "+y);
return ans;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////Lazy work/////////////////////////////////////////////////////////////
static void input(int[] a){
for (int i=0;i<a.length;i++){
a[i] = in.nextInt();
}
}
static void input(long[] a){
for (int i=0;i<a.length;i++){
a[i] = in.nextLong();
}
}
static void input(String[] a){
for (int i=0;i<a.length;i++){
a[i] = in.next();
}
}
static void swap(char[] c,int i,int j){
char t = c[i];
c[i] = c[j];
c[j] = t;
}
static void swap(int[] c,int i,int j){
int t = c[i];
c[i] = c[j];
c[j] = t;
}
static void swap(long[] c,int i,int j){
long t = c[i];
c[i] = c[j];
c[j] = t;
}
static void print(int[] a){
for (int i=0;i<a.length;i++){
out.printf(a[i]+" ");
}
out.println();
}
static void print(int[][] a){
for (int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.printf(a[i][j]+" ");
}
out.println();
}
}
static void print(long[] a){
for (int i=0;i<a.length;i++){
out.printf(a[i]+" ");
}
out.println();
}
static void print(char[] a){
for (int i=0;i<a.length;i++){
out.printf(a[i]+" ");
}
out.println();
}
static void print(String s){
for (int i=0;i<s.length();i++){
out.printf(s.charAt(i)+" ");
}
out.println();
}
static void print(ArrayList<Integer> a){
a.forEach(e -> out.printf(e+" "));
out.println();
}
static void print(LinkedList<Integer> a){
a.forEach(e -> out.printf(e+" "));
out.println();
}
static void print(HashSet<Integer> a){
a.forEach(e -> out.printf(e+" "));
out.println();
}
static void print(HashMap<Integer,Integer> a){
for(Map.Entry<Integer, Integer> mp : a.entrySet()){
out.println(mp.getKey() + " "+ mp.getValue());
}
}
static void reverse(int[] a){
int i=0,j=a.length-1;
while(i<j){
int t = a[i];
a[i] = a[j];
a[j]= t;
i++;
j--;
}
}
static String reverse(String s){
char[] a = s.toCharArray();
int i=0,j=a.length-1;
while(i<j){
char t = a[i];
a[i] = a[j];
a[j]= t;
i++;
j--;
}
return String.valueOf(a);
}
}
class CompareP implements Comparator<Pair> {
public int compare(Pair a, Pair b){
long dif = a.v - b.v;
if (dif > 0) return 1;
if (dif < 0) return -1;
return 0;
}
}
class CompareT implements Comparator<Triplet> {
public int compare(Triplet a, Triplet b){
long dif = a.z - b.z;
if (dif > 0) return 1;
if (dif < 0) return -1;
return 0;
}
}
class Triplet{
long x;
long y;
long z;
public Triplet(long x,long y,long z){
this.x = x;
this.y = y;
this.z = z;
}
}
class Pair {
int k;
int v;
public Pair(int k, int v) {
this.k = k;
this.v = v;
}
}
class ncr
{
public int mod = 1000003;
public long[] fact = new long[mod+1];
public long[] ifact = new long[mod+1];
public int nCr(long n, long r)
{
preFactFermat();
long ans = 1;
// while(n>0 && r>0){
// int a=(int) (n%mod),b= (int)(r%mod);
// n = n/mod;r=r/mod;
// if(a<b){
// return 0;
// }else{
// ans = (ans* (fact[a]*((ifact[b]*ifact[a-b])%mod)%mod))%mod;
// }
// }
ans = lucas(n,r,ans);
return (int)ans;
}
public long lucas(long n,long r,long ans){
if(r==0)return 1;
long ni=n%mod,ri=r%mod;
return (lucas(n/mod,r/mod,ans)*(fermat(ni,ri,ans)%mod))%mod;
}
public long fermat(long n,long r,long ans){
if(n<r){
return 0;
}
ans = (ans* (fact[(int)n]*((ifact[(int)r]*ifact[(int)(n-r)])%mod)%mod))%mod;
return ans;
}
public void preFactFermat(){
fact[1] = 1;
fact[0] = 1;
ifact[0] = expo(1,mod-2);
ifact[1] = expo(1,mod-2);
for(int i=2;i<=mod;i++){
fact[i] = (i*(fact[i-1]%mod))%mod;
ifact[i] = expo(fact[i],mod-2);
}
}
public long expo(long a,long b){
long ans = b;
long res =1;
if(b<0){
ans = -1*b;
}
while(ans>0){
if((ans&1)==1){
res = (res*a)%mod;
}
a = (a*a)%mod;
ans = ans>>1;
}
if(b<0){
res = 1/res;
}
return res;
}
}
class FenwickTree{
int[] ft;
public void print(){
for (int i=0;i<ft.length;i++){
System.out.printf(ft[i]+" ");
}
}
public FenwickTree(int[] a){
ft = new int[a.length+1];
for (int i=0;i<a.length;i++){
this.update(i,a[i]);
}
}
public int getSum(int i){
int sum = 0;
while(i>0){
sum += ft[i];
i = i - (i & (-i));
}
return sum;
}
public void update(int i,int d){
i = i +1;
while(i<ft.length){
ft[i] += d;
i = i + (i &(-i));
}
}
}
class SegmentTree{
int[] st;
public SegmentTree(int[] a){
st = new int[a.length*4];
construct(a,0,a.length-1,0);
}
void print(){
for(int i=0;i<st.length;i++){
System.out.printf(st[i]+" ");
}
System.out.println();
}
int construct(int[] a,int ss,int se,int si){
if(ss==se){
st[si] = a[ss];
return a[ss];
}
int mid = (ss+se)/2;
st[si] = construct(a,ss,mid,2*si+1) + construct(a,mid+1,se,2*si+2);
return st[si];
}
int getSum(int qs,int qe,int ss,int se,int si){
if(qe<ss || qs>se){
return 0;
}
if(ss>=qs && se <= qe){
return st[si];
}
int mid = (ss+se)/2;
return getSum(qs,qe,ss,mid,2*si+1) + getSum(qs,qe,mid+1,se,2*si+2);
}
void update(int ss,int se,int si,int i,int diff){
if(ss > i || i> se){
return;
}
this.st[si] += diff;
if(ss< se){
int mid = (ss+se)/2;
update(ss,mid,2*si+1,i,diff);
update(mid+1,se,2*si+2,i,diff);
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | ca0cfdbe042b0d2c2904557a43a461d4 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
C. Differential Sorting
My idea:
1. make bigger to smaller
2. -7 5 -4 2 -1 5
need replace 5 by -7<= a[y] - a[z] < -4 min
* */
public class C {
static int n;
static int[] a;
static int[] ans;
static int cnt;
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int T = sc.nextInt();
while (T-- > 0) {
n = sc.nextInt();
a = new int[n];
ans = new int[n];
cnt = 0;
for(int i = 0; i < n; i++) a[i] = sc.nextInt();
if(solver() == -1) System.out.println(-1);
else{
System.out.println(cnt);
for(int i = 0; i < cnt; i++) System.out.println((ans[i] + 1)+ " " + (n - 1) + " " + (n));
}
}
}
static int solver() {
boolean flag = false;
for(int i = 0; i < n - 1; i++){
if(a[i] > a[i + 1]){
flag = true;
break;
}
}
if(!flag) return 0;
if(a[n - 1] >= a[n - 2]){
if(a[n - 1] < 0){ // return -1 if not sort already
return -1;
}else{ // must can sort
int diff = a[n - 2] - a[n - 1];
for (int i = 0; i < n - 2; i++) if (a[i] != diff) ans[cnt++] = i;
return cnt;
}
}
return -1;
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 3d9edc2991fe4082c340ac676f54f253 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Test{
static FastReader scan;
static void solve(){
int n=scan.nextInt();
long[]arr=new long[n];
for(int i=0;i<n;i++){
arr[i]=scan.nextLong();
}
if(arr[n-1]<arr[n-2]){
System.out.println(-1);
return;
}
if(arr[n-1]<0){
for(int i=0;i<n-1;i++){
if(arr[i]>arr[i+1]){
System.out.println(-1);
return;
}
}
System.out.println(0);
return;
}
System.out.println(n-2);
for(int i=n-3;i>=0;i--){
System.out.println((i+1)+" "+(i+2)+" "+(n));
}
}
public static void main (String[] args) throws java.lang.Exception{
scan=new FastReader();
int t=scan.nextInt();
while(t-->0){
solve();
}
}
static class Pair{
long x;
long y;
long z;
Pair(long x,long y,long z){
this.x=x;
this.y=y;
this.z=z;
}
}
static boolean sq(int i){
int temp=(int)Math.sqrt(i);
return temp*temp==i;
}
static boolean cb(int i){
int temp=(int)Math.cbrt(i);
return temp*temp*temp==i;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void printLong(long []arr){
for(long x:arr)System.out.print(x+" ");
}
static void printInt(int []arr){
for(int x:arr)System.out.print(x+" ");
}
static void scanInt(int []arr){
for(int i=0;i<arr.length;i++){
arr[i]=scan.nextInt();
}
}
static void scanLong(long []arr){
for(int i=0;i<arr.length;i++){
arr[i]=scan.nextLong();
}
}
static long gcd(long a, long b){
if (b == 0)
return a;
return gcd(b, a % b);
}
static long power(long x, long y, long mod){
long res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0){
if ((y & 1) != 0)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
static long add(long a,long b,long mod){
a = a % mod;
b = b % mod;
return (((a + b) % mod) + mod) % mod;
}
static long sub(long a, long b,long mod){
a = a % mod;
b = b % mod;
return (((a - b) % mod) + mod) % mod;
}
static long mul(long a, long b,long mod){
a = a % mod;
b = b % mod;
return (((a * b) % mod) + mod) % mod;
}
static long mminvprime(long a, long b,long mod) {
return power(a, b - 2,mod);
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 348902d553d2d77587f7eda8a5b7d710 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | //jai Shree Krishna
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Collections;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Comparator;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class A{
// author: Tarun Verma
static FastScanner sc = new FastScanner();
static int inf = Integer.MAX_VALUE;
static long mod = 1000000007;
static BufferedWriter output = new BufferedWriter(
new OutputStreamWriter(System.out));
static PrintWriter out=new PrintWriter(System.out);
/* Common Mistakes By Me
* make sure to read the bottom part of question
* special cases (n=1?)
* in BIT MASKING try don't forget a^b=c == a^c=b
* READ READ AND READ THE Test Cases VERY PROPERLY AND NEVER BE OVERCONFIDENT
* Always Reset vis,adj array upto n+1 otherwise can cause TLE
* Try to see array from back in increase and decrease questions
*/
public static void solve() {
int n = sc.nextInt();
long arr[] = sc.readLongArray(n);
long min = arr[n-2];
long max = arr[n-1];
boolean ok = true;
for(int i =0;i<n-1;i++) {
if(arr[i] > arr[i+1]) {
ok = false;
break;
}
}
if(ok) {
System.out.println(0);
return;
}
if(min - max <= min && min <= max) {
System.out.println(n-2);
for(int i = 0;i<n-2;i++) {
System.out.println((i + 1) + " " + (n-1) + " " + n);
}
return;
}
System.out.println(-1);
}
public static void main(String[] args) {
int t = 1;
t = sc.nextInt();
outer: for (int tt = 0; tt < t; tt++) {
solve();
}
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////DO NOT BELOW THIS LINE //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
static Comparator<pair> comp = new Comparator<pair>() {
@Override
public int compare(pair o1, pair o2) {
if(o1.fr == o2.fr) {
return o1.sc - o2.sc;
}
return o1.fr - o2.fr;
}
};
static int max(int a, int b) { return Math.max(a, b);}
static int min(int a, int b) {return Math.min(a, b);}
static long max(long a, long b){return Math.max(a, b);}
static long min(long a, long b) {return Math.min(a, b);}
static void sort(int arr[]) {
ArrayList<Integer> a = new ArrayList<Integer>();
for(int i: arr) a.add(i);
Collections.sort(a);
for(int i=0;i<arr.length;i++) arr[i] = a.get(i);
}
static void sort(long arr[]) {
ArrayList<Long> a = new ArrayList<Long>();
for(long i: arr) a.add(i);
Collections.sort(a);
for(int i=0;i<arr.length;i++) arr[i] = a.get(i);
}
static int abs(int a) {return Math.abs(a);}
static long abs(long a) {return Math.abs(a);}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
int[][] read2dArray(int n, int m) {
int arr[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = nextInt();
}
}
return arr;
}
ArrayList<Integer> readArrayList(int n) {
ArrayList<Integer> arr = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
int a = nextInt();
arr.add(a);
}
return arr;
}
long nextLong() {
return Long.parseLong(next());
}
}
static class pair {
int fr, sc;
pair(int fr, int sc) {
this.fr = fr;
this.sc = sc;
}
}
////////////////////////////////////////////////////////////////////////////////////
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 3bcd4976fb64bc8a5399c3652fa8ee07 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static final PrintWriter out =new PrintWriter(System.out);
static final FastReader sc = new FastReader();
//I invented a new word!Plagiarism!
//Did you hear about the mathematician who’s afraid of negative numbers?He’ll stop at nothing to avoid them
//What do Alexander the Great and Winnie the Pooh have in common? Same middle name.
//I finally decided to sell my vacuum cleaner. All it was doing was gathering dust!
//ArrayList<Integer> a=new ArrayList <Integer>();
//PriorityQueue<Integer> pq=new PriorityQueue<>();
//char[] a = s.toCharArray();
// char s[]=sc.next().toCharArray();
public static boolean sorted(int a[])
{
int n=a.length,i;
int b[]=new int[n];
for(i=0;i<n;i++)
b[i]=a[i];
Arrays.sort(b);
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
return false;
}
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
int tes=sc.nextInt();
label: while(tes-->0)
{
int n=sc.nextInt();
int a[]=new int[n];
int i;
for(i=0;i<n;i++)
a[i]=sc.nextInt();
if(sorted(a))
{
System.out.println(0);
continue label;
}
for(i=0;i<n-2;i++)
a[i]=a[n-2]-a[n-1];
if(!sorted(a))
{
System.out.println(-1);
continue;
}
System.out.println(n-2);
for(i=1;i<=n-2;i++)
System.out.println(i+" "+(n-1)+" "+n);
}
}
public static int first(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x)
return mid;
else if (x > arr.get(mid))
return first(arr, (mid + 1), high, x, n);
else
return first(arr, low, (mid - 1), x, n);
}
return -1;
}
public static int last(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x)
return mid;
else if (x < arr.get(mid))
return last(arr, low, (mid - 1), x, n);
else
return last(arr, (mid + 1), high, x, n);
}
return -1;
}
public static int lis(int[] arr) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
for(int i = 1 ; i<n;i++) {
int x = al.get(al.size()-1);
if(arr[i]>=x) {
al.add(arr[i]);
}else {
int v = upper_bound(al, 0, al.size(), arr[i]);
al.set(v, arr[i]);
}
}
return al.size();
}
public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while(e>=s)
{
int mid=s+((e-s)/2);
if(ar.get(mid)>=k)
e=mid-1;
else
s=mid+1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long mod=1000000007;
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | e188d5139115c0665faed2efabe42db4 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static final PrintWriter out =new PrintWriter(System.out);
static final FastReader sc = new FastReader();
//I invented a new word!Plagiarism!
//Did you hear about the mathematician who’s afraid of negative numbers?He’ll stop at nothing to avoid them
//What do Alexander the Great and Winnie the Pooh have in common? Same middle name.
//I finally decided to sell my vacuum cleaner. All it was doing was gathering dust!
//ArrayList<Integer> a=new ArrayList <Integer>();
//PriorityQueue<Integer> pq=new PriorityQueue<>();
//char[] a = s.toCharArray();
// char s[]=sc.next().toCharArray();
public static boolean sorted(int a[])
{
int n=a.length,i;
int b[]=new int[n];
for(i=0;i<n;i++)
b[i]=a[i];
Arrays.sort(b);
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
return false;
}
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
int tes=sc.nextInt();
label: while(tes-->0)
{
int n=sc.nextInt();
int a[]=new int[n];
int i;
for(i=0;i<n;i++)
a[i]=sc.nextInt();
if(sorted(a))
{
System.out.println(0);
continue label;
}
if(a[n-2]>a[n-1])
{
System.out.println(-1);
continue label;
}
for(i=0;i<n-2;i++)
a[i]=a[n-2]-a[n-1];
if(!sorted(a))
{
System.out.println(-1);
continue;
}
System.out.println(n-2);
for(i=1;i<=n-2;i++)
System.out.println(i+" "+(n-1)+" "+n);
}
}
public static int first(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x)
return mid;
else if (x > arr.get(mid))
return first(arr, (mid + 1), high, x, n);
else
return first(arr, low, (mid - 1), x, n);
}
return -1;
}
public static int last(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x)
return mid;
else if (x < arr.get(mid))
return last(arr, low, (mid - 1), x, n);
else
return last(arr, (mid + 1), high, x, n);
}
return -1;
}
public static int lis(int[] arr) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
for(int i = 1 ; i<n;i++) {
int x = al.get(al.size()-1);
if(arr[i]>=x) {
al.add(arr[i]);
}else {
int v = upper_bound(al, 0, al.size(), arr[i]);
al.set(v, arr[i]);
}
}
return al.size();
}
public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while(e>=s)
{
int mid=s+((e-s)/2);
if(ar.get(mid)>=k)
e=mid-1;
else
s=mid+1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long mod=1000000007;
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 2890baaf729acc8a59729dc42a800681 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | //Utilities
import java.io.*;
import java.util.*;
public class a {
static int t;
static int n;
static long[] a;
static int res;
public static void main(String[] args) throws IOException {
t = in.iscan();
outer : while (t-- > 0) {
n = in.iscan(); a = new long[n+1];
for (int i = 1; i <= n; i++) {
a[i] = in.lscan();
}
ArrayList<Integer> x = new ArrayList<Integer>();
ArrayList<Integer> y = new ArrayList<Integer>();
ArrayList<Integer> z = new ArrayList<Integer>();
for (int i = n-1; i >= 1; i--) {
if (a[i] > a[i+1]) {
if (i == n-1) {
out.println(-1);
continue outer;
}
a[i] = a[i+1] - a[n];
if (a[i] > a[i+1]) {
out.println(-1);
continue outer;
}
x.add(i); y.add(i+1); z.add(n);
}
}
int sz = x.size();
out.println(sz);
for (int i = 0; i < sz; i++) {
out.println(x.get(i) + " " + y.get(i) + " " + z.get(i));
}
}
out.close();
}
static INPUT in = new INPUT(System.in);
static PrintWriter out = new PrintWriter(System.out);
private static class INPUT {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public INPUT (InputStream stream) {
this.stream = stream;
}
public INPUT (String file) throws IOException {
this.stream = new FileInputStream (file);
}
public int cscan () throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read (buf);
}
if (numChars == -1)
return numChars;
return buf[curChar++];
}
public int iscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
int res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public String sscan () throws IOException {
int c = cscan ();
while (space (c))
c = cscan ();
StringBuilder res = new StringBuilder ();
do {
res.appendCodePoint (c);
c = cscan ();
}
while (!space (c));
return res.toString ();
}
public double dscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
double res = 0;
while (!space (c) && c != '.') {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
res *= 10;
res += c - '0';
c = cscan ();
}
if (c == '.') {
c = cscan ();
double m = 1;
while (!space (c)) {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
m /= 10;
res += (c - '0') * m;
c = cscan ();
}
}
return res * sgn;
}
public long lscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
long res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public boolean space (int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
public static class UTILITIES {
static final double EPS = 10e-6;
public static void sort(int[] a, boolean increasing) {
ArrayList<Integer> arr = new ArrayList<Integer>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(long[] a, boolean increasing) {
ArrayList<Long> arr = new ArrayList<Long>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(double[] a, boolean increasing) {
ArrayList<Double> arr = new ArrayList<Double>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static int lower_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static int upper_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static void updateMap(HashMap<Integer, Integer> map, int key, int v) {
if (!map.containsKey(key)) {
map.put(key, v);
}
else {
map.put(key, map.get(key) + v);
}
if (map.get(key) == 0) {
map.remove(key);
}
}
public static long gcd (long a, long b) {
return b == 0 ? a : gcd (b, a % b);
}
public static long lcm (long a, long b) {
return a * b / gcd (a, b);
}
public static long fast_pow_mod (long b, long x, int mod) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod;
return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod;
}
public static long fast_pow (long b, long x) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow (b * b, x / 2);
return b * fast_pow (b * b, x / 2);
}
public static long choose (long n, long k) {
k = Math.min (k, n - k);
long val = 1;
for (int i = 0; i < k; ++i)
val = val * (n - i) / (i + 1);
return val;
}
public static long permute (int n, int k) {
if (n < k) return 0;
long val = 1;
for (int i = 0; i < k; ++i)
val = (val * (n - i));
return val;
}
// start of permutation and lower/upper bound template
public static void nextPermutation(int[] nums) {
//find first decreasing digit
int mark = -1;
for (int i = nums.length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
mark = i - 1;
break;
}
}
if (mark == -1) {
reverse(nums, 0, nums.length - 1);
return;
}
int idx = nums.length-1;
for (int i = nums.length-1; i >= mark+1; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break;
}
}
swap(nums, mark, idx);
reverse(nums, mark + 1, nums.length - 1);
}
public static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
public static void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
static int lower_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= cmp) high = mid;
else low = mid + 1;
}
return low;
}
static int upper_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > cmp) high = mid;
else low = mid + 1;
}
return low;
}
// end of permutation and lower/upper bound template
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 7d972e88085c14e41f88dd50c05ad766 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static Scanner obj = new Scanner(System.in);
public static PrintWriter out = new PrintWriter(System.out);
public static class three
{
int a;
int b;
int c;
three(int a,int b,int c)
{
this.a=a;
this.b=b;
this.c=c;
}
}
public static void main(String[] args) {
int len = obj.nextInt();
while (len-- != 0) {
int n=obj.nextInt();
int[] a=new int[n];
boolean ans=true;
for(int i=0;i<n;i++) {
a[i]=obj.nextInt();
if(i>0)
{
if(a[i]<a[i-1])ans=false;
}
}
if(ans)out.println(0);
else
{
if(a[n-1]>=a[n-2] && a[n-1]>=0)
{
Vector<three> v=new Vector<>();
for(int i=n-2;i>=1;i--) v.add(new three(i,n-1,n));
out.println(v.size());
for(three nd:v)out.println(nd.a+" "+nd.b+" "+nd.c);
}
else out.println(-1);
}
}
out.flush();
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | af3f26c4f2b5520c9edb8bbb67f3a100 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.io.*;
public class c5{
static class Pair{
int first;
int last;
public Pair(int first , int last){
this.first = first;
this.last = last;
}
public int getFirst(){
return first;
}
public int getLast(){
return last;
}
}
static final int M = 1000000007;
// Fast input
static class Hrittik_FastReader{
StringTokenizer st;
BufferedReader br;
public int n;
public Hrittik_FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
double nextDouble(){
return Double.parseDouble(next());
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
}
// Fast output
static class Hrittik_FastWriter {
private final BufferedWriter bw;
public Hrittik_FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void close() throws IOException {
bw.close();
}
}
// GCD of two integers
private static int gcd(int a , int b){
if(b == 0) return a;
return gcd(b, a%b);
}
// GCD of two long integers
private static long gcd(long a , long b){
if(b == 0) return a;
return gcd(b, a%b);
}
// LCM of two integers
private static int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
// LCM of two long integers
private static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
// swap two elements of an array
private static void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// calculating x ^ y
private static long power(long x, long y){
long res = 1;
x = x % M;
while(y > 0){
if ((y & 1) == 1){
res = (res * x) % M;
}
y = y >> 1;
x = (x * x) % M;
}
return res;
}
static void sortPairByFirst(Pair arr[]){
Arrays.sort(arr , new Comparator<Pair>(){
@Override public int compare(Pair p1 , Pair p2){
return p1.first - p2.first;
}
});
}
static void sortPairByLast(Pair arr[]){
Arrays.sort(arr , new Comparator<Pair>(){
@Override public int compare(Pair p1 , Pair p2){
return p1.last - p2.last;
}
});
}
static boolean arraySortedOrNot(long arr[], int n)
{
// Array has one or no element
if (n == 0 || n == 1)
return true;
for (int i = 1; i < n; i++)
// Unsorted pair found
if (arr[i - 1] > arr[i])
return false;
// No unsorted pair found
return true;
}
public static void main(String[] args) {
try {
Hrittik_FastReader Sc =new Hrittik_FastReader();
Hrittik_FastWriter out = new Hrittik_FastWriter();
int t = Sc.nextInt();
outer : while(t-- > 0){
// write your code here
int n = Sc.nextInt();
long a[] = new long[n];
for(int i = 0; i < n; i++){
a[i] = Sc.nextLong();
}
if(arraySortedOrNot(a, n)){
System.out.println("0");
continue;
}
if(a[n-1] < a[n-2]){
System.out.println("-1");
continue;
}
if(a[n-1] >= 0){
System.out.println(n-2);
for(int i = 0; i <= n-3; i++){
System.out.println((i+1) + " " + (n-2+1) + " " + (n-1+1));
}
continue;
}
System.out.println("-1");
}
out.close();
}
catch (Exception e) {
return;
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | f29e5dcb08ad6fae7b569619f70b343e | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes |
import java.io.*;
import java.math.*;
import java.util.*;
// @author : Dinosparton
public class test {
static class Pair{
long x;
long y;
Pair(long x,long y){
this.x = x;
this.y = y;
}
}
static class Sort implements Comparator<Pair>
{
@Override
public int compare(Pair a, Pair b)
{
if(a.x!=b.x)
{
return (int)(a.x - b.x);
}
else
{
return (int)(a.y-b.y);
}
}
}
static class Compare {
void compare(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
if(p1.x!=p2.x) {
return (int)(p1.x - p2.x);
}
else {
return (int)(p1.y - p2.y);
}
}
});
// for (int i = 0; i < n; i++) {
// System.out.print(arr[i].x + " " + arr[i].y + " ");
// }
// System.out.println();
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner();
StringBuilder res = new StringBuilder();
int tc = sc.nextInt();
while(tc-->0) {
int n = sc.nextInt();
long a[] = new long[n];
for(int i=0;i<n;i++) {
a[i] = sc.nextLong();
}
boolean flag = true;
for(int i=1;i<n;i++) {
if(a[i]<a[i-1]) {
flag = false;
break;
}
}
if(flag) {
res.append(0+"\n");
continue;
}
if(a[n-2]-a[n-1] <= a[n-2] && a[n-2]<=a[n-1] ) {
res.append(n-2+"\n");
for(int i=0;i<n-2;i++) {
res.append((i+1)+" "+(n-1)+" "+(n)+"\n");
}
}
else {
res.append(-1+"\n");
}
}
System.out.println(res);
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | fb226c562e01243f8063f2d3d9e60280 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int[]arr=new int[n];
int []narr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
narr[i]=arr[i];
}
if(arr[n-1]<arr[n-2]){
System.out.println(-1);
continue;
}else if(arr[n-1]>=0){
System.out.println(n-2);
for(int i=0;i<n-2;i++){
System.out.println(i+1+" "+ (n-1) +" "+n);
}
}else{
Arrays.parallelSort(arr);
boolean flag=true;
for(int i=0;i<n;i++){
if(arr[i]!=narr[i]){
flag=false;
break;
}
}
if(flag==true){
System.out.println(0);
continue;
}else{
System.out.println(-1);
continue;
}
}
// int count=0;
// int [][]ans=new int[n][3];
// int k=0;
// for(int i=n-2;i>=0;i--){
// if(i-1>=0 ){
// arr[i-1]=arr[n-2]-arr[n-2+1];
// ans[k][0]=i;
// ans[k][1]=n-2+1;
// ans[k][2]=n-2+2;
// k++;
// count++;
// }
// }
// System.out.println(count);
// for(int i=0;i<ans.length;i++){
// if(ans[i][0]==0){
// break;
// }
// System.out.print(ans[i][0] +" ");
// System.out.print(ans[i][1]+" ");
// System.out.print(ans[i][2]+" ");
// System.out.println();
// }
// for(int i=0;i<arr.length;i++){
// System.out.print(arr[i]+" ");
// }
// System.out.println();
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | bc2083d776e4734a0e1c54a9d676712d | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.stream.Stream;
public class cf772c {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; ++i) {
int t = Integer.parseInt(br.readLine());
String string = br.readLine();
Long[] array = Stream.of(string.split(" ")).map(Long::parseLong).toArray(Long[]::new);
// (ArrayList<Long>) Stream.of(string.split(" ")).map(Long::parseLong)
// .collect(Collectors.toList());
if (array[t - 2] > array[t - 1]) {
System.out.println(-1);
continue;
}
boolean inc = true;
for (int j = 0; j < t - 1; ++j) {
if (array[j] > array[j + 1]) {
inc = false;
break;
}
}
if (inc) {
System.out.println(0);
continue;
}
if (array[t - 1] >= 0) {
System.out.println(t - 2);
for (int j = t - 2; j > 0; --j) {
System.out.println(j + " " + (j + 1) + " " + t);
}
} else {
System.out.println(-1);
}
}
System.out.close();
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | bb9f15fe8790ccfbf084fd9144046d77 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
public class k
{ //public static int mod=1000000007;
public static long printDivisors(long n, int k)
{
// Note that this loop runs till square root
long x=(long) Math.sqrt(n);
// int p=0;
long sum=0;
for (long i=1; i<=x; i++)
{
if (n%i == 0)
{
// If divisors are equal, print only one
if (n/i == i)
sum=sum+(long)(i);
else // Otherwise print both
sum=sum+(long)(i)+(long)(n/i);
}
if(sum>k)return -1;
}
return sum;
}
public static ArrayList<Long> Factors(long n)
{
ArrayList<Long> arr=new ArrayList<Long>();
int k=0;
while (n%2==0)
{
k++;
n /=2;
arr.add((long)2);
}
int p=(int) Math.sqrt(n);
for (int i = 3; i <=p; i+= 2)
{ if(n==1)break;
while (n%i == 0)
{
k++;
arr.add((long)i);
n /= i;
}
}
if (n > 2)
{
arr.add(n);
}
return arr;
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static long gcd(long x, long p)
{
if (x == 0)
return p;
return gcd(p%x, x);
}
// method to return LCM of two numbers
static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static int sieve = 1000000 ;
static boolean[] prime = new boolean[sieve + 1] ;
static ArrayList<Integer> pr=new ArrayList<Integer>();
public static void sieveOfEratosthenes()
{
// FALSE == prime and 1 // TRUE == COMPOSITE
// time complexity = 0(NlogLogN)== o(N)
// gives prime nos bw 1 to N // size - 1e7(at max)
for(int i = 4; i<= sieve ; i++)
{
prime[i] = true ; i++ ;
}
for(int p = 3; p*p <= sieve; p++)
{
if(prime[p] == false)
{
pr.add(p);
for(int i = p*p; i <= sieve; i += p)
prime[i] = true;
}
p++ ;
}
}
public static void arrInpInt(int [] arr, int n) throws IOException
{
Reader reader = new Reader();
for(int i=0;i<n;i++)
{
arr[i]=reader.nextInt();
}
}
public static void arrInpLong(long [] arr, int n) throws IOException
{
Reader reader = new Reader();
for(int i=0;i<n;i++)
{
arr[i]=reader.nextLong();
}
}
public static void printArr(int[] arr)
{
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static int[] decSort(int[] arr)
{
int[] arr1 = Arrays.stream(arr).boxed().sorted(Collections.reverseOrder()).mapToInt(Integer::intValue).toArray();
return arr1;
}
//if present - return the first occurrence of the no
//not present- return the index of next greater value
//if greater than all the values return N(taking high=N-1)
//if smaller than all the values return 0(taking low =0)
static int lower_bound(int arr[], int low,int high, int X)
{
if (low > high) {
return low;
}
int mid = low + (high - low) / 2;
if (arr[mid] >= X) {
return lower_bound(arr, low,
mid - 1, X);
}
return lower_bound(arr, mid + 1,
high, X);
}
//if present - return the index of next greater value
//not present- return the index of next greater value
//if greater than all the values return N(taking high=N-1)
//if smaller than all the values return 0(taking low =0)\
static int upper_bound(int arr[], int low, int high, int X)
{
if (low > high)
return low;
int mid = low + (high - low) / 2;
if (arr[mid] <= X) {
return upper_bound(arr, mid + 1,
high, X);
}
return upper_bound(arr, low,
mid - 1, X);
}
public static class Pair {// comparator with class
int x;
int y;
public Pair(int x, int y)
{
this.x = x;
this.y = y;
}
}
public static void sortbyColumn(int arr[][], int col) // send 2d array and col no
{
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else if (entry1[col] < entry2[col])
return -1;
else return 0;
}
});
}
public static void sortbyColumn1(int arr[][], int col) // send 2d array and col no
{
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else if (entry1[col] < entry2[col])
return -1;
else if(entry1[col] == entry2[col])
{
if(entry1[col-1]>entry2[col-1])
return -1;
else if(entry1[col-1]<entry2[col-1])
return 1;
else return 0;
}
else return 0;
}
});
}
public static void print2D(int mat[][])
{
// Loop through all rows
for (int i = 0; i < mat.length; i++)
{ // Loop through all elements of current row
{
for (int j = 0; j < mat[i].length; j++)
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
public static int biggestFactor(int num) {
int result = 1;
for(int i=2; i*i <=num; i++){
if(num%i==0){
result = num/i;
break;
}
}
return result;
}
public static int knapsack(int[] weights,int[] price, int totW)
{
int[] dp1=new int[totW+1];
int[] dp2=new int[totW+1];
int N=totW;
int ans=0;
for(int i=0;i<price.length;i++)
{
for(int j=0;j<=N;j++)
{
if(weights[i]>j)
{
if(i%2==0)
{
dp1[j]=dp2[j];
}
else
{
dp2[j]=dp1[j];
}
}
else
{
if(i%2==0)
{
dp1[j]=Math.max(dp2[j],dp2[j-weights[i]]+price[i]);
}
else
{
dp2[j]=Math.max(dp1[j], dp1[j-weights[i]]+price[i]);
}
}
}
if(i%2==0)ans=dp1[N];
else ans=dp2[N];
}
return ans;
}
public static class p
{
int no;
int h;
public p(int no, long h)
{
this.no=no;
this.h=(int) h;
}
}
static class com implements Comparator<p>{
public int compare(p s1, p s2) {
if (s1.h > s2.h)
return -1;
else if (s1.h < s2.h)
return 1;
else if(s1.h==s2.h)
{
if(s1.no>s2.no)return -1;
else return 1;
}
return 0;
}
}
static long hcf(long a,long b)
{
while (b > 0)
{
long temp = b;
b = a % b;
a = temp;
}
return a;
}
static int lower_bound_arr(ArrayList<Integer> arr, int low,
int high, int X)
{
if (low > high) {
return low;
}
int mid = low + (high - low) / 2;
if (arr.get(mid) >= X) {
return lower_bound_arr(arr, low,
mid - 1, X);
}
return lower_bound_arr(arr, mid + 1,
high, X);
}
public static int func2(int m,int[] arr,int k,int[] arr1)
{
for(int i=0;i<arr.length;i++)
{
int p=arr[i];
int q=arr[i]+m;
int in=(q<=arr.length?arr1[q]:arr.length)-arr1[p-1];
if((in)-(arr.length-in)>=k)return arr[i];
}
return 0;
}
public static boolean func(int m,int[] arr,int k,int[] arr1)
{
for(int i=0;i<arr.length;i++)
{
int p=arr[i];
int q=arr[i]+m;
int in=(q<=arr.length?arr1[q]:arr.length)-arr1[p-1];
if((in)-(arr.length-in)>=k)return true;
}
return false;
}
public static int binarySearch(int min, int max, int[] arr,int k,int[] arr1)
{
int l = min, r = max;
while (l <= r) {
int m = l + (r - l) / 2;
boolean x11=func(m,arr,k,arr1);
boolean x1=func(m-1,arr,k,arr1);
if(x11 && !x1)return m;
//Check if x is present at mid
// if (arr[m] == x)
// return m;
// If x greater, ignore left half
if (!x1 && !x11)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
return max;
}
static long isP(long x)
{
if (x >= 0) {
// Find floating point value of
// square root of x.
long sr = (long)Math.sqrt(x);
// if product of square root
// is equal, then
// return T/F
long k=sr*sr;
if(k == x)return sr;
}
return -1;
}
// public static long func(Integer[] arr, int k)
// {
//
// }
// public static HashMap<String,Integer> map1=new HashMap<String,Integer>();
// public static int function1(int[][] arr1,Integer[] arr, int start, int end)
// {
//// System.out.println("1");
//
// int p=0;
// int n=0;
// p=arr1[end][0]-arr1[start-1][0];
// n=arr1[end][1]-arr1[start-1][1];
//// return Math.abs(n-p);
// if(n==p)return 0;
// if(map1.containsKey(start+" "+end))return map1.get(start+" "+end);
// else
// {
// int min=Integer.MAX_VALUE;
// int n1=0;
// int p1=0;
// for(int i=end-1;i>=start-1;i--)
// {
//// System.out.println("pp");
//// int P=p-(arr[i]==1?1:0)-p1+n1;
//// int N=n-(arr[i]==-1?1:0)-n1+p1;
// int P=(arr[i]==1?1:0)-p1+n1;
// int N=(arr[i]==-1?1:0)-n1+p1;
// if(arr[i]==-1)n1++;
// else p1++;
//
// p=arr1[end][0]-arr1[i+1][0];
// n=arr1[end][1]-arr1[i+1][1];
//
//// min=Math.min(Math.abs(P-N), min);
//// map1.put((i+1)+" "+end,min+1);
// }
//
//
//
// return min;
// }
// }
public static long pwmd(long a, long n,long mod) {
if (n == 0)
return 1;
long pt = pwmd(a, n / 2,mod);
pt *= pt;
pt %= mod;
if ((n & 1) > 0) {
pt *= a;
pt %= mod;
}
return pt;
}
public static void main(String args[]) throws NumberFormatException, IOException ,java.lang.Exception
{
Reader reader = new Reader();
long mod= 998244353;
//// power();
long[] pow2 =new long[64];
pow2[0]=1l;
for(int i=1;i<64;i++)
{
// pow2[i]=(int) Math.pow(2, i);
pow2[i]=((long)(2)*pow2[i-1]);
// System.out.println(pow2[i]);
}
// sieveOfEratosthenes();
//Scanner reader=new Scanner(System.in);
// PrintWriter out = new PrintWriter(System.out);
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// int cases=Integer.parseInt(br.readLine());
// int cases=1;
int cases=reader.nextInt();
while (cases-->0){
// long N=reader.nextLong();
//
// long M=reader.nextLong();
// long X=reader.nextLong();
// long Y=reader.nextLong();
// long H2=reader.nextLong();
// long D2=reader.nextLong();
//
// long C=reader.nextLong();
// long W=reader.nextLong();
// long A=reader.nextLong();
int N=reader.nextInt();
// int M=reader.nextInt();
// int K=reader.nextInt();
// int P=reader.nextInt();
// int K=reader.nextInt();
// long M=reader.nextLong();
//
// long X=reader.nextLong();
// String p="";
// while(p.equals(""))p=br.readLine();
//////
// String[] first1=br.readLine().split(" ");
// int N=Integer.parseInt(first1[0]);
// int M=Integer.parseInt(first1[1]);
// long K=Long.parseLong(first1[0]);
// long X=Long.parseLong(first1[1]);
// String s3=br.readLine();
// char[] s11=s2.toCharArray();
// char[] s12=new char[s11.length];
int max=Integer.MIN_VALUE;
// int max1=Integer.MIN_VALUE;
int min=Integer.MAX_VALUE;
// long min=Inteeg.MAX_VALUE;
// int min1=Integer.MAX_VALUE;
// int min2=Integer.MAX_VALUE;
// HashMap<Integer, Integer> map=new HashMap<Integer,Integer>();
// PriorityQueue<Integer> q = new PriorityQueue<Integer>(Collections.reverseOrder());
// PriorityQueue<Long> q = new PriorityQueue<Long>(Collections.reverseOrder());
// HashMap<Integer,TreeSet<Integer>> map=new HashMap<Integer,TreeSet<Integer>>();
// HashMap<Long,Long> map=new HashMap<Long,Long>();
// HashMap<String,String> map1=new HashMap<String,String>();
// HashMap<Integer,Integer> map1=new HashMap<Integer,Integer>();
// List<TreeMap<Integer,Integer>> map = new ArrayList<TreeMap<Integer,Integer>>();
// HashSet<Character> set =new HashSet<Character>();
// HashSet<String> set1 =new HashSet<String>();
// HashSet<Integer> map1 =new HashSet<Integer>();
// HashSet<Integer> set =new HashSet<Integer>();
// TreeSet<Integer> a =new TreeSet<Integer>();
//TreeSet<Long> b =new TreeSet<Long>();
// TreeSet<Integer> map=new TreeSet<Integer>();
// int[] arr=new int[(int)M];
// int[] arr=new int[N];
// int[] arr2=new int[64];
// int[] ev=new int[N];
// int[] od=new int[N];
//
long[] arr=new long[N];
// int[] arr2=new int[32];// i00nt[] odd=new int[100001];
// int[] arr=new int[N];
// int[] arr2=new int[5];
// Integer[] arr=new Integer[N];
// Integer[] arr1=new Integer[N];
// long[] arr=new long[N];
// Long[] arr=new Long[N];
// int[][] arr1=new int[P][2];
// ArrayList<Long> list=new ArrayList<Long>();
// ArrayList<String> l=new ArrayList<String>();
ArrayList<String> even=new ArrayList<String>();
// ArrayList<Integer> odd=new ArrayList<Integer>();
// ArrayList<Long> bees=new ArrayList<Long>();
// boolean[]arr1=new boolean[N];
int ind=-1;
// int pos=-1;
for(int i=0;i<N;i++)
{
arr[i]=reader.nextLong();
if(arr[i]>=0)
{
ind=i;
}
}
if(arr[N-1]<arr[N-2])
{
output.append("-1 \n");continue;
}
int c=0;
for(int i=N-3;i>=0;i--)
{
if(arr[i]>arr[i+1])
{
if(ind>i)
{
c++;
arr[i]=arr[i+1]-arr[ind];
even.add((i+1)+" "+(i+2)+" "+(ind+1));
}
else
{
c=-1;
break;
}
}
}
if(c==-1)
{
// System.out.println(-1);
output.append("-1 \n");
}
else
{
output.append(c+" \n");
for(int i=0;i<even.size();i++)
{
output.append(even.get(i)+"\n");
}
}
}
// System.out.println(ans);
// output.append(ans+"\n");
output.flush();
}
}
// output.flush();
// output.flush();
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | b1a30ab9a0d2b54890fdf4b949b0b196 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void sort(int a[]){ // int -> long
ArrayList<Integer> arr=new ArrayList<>(); // Integer -> Long
for(int i=0;i<a.length;i++)
arr.add(a[i]);
Collections.sort(arr);
for(int i=0;i<a.length;i++)
a[i]=arr.get(i);
}
private static long gcd(long a, long b){
if(b==0)return a;
return gcd(b,a%b);
}
private static long pow(long x,long y){
if(y==0)return 1;
long temp = pow(x, y/2);
if(y%2==1){
return x*temp*temp;
}
else{
return temp*temp;
}
}
static int log(long n){
if(n<=1)return 0;
long temp = n;
int res = 0;
while(n>1){
res++;
n/=2;
}
return (1<<res)==temp?res:res+1;
}
static int mod = (int)1e9+7;
static int INF = Integer.MAX_VALUE;
static PrintWriter out;
static FastReader sc ;
public static void main(String[] args) throws IOException {
sc = new FastReader();
out = new PrintWriter(System.out);
// primes();
// ================================ //
int test = sc.nextInt();
while (test-- > 0) {
int n = sc.nextInt();
int[]arr =new int[n];
for(int i=0;i<n;i++)arr[i] = sc.nextInt();
solver(arr, n);
}
// ================================ //
// int n = sc.nextInt();
// solver();
// ================================ //
out.flush();
}
public static void solver(int[]arr, int n) {
if(n>=2 && arr[n-1]<arr[n-2]){
out.println( -1);
return;
}
if(arr[n-1]<0){
for(int i=0;i<n-1;i++){
if(arr[i]>arr[i+1]){
out.println( -1);
return;
}
}
out.println( 0);
return;
}
out.println( n-2);
for(int i=0;i<n-2;i++)out.println(mk(i,n-2,n-1));
}
static String mk(int n, int a, int b){
n++;
a++;
b++;
return n+" "+a+" "+b;
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 262a11eea7af6cd3fe4b6227a8911243 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | //package MyPackage;
import java.util.*;
import java.io.*;
public class codec{
static class Pair
{
int x;
int y;
int z;
Pair(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
}
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
// private static boolean isPal(String s)
// {
// int l = 0, r = s.length() - 1;
// while(l < r)
// {
// if(s.charAt(l++) != s.charAt(r--))
// return false;
// }
// return true;
// }
// public static void swap(int[][] arr, int i, int j, int a, int b) {
//// arr[i] = (arr[i] + arr[j]) - (arr[j] = arr[i]);
// int t = arr[i][j];
// arr[i][j] = arr[a][b];
// arr[a][b] = t;
// }
// static int[] pre;
//
// private static long func(int i, int b[], int c[], int k, long dp[][])
// {
//// if(k < 0) return Long.MIN_VALUE;
//
// if(k == 0 || i >= b.length)
// return 0;
//
// if(dp[i][k] != -1)
// return dp[i][k];
//
// if(k - pre[b[i]] < 0)
// {
// long nahi = func(i + 1, b, c, k, dp);
// return nahi;
// }
// else
// {
// long pick = c[i] + func(i + 1, b, c, k - pre[b[i]], dp);
// long notpick = func(i + 1, b, c, k, dp);
//
// return dp[i][k] = Math.max(pick, notpick);
// }
// }
public static void main(String[] args) {
try {
FastReader in=new FastReader();
FastWriter out = new FastWriter();
StringBuilder sb = new StringBuilder();
int testCases=in.nextInt();
while(testCases-- > 0){
// write code here
int n = in.nextInt();
long a[] = new long[n];
for(int i = 0; i < n; i++)
{
a[i] = in.nextLong();
}
// if(a[n - 2] > a[n - 1])
// {
// sb.append(-1 + "\n");
// continue;
// }
int pos = -1, f = 0, s = 1;
List<Pair> al = new ArrayList<>();
for(int i = n - 1; i >= 0; i--)
{
if(f == 0 && a[i] >= 0)
{
f = 1;
pos = i;
}
if(i == n - 1) continue;
if(a[i] > a[i + 1])
{
s = 0;
if(pos != -1 && i + 1 < pos)
{
al.add(new Pair(i + 1, i + 2, pos + 1));
a[i] = a[i + 1] - a[pos];
}
else
break;
}
}
if(al.size() == 0)
{
if(s == 1)
sb.append(0 + "\n");
else
sb.append(-1 + "\n");
}
else
{
sb.append(al.size() + "\n");
for(Pair p: al)
{
sb.append(p.x + " " + p.y + " " + p.z + "\n");
}
// System.out.println();
// for(int i = 0; i < n; i++)
// System.out.print(a[i] + " ");
// System.out.println();
}
}
out.println(sb);
out.close();
}
catch (Exception e) {
return;
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | fbe901174bf739e02908e662053067a1 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
public class CF772_C {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int t=cs.nextInt();
while(t-->0){
int n=cs.nextInt();
int[] a=new int[n];
int[] b=new int[n];
for(int i=0;i<n;i++){
a[i]=cs.nextInt();
b[i]=a[i];
}
Arrays.sort(b);
if(Arrays.equals(a,b)){
System.out.println("0");
}
//|| (long)Math.abs(a[n-2]-a[n-1])>(long)1e18
else if(a[n-2]>a[n-1]){
System.out.println("-1");
}
else{
a[n-3]=a[n-2]-a[n-1];
if(a[n-3]<=a[n-2] && a[n-3]<=a[n-1] && a[n-2]<=a[n-1]){
System.out.println(n-2);
for(int i=1;i<=n-2;i++){
System.out.println(i+" "+(n-1)+" "+n);
}
}
else {
System.out.println("-1");
}
}
}
cs.close();
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | f84629e8e8775754533103052cf8e14d | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
public class CF772_C {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int t=cs.nextInt();
while(t-->0){
int n=cs.nextInt();
int[] a=new int[n];
int[] b=new int[n];
for(int i=0;i<n;i++){
a[i]=cs.nextInt();
b[i]=a[i];
}
Arrays.sort(b);
if(Arrays.equals(a,b)){
System.out.println("0");
}
//|| (long)Math.abs(a[n-2]-a[n-1])>(long)1e18
else if(a[n-2]>a[n-1] || (long)Math.abs(a[n-2]-a[n-1])>(long)1e18){
System.out.println("-1");
}
else{
a[n-3]=a[n-2]-a[n-1];
if(a[n-3]<=a[n-2] && a[n-3]<=a[n-1] && a[n-2]<=a[n-1]){
System.out.println(n-2);
for(int i=1;i<=n-2;i++){
System.out.println(i+" "+(n-1)+" "+n);
}
}
else {
System.out.println("-1");
}
}
}
cs.close();
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 054f300c718d3250c1ff48f56a6801d3 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
public class test extends PrintWriter {
test() { super(System.out); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
test o = new test(); o.main(); o.flush();
}
void main() {
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int []a = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
}
if (a[n] < a[n - 1]) {
println(-1);
continue;
}
if (a[n] < 0) {
boolean ok = true;
for (int i = 2; i <= n; i++) {
if (a[i] < a[i-1]) {
ok = false;
break;
}
}
if (ok) println(0);
else println(-1);
continue;
}
println(n - 2);
for (int i = 3; i <= n; i++) {
println((i-2) + " " + (n-1) + " " + n);
}
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 2f37b429b5e5ea060d7629c0b1d80ab9 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.math.*;
public class C_Differential_Sorting
{
public static void main(String[] args) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
// int N = Integer.parseInt(st.nextToken());
// int M = Integer.parseInt(st.nextToken());
// /*
int T = Integer.parseInt(st.nextToken());
StringBuilder sb = new StringBuilder();
FastScanner fs = new FastScanner();
while(T-->0)
{
st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
long[] arr = readArr2(N, infile, st);
long[] arr2 = new long[N];
for(int i = 0;i<N;i++){
arr2[i] = arr[i];
}
if(arr[N-1]<arr[N-2]){
System.out.println(-1);
continue;
}
int a = 0;
List<Integer> ar = new ArrayList<Integer>();
int i = N-3;
long min = arr[N-1];
long max = arr[N-2];
while(i>=0){
if(arr[i]>arr[i+1]){
arr[i] = arr[i+1]-arr[N-1];
// arr[i] = arr[i+1] - max(arr[i+2],arr2[i+2]);
a++;
ar.add(N);
ar.add(i+2);
ar.add(i+1);
}
if(arr[i]>arr[i+1]){
a = -1;
break;
}
i--;
}
if(a==-1){
System.out.println(-1);
}
else{
System.out.println(a);
int ii = 2;
while(ii<ar.size()){
System.out.print(ar.get(ii) +" "+ ar.get(ii-1)+" "+ar.get(ii-2));
System.out.println();
ii = ii+3;
}
}
}
// */
// BufferedReader infile = new BufferedReader(new FileReader());
// System.setOut(new PrintStream(new File()));
}
public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception
{
int[] arr = new int[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
return arr;
}
public static void print(int[] arr)
{
//for debugging only
for(int x: arr)
out.print(x+' ');
out.println();
}
public static long[] readArr2(int N, BufferedReader infile, StringTokenizer st) throws Exception
{
long[] arr = new long[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Long.parseLong(st.nextToken());
return arr;
}
public static boolean isPrime(long n)
{
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
public static long gcd(long a, long b)
{
if(a > b)
a = (a+b)-(b=a);
if(a == 0L)
return b;
return gcd(b%a, a);
}
public static ArrayList<Integer> findDiv(int N)
{
//gens all divisors of N
ArrayList<Integer> ls1 = new ArrayList<Integer>();
ArrayList<Integer> ls2 = new ArrayList<Integer>();
for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++)
if(N%i == 0)
{
ls1.add(i);
ls2.add(N/i);
}
Collections.reverse(ls2);
for(int b: ls2)
if(b != ls1.get(ls1.size()-1))
ls1.add(b);
return ls1;
}
public static void sort(int[] arr)
{
//because Arrays.sort() uses quicksort which is dumb
//Collections.sort() uses merge sort
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public static long power(long x, long y, long p)
{
//0^0 = 1
long res = 1L;
x = x%p;
while(y > 0)
{
if((y&1)==1)
res = (res*x)%p;
y >>= 1;
x = (x*x)%p;
}
return res;
}
//custom multiset (replace with HashMap if needed)
public static void push(TreeMap<Integer, Integer> map, int k, int v)
{
//map[k] += v;
if(!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k)+v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v)
{
//assumes map[k] >= v
//map[k] -= v
int lol = map.get(k);
if(lol == v)
map.remove(k);
else
map.put(k, lol-v);
}
public static int[] compress(int[] arr)
{
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int boof = 1; //min value
for(int x: ls)
if(!map.containsKey(x))
map.put(x, boof++);
int[] brr = new int[arr.length];
for(int i=0; i < arr.length; i++)
brr[i] = map.get(arr[i]);
return brr;
}
public static long[][] multiply(long[][] left, long[][] right)
{
long MOD = 1000000007L;
int N = left.length;
int M = right[0].length;
long[][] res = new long[N][M];
for(int a=0; a < N; a++)
for(int b=0; b < M; b++)
for(int c=0; c < left[0].length; c++)
{
res[a][b] += (left[a][c]*right[c][b])%MOD;
if(res[a][b] >= MOD)
res[a][b] -= MOD;
}
return res;
}
public static long[][] power(long[][] grid, long pow)
{
long[][] res = new long[grid.length][grid[0].length];
for(int i=0; i < res.length; i++)
res[i][i] = 1L;
long[][] curr = grid.clone();
while(pow > 0)
{
if((pow&1L) == 1L)
res = multiply(curr, res);
pow >>= 1;
curr = multiply(curr, curr);
}
return res;
}
}
class DSU
{
public int[] dsu;
public int[] size;
public DSU(int N)
{
dsu = new int[N+1];
size = new int[N+1];
for(int i=0; i <= N; i++)
{
dsu[i] = i;
size[i] = 1;
}
}
//with path compression, no find by rank
public int find(int x)
{
return dsu[x] == x ? x : (dsu[x] = find(dsu[x]));
}
public void merge(int x, int y)
{
int fx = find(x);
int fy = find(y);
dsu[fx] = fy;
}
public void merge(int x, int y, boolean sized)
{
int fx = find(x);
int fy = find(y);
size[fy] += size[fx];
dsu[fx] = fy;
}
}
class LCA
{
public int N, root;
public ArrayDeque<Integer>[] edges;
private int[] enter;
private int[] exit;
private int LOG = 17; //change this
private int[][] dp;
public LCA(int n, ArrayDeque<Integer>[] edges, int r)
{
N = n; root = r;
enter = new int[N+1];
exit = new int[N+1];
dp = new int[N+1][LOG];
this.edges = edges;
int[] time = new int[1];
//change to iterative dfs if N is large
dfs(root, 0, time);
dp[root][0] = 1;
for(int b=1; b < LOG; b++)
for(int v=1; v <= N; v++)
dp[v][b] = dp[dp[v][b-1]][b-1];
}
private void dfs(int curr, int par, int[] time)
{
dp[curr][0] = par;
enter[curr] = ++time[0];
for(int next: edges[curr])
if(next != par)
dfs(next, curr, time);
exit[curr] = ++time[0];
}
public int lca(int x, int y)
{
if(isAnc(x, y))
return x;
if(isAnc(y, x))
return y;
int curr = x;
for(int b=LOG-1; b >= 0; b--)
{
int temp = dp[curr][b];
if(!isAnc(temp, y))
curr = temp;
}
return dp[curr][0];
}
private boolean isAnc(int anc, int curr)
{
return enter[anc] <= enter[curr] && exit[anc] >= exit[curr];
}
}
class FastScanner
{
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | ceaa503bf1fa7c25ee1593ada47ff63e | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | /**
* 02/20/22 morning
* https://codeforces.com/contest/1635/problem/C
*/
// package codeforce.cf.div2.r772;
import java.util.*;
import java.io.*;
public class C {
static PrintWriter pw;
void solve(int n, int[] a) {
if (a[n - 2] > a[n - 1]) {
pr(-1);
return;
}
if (a[n - 1] < 0) {
pr(test(a) ? 0 : -1);
} else {
pr(n - 2);
for (int i = 1; i <= n - 2; i++) pr(i + " " + (n - 1) + " " + n);
}
}
boolean test(int[] a) {
int n = a.length;
int[] b = Arrays.copyOf(a, n);
Arrays.sort(b);
return Arrays.equals(a, b);
}
private void run() {
// read_write_file(); // comment this before submission
FastScanner fs = new FastScanner();
int t = fs.nextInt();
while (t-- > 0) {
int n = fs.nextInt();
int[] a = fs.readArray(n);
solve(n, a);
}
}
private final String INPUT = "input.txt";
private final String OUTPUT = "output.txt";
void read_write_file() {
FileInputStream instream = null;
PrintStream outstream = null;
try {
instream = new FileInputStream(INPUT);
outstream = new PrintStream(new FileOutputStream(OUTPUT));
System.setIn(instream);
System.setOut(outstream);
} catch (Exception e) {
}
}
public static void main(String[] args) {
pw = new PrintWriter(System.out);
new C().run();
pw.close();
}
<T> void pr(T t) {
pw.println(t);
}
class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
void tr(Object... o) {
pw.println(Arrays.deepToString(o));
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | ec970682e9ceed72c9f1300f1357e3c9 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.io.*;
public class q7 implements Runnable {
public static void main(String[] args) {
new Thread(null, new q7(), "whatever", 1 << 26).start();
}
//
private FastScanner sc;
private PrintWriter pw;
public void run() {
try {
boolean isSumitting = true;
// isSumitting = false;
if (isSumitting) {
pw = new PrintWriter(System.out);
sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
} else {
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
sc = new FastScanner(new BufferedReader(new FileReader("input.txt")));
}
} catch (Exception e) {
throw new RuntimeException();
}
int t = sc.nextInt();
// int t = 1;
while (t-- > 0) {
// sc.nextLine();
// System.out.println("for t=" + t);
solve();
}
pw.close();
}
public long mod = 1_000_000_007;
private class Pair {
int first, second, third;
Pair(int first, int second, int third) {
this.first = first + 1;
this.second = second + 1;
this.third = third + 1;
}
}
public void solve() {
int n = sc.nextInt();
long[] arr = new long[n];
boolean ok = true;
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
if (i > 0 && arr[i] < arr[i - 1]) {
ok = false;
}
}
if (ok) {
pw.println(0);
return;
}
if (arr[n - 1] < 0 || arr[n - 2] > arr[n - 1]) {
pw.println(-1);
return;
}
pw.println((n - 2));
for (int i = n - 2; i >= 1; i--) {
pw.println(i + " " + (n - 1) + " " + (n));
}
}
class FastScanner {
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(BufferedReader bf) {
reader = bf;
tokenizer = null;
}
public String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public float nextFloat() {
return Float.parseFloat(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String[] nextStringArray(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++) {
a[i] = next();
}
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
}
private static class Sorter {
public static <T extends Comparable<? super T>> void sort(T[] arr) {
Arrays.sort(arr);
}
public static <T> void sort(T[] arr, Comparator<T> c) {
Arrays.sort(arr, c);
}
public static <T> void sort(T[][] arr, Comparator<T[]> c) {
Arrays.sort(arr, c);
}
public static <T extends Comparable<? super T>> void sort(ArrayList<T> arr) {
Collections.sort(arr);
}
public static <T> void sort(ArrayList<T> arr, Comparator<T> c) {
Collections.sort(arr, c);
}
public static void normalSort(int[] arr) {
Arrays.sort(arr);
}
public static void normalSort(long[] arr) {
Arrays.sort(arr);
}
public static void sort(int[] arr) {
timSort(arr);
}
public static void sort(int[] arr, Comparator<Integer> c) {
timSort(arr, c);
}
public static void sort(int[][] arr, Comparator<Integer[]> c) {
timSort(arr, c);
}
public static void sort(long[] arr) {
timSort(arr);
}
public static void sort(long[] arr, Comparator<Long> c) {
timSort(arr, c);
}
public static void sort(long[][] arr, Comparator<Long[]> c) {
timSort(arr, c);
}
private static void timSort(int[] arr) {
Integer[] temp = new Integer[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(int[] arr, Comparator<Integer> c) {
Integer[] temp = new Integer[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(int[][] arr, Comparator<Integer[]> c) {
Integer[][] temp = new Integer[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
}
private static void timSort(long[] arr) {
Long[] temp = new Long[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(long[] arr, Comparator<Long> c) {
Long[] temp = new Long[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(long[][] arr, Comparator<Long[]> c) {
Long[][] temp = new Long[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
}
}
public long fastPow(long x, long y, long mod) {
if (y == 0) return 1;
if (y == 1) return x % mod;
long temp = fastPow(x, y / 2, mod);
long ans = (temp * temp) % mod;
return (y % 2 == 1) ? (ans * (x % mod)) % mod : ans;
}
public long fastPow(long x, long y) {
if (y == 0) return 1;
if (y == 1) return x;
long temp = fastPow(x, y / 2);
long ans = (temp * temp);
return (y % 2 == 1) ? (ans * x) : ans;
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 3874ee8470dac838aa19ffa9677f44aa | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.io.*;
public class Main implements Runnable {
public static void main(String[] args) {
new Thread(null, new Main(), "whatever", 1 << 26).start();
}
//
private FastScanner sc;
private PrintWriter pw;
public void run() {
try {
boolean isSumitting = true;
// isSumitting = false;
if (isSumitting) {
pw = new PrintWriter(System.out);
sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
} else {
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
sc = new FastScanner(new BufferedReader(new FileReader("input.txt")));
}
} catch (Exception e) {
throw new RuntimeException();
}
int t = sc.nextInt();
// int t = 1;
while (t-- > 0) {
// sc.nextLine();
// System.out.println("for t=" + t);
solve();
}
pw.close();
}
public long mod = 1_000_000_007;
private class Pair {
int first, second, third;
Pair(int first, int second, int third) {
this.first = first + 1;
this.second = second + 1;
this.third = third + 1;
}
}
ArrayList<Pair> ans;
int[][] dp;
boolean notPossible;
int anss;
public void solve() {
int n = sc.nextInt();
long[] arr = new long[n];
boolean ok = true;
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
if (i > 0 && arr[i] < arr[i - 1]) {
ok = false;
}
}
if (ok) {
pw.println(0);
return;
}
if (arr[n - 1] < 0 || arr[n - 2] > arr[n - 1]) {
pw.println(-1);
return;
}
notPossible = false;
ans = new ArrayList<Pair>();
pw.println((n - 2));
for (int i = n - 2; i >= 1; i--) {
pw.println(i + " " + (n - 1) + " " + (n));
}
}
class FastScanner {
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(BufferedReader bf) {
reader = bf;
tokenizer = null;
}
public String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public float nextFloat() {
return Float.parseFloat(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String[] nextStringArray(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++) {
a[i] = next();
}
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
}
private static class Sorter {
public static <T extends Comparable<? super T>> void sort(T[] arr) {
Arrays.sort(arr);
}
public static <T> void sort(T[] arr, Comparator<T> c) {
Arrays.sort(arr, c);
}
public static <T> void sort(T[][] arr, Comparator<T[]> c) {
Arrays.sort(arr, c);
}
public static <T extends Comparable<? super T>> void sort(ArrayList<T> arr) {
Collections.sort(arr);
}
public static <T> void sort(ArrayList<T> arr, Comparator<T> c) {
Collections.sort(arr, c);
}
public static void normalSort(int[] arr) {
Arrays.sort(arr);
}
public static void normalSort(long[] arr) {
Arrays.sort(arr);
}
public static void sort(int[] arr) {
timSort(arr);
}
public static void sort(int[] arr, Comparator<Integer> c) {
timSort(arr, c);
}
public static void sort(int[][] arr, Comparator<Integer[]> c) {
timSort(arr, c);
}
public static void sort(long[] arr) {
timSort(arr);
}
public static void sort(long[] arr, Comparator<Long> c) {
timSort(arr, c);
}
public static void sort(long[][] arr, Comparator<Long[]> c) {
timSort(arr, c);
}
private static void timSort(int[] arr) {
Integer[] temp = new Integer[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(int[] arr, Comparator<Integer> c) {
Integer[] temp = new Integer[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(int[][] arr, Comparator<Integer[]> c) {
Integer[][] temp = new Integer[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
}
private static void timSort(long[] arr) {
Long[] temp = new Long[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(long[] arr, Comparator<Long> c) {
Long[] temp = new Long[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(long[][] arr, Comparator<Long[]> c) {
Long[][] temp = new Long[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
}
}
public long fastPow(long x, long y, long mod) {
if (y == 0) return 1;
if (y == 1) return x % mod;
long temp = fastPow(x, y / 2, mod);
long ans = (temp * temp) % mod;
return (y % 2 == 1) ? (ans * (x % mod)) % mod : ans;
}
public long fastPow(long x, long y) {
if (y == 0) return 1;
if (y == 1) return x;
long temp = fastPow(x, y / 2);
long ans = (temp * temp);
return (y % 2 == 1) ? (ans * x) : ans;
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 764645e631b551399c5e767c3558434c | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | //package com.company;
import java.io.*;
import java.util.*;
public class Main{
static boolean[] primecheck = new boolean[1000002];
static ArrayList<Integer>[] adj;
static int[] vis;
static int mod = (int)1e9 + 7;
public static void main(String[] args) {
OutputStream outputStream = System.out;
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(outputStream);
PROBLEM solver = new PROBLEM();
int t = 1;
t = in.ni();
for (int i = 0; i < t; i++) {
//out.print("Case #" + (i+1) + ": ");
solver.solve(in, out);
}
out.close();
}
static class PROBLEM {
public void solve(FastReader in, PrintWriter out) {
int n = in.ni(), a[] = in.ra(n);
if(a[n-2] > a[n-1]){
out.println(-1);
return;
}
if(a[n-1] >= 0) {
out.println(n - 2);
for (int i = n - 3; i >= 0; i--) {
out.println((i + 1) + " " + (i + 2) + " " + (n));
}
}else{
for (int i = 0; i < n - 1; i++) {
if(a[i] > a[i+1]){
out.println(-1);
return;
}
}
out.println(0);
}
}
public static void pa(int[] a, PrintWriter out){
for (int i = 0; i < a.length; i++) {
out.print(a[i] + " ");
}
out.println();
}
}
public static int f(String s, char c){
char[] a = s.toCharArray();
int ctr = 0;
for (int i = 0; i < a.length; i++) {
if(a[i] == c) ctr++;
}
return ctr - (a.length-ctr);
}
public static void sortbyColumn(int arr[][], int col)
{
// Using built-in sort function Arrays.sort
Arrays.sort(arr, new Comparator<int[]>() {
@Override
// Compare values according to columns
public int compare(final int[] entry1,
final int[] entry2) {
// To sort in descending order revert
// the '>' Operator
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
}); // End of function call sort().
}
static boolean isPoT(int n){
double p = Math.log((double)n)/Math.log(2D);
return (p == (int)p);
}
static void dfs(int i){
vis[i] = 1;
for(int j: adj[i]){
if(vis[j] == 0) {
dfs(j);
}
}
}
static long sigmaK(long k){
return (k*(k+1))/2;
}
static void swap(int[] a, int l, int r) {
int temp = a[l];
a[l] = a[r];
a[r] = temp;
}
static int binarySearch(int[] a, int l, int r, int x){
if(r>=l){
int mid = l + (r-l)/2;
if(a[mid] == x) return mid;
if(a[mid] > x) return binarySearch(a, l, mid-1, x);
else return binarySearch(a,mid+1, r, x);
}
return -1;
}
static long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static long lcm(long a, long b){
return (a / gcd(a, b)) * b;
}
static boolean isSquare(double a) {
boolean isSq = false;
double b = Math.sqrt(a);
double c = Math.sqrt(a) - Math.floor(b);
if (c == 0) isSq = true;
return isSq;
}
static long fast_pow(long a, long b) { //Jeel bhai OP
if(b == 0)
return 1L;
long val = fast_pow(a, b / 2);
if(b % 2 == 0)
return val * val % mod;
else
return val * val % mod * a % mod;
}
static int exponentMod(int A, int B, int C) {
// Base cases
if (A == 0)
return 0;
if (B == 0)
return 1;
// If B is even
long y;
if (B % 2 == 0) {
y = exponentMod(A, B / 2, C);
y = (y * y) % C;
}
// If B is odd
else {
y = A % C;
y = (y * exponentMod(A, B - 1, C) % C) % C;
}
return (int) ((y + C) % C);
}
// static class Pair implements Comparable<Pair>{
//
// int x;
// int y;
//
// Pair(int x, int y){
// this.x = x;
// this.y = y;
// }
//
// public int compareTo(Pair o){
//
// int ans = Integer.compare(x, o.x);
// if(o.x == x) ans = Integer.compare(y, o.y);
//
// return ans;
//
//// int ans = Integer.compare(y, o.y);
//// if(o.y == y) ans = Integer.compare(x, o.x);
////
//// return ans;
// }
// }
static class Tuple implements Comparable<Tuple>{
int x, y, id;
Tuple(int x, int y, int id){
this.x = x;
this.y = y;
this.id = id;
}
public int compareTo(Tuple o){
int ans = Integer.compare(x, o.x);
if(o.x == x) ans = Integer.compare(y, o.y);
return ans;
}
}
public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public int compareToY(Pair<U, V> b) {
int cmpU = y.compareTo(b.y);
return cmpU != 0 ? cmpU : x.compareTo(b.x);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(next());
}
long nl() {
return Long.parseLong(next());
}
double nd() {
return Double.parseDouble(next());
}
char nc() {
return next().charAt(0);
}
boolean nb() {
return !(ni() == 0);
}
// boolean nextBoolean(){return Boolean.parseBoolean(next());}
String nline() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] ra(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) array[i] = ni();
return array;
}
}
private static int[] mergeSort(int[] array) {
//array.length replaced with ctr
int ctr = array.length;
if (ctr <= 1) {
return array;
}
int midpoint = ctr / 2;
int[] left = new int[midpoint];
int[] right;
if (ctr % 2 == 0) {
right = new int[midpoint];
} else {
right = new int[midpoint + 1];
}
for (int i = 0; i < left.length; i++) {
left[i] = array[i];
}
for (int i = 0; i < right.length; i++) {
right[i] = array[i + midpoint];
}
left = mergeSort(left);
right = mergeSort(right);
int[] result = merge(left, right);
return result;
}
private static int[] merge(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
int leftPointer = 0, rightPointer = 0, resultPointer = 0;
while (leftPointer < left.length || rightPointer < right.length) {
if (leftPointer < left.length && rightPointer < right.length) {
if (left[leftPointer] < right[rightPointer]) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
} else if (leftPointer < left.length) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
}
return result;
}
public static void Sieve(int n) {
Arrays.fill(primecheck, true);
primecheck[0] = false;
primecheck[1] = false;
for (int i = 2; i * i < n + 1; i++) {
if (primecheck[i]) {
for (int j = i * 2; j < n + 1; j += i) {
primecheck[j] = false;
}
}
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | f799a194ba042aea97a6f2f44402c3dc | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
/**
*
* @author eslam
*/
public class DifferentialSorting {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws IOException {
FastReader input = new FastReader();
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
int t = input.nextInt();
loop:
while (t-- > 0) {
int n = input.nextInt();
long a[] = new long[n];
ArrayList<ter> ans = new ArrayList<>();
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
}
for (int i = n - 2; i > -1; i--) {
if (a[i] > a[i + 1]) {
int index = n - 1;
long value = Long.MIN_VALUE;
while (index > i + 1) {
if (a[i + 1] - a[index] > a[i + 1]) {
log.write("-1\n");
continue loop;
} else if (Math.abs(a[i + 1] - a[index]) == (long) Math.pow(10, 18)) {
index--;
} else {
value = a[i + 1] - a[index];
break;
}
}
if (value == Long.MIN_VALUE) {
log.write("-1\n");
continue loop;
}else{
a[i] = value;
ans.add(new ter(i+1, i+2, index+1));
}
}
}
log.write(ans.size()+"\n");
for (int i = 0; i < ans.size(); i++) {
log.write(ans.get(i).x+" "+ans.get(i).y+" "+ans.get(i).z+"\n");
}
}
log.flush();
}
static class pair {
long x;
int y;
public pair(long x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + " " + y;
}
}
static class ter {
int x;
int y;
int z;
public ter(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public String toString() {
return x + " " + y;
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 2059368dea0c640eeec529f6891ab44d | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | /*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = sc.nextInt();
for (int t = 1; t <= test; t++) {
solve();
}
out.close();
}
private static void solve() {
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
if (arr[n - 1] < arr[n - 2]) {
out.println(-1);
return;
}
if (arr[n - 1] < 0 && arr[n - 2] < 0) {
boolean ok = true;
for (int i = 1; i < n; i++) {
if (arr[i] < arr[i - 1]) {
ok = false;
break;
}
}
if(ok) {
out.println(0);
}else {
out.println(-1);
}
return;
}
out.println(n - 2);
for (int i = 1; i < n - 1; i++) {
out.println(i + " " + (n - 1) + " " + n);
}
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | d9e77faf0a50ef4621e982da06006c9b | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.*;
import java.util.*;
public class Mainnn {
// static final File ip = new File("input.txt");
// static final File op = new File("output.txt");
// static {
// try {
// System.setOut(new PrintStream(op));
// System.setIn(new FileInputStream(ip));
// } catch (Exception e) {
// }
// // in = new InputReader(System.in);
// }
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
// out.println(result); // print via PrintWriter
int test = sc.nextInt();
while (test-- > 0) {
int n = sc.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextLong();
}
ArrayList<long[]> as = new ArrayList<>();
if(a[n-2]>a[n-1]) out.println(-1);
else{
if(a[n-1]>=0){
out.println(n-2);
for(int i=1;i<n-1;i++){
out.println(i+" "+(n-1)+" "+(n));
}
}
else{
int f = 1;
for(int i =1;i<n;i++)
{
if(a[i-1] > a[i]) {
f = 0;
break;
}
}
if(f == 1) out.println(0);
else out.println(-1);
}
}
}
// Stop writing your solution here. -------------------------------------
out.close();
}
// -----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
// -----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// --------------------------------------------------------
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 18b04e7703cf45217cd7ee78c12a6c75 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
public class pariNa {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long t=sc.nextLong();
StringBuilder finalAnswer=new StringBuilder();
//finalAnswer.append().append('\n');
outer:
while(t-->0) {
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
boolean sorted = true;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i+1]) {
sorted = false;
break;
}
}
if(sorted) {
System.out.println(0);
}
// System.out.println(arr[n-2]+" "+arr[n-1]);
// System.out.println((arr[n-2]-arr[n-1])<=arr[n-2]);
else if(arr[n-2]<=arr[n-1] && (arr[n-2]-arr[n-1])<=arr[n-2] && (arr[n-2]-arr[n-1])<(long)Math.pow(10, 18)) {
System.out.println(n-2);
for(int i=0;i<n-2;i++) {
System.out.println(i+1+" "+(n-1)+" "+n);
}
}
else {
System.out.println(-1);
}
}
//System.out.println(finalAnswer);
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | fd72e507c8b9b1855110fa6c48d5fdea | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes |
// import java.io.*;
// import java.util.*;
// public class Main{
// static class FastReader {
// BufferedReader br;
// StringTokenizer st;
// public FastReader()
// {
// br = new BufferedReader(
// new InputStreamReader(System.in));
// }
// String next()
// {
// while (st == null || !st.hasMoreElements()) {
// try {
// st = new StringTokenizer(br.readLine());
// }
// catch (IOException e) {
// e.printStackTrace();
// }
// }
// return st.nextToken();
// }
// int nextInt() { return Integer.parseInt(next()); }
// long nextLong() { return Long.parseLong(next()); }
// double nextDouble()
// {
// return Double.parseDouble(next());
// }
// String nextLine()
// {
// String str = "";
// try {
// str = br.readLine();
// }
// catch (IOException e) {
// e.printStackTrace();
// }
// return str;
// }
// }
// public static class Pair implements Comparable<Pair>
// {
// double cpu;
// int tu;
// int op;
// int cost;
// Pair(double cpu,int tu,int op,int cost)
// {
// this.cpu=cpu;
// this.tu=tu;
// this.op=op;
// this.cost=cost;
// }
// public int compareTo(Pair o)
// {
// return (int)(o.cpu-this.cpu);
// }
// }
// public static void main(String[] args) throws Exception {
// FastReader sc = new FastReader();
// int t=sc.nextInt();
// while(t-->0)
// {
// int n=sc.nextInt();
// int k=sc.nextInt();
// int[] val=new int[n];
// int[] coins=new int[n];
// for(int i=0;i<n;i++)
// {
// val[i]=sc.nextInt();
// }
// for(int i=0;i<n;i++)
// {
// coins[i]=sc.nextInt();
// }
// Pair cost[]=new Pair[n];
// for(int i=0;i<n;i++)
// {
// double sum=val[i]/(i+1)+1;
// int op=Math.log(10)/Math.sum;
// if(val[i]%(i+1)!=0)
// {
// op++;
// }
// int tu=val[i];
// double cpu=coins[i]/val[i];
// cost[i]=new Pair(cpu,tu,op,coins[i]);
// }
// Arrays.sort(cost);
// for( Pair i:cost)
// {
// System.out.print(i.cpu+","+i.op+"-"+i.cost+"**");
// }
// System.out.println();
// long ans=0;
// for(int i=0;i<n;i++)
// {
// long amt=cost[i].cost;
// if(cost[i].op<=k)
// {
// k-=cost[i].op;
// ans+=amt;
// System.out.print(cost[i].op+" ");
// }
// }
// System.out.println(ans);
// }
// }
// }
import java.io.*;
import java.util.*;
import javax.lang.model.element.Element;
public class Main{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static class Pair implements Comparable<Pair>
{
int i;
int j;
Pair(int i,int j)
{
this.i=i;
this.j=j;
}
public int compareTo(Pair o)
{
return this.i-o.i;
}
}
public static void main(String[] args) throws Exception
{
FastReader sc = new FastReader();
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
if(isSorted(arr))
{
System.out.println("0");
}
else if(arr[n-1]>=arr[n-2]&&arr[n-1]>=0)
{
System.out.println(n-1);
System.out.println(1+" "+2+" "+3);
for(int i=1;i<=n-2;i++)
{
System.out.println(i+" "+(n-1)+" "+n);
}
}
else
{
System.out.println("-1");
}
}
}
public static boolean isSorted(int[] arr)
{
for(int i=0;i<arr.length-1;i++)
{
if(arr[i]>arr[i+1])
return false;
}
return true;
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 6072f501094578dc62802fc51ecec702 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandles;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class c{
static int []f;
//static int []f2;
static int []size;
//static int []size2;
//static int []a=new int [500001];
static int max=0;
static long ans=0;
static Set<Integer>set;
static int k;
static long mod= 998244353;
public static void main(String []args) {
MyScanner s=new MyScanner();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int t=s.nextInt();
while(t-->0)
{
int n=s.nextInt();
long []f=new long [n];
for(int i=0;i<n;i++)
f[i]=s.nextInt();
List<int []>list=new ArrayList<>();
int []a=new int [n];
Arrays.fill(a,-1);
if(f[n-1]>=0)a[n-1]=n;
for(int i=n-2;i>=0;i--)
{
if(a[i+1]>=0)
{
a[i]=a[i+1];
}
else if(f[i]>=0)
a[i]=i+1;;
}
a[n-1]=-1;
boolean b=true;
for(int i=n-1;i>0;i--)
{
if(f[i-1]>f[i])
{
if(a[i]==-1)
{
b=false;
}
else
{
list.add(new int [] {i,i+1,a[i]});
f[i-1]=f[i]-f[a[i]-1];
}
}
}
if(b)
{
out.println(list.size());
for(int []x: list)
{
for(int xx:x)
out.print(xx+" ");
out.println();
}
}
else out.println(-1);
}
out.close();
}
public static boolean is(StringBuilder s)
{
int l=0,r=s.length()-1;
while(l<r)
{
if(s.charAt(l)!=s.charAt(r))
return false;
l++;r--;
}
return true;
}
public static void dfs(char [][]a,char [][]b,int x,int y,int [][]v) {
if(k==0)return ;
b[x][y]='.';
k--;
int []dx= {0,1,0,-1};
int []dy= {1,0,-1,0};
for(int i=0;i<4;i++) {
int x1=x+dx[i];
int y1=y+dy[i];
if(x1<0||x1>=a.length||y1<0||y1>=a[0].length||v[x1][y1]==1||a[x1][y1]=='#')
continue;
v[x1][y1]=1;
dfs(a,b,x1,y1,v);
}
}
public static void swap(int []a) {
int l=0,r=a.length-1;
while(l<r) {
int t=a[l];
a[l]=a[r];
a[r]=t;
l++;r--;
}
}
public static boolean is(int j) {
for(int i=2;i<=(int )Math.sqrt(j);i++) {
if(j%i==0)return false;
}
return true;
}
public static int find (int []father,int x) {
if(x!=father[x])
x=find(father,father[x]);
return father[x];
}
public static void union(int []father,int x,int y,int []size) {
x=find(father,x);
y=find(father,y);
if(x==y)
return ;
if(size[x]<size[y]) {
int tem=x;
x=y;
y=tem;
}
father[y]=x;
size[x]+=size[y];
return ;
}
public static void shufu(int []f) {
for(int i=0;i<f.length;i++) {
int k=(int)(Math.random()*(f.length));
int t=f[k];
f[k]=f[0];
f[0]=t;
}
}
public static void shufu1(long []f) {
for(int i=0;i<f.length;i++) {
int k=(int)(Math.random()*(f.length));
long t=f[k];
f[k]=f[0];
f[0]=t;
}
}
public static int gcd(int x,int y) {
return y==0?x:gcd(y,x%y);
}
public static int lcm(int x,int y) {
return x*y/gcd(x,y);
}
/*
public static void buildertree(int k,int l,int r) {
if(l==r)
{
f[k]=a[l];
return ;
}
int m=l+r>>1;
buildertree(k+k,l,m);
buildertree(k+k+1,m+1,r);
f[k]=
}
public static void update(int u,int l,int r,int x,int c)
{
if(l==x && r==x)
{
f[u]=c;
return;
}
int mid=l+r>>1;
if(x<=mid)update(u<<1,l,mid,x,c);
else if(x>=mid+1)update(u<<1|1,mid+1,r,x,c);
f[u]=Math.max(f[u+u], f[u+u+1]);
}
public static int query(int k,int l,int r,int x,int y) {
if(x==l&&y==r) {
return f[k];
}
int m=l+r>>1;
if(y<=m) {
return query(k+k,l,m,x,y);
}
else if(x>m)return query(k+k+1,m+1,r,x,y);
else {
int i=query(k+k,l,m,x,m),j=query(k+k+1,m+1,r,m+1,y);
return Math.max(j, Math.max(i+j, i));
}
}
public static void calc(int k,int l,int r,int x,int z) {
f[k]+=z;
if(l==r) {
return ;
}
int m=l+r>>1;
if(x<=m)
calc(k+k,l,m,x,z);
else calc(k+k+1,m+1,r,x,z);
}
*/
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", "");
cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname;
final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in");
br = new BufferedReader(new InputStreamReader(fin.exists()
? new FileInputStream(fin) : System.in));
} catch (Exception e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
public String next() {
try {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 93b0979c00cb5908fe6f3f54632fafc1 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
public class MergeSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
sc.nextLine();
while(test-->0){
int n = sc.nextInt();
long[] a = new long[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
if(a[n-2]>a[n-1]){
System.out.println(-1);
continue;
}
if(a[n-1]<0){
boolean isPre = true;
for(int i=1;i<n;i++){
if(a[i]<a[i-1]){
System.out.println(-1);
isPre = false;
break;
}
}
if(isPre){
System.out.println(0);
}
}else {
System.out.println(n-2);
for(int i=n-3;i>=0;i--){
a[i]=a[i+1]-a[n-1];
System.out.println((i+1)+" "+(i+2)+" "+n);
}
}
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 77884a1488ca5c24dbdd964c3b46ddf1 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.text.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
PrintWriter writer = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
long arr[] = new long[n];
for(int i = 0 ; i<n; i++)arr[i] = sc.nextInt();
boolean exist = true;
ArrayList<Pair> list = new ArrayList<>();
long vv = (long) 1e18;
if(arr[n-2] > arr[n-1])exist = false;
else {
for(int i = n-3; i >= 0; i--) {
if(arr[i] <= arr[i+1])continue;
else {
long val = arr[i+1] - arr[n-1];
if((Math.abs(val) >= vv) || val > arr[i]) {
exist = false;
break;
}else {
list.add(new Pair(i+1, i+2,n));
arr[i] = val;
}
}
}
}
boolean sorted = true;
for (int i = 0; i < n-1; i++) {
if(arr[i+1] < arr[i]) exist = false;
}
if(!exist)writer.println(-1);
else {
writer.println(list.size());
for(Pair pp : list)writer.println(pp);
}
}
writer.flush();
writer.close();
}
private static long power (long a, long n, long p) {
long res = 1;
while(n!=0) {
if(n%2==1) {
res=(res*a)%p;
n--;
}else {
a= (a*a)%p;
n/=2;
}
}
return res;
}
private static boolean isPrime(int c) {
for (int i = 2; i*i <= c; i++) {
if(c%i==0)return false;
}
return true;
}
private static int find(int a , int arr[]) {
if(arr[a] != a) return arr[(int)a] = find(arr[(int)a], arr);
return arr[(int)a];
}
private static void union(int a, int b, int arr[]) {
int aa = find(a,arr);
int bb = find(b, arr);
arr[aa] = bb;
}
private static int gcd(int a, int b) {
if(a==0)return b;
return gcd(b%a, a);
}
public static int[] readIntArray(int size, FastReader s) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = s.nextInt();
}
return array;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
class SegmentTree{
int size;
long arr[];
SegmentTree(int n){
size = 1;
while(size<n)size*=2;
arr = new long[size*2];
}
public void build(int input[]) {
build(input, 0,0, size);
}
public void set(int i, int v) {
set(i,v,0,0,size);
}
// sum from l + r-1
public long sum(int l, int r) {
return sum(l,r,0,0,size);
}
private void build(int input[], int x, int lx, int rx) {
if(rx-lx==1) {
if(lx < input.length )
arr[x] = 1;
return;
}
int mid = (lx+rx)/2;
build(input, 2*x+1, lx, mid);
build(input,2*x+2,mid,rx);
arr[x] = arr[2*x+1] + arr[2*x+2];
}
private void set(int i, int v, int x, int lx, int rx) {
if(rx-lx==1) {
arr[x] = v;
return;
}
int mid = (lx+rx)/2;
if(i < mid) set(i, v, 2*x+1, lx, mid);
else set(i,v,2*x+2,mid,rx);
arr[x] = arr[2*x+1] + arr[2*x+2];
}
private long sum(int l, int r, int x, int lx, int rx) {
if(lx>=r || rx <= l)return 0;
if(lx>=l && rx <= r)return arr[x];
int mid = (lx+rx)/2;
long s1 = sum(l,r,2*x+1,lx,mid);
long s2 = sum(l,r,2*x+2,mid,rx);
return s2+s1;
}
// int first_above(int v, int l, int x, int lx, int rx) {
// if(arr[x].a<v)return -1;
// if(rx<=l)return -1;
// if(rx-lx==1)return lx;
// int m = (lx+rx)/2;
// int res = first_above(v,l,2*x+1,lx,m);
// if(res==-1)res = first_above(v,l,2*x+2,m,rx);
// return res;
//
// }
}
class Trie{
Trie arr[] = new Trie[26];
boolean isEnd;
Trie(){
for(int i = 0; i < 26; i++)arr[i] = null;
isEnd = false;
}
}
class Pair implements Comparable<Pair>{
long a;
long b;
long c;
Pair(long a, long b, long c){
this.a = a;
this.b = b;
this.c = c;
}
Pair(long a, long b){
this.a = a;
this.b = b;
this.c = 0;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null || obj.getClass()!= this.getClass()) return false;
Pair pair = (Pair) obj;
return (pair.a == this.a && pair.b == this.b && pair.c == this.c);
}
@Override
public int compareTo(Pair o) {
// if(o.a != this.a) return Long.compare(this.a, o.a);
// else
return Long.compare(o.b, this.b);
}
@Override
public String toString() {
return this.a + " " + this.b + " " + this.c;
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 1edcff884daba12d6e45160c4887000b | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
// when can't think of anything -->>
// 1. In sorting questions try to think about all possibilities like sorting from start, end, middle.
// 2. Two pointers, brute force.
// 3. In graph query questions try to solve it reversely or try to process all the queries in a single parse.
// 4. If order does not matter then just sort the data if constraints allow. It'll never harm.
// 5. In greedy problems if you are just overwhelmed by the possibilities and stuck, try to code whatever you come up with.
// 6. Think like a robot, just consider all the possibilities not probabilities if you still can't solve.
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
PrintWriter writer = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
long arr[] = new long[n];
long maxx = 1000000000000000000l;
boolean alreadySorted = true;
for (int i = 0; i< n; i++) {
arr[i] = sc.nextInt();
if(i==0)continue;
if(arr[i] < arr[i-1])alreadySorted = false;
}
if(arr[n-1] < arr[n-2]) {
writer.println(-1);
}else if(alreadySorted) {
writer.println(0);
}else {
List<String> list = new ArrayList<>();
for (int i = n-3; i >= 0; i--) {
long curr = arr[i];
long next = arr[i+1] - arr[n-1];
if(next < curr && Math.abs(next) < maxx) {
arr[i] = next;
String str = (i+1) + " " + (i+2) + " " + n;
list.add(str);
}
}
boolean sorted = true;
for (int i = 0; i < n-1; i++) {
if(arr[i+1] < arr[i]) sorted = false;
}
if(!sorted)writer.println(-1);
else {
// writer.println(Arrays.toString(arr));
writer.println(list.size());
for (String s : list)writer.println(s);
}
}
}
writer.flush();
writer.close();
}
private static boolean isPrime(int c) {
for (int i = 2; i*i <= c; i++) {
if(c%i==0)return false;
}
return true;
}
private static int find(int a , int arr[]) {
if(arr[a] != a) return arr[a] = find(arr[a], arr);
return arr[a];
}
private static void union(int a, int b, int arr[]) {
int aa = find(a,arr);
int bb = find(b, arr);
arr[aa] = bb;
}
private static int gcd(int a, int b) {
if(a==0)return b;
return gcd(b%a, a);
}
public static int[] readIntArray(int size, FastReader s) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = s.nextInt();
}
return array;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
class Pair implements Comparable<Pair>{
int a;
int b;
Pair(int a, int b){
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null || obj.getClass()!= this.getClass()) return false;
Pair pair = (Pair) obj;
return (pair.a == this.a && pair.b == this.b);
}
@Override
public int hashCode()
{
return Objects.hash(a,b);
}
@Override
public int compareTo(Pair o) {
if(this.a == o.a) {
return Integer.compare(this.b, o.b);
}else {
return Integer.compare(this.a, o.a);
}
}
@Override
public String toString() {
return this.a + " " + this.b;
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 764a8235e8edc33c11119079ac2118e7 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.*;
import java.util.*;
public class TimePass {
public static int[][] solve(int n,int arr[]) {
List<int[]>list=new ArrayList<>();
if(arr[n-1]<arr[n-2]) {
list.add(new int[] {Integer.MAX_VALUE});
}else {
for(int i=n-3;i>=0;i--) {
int min=Math.min(arr[i], arr[n-2]-arr[n-1]);
int max=Math.max(arr[i], arr[n-2]-arr[n-1]);
if(max<=arr[i+1]) {
if(max!=arr[i]) list.add(new int[] {i+1,n-1,n});
arr[i]=max;
}else if(min<=arr[i+1]) {
if(min!=arr[i]) list.add(new int[] {i+1,n-1,n});
arr[i]=min;
}else {
list=new ArrayList<>();
list.add(new int[] {Integer.MAX_VALUE});
break;
}
}
}
int ans[][]=new int[list.size()][];
for(int i=0;i<list.size();i++) {
ans[i]=list.get(i);
}
return ans;
}
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
int testCases=Integer.parseInt(br.readLine());
while(testCases-->0){
String input[]=br.readLine().split(" ");
int n=Integer.parseInt(input[0]);
input=br.readLine().split(" ");
int arr[]=new int[n];
for(int i=0;i<n;i++) {
arr[i]=Integer.parseInt(input[i]);
}
int[][] ans=solve(n,arr);
if(ans.length==1&&ans[0][0]==Integer.MAX_VALUE) {
out.print(-1);
out.print('\n');
continue;
}
out.print(ans.length);
out.print('\n');
for(int i=0;i<ans.length;i++) {
for(int j=0;j<ans[i].length;j++) {
out.print(ans[i][j]+" ");
}
out.print('\n');
}
}
out.close();
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | a0d7433e6133edf8bbf6ca37aaee1d11 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes |
import java.io.*;
import java.util.*;
public class ProblemA{
static long mod = 1000000007L;
// map.put(a[i],map.getOrDefault(a[i],0)+1);
static MyScanner sc = new MyScanner();
//<----------------------------------------------WRITE HERE------------------------------------------->
static void solve(){
int n = sc.nextInt();
long[] a= new long[n];
long max = 1000000000000000000L;
for(int i=0;i<n;i++) {
a[i] = sc.nextLong();
}
if(a[n-1]<a[n-2]) {
out.println("-1");
return;
}
long ans =0;
List<String> li = new ArrayList<>();
if(a[n-1] < 0) {
for(int i=1;i<n;i++) {
if(a[i]<a[i-1]) {
out.println("-1");
return;
}
}
out.println(0);
return;
}
for(int i=n-3;i>=0;i--) {
if(a[i] < a[i+1]) continue;
li.add((i+1)+" "+(i+2)+" "+n);
ans++;
a[i] = a[i+1]-a[n-1];
if(Math.abs(a[i])>= max) {
out.println("-1");
return;
}
}
out.println(ans);
for(String s:li) out.println(s);
}
public static int solve(List<Pair> li, int f) {
int lo = 0;
int hi = li.size()-1;
while(lo<=hi) {
int mid = (lo+hi)>>1;
if(li.get(mid).val>f) {
hi = mid-1;
}else
lo = mid+1;
}
return lo;
}
//<----------------------------------------------WRITE HERE------------------------------------------->
static void swap(char[] a,int i,int j) {
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static int LowerBound(int a[], int x) {
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] >= x)
r = m;
else
l = m;
}
return r;
}
static int UpperBound(int a[], int x) {
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] <= x)
l = m;
else
r = m;
}
return l + 1;
}
static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static boolean isPali(String str){
int i = 0;
int j = str.length()-1;
while(i<j){
if(str.charAt(i)!=str.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
static void priArr(int[] a) {
for(int i=0;i<a.length;i++) {
out.print(a[i] + " ");
}
out.println();
}
static long gcd(long a,long b){
if(b==0) return a;
return gcd(b,a%b);
}
static String reverse(String str){
char arr[] = str.toCharArray();
int i = 0;
int j = arr.length-1;
while(i<j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
String st = new String(arr);
return st;
}
static void reverse(int[] arr){
int i = 0;
int j = arr.length-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
static boolean isprime(int n){
if(n==1) return false;
if(n==3 || n==2) return true;
if(n%2==0 || n%3==0) return false;
for(int i = 5;i*i<=n;i+= 6){
if(n%i== 0 || n%(i+2)==0){
return false;
}
}
return true;
}
static class Pair implements Comparable<Pair>{
int val;
int ind;
Pair(int v,int f){
val = v;
ind = f;
}
public int compareTo(Pair p){
return this.ind - p.ind;
}
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
// int t= 1;
while(t-- >0){
solve();
}
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
int[] reverse(int arr[]){
int n= arr.length;
int i = 0;
int j = n-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;i++;
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | d68d01543d5dea3081737dded1cebb11 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | //package prog_temps;
import java.util.*;
import java.io.*;
public class Java_Template {
static boolean[] primecheck = new boolean[1000002];
static int M = 1000000007;
static int mn = Integer.MIN_VALUE;
static int mx = Integer.MAX_VALUE;
static int vis[];
static ArrayList<ArrayList<Integer>> list;
public static char rev(char c){
int diff = c-'A';
char ans = (char)('Z' - diff);
return ans;
}
public static void swap(int a[], int i,int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static int getFirstSetBitPos(int n)
{
return (int)((Math.log10(n & -n)) / Math.log10(2));
}
public static void sortbyColumn(int arr[][], int col)
{
// Using built-in sort function Arrays.sort
Arrays.sort(arr, new Comparator<int[]>() {
@Override
// Compare values according to columns
public int compare(final int[] entry1,
final int[] entry2) {
// To sort in descending order revert
// the '>' Operator
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
}); // End of function call sort().
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
PrintWriter w = new PrintWriter(System.out);
int t = 1;
t = sc.nextInt();
while (t-- > 0) {
solve(sc, w);
}
w.close();
}
public static void solve(FastReader sc, PrintWriter w) throws Exception {
int n = sc.nextInt();
boolean isAsc = true;
int arr[] = new int[n];
for (int i = 0; i <n; i++) {
int val= sc.nextInt();
arr[i] = val;
if(i!=0 && (arr[i-1] > arr[i])){
isAsc = false;
}
}
if(isAsc){
System.out.println(0);
return;
}
long maxval = power(10,18);
arr[n-3] = arr[n-2]-arr[n-1];
if(arr[n-2] > arr[n-1] || Math.abs((long)arr[n-2] - (long)arr[n-1])> maxval){
System.out.println(-1);
}
else if(arr[n-3]<= arr[n-2] && arr[n-3]<= arr[n-1] && arr[n-2]<= arr[n-1] ) {
System.out.println(n-2);
for (int i = 0; i < n-2; i++) {
System.out.println((i+1)+" "+(n-1)+" "+(n));
}
}else {
System.out.println(-1);
}
}
public static void debug(String X , String x){
System.out.println(X+ " : "+x);
}
public static void debug(String X , int x){
System.out.println(X+ " : "+x);
}
public static void debug(String X , long x){
System.out.println(X+ " : "+x);
}
public static void debug(String X , double x){
System.out.println(X+ " : "+x);
}
public static void debug(String X , boolean x){
System.out.println(X+ " : "+x);
}
public static void debug(String X , int[] x){
System.out.print(X+ " : ");
for (int ele :
x) {
System.out.print(ele+" ");
}
System.out.println();
}
public static void debug(String X , long[] x){
System.out.print(X+ " : ");
for (long ele :
x) {
System.out.print(ele+" ");
}
System.out.println();
}
public static void merge(
int[] a, int[] l, int[] r, int left, int right) {
int i = 0, j = 0, k = 0;
while (i < left && j < right) {
if (l[i] <= r[j]) {
a[k++] = l[i++];
}
else {
a[k++] = r[j++];
}
}
while (i < left) {
a[k++] = l[i++];
}
while (j < right) {
a[k++] = r[j++];
}
}
public static void mergeSort(int[] a, int n) {
if (n < 2) {
return;
}
int mid = n / 2;
int[] l = new int[mid];
int[] r = new int[n - mid];
for (int i = 0; i < mid; i++) {
l[i] = a[i];
}
for (int i = mid; i < n; i++) {
r[i - mid] = a[i];
}
mergeSort(l, mid);
mergeSort(r, n - mid);
merge(a, l, r, mid, n - mid);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
void readArr(int[] ar, int n) {
for (int i = 0; i < n; i++) {
ar[i] = nextInt();
}
}
}
public static boolean perfectSqr(long a) {
long sqrt = (long) Math.sqrt(a);
if (sqrt * sqrt == a) {
return true;
}
return false;
}
public static void Sieve(int n) {
Arrays.fill(primecheck, true);
primecheck[0] = false;
primecheck[1] = false;
for (int i = 2; i * i < n + 1; i++) {
if (primecheck[i]) {
for (int j = i * 2; j < n + 1; j += i) {
primecheck[j] = false;
}
}
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long power(long x, long y) {
long res = 1; // Initialize result
while (y > 0) {
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = res * x;
// y must be even now
y = y >> 1; // y = y/2
x = x * x; // Change x to x^2
}
return res;
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 18c7a9a1a71c332da552b132da2a2e27 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private FastWriter wr;
private Reader rd;
public final int MOD = 1000000007;
/************************************************** FAST INPUT IMPLEMENTATION *********************************************/
class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
public Reader(String path) throws FileNotFoundException {
br = new BufferedReader(
new InputStreamReader(new FileInputStream(path)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public int ni() throws IOException {
return rd.nextInt();
}
public long nl() throws IOException {
return rd.nextLong();
}
public void yOrn(boolean flag){
if(flag){
wr.println("YES");
}else {
wr.println("NO");
}
}
char nc() throws IOException {
return rd.next().charAt(0);
}
public String ns() throws IOException {
return rd.nextLine();
}
public Double nd() throws IOException {
return rd.nextDouble();
}
public int[] nai(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
public long[] nal(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
/************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE) innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map) write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
/********************************************************* USEFUL CODE **************************************************/
boolean[] SAPrimeGenerator(int n) {
// TC-N*LOG(LOG N)
//Create Prime Marking Array and fill it with true value
boolean[] primeMarker = new boolean[n + 1];
Arrays.fill(primeMarker, true);
primeMarker[0] = false;
primeMarker[1] = false;
for (int i = 2; i <= n; i++) {
if (primeMarker[i]) {
// we start from 2*i because i*1 must be prime
for (int j = 2 * i; j <= n; j += i) {
primeMarker[j] = false;
}
}
}
return primeMarker;
}
private void tr(Object... o) {
if (!oj) System.out.println(Arrays.deepToString(o));
}
class Pair<F, S> {
private F first;
private S second;
Pair(F first, S second) {
this.first = first;
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
@Override
public String toString() {
return "Pair{" +
"first=" + first +
", second=" + second +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<F, S> pair = (Pair<F, S>) o;
return first == pair.first && second == pair.second;
}
@Override
public int hashCode() {
return Objects.hash(first, second);
}
}
class PairThree<F, S, X> {
private F first;
private S second;
private X third;
PairThree(F first, S second,X third) {
this.first = first;
this.second = second;
this.third=third;
}
public F getFirst() {
return first;
}
public void setFirst(F first) {
this.first = first;
}
public S getSecond() {
return second;
}
public void setSecond(S second) {
this.second = second;
}
public X getThird() {
return third;
}
public void setThird(X third) {
this.third = third;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PairThree<?, ?, ?> pair = (PairThree<?, ?, ?>) o;
return first.equals(pair.first) && second.equals(pair.second) && third.equals(pair.third);
}
@Override
public int hashCode() {
return Objects.hash(first, second, third);
}
}
public static void main(String[] args) throws IOException {
new Main().run();
}
public void run() throws IOException {
if (oj) {
rd = new Reader();
wr = new FastWriter(System.out);
} else {
File input = new File("input.txt");
File output = new File("output.txt");
if (input.exists() && output.exists()) {
rd = new Reader(input.getPath());
wr = new FastWriter(output.getPath());
} else {
rd = new Reader();
wr = new FastWriter(System.out);
oj = true;
}
}
long s = System.currentTimeMillis();
solve();
wr.flush();
tr(System.currentTimeMillis() - s + "ms");
}
/***************************************************************************************************************************
*********************************************************** MAIN CODE ******************************************************
****************************************************************************************************************************/
boolean[] sieve;
public void solve() throws IOException {
int t = 1;
t = ni();
while (t-- > 0) {
go();
}
}
/********************************************************* MAIN LOGIC HERE ****************************************************/
long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
public void go() throws IOException {
int n=ni();
long[] arr=nal(n);
if(arr[n-2]>arr[n-1]){
wr.println(-1);
return;
}else {
if(arr[n-1]<0){
boolean flag=true;
for(int i=0;i<n-1;i++){
if(arr[i]>arr[i+1]){
flag=false;
break;
}
}
if(flag){
wr.println(0);
}else {
wr.println(-1);
}
}else {
wr.println(n-2);
for(int i=0;i<n-2;i++){
wr.println((i+1)+" "+(n-1)+" "+n);
}
}
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | e138b7c1843c0d67c33cb375b6a09b62 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
long arr[] = new long[n];
for(int i=0;i<n;i++) {
arr[i] = sc.nextInt();
}
if(arr[n-1]<arr[n-2]) {
System.out.println(-1);
continue;
}
boolean b = false;
for(int i=0;i<n-2;i++) {
if(arr[i]>arr[i+1]) {
b = true;
break;
}
}
if(b) {
if((arr[n-2]-arr[n-1])>arr[n-2]) {
System.out.println(-1);
}
else {
System.out.println(n-2);
for(int i=1;i<=n-2;i++) {
System.out.println(i+" "+(n-1)+" "+n);
}
}
}
else {
System.out.println(0);
}
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | a30c8500db838f51176e00a07bcca312 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static class JoinSet {
int[] fa;
JoinSet(int n) {
fa = new int[n];
for (int i = 0; i < n; i++) fa[i] = i;
}
int find(int t) {
if (t != fa[t]) fa[t] = find(fa[t]);
return fa[t];
}
void join(int x, int y) {
x = find(x);
y = find(y);
fa[x] = y;
}
}
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static int mod = (int)1e9+7;
static int[][] dir1 = new int[][]{{0,1},{0,-1},{1,0},{-1,0}};
static int[][] dir2 = new int[][]{{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
static boolean[] prime = new boolean[10];
static {
for (int i = 2; i < prime.length; i++) prime[i] = true;
for (int i = 2; i < prime.length; i++) {
if (prime[i]) {
for (int k = 2; i * k < prime.length; k++) {
prime[i * k] = false;
}
}
}
}
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static int get() throws Exception {
String ss = bf.readLine();
if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" "));
return Integer.parseInt(ss);
}
static long getx() throws Exception {
String ss = bf.readLine();
if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" "));
return Long.parseLong(ss);
}
static int[] getint() throws Exception {
String[] s = bf.readLine().split(" ");
int[] a = new int[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(s[i]);
}
return a;
}
static long[] getlong() throws Exception {
String[] s = bf.readLine().split(" ");
long[] a = new long[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Long.parseLong(s[i]);
}
return a;
}
static String getstr() throws Exception {
return bf.readLine();
}
static void println() throws Exception {
bw.write("\n");
}
static void print(int a) throws Exception {
bw.write(a + "\n");
}
static void print(long a) throws Exception {
bw.write(a + "\n");
}
static void print(String a) throws Exception {
bw.write(a + "\n");
}
static void print(int[] a) throws Exception {
for (int i : a) {
bw.write(i + " ");
}
println();
}
static void print(long[] a) throws Exception {
for (long i : a) {
bw.write(i + " ");
}
println();
}
static void print(char[] a) throws Exception {
for (char i : a) {
bw.write(i +"");
}
println();
}
static long pow(long a, long b) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans *= a;
}
a *= a;
b >>= 1;
}
return ans;
}
static int powmod(long a, long b, int mod) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return (int) ans;
}
static void sort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++) b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++) a[i] = b[i];
}
static void sort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++) b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++) a[i] = b[i];
}
static int max(int a, int b) {
return Math.max(a,b);
}
static int min(int a, int b) {
return Math.min(a,b);
}
static long max(long a, long b) {
return Math.max(a,b);
}
static long min(long a, long b) {
return Math.min(a,b);
}
static int max(int[] a) {
int max = a[0];
for(int i : a) max = max(max,i);
return max;
}
static int min(int[] a) {
int min = a[0];
for(int i : a) min = min(min,i);
return min;
}
static long max(long[] a) {
long max = a[0];
for(long i : a) max = max(max,i);
return max;
}
static long min(long[] a) {
long min = a[0];
for(long i : a) min = min(min,i);
return min;
}
static int query(int x) throws Exception {
print("? " + x);
bw.flush();
return get();
}
public static void main(String[] args) throws Exception {
int t = get();
while (t-- > 0){
int n = get();
long[] a = getlong();
List<int[]> ans = new ArrayList<>();
boolean ok = a[n-2] <= a[n-1];
for(int i = n-3;i >= 0;i--){
if(a[i] <= a[i+1]) continue;
a[i] = a[i+1]-a[n-1];
if(!ok || a[i] > a[i+1]) {
ok = false;
break;
}
ans.add(new int[]{i+1,i+2,n});
}
if(!ok) print(-1);
else{
print(ans.size());
for(int i[] : ans) print(i);
}
}
bw.flush();
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 4f4512d7ee0160355f1b8f604c84423b | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces_1635A {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(br.readLine());
int n, ai;
while ((t--) > 0) {
n = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < n; i++) {
ai = Integer.parseInt(st.nextToken());
arr.add(ai);
}
if (arr.get(n - 2) > arr.get(n - 1)) {
output.write("-1");
output.write("\n");
continue;
}
boolean isSorted = true;
for (int i = 0; i < n - 1; i++) {
if (arr.get(i) > arr.get(i + 1)) {
isSorted = false;
break;
}
}
if (arr.get(n - 1) < 0) {
if (isSorted) {
output.write("0");
output.write("\n");
continue;
}
output.write("-1");
output.write("\n");
continue;
}
output.write(String.format("%d%n", arr.size() - 2));
for (int i = 0; i < n - 2; i++) {
output.write(String.format("%d %d %d%n", i + 1, n - 1, n));
}
}
output.flush();
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 0d9433a9685f4eed26993b83ba7373c4 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int k = input.nextInt();
while (k-- != 0) {
int n = input.nextInt();
long[] arr = new long[n];
boolean flag = true;
for (int i = 0; i < n; i++) {
arr[i] = input.nextLong();
if(i > 0){
if(arr[i] < arr[i - 1]) flag = false;
}
}
if(flag){
System.out.println(0);
continue;
}
if (n == 1) { // 为1
System.out.println("0");
} else { // 不为1
if (arr[n - 2] > arr[n - 1]) { // 最后两个数递减
System.out.println(-1);
} else { // mid<=n-2<=n-1
long mid = arr[n - 2] - arr[n - 1];
if (mid > arr[n-2] || Math.abs(mid) >= 1000000000000000000L) {
System.out.println(-1);
} else {
System.out.println(n-2);
for (int i = 0; i < n - 2; i++) {
System.out.println((i + 1) + " " + (n-1) + " " + (n));
}
}
}
}
// 只要最后两个非递减且倒数他们的差小于倒数第二个
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | bcf33550e13988992c87ce48036aadcd | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main2 {
public static boolean sorted(int [] arr){
for (int i = arr.length-2; i >= 0; i--) {
if(arr[i] > arr[i+1])return false;
}
return true;
}
public static void main(String[] args) {
FastScanner fs = new FastScanner();
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = fs.nextInt();
while (t-->0) {
int n = fs.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = fs.nextInt();
}
if (n==1) System.out.println(0);
else if(sorted(arr)) System.out.println(0);
else if(arr[n-2] < 0 && arr[n-1] < 0) System.out.println(-1);
else {
if (arr[n - 2] <= arr[n - 1]) {
int i = n - 3;
System.out.println(n-2);
for (; i >= 0; i--) {
System.out.println((i+1) + " " + (n-1) + " " + n);
}
} else {
System.out.println(-1);
}
}
}
pw.flush();
}
}
class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
int [] sort(int [] arr)
{
List<Integer> list = new ArrayList<>();
for(int i : arr) list.add(i);
Collections.sort(list);
int res[] = new int[arr.length];
for(int i=0;i<arr.length;i++) res[i] = list.get(i);
return res;
}
void debug(int a)
{
System.out.println("This is var "+a);
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 7b3be3aa135df481cecd333c7e10f727 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.io.*;
import java.security.KeyStore.Entry;
public class Main{
static final Random random=new Random();
static long mod=1000000007L;
static HashMap<String,Integer>map=new HashMap<>();
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
int[] readIntArray(int n){
int[] res=new int[n];
for(int i=0;i<n;i++)res[i]=nextInt();
return res;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
static int ans=0;
public static void main(String[] args) {
try {
FastReader in=new FastReader();
FastWriter out = new FastWriter();
int testCases=in.nextInt();
//int testCases=1;
while(testCases-- > 0){
solve(in);
}
out.close();
} catch (Exception e) {
System.out.println(e);
return;
}
}
public static void solve( FastReader in){
int n=in.nextInt();
//String s=in.next();
//String t=in.next();
//long y=in.nextInt();
//long n=in.nextLong();
//int q=in.nextInt();
//int m=in.nextInt();
//long k=in.nextLong();
StringBuilder res=new StringBuilder();
int[] arr=in.readIntArray(n);
List<int[]> ls=new ArrayList<>();
if(arr[n-1]<arr[n-2]){debug("-1");return;}
int[] brr=arr.clone();
sort(brr);
if(Arrays.equals(arr, brr)){debug("0");return;}
if(arr[n-1]>=0){
for(int i=0;i<n-2;i++){
ls.add(new int[]{i+1,n-1,n});
}
debug(""+ls.size());
for(int i=0;i<ls.size();i++){
debug(""+ls.get(i)[0]+" "+ls.get(i)[1]+" "+ls.get(i)[2]);
}
return;
}
debug("-1");
//System.out.println(res.toString());
}
static void dfs1(Map<String,String> hp,int i,int n,String r){
if(i>n){
ans+=ch(hp,n,r);
return;
}
for(char x='a';x<='f';x++){
dfs1(hp,i+1,n,r+x);
}
}
static int ch(Map<String,String> hp,int n,String r){
for(int i=0;i<n-1;i++){
String x=r.substring(0,2);
if(!hp.containsKey(x))return 0;
r=hp.get(x)+r.substring(2);
}
if(r.equals("a"))return 1;
return 0;
}
static int gcd(int a,int b){
if(b==0){
return a;
}
return gcd(b,a%b);
}
static void sort(int[] arr)
{
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
static void reversesort(int[] arr)
{
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
Collections.reverse(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static void debug(String x){
System.out.println(x);
}
static < E > void print(E res)
{
System.out.println(res);
}
static String rString(String s){
StringBuilder sb=new StringBuilder();
sb.append(s);
return sb.reverse().toString();
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | e3cabc4068e9edfb19c3db23f05c17c1 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.io.*;
public class sagar{
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder("");
Reader rd = new Reader();
int t = rd.nextInt();
int n;
long[] arr;
while(t>0){
n = rd.nextInt();
arr = new long[n];
ss = new StringBuilder("");
for(int i=0;i<n;i++){
arr[i] = rd.nextLong();
}
int temp = fun(arr,n);
if(temp == -1){
sb.append(-1+"\n");
}
else{
sb.append(temp+"\n");
sb.append(ss.toString());
}
t--;
}
System.out.println(sb.toString());
}
public static StringBuilder ss ;
public static int fun(long[] arr,int n){
//System.out.println("mini="+arr[n-1]);
if(arr[n-2] > arr[n-1]){
return -1;
}
long mini = arr[n-1];
//System.out.println("mini="+mini);
int count = 0;
for(int i=n-3;i>=0;i--){
if(arr[i] > arr[i+1]){
if(arr[i+1] - mini > arr[i+1]){
return -1;
}
else{
count++;
arr[i] = arr[i+1]-mini;
//System.out.println("index="+i+" arr[i]="+arr[i]);
ss.append((i+1) +" "+ (i+2)+" "+(n)+"\n");
}
}
}
return count;
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[1200]; // line length
int cnt = 0, c;
c = read();
while (c <= ' ')
c = read();
while ((c) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
c =read();
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 3778883a11bfc784e673bb4064a7c7f1 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t =Integer.parseInt(br.readLine());
while(t-->0) {
int n=Integer.parseInt(br.readLine());
long[] a=new long[n];
String[] sa= br.readLine().split(" ");
for(int i=0;i<n;i++)
a[i]=Long.parseLong(sa[i]);
if(a[n-1]<a[n-2]) pw.println(-1);
else {
boolean flag=false;
List<Integer> ls=new ArrayList<>();
for(int i=n-3;i>=0;i--) {
//last element is max;
if(a[i+1]<a[i]) {
// we need to change
//i.e decrease the value
a[i]=a[i+1]-a[n-1];
ls.add(i+1);
}
if(a[i]>a[i+1])
flag=true;
}
if(flag)
pw.println(-1);
else {
pw.println(ls.size());
for(int b:ls)
pw.println(b+ " "+(b+1)+" "+n);
}
}
}
pw.flush();
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 95a442f220fcaee9d2ddebcc09623d41 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class cf02 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t =Integer.parseInt(br.readLine());
while(t-->0) {
int n=Integer.parseInt(br.readLine());
long[] a=new long[n];
String[] sa= br.readLine().split(" ");
for(int i=0;i<n;i++)
a[i]=Long.parseLong(sa[i]);
if(a[n-1]<a[n-2]) pw.println(-1);
else {
boolean flag=false;
List<Integer> ls=new ArrayList<>();
for(int i=n-3;i>=0;i--) {
//last element is max;
if(a[i+1]-a[n-1]<a[i]) {
// we need to change
//i.e decrease the value
a[i]=a[i+1]-a[n-1];
ls.add(i+1);
}
if(a[i]>a[i+1])
flag=true;
}
if(flag)
pw.println(-1);
else {
pw.println(ls.size());
for(int b:ls)
pw.println(b+ " "+(b+1)+" "+n);
}
}
}
pw.flush();
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 2c48ef0853d48d2d126af03be6a5d787 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main (String[] args) {
FastReader sc=new FastReader();
int t = sc.nextInt();
while (t-->0){
int n = sc.nextInt();
int a[] = new int[n];
for(int i=0; i<n; i++) {
a[i] = sc.nextInt();
}
int i1 = a[n-1], i2 = a[n-2];
boolean flag = true;
for(int i=0; i<n-1; i++){
if(a[i] >a[i+1]){
flag = false;
break;
}
}
if(flag){
System.out.println("0");
continue;
}
if(i2-i1<= i2 && i2<=i1) {
System.out.println(n - 2);
for (int i = 0; i < n - 2; i++) {
System.out.println((i + 1) + " " + (n - 1) + " " + n);
}
continue;
}
System.out.println("-1");
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | f0a8e270a2b43b5127c2fc509113a934 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.io.*;
import java.util.*;
public class C {
static int mod=(int)1e9+7;
public static void main(String[] args) {
var io = new Copied(System.in, System.out);
// int k=1;
int t = 1;
t = io.nextInt();
for (int i = 0; i < t; i++) {
// io.print("Case #" + k + ": ");
solve(io);
// k++;
}
io.close();
}
public static void solve(Copied io) {
int n=io.nextInt();
int a[]=io.readArray(n);
int f=a[n-2],s=a[n-1];
if(f>s){
io.println(-1);
return;
}
if(s>=0){
io.println(n-2);
for(int i=n-3;i>=0;i--){
io.println((i+1)+" "+(n-1)+" "+(n));
}
}
else if(s<0){
boolean ans=true;
for(int i=0;i<n-1;i++){
ans&=(a[i]<=a[i+1]);
}
if(ans){
io.println(0);
}
else{
io.println(-1);
}
}
}
static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static void binaryOutput(boolean ans, Copied io){
String a=new String("YES"), b=new String("NO");
if(ans){
io.println(a);
}
else{
io.println(b);
}
}
static int power(int x, int y, int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static void printArr(int[] arr) {
for (int x : arr)
System.out.print(x + " ");
System.out.println();
}
static int[] listToInt(ArrayList<Integer> a){
int n=a.size();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=a.get(i);
}
return arr;
}
static int mod_mul(int a, int b){ a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod;}
static int mod_add(int a, int b){ a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod;}
static int mod_sub(int a, int b){ a = a % mod; b = b % mod; return (((a - b) % mod) + mod) % mod;}
}
// class Pair implements Cloneable, Comparable<Pair>
// {
// int x,y;
// Pair(int a,int b)
// {
// this.x=a;
// this.y=b;
// }
// @Override
// public boolean equals(Object obj)
// {
// if(obj instanceof Pair)
// {
// Pair p=(Pair)obj;
// return p.x==this.x && p.y==this.y;
// }
// return false;
// }
// @Override
// public int hashCode()
// {
// return Math.abs(x)+101*Math.abs(y);
// }
// @Override
// public String toString()
// {
// return "("+x+" "+y+")";
// }
// @Override
// protected Pair clone() throws CloneNotSupportedException {
// return new Pair(this.x,this.y);
// }
// @Override
// public int compareTo(Pair a)
// {
// int t= this.x-a.x;
// if(t!=0)
// return t;
// else
// return this.y-a.y;
// }
// }
// class DescendingOrder<T> implements Comparator<T>{
// @Override
// public int compare(Object o1,Object o2){
// // -1 if want to swap and (1,0) otherwise.
// int addingNumber=(Integer) o1,existingNumber=(Integer) o2;
// if(addingNumber>existingNumber) return -1;
// else if(addingNumber<existingNumber) return 1;
// else return 0;
// }
// }
class Copied extends PrintWriter {
public Copied(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Copied(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public String next() {
return nextToken();
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 878f0db9424e4600344b13fd5b25c3b6 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static MyScanner sc = new MyScanner();
static Output out = new Output();
public static void main(String[] args) {
int t=sc.nextInt();
for(int i=0;i<t;i++) {
solve();
}
out.flush();
}
public static void solve() {
int n=sc.nextInt();
int a[]=sc.nextIntArr(n);
boolean check=true;
for(int i=0;i<n-1;i++) {
if(a[i+1]<a[i]) {
check=false;
break;
}
}
if(check){
out.println("0");
return;
}
long n1=a[n-1],n2=a[n-2];
if(n1>=n2 && n2-n1<=n2) {
out.println(n-2);
for(int i=0;i<n-2;i++) {
out.println(i+1+" "+(n-1)+" "+(n));
}
}
else {
out.println(-1);
}
}
}
class Array{
static void sort(int[] arr){
mergesort(arr,0,arr.length-1);
}
static void sort(int[] arr,int startIndex,int endIndex){
mergesort(arr,startIndex,endIndex);
}
static void mergesort(int[] arr,int b,int e) {
if(b<e) {
int m=b+(e-b)/2;
mergesort(arr,b,m);
mergesort(arr,m+1,e);
merge(arr,b,m,e);
}
return;
}
static void merge(int[] arr,int b,int m,int e) {
int len1=m-b+1,len2=e-m;
int[] l=new int[len1];
int[] r=new int[len2];
for(int i=0;i<len1;i++)l[i]=arr[b+i];
for(int i=0;i<len2;i++)r[i]=arr[m+1+i];
int i=0,j=0,k=b;
while(i<len1 && j<len2) {
if(l[i]<r[j])arr[k++]=l[i++];
else arr[k++]=r[j++];
}
while(i<len1)arr[k++]=l[i++];
while(j<len2)arr[k++]=r[j++];
return;
}
public static void SortColoumn (int[][] array, final int columnIndex){
Arrays.sort(array, new Comparator<int[]>() {
@Override
public int compare(int[] first, int[] second) {
if(first[columnIndex] < second[columnIndex]) return 1;
else return -1;
}
});
}
// 2d sort
// static void sort(int[][] arr){
// mergesort(arr,0,arr.length-1);
// }
// static void sort(int[][] arr,int startIndex,int endIndex){
// mergesort(arr,startIndex,endIndex);
// }
//
// static void mergesort(int[][] arr,int b,int e) {
// if(b<e) {
// int m=b+(e-b)/2;
// mergesort(arr,b,m);
// mergesort(arr,m+1,e);
// merge(arr,b,m,e);
// }
// return;
// }
// static void merge(int[][] arr,int b,int m,int e) {
// int len1=m-b+1,len2=e-m;
// int[][] l=new int[len1][2];
// int[][] r=new int[len2][2];
// for(int i=0;i<len1;i++) {
// l[i][0]=arr[b+i][0];
// l[i][1]=arr[b+i][1];
// }
// for(int i=0;i<len2;i++) {
// r[i][0]=arr[m+1+i][0];
// r[i][1]=arr[m+1+i][1];
// }
// int i=0,j=0,k=b;
// while(i<len1 && j<len2) {
// if(l[i][0]<r[j][0]) {
// arr[k++][0]=l[i++][0];
// arr[k-1][1]=l[i-1][1];
// }
// else {
// arr[k++][0]=r[j++][0];
// arr[k-1][1]=r[j-1][1];
// }
// }
// while(i<len1) {
// arr[k++][0]=l[i++][0];
// arr[k-1][1]=l[i-1][1];
// }
// while(j<len2) {
// arr[k++][0]=r[j++][0];
// arr[k-1][1]=r[j-1][1];
// }
// return;
// }
}
class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] nextIntArr(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArr(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public double[] nextDoubleArr(int n) {
double a[] = new double[n];
for (int i = 0; i < n; i++)
a[i] = nextDouble();
return a;
}
public String[] nextArr(int n) {
String a[] = new String[n];
for (int i = 0; i < n; i++)
a[i] = next();
return a;
}
public String[] nextLineArr(int n) {
String a[] = new String[n];
for (int i = 0; i < n; i++)
a[i] = nextLine();
return a;
}
}
class Output {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
public void print() {}
public void println() {
try {
out.write("\n");
} catch (Exception e) {}
}
public void print(Object s) {
try {
out.write(s + "");
} catch (Exception e) {}
}
public void println(Object s) {
try {
out.write(s + "\n");
} catch (Exception e) {}
}
public void flush() {
try {
out.flush();
} catch (Exception e) {}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 1fce6c5ca7819b9af0af6f9bc2ee8d98 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static long mod = (int)1e9+7;
// static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
int t=sc.nextInt();
// int t=1;
while(t-->0)
{
int n = sc.nextInt();
long arr[] = sc.readArrayLong(n);
ArrayList<Integer> list = new ArrayList<>();
long ans = 0;
long secondLast = arr[n - 2];
long last = arr[n - 1];
boolean flag = true;
for(int i=0;i<n-1;i++)
{
if(arr[i] > arr[i + 1])
{
flag = false;
break;
}
}
if(!flag)
{
long diff = secondLast - last;
if(diff <= secondLast && secondLast <= last)
{
System.out.println(n - 2);
for(int i=0;i<n-2;i++)
{
System.out.println((i+1)+" "+(n-1)+" "+(n));
}
}
else
{
System.out.println(-1);
}
}
else
{
System.out.println(0);
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
static void reverse_sorted(int[] arr)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list , Collections.reverseOrder());
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static int LowerBound(int a[], int x) { // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value
int l=-1,r=list.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(list.get(m)<=x) l=m;
else r=m;
}
return l+1;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static class Queue_Pair implements Comparable<Queue_Pair> {
int first , second;
public Queue_Pair(int first, int second) {
this.first=first;
this.second=second;
}
public int compareTo(Queue_Pair o) {
return Integer.compare(o.first, first);
}
}
static void leftRotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
static void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n-1] = temp;
}
static boolean isPalindrome(String str)
{
// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;
// While there are characters to compare
while (i < j) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
static boolean palindrome_array(char arr[], int n)
{
// Initialise flag to zero.
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
return false;
else
return true;
}
static boolean allElementsEqual(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
static boolean allElementsDistinct(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]!=arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
public static void reverse_Long(long[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
long temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
static boolean isSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static boolean isReverseSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] < a[i + 1]) {
return false;
}
}
return true;
}
static int[] rearrangeEvenAndOdd(int arr[], int n)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
int[] array = list.stream().mapToInt(i->i).toArray();
return array;
}
static long[] rearrangeEvenAndOddLong(long arr[], int n)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
long[] array = list.stream().mapToLong(i->i).toArray();
return array;
}
static boolean isPrime(long n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (long i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static long getSum(long n)
{
long sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcdLong(long a, long b)
{
if (b == 0)
return a;
return gcdLong(b, a % b);
}
static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static int countDigit(int n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
}
class Pair
{
int first;
int second;
Pair(int first , int second)
{
this.first = first;
this.second = second;
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 81e2372a8f9c364348a76d96936e5211 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static PrintWriter out;
static Kioken sc;
public static void main(String[] args) throws FileNotFoundException {
boolean t = true;
boolean f = false;
if (f) {
out = new PrintWriter("output.txt");
sc = new Kioken("input.txt");
} else {
out = new PrintWriter((System.out));
sc = new Kioken();
}
int tt = 1;
tt = sc.nextInt();
while (tt-- > 0) {
solve();
}
out.flush();
out.close();
}
public static void solve() {
int n = sc.nextInt();
long[] arr = new long[n];
for(int i = 0; i < n; i++){
arr[i] = sc.nextLong();
}
if(arr[n - 1] < arr[n - 2]){
out.println(-1);
return;
}
// long[] copy = Arrays.copyOf(arr, arr.length);
List<int[]> ll = new ArrayList<>();
long ps = -1;
int index = -1;
if(arr[n - 1] >= 0){
ps = arr[n - 1];
index = n - 1;
}
if(arr[n - 2] >= 0){
ps = arr[n - 2];
index = n - 2;
}
for(int i = n - 3; i >= 0; i--){
if(arr[i] > arr[i+1]){
if(arr[i+1] < 0 && arr[i+2] < 0){
if(ps == -1){
out.println(-1);
return;
}else{
arr[i] = arr[i+1] - ps;
ll.add(new int[]{i+1, i+1+1, index+1});
}
}else{
arr[i] = arr[i+1] - arr[i+2];
ll.add(new int[]{i+1, i+1+1, i+2+1});
}
}
if(arr[i] >= 0){
ps = arr[i];
index = i;
}
}
out.println(ll.size());
for(int i = 0; i < ll.size(); i++){
int[] v = ll.get(i);
out.println(v[0] + " " + v[1] + " " + v[2]);
// copy[v[0] - 1] = copy[v[1] - 1] - copy[v[2] - 1];
}
// out.println(Arrays.toString(arr));
// out.println(Arrays.toString(copy));
}
public static long gcd(long a, long b) {
while (b != 0) {
long rem = a % b;
a = b;
b = rem;
}
return a;
}
public static long leftShift(long a) {
return (long) Math.pow(2, a);
}
public static boolean[] sieve(int n) {
boolean isPrime[] = new boolean[n + 1];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (!isPrime[i])
continue;
for (int j = i * 2; j <= n; j = j + i) {
isPrime[j] = false;
}
}
return isPrime;
}
public static void reverse(int[] arr) {
Arrays.sort(arr);
int n = arr.length;
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}
return;
}
public static int lower_bound(ArrayList<Integer> ar, int k) {
int s = 0, e = ar.size();
while (s != e) {
int mid = s + e >> 1;
if (ar.get(mid) <= k) {
s = mid + 1;
} else {
e = mid;
}
}
return Math.abs(s) - 1;
}
public static int upper_bound(ArrayList<Integer> ar, int k) {
int s = 0;
int e = ar.size();
while (s != e) {
int mid = s + e >> 1;
if (ar.get(mid) < k) {
s = mid + 1;
} else {
e = mid;
}
}
if (s == ar.size()) {
return -1;
}
return s;
}
static class Kioken {
// FileInputStream br = new FileInputStream("input.txt");
BufferedReader br;
StringTokenizer st;
Kioken(String filename) {
try {
FileReader fr = new FileReader(filename);
br = new BufferedReader(fr);
st = new StringTokenizer("");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
Kioken() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean hasNext() {
String next = null;
try {
next = br.readLine();
} catch (Exception e) {
}
if (next == null || next.length() == 0) {
return false;
}
st = new StringTokenizer(next);
return true;
}
}
class DSU {
int[] parent, size;
DSU(int n) {
parent = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
int findParent(int i) {
if (parent[i] == i) {
return i;
}
return parent[i] = findParent(parent[i]);
}
void Union(int u, int v) {
int parent_u = findParent(u);
int parent_v = findParent(v);
if (parent_u == parent_v)
return;
// small attached to big, since we want to reduce overall size
if (size[parent_u] < size[parent_v]) {
parent[parent_u] = parent_v;
size[parent_v]++;
} else {
parent[parent_v] = parent_u;
size[parent_u]++;
}
}
}
// SEGMENT-TREE
static class SegmentTree {
int[] arr = new int[4 * 100000];
int[] givenArr;
// HINT: This can be updated with ques.
int build(int index, int l, int r) {
if (l == r) {
return arr[index] = givenArr[l];
}
int mid = (l + r) / 2;
return arr[index] = build(2 * index + 1, l, mid) + build(2 * index + 2, mid + 1, r);
}
SegmentTree(int[] nums) {
givenArr = nums;
build(0, 0, nums.length - 1);
}
// HINT: This can be updated with ques.
void update(int index, int l, int r, int diff, int i) {
if (i >= arr.length) {
return;
}
if (index >= l && index <= r) {
arr[i] = arr[i] + diff;
}
if (index < l || index > r) {
return;
}
int mid = (l + r) / 2;
update(index, l, mid, diff, 2 * i + 1);
update(index, mid + 1, r, diff, 2 * i + 2);
return;
}
void update(int index, int val) {
int diff = val - givenArr[index];
givenArr[index] = val;
update(index, 0, givenArr.length - 1, diff, 0);
}
int query(int left, int right, int l, int r, int i) {
// not overlapping
if (r < left || l > right) {
return 0;
}
// total - overlapping
if (l >= left && r <= right) {
return arr[i];
}
// partial overlapping
int mid = (l + r) / 2;
int le = query(left, right, l, mid, 2 * i + 1);
int ri = query(left, right, mid + 1, r, 2 * i + 2);
return le + ri;
}
// HINT: for max sum, can be changed according to ques.
int query(int l, int r) {
return query(l, r, 0, givenArr.length - 1, 0);
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 9d3700d3fa9b582d82dfc25469bf7cba | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
long arr[] = new long[n];
for(int i=0;i<n;i++) arr[i] = sc.nextLong();
int smax[] = new int[n]; smax[n-1] = n-1;
int y = n-2, z = n-1;
long min = arr[y] - arr[z];
for(int i=n-2;i>=0;i--){
if(arr[i] > arr[smax[i+1]])
smax[i] = i;
else
smax[i] = smax[i+1];
}
if(arr[n-2] > arr[n-1]){
System.out.println(-1);
} else {
List<String> ans = new ArrayList<>();
boolean flag = true, placed = false;
for(int i=n-3;i>=0;i--){
if(placed){
ans.add((i+1) + " " + (y+1) + " " + (z+1));
continue;
}
if(arr[i] > arr[i+1]){
if(min > arr[i+1]){
flag = false;
break;
}
arr[i] = arr[y] - arr[z];
placed = true;
ans.add((i+1) + " " + (y+1) + " " + (z+1));
}
if(arr[i] - arr[smax[i+1]] < min){
min = arr[i] - arr[smax[i+1]];
y = i;
z = smax[i+1];
}
}
if(flag){
System.out.println(ans.size());
for(String s : ans)
System.out.println(s);
} else {
System.out.println(-1);
}
}
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 2c1f34ec67012041988c79571e91526a | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution
{
private static class FastIO {
private static class FastReader
{
BufferedReader br;
StringTokenizer st;
FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
private static PrintWriter out = new PrintWriter(System.out);
private static FastReader in = new FastReader();
public void print(String s) {out.print(s);}
public void println(String s) {out.println(s);}
public void println() {
println("");
}
public void print(int i) {out.print(i);}
public void print(long i) {out.print(i);}
public void print(char i) {out.print(i);}
public void print(double i) {out.print(i);}
public void println(int i) {out.println(i);}
public void println(long i) {out.println(i);}
public void println(char i) {out.println(i);}
public void println(double i) {out.println(i);}
public static int[] getIntArray(int n) {
int[] res = new int[n];
for(int i = 0; i < n; i++) {
res[i] = in.nextInt();
}
return res;
}
public static List<Integer> getIntList(int n) {
List<Integer> list = new ArrayList<>();
for(int i = 0; i < n; i++) {
list.add(in.nextInt());
}
return list;
}
public static void printKickstartCase(int i) {
out.print("Case #" + i + ": ");
}
public String next() {return in.next();}
int nextInt() { return in.nextInt(); }
char nextChar() {return in.next().charAt(0);}
long nextLong() { return in.nextLong(); }
double nextDouble() { return in.nextDouble(); }
String nextLine() {return in.nextLine();}
public void close() {
out.flush();
out.close();
}
}
private static final FastIO io = new FastIO();
private static int M = 1000000007;
private static class Triplet {
int x, y, z;
Triplet(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
private static boolean isSorted(int[] a) {
for(int i = 0; i < a.length - 1; i++) {
if(a[i] > a[i + 1]) {
return false;
}
}
return true;
}
public static void main(String[] args)
{
int t = io.nextInt();
for(int z = 0; z < t; z ++) {
int n = io.nextInt();
int[] a = FastIO.getIntArray(n);
int count = 0;
if(a[n - 2] > a[n - 1]) {
io.println(-1);
continue;
}
if(isSorted(a)) {
io.println(0);
continue;
}
int[] max_right = new int[n];
int[] max_right_index = new int[n];
max_right[n - 1] = Integer.MIN_VALUE + 1000;
for(int i = n - 2; i >= 0; i--) {
if(max_right[i + 1] > a[i + 1]) {
max_right[i] = max_right[i + 1];
max_right_index[i] = max_right_index[i + 1];
}
else {
max_right[i] = a[i + 1];
max_right_index[i] = i + 1;
}
}
int[] diff = new int[n];
diff[n - 1] = Integer.MAX_VALUE;
int[] diff_start_index = new int[n];
int[] diff_end_index = new int[n];
for(int i = n - 2; i >= 0; i--) {
if(a[i] - max_right[i] < diff[i + 1]) {
diff[i] = a[i] - max_right[i];
diff_start_index[i] = i;
diff_end_index[i] = max_right_index[i];
}
else {
diff[i] = diff[i + 1];
diff_start_index[i] = diff_start_index[i + 1];
diff_end_index[i] = diff_end_index[i + 1];
}
}
Stack<Triplet> res = new Stack<>();
for(int i = n - 3; i >= 0; i--) {
if(diff[i + 1] < a[i]) {
res.push(new Triplet(i + 1, diff_start_index[i + 1] + 1, diff_end_index[i + 1] + 1));
a[i] = diff[i + 1];
count ++;
}
}
if(isSorted(a)) {
io.println(count);
while(!res.isEmpty()) {
Triplet tr = res.pop();
io.println(tr.x + " " + tr.y + " " + tr.z);
}
}
else {
io.println(-1);
}
}
io.close();
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | e21cf7f0ebf79022d17988a6320fecc2 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.Map.Entry;
public class codeforces {
static int mod = 1000000007;
public static void main(String[] args) {
FastReader sc = new FastReader();
try {
StringBuilder ss = new StringBuilder();
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int a[] = new int[n];
for(int i = 0 ; i <n;i++) {
a[i] = sc.nextInt();
}
long moves = 0;
boolean flag = true;
for(int i = 0 ; i<n-1 ; i++) {
if(a[i]>a[i+1]) {
flag = false;
break;
}
}
if(flag) {
ss.append(0 +"\n");
}
else {
if(a[n-2]<=a[n-1] ) {
if(a[n-2]<0 && a[n-1]<0) {
ss.append(-1 +"\n");
}
else {
moves = n-2;
ss.append(moves +"\n");
for(int i = 0 ; i <n-2 ; i++) {
ss.append((i+1) +" "+(n-1) +" "+n +"\n");
}
}
}
else {
ss.append(-1 +"\n");
}
}
}
System.out.println(ss);
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
static long pow(long a, long b) {
long ans = 1;
long temp = a;
while(b>0) {
if((b&1) == 1) {
ans*=temp;
}
temp = temp*temp;
b = b>>1;
}
return ans;
}
static long ncr(int n, int r) {
return fact(n) / (fact(r) *
fact(n - r));
}
static long fact(long n)
{
long res = 1;
for (int i = 2; i <= n; i++) {
res = (res * i);
}
return res;
}
static long gcd(long a, long b) {
if(b == 0) {
return a;
}
return gcd(b , a%b);
}
static ArrayList<Integer> factor(long n) {
ArrayList<Integer> al = new ArrayList<>();
for(int i = 1 ; i*i<=n;i++) {
if(n%i == 0) {
if(n/i == i) {
al.add(i);
}
else {
al.add(i);
al.add((int) (n/i));
}
}
}
return al;
}
static class Pair implements Comparable<Pair>{
long a;
char b ;
Pair(long a, char b){
this.a = a;
this.b = b;
}
@Override
public int compareTo(Pair o) {
// TODO Auto-generated method stub
return (int)(this.a - o.a);
}
}
static ArrayList<Integer> sieve(int n) {
boolean a[] = new boolean[n+1];
Arrays.fill(a, false);
for(int i = 2 ; i*i <=n ; i++) {
if(!a[i]) {
for(int j = 2*i ; j<=n ; j+=i) {
a[j] = true;
}
}
}
ArrayList<Integer> al = new ArrayList<>();
for(int i = 2 ; i <=n;i++) {
if(!a[i]) {
al.add(i);
}
}
return al;
}
static ArrayList<Long> pf(long n) {
ArrayList<Long> al = new ArrayList<>();
while(n%2 == 0) {
al.add(2l);
n = n/2;
}
for(long i = 3 ; i*i<=n ; i+=2) {
while(n%i == 0) {
al.add(i);
n = n/i;
}
}
if(n>2) {
al.add( n);
}
return al;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | ce8f74387a3f7c1450bf28b5df1fd1ea | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.*;
import java.util.*;
public class MyClass {
public static void main(String args[])throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t-->0){
int n=Integer.parseInt(br.readLine());
String s[]=br.readLine().split(" ");
int arr[]=new int[n];
int a[]=new int[n];
for(int i=0;i<n;i++) a[i]=arr[i]=Integer.parseInt(s[i]);
Arrays.sort(a);
if(Arrays.equals(a,arr)){
System.out.println("0");
continue;
}
int z=arr[n-1],y=arr[n-2],x=arr[n-3];
if(z<y||z<0){
System.out.println("-1");
continue;
}
String st=n-1+" "+n;
System.out.println(n-2);
for(int i=0;i<n-2;i++) System.out.println(i+1+" "+st);
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 21ad13f34c86a961a37cbb1553548c16 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.*;
import java.lang.*;
import java.util.*;
public class ComdeFormces {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
FastReader sc=new FastReader();
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
int t=sc.nextInt();
while(t--!=0) {
int n=sc.nextInt();
long a[]=new long[n];
for(int i=0;i<n;i++)a[i]=sc.nextLong();
// Arrays.sort(a);
int min=a.length-1;
long ch=1000000000;
ArrayList<trip> ar=new ArrayList<>();
boolean sorted=true;
if(a[a.length-2]>a[a.length-1])sorted=false;
else {
for(int i=a.length-2;i>=0;i--) {
if(a[i]>a[i+1] ) {
if( i+2<a.length) {
if(a[i+1]-a[a.length-1]<2*ch) {
a[i]=a[i+1]-a[a.length-1];
ar.add(new trip(i+1,i+2,a.length));
}
else {
a[i]=a[i+1]-a[min];
if(a[i]>=2*ch) {
sorted=false;
break;
}
ar.add(new trip(i+1,i+2,min+1));
}
}
}
if(a[i]>=0) {
if(a[min]>a[i])min=i;
}
}
for(int i=0;i<a.length-1;i++) {
if(a[i]>a[i+1] ) {
sorted=false;
break;
}
}
}
if(sorted) {
log.write(ar.size()+"\n");
for(int i=0;i<ar.size();i++) {
log.write(ar.get(i).a+" "+ar.get(i).b+" "+ar.get(i).c+"\n");
}
}
else log.write("-1\n");
// log.write("\n");
log.flush();
}
}
//static int fd(pair a[], long wd,int e) {
// int s=0;
// while(s<=e) {
// int m=s+(e-s)/2;
// }
//
// return -2;
//}
public static String rever(String s) {
char a[]=s.toCharArray();
for(int i=0;i<a.length/2;i++) {
char temp=a[i];
a[i]=a[a.length-1-i];
a[a.length-1-i]=temp;
}
return String.valueOf(a);
}
//static ArrayList<Integer> pr(int n){
// boolean vis[]=new boolean[n+1];
// vis[0]=false;
// vis[1]=false;
// for(int i=2;i<=n;i++) {
// if(vis[i]) {
// for(int j=2*i;j<=n;j+=i) {
// vis[j]=false;
// }
// }
// }
//}
static int subset(int a[],int j,int dp[][], int sum) {
if(j==0) {
if(sum==0 || sum-a[j]==0)return 1;
else return 0;
}
if(sum==0)return 1;
if(dp[j][sum]>0)return dp[j][sum];
// p("for j="+j+"sum="+sum,"s");
if(sum-a[j]>=0) {
int p=subset(a,j-1,dp,sum-a[j]);
int q=subset(a,j-1,dp,sum);
return dp[j][sum]=p+q;
}
else{
return dp[j][sum]=subset(a,j-1,dp,sum);
}
}
static long slv(int a[],int b[],long dp[][],int end,int k,int i) {
if(i<1 ) {
if(k==0) {
return (end-a[0])*b[0];
}
else return Integer.MAX_VALUE;
}
if(k<0)return Integer.MAX_VALUE;
if(k==0) {
return (end-a[0])*b[0];
}
if(dp[i][k]!=0)return dp[i][k];
long ans1=slv(a,b,dp,a[i],k-1,i-1);
long ans2=slv(a,b,dp,end,k,i-1);
long val=(end-a[i])*b[i];
return dp[i][k]=Math.min(val+ans1,ans2);
}
static int bss(int[] a,int s, long k) {
int e=a.length-1;
// int max=-1;
while(s<=e) {
int m=s+(e-s)/2;
if(a[m]==k) {
return m;
}
else if(a[m]<k)s=m+1;
else e=m-1;
}
return -1;
}
static int solv(int[] a,int len) {
if(len%2==0) {
int ans=0;
for(int i=0;i<a.length;i++){
if(a[i]>0){
if(a[i]%2==0) {
ans+=a[i];
}
else if(a[i]%2!=0) {
ans+=(a[i]/2)*2;
}
}
}
int cnt=ans/len;
return cnt;
}
else {
int ans=0,one=0;
for(int i=0;i<a.length;i++){
if(a[i]>0){
if(a[i]%2==0) {
ans+=a[i];
}
else {
ans+=(a[i]/2)*2;
one++;
}
}
}
int n=len-1;
int cnt=ans/n;
int mod=cnt%n+one;
if(cnt>=mod)return cnt;
return mod;
}
}
//debug
static pair bss(ArrayList<pair> a,int el,int ind) {
int s=0;
int e=a.size()-1;
pair ans=new pair(-1,-1);
while(s<=e) {
int m=s+(e-s)/2;
if(a.get(m).a==el) {
ans=a.get(m);
e=m-1;
}
if(a.get(m).a>el)e=m-1;
else s=m+1;
}
return ans;
}
public static <E> void p(E[][] a,String s) {
System.out.println(s);
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[0].length;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void p(int[] a,String s) {
System.out.print(s+"=");
for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");
System.out.println();
}
public static <E> void p(E a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(ArrayList<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(LinkedList<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(HashSet<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(Stack<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(Queue<E> a,String s){
System.out.println(s+"="+a);
}
//utils
static ArrayList<Integer> divisors(int n){
ArrayList<Integer> ar=new ArrayList<>();
for (int i=2; i<=Math.sqrt(n); i++){
if (n%i == 0){
if (n/i == i) {
ar.add(i);
}
else {
ar.add(i);
ar.add(n/i);
}
}
}
return ar;
}
static int primeDivisor(int n){
ArrayList<Integer> ar=new ArrayList<>();
int cnt=0;
boolean pr=false;
while(n%2==0) {
pr=true;
n/=2;
}
if(pr)ar.add(2);
for(int i=3;i*i<=n;i+=2) {
pr=false;
while(n%i==0) {
n/=i;
pr=true;
}
if(pr)ar.add(i);
}
if(n>2) ar.add(n);
return ar.size();
}
static String rev(String s) {
char temp[]=s.toCharArray();
for(int i=0;i<temp.length/2;i++) {
char tp=temp[i];
temp[i]=temp[temp.length-1-i];
temp[temp.length-1-i]=tp;
}
return String.valueOf(temp);
}
static int bs(ArrayList<pair> arr,int el) {
int start=0;
int end=arr.size()-1;
while(start<=end) {
int mid=start+(end-start)/2;
if(arr.get(mid).a==el)return mid;
else if(arr.get(mid).a<el)start=mid+1;
else end=mid-1;
}
if(start>arr.size()-1)return -2;
return -1;
}
static long find(int s,long a[]) {
if(s>=a.length)return -1;
long num=a[s];
for(int i=s;i<a.length;i+=2) {
num=gcd(num,a[i]);
if(num==1 || num==0)return -1;
}
return num;
}
static long gcd(long a,long b) {
if(b==0)return a;
else return gcd(b,a%b);
}
static int gcd(int a,int b) {
if(b==0)return a;
else return gcd(b,a%b);
}
static long factmod(long n,long mod,long img) {
if(n==0)return 1;
long ans=1;
long temp=1;
while(n--!=0) {
if(temp!=img) {
ans=((ans%mod)*((temp)%mod))%mod;
}
temp++;
}
return ans%mod;
}
static int bs(long a[] ,long num) {
int start=0;
int end=a.length-1;
while(start<=end) {
int mid=start+(end-start)/2;
if(a[mid]==num) {
return mid;
}
else if(a[mid]<num)start=mid+1;
else end=mid-1;
}
return start;
}
static int ncr(int n, int r){
if(r>n-r)r=n-r;
int ans=1;
for(int i=0;i<r;i++){
ans*=(n-i);
ans/=(i+1);
}
return ans;
}
public static class trip{
long a,b;
int c;
public trip(long a,long b,int c) {
this.a=a;
this.b=b;
this.c=c;
}
public int compareTo(trip q) {
return (int)(this.a-q.a);
}
}
static void mergesort(int[] a,int start,int end) {
if(start>=end)return ;
int mid=start+(end-start)/2;
mergesort(a,start,mid);
mergesort(a,mid+1,end);
merge(a,start,mid,end);
}
static void merge(int[] a, int start,int mid,int end) {
int ptr1=start;
int ptr2=mid+1;
int b[]=new int[end-start+1];
int i=0;
while(ptr1<=mid && ptr2<=end) {
if(a[ptr1]<=a[ptr2]) {
b[i]=a[ptr1];
ptr1++;
i++;
}
else {
b[i]=a[ptr2];
ptr2++;
i++;
}
}
while(ptr1<=mid) {
b[i]=a[ptr1];
ptr1++;
i++;
}
while(ptr2<=end) {
b[i]=a[ptr2];
ptr2++;
i++;
}
for(int j=start;j<=end;j++) {
a[j]=b[j-start];
}
}
public static class FastReader {
BufferedReader b;
StringTokenizer s;
public FastReader() {
b=new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(s==null ||!s.hasMoreElements()) {
try {
s=new StringTokenizer(b.readLine());
}
catch(IOException e) {
e.printStackTrace();
}
}
return s.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str="";
try {
str=b.readLine();
}
catch(IOException e) {
e.printStackTrace();
}
return str;
}
boolean hasNext() {
if (s != null && s.hasMoreTokens()) {
return true;
}
String tmp;
try {
b.mark(1000);
tmp = b.readLine();
if (tmp == null) {
return false;
}
b.reset();
} catch (IOException e) {
return false;
}
return true;
}
}
public static class pair{
long a;
long b;
public pair(long a,long b) {
this.a=a;
this.b=b;
}
// public int compareTo(pair b) {
// return this.a-b.a;
//
// }
// public int compareToo(pair b) {
// return this.b-b.b;
// }
@Override
public String toString() {
return "{"+this.a+" "+this.b+"}";
}
}
static long pow(long a, long pw) {
long temp;
if(pw==0)return 1;
temp=pow(a,pw/2);
if(pw%2==0)return temp*temp;
return a*temp*temp;
}
static int pow(int a, int pw) {
int temp;
if(pw==0)return 1;
temp=pow(a,pw/2);
if(pw%2==0)return temp*temp;
return a*temp*temp;
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 25dcfa538a0459b79af4467b4403c104 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.*;
import java.util.*;
public class codeforces {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]) {
if (System.getProperty("ONLINE_JUDGE") == null) {
// Input is a file
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
System.err.println("Error");
}
} else {
// Input is System.in
}
FastReader sc = new FastReader();
// Scanner sc = new Scanner(System.in);
//System.out.println(java.time.LocalTime.now());
StringBuilder sb = new StringBuilder();
int t = sc.nextInt();
while(t>0) {
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n; i++){
arr[i] = sc.nextInt();
}
if(arr[n-1]<arr[n-2]){
sb.append(-1 + "\n");
}else{
if(arr[n-1] <0){
int k = 0;
for(int i = 1; i<n; i++){
if(arr[i] < arr[i-1]){
k = -1;
break;
}
}
sb.append(k + "\n");
}else{
sb.append(n-2 + "\n");
for(int i = n-3; i>=0; i--){
sb.append( (i+1) + " " +(i+2) + " " +(n) + "\n");
}
}
}
//sb.append("\n");
t--;
}
System.out.println(sb);
}
//////////nCr////////////////////////////////////////
///////// SUM OF EACH DIGIT OF A NUMBER ///////////////
public static long digSum(long a) {
long sum = 0;
while(a>0) {
sum += a%10;
a /= 10;
}
return sum;
}
///////// TO CHECK NUMBER IS PRIME OR NOT ///////////////
public static boolean isPrime(int n) {
if(n<=1)return false;
if(n <= 3)return true;
if(n%2==0 || n%3==0)return false;
for(int i = 5; i*i<=n; i+=6) {
if(n%i == 0 || n%(i+2) == 0)return false;
}
return true;
}
///////// NEXT PRIME NUMBER BIGGER THAN GIVEN NUMBER ///////////////
public static int nextPrime(int n) {
while(true) {
n++;
if(isPrime(n)) break;
}
return n;
}
///////// GCD ///////////////
public static int gcd(int a, int b) {
if(b == 0)return a;
return gcd(b, a%b);
}
///////// LCM ///////////////
public static int lcm(int a, int b) {
return (a*b)/gcd(a,b);
}
///////// IS POWER OF 2 ///////////////
public static boolean isPowerOfTwo (int x){
/* First x in the below expression is
for the case when x is 0 */
return x!=0 && ((x&(x-1)) == 0);
}
}
class Pair{
int x;
int y;
Pair(int x, int y){
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if(this == o)return true;
if(o == null || this.getClass() != o.getClass())return false;
Pair p = (Pair)o;
return x == p.x && y == p.y;
}
@Override
public int hashCode(){
return Objects.hash(x , y);
}
// @Override
// public int compareTo(Pair o) {
// }
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 5472106d2125a80b098e243dbe6f26b9 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
/**
*
* @Har_Har_Mahadev
*/
/**
* Main , Solution , Remove Public
*/
public class C {
public static void process() throws IOException {
int n = sc.nextInt();
long arr[] = sc.readArrayLong(n);
long ss = arr[n-1], ff = arr[n-2];
if(ff > ss) {
System.out.println(-1);
return;
}
if(ss < 0) {
for(int i = 0; i+1<n; i++) {
if(arr[i] > arr[i+1]) {
System.out.println(-1);
return;
}
}
System.out.println(0);
return;
}
System.out.println(n-2);
for(int i = 1; i+2<=n; i++) {
System.out.println(i+" "+(n-1)+" "+(n));
}
}
//=============================================================================
//--------------------------The End---------------------------------
//=============================================================================
private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353;
private static int N = 0;
private static void google(int tt) {
System.out.print("Case #" + (tt) + ": ");
}
static FastScanner sc;
static FastWriter out;
public static void main(String[] args) throws IOException {
boolean oj = true;
if (oj) {
sc = new FastScanner();
out = new FastWriter(System.out);
} else {
sc = new FastScanner("input.txt");
out = new FastWriter("output.txt");
}
long s = System.currentTimeMillis();
int t = 1;
t = sc.nextInt();
int TTT = 1;
while (t-- > 0) {
// google(TTT++);
process();
}
out.flush();
// tr(System.currentTimeMillis()-s+"ms");
}
private static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static void tr(Object... o) {
if (!oj)
System.err.println(Arrays.deepToString(o));
}
static class Pair implements Comparable<Pair> {
long x;
int y;
Pair(long x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return Long.compare(this.x, o.x);
}
/*
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair key = (Pair) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
*/
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static int ceil(int x, int y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long sqrt(long z) {
long sqz = (long) Math.sqrt(z);
while (sqz * 1L * sqz < z) {
sqz++;
}
while (sqz * 1L * sqz > z) {
sqz--;
}
return sqz;
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
public static long gcd(long a, long b) {
if (a > b)
a = (a + b) - (b = a);
if (a == 0L)
return b;
return gcd(b % a, a);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int lower_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = -1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] > x) {
high = mid - 1;
} else {
ans = mid;
low = mid + 1;
}
}
return ans;
}
public static int upper_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = arr.length;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
static void ruffleSort(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void reverseArray(int[] a) {
int n = a.length;
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
//custom multiset (replace with HashMap if needed)
public static void push(TreeMap<Integer, Integer> map, int k, int v) {
//map[k] += v;
if (!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k) + v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v) {
//assumes map[k] >= v
//map[k] -= v
int lol = map.get(k);
if (lol == v)
map.remove(k);
else
map.put(k, lol - v);
}
// compress Big value to Time Limit
public static int[] compress(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int boof = 1; //min value
for (int x : ls)
if (!map.containsKey(x))
map.put(x, boof++);
int[] brr = new int[arr.length];
for (int i = 0; i < arr.length; i++)
brr[i] = map.get(arr[i]);
return brr;
}
// Fast Writer
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE)
innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000)
return 10;
if (l >= 100000000)
return 9;
if (l >= 10000000)
return 8;
if (l >= 1000000)
return 7;
if (l >= 100000)
return 6;
if (l >= 10000)
return 5;
if (l >= 1000)
return 4;
if (l >= 100)
return 3;
if (l >= 10)
return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L)
return 19;
if (l >= 100000000000000000L)
return 18;
if (l >= 10000000000000000L)
return 17;
if (l >= 1000000000000000L)
return 16;
if (l >= 100000000000000L)
return 15;
if (l >= 10000000000000L)
return 14;
if (l >= 1000000000000L)
return 13;
if (l >= 100000000000L)
return 12;
if (l >= 10000000000L)
return 11;
if (l >= 1000000000L)
return 10;
if (l >= 100000000L)
return 9;
if (l >= 10000000L)
return 8;
if (l >= 1000000L)
return 7;
if (l >= 100000L)
return 6;
if (l >= 10000L)
return 5;
if (l >= 1000L)
return 4;
if (l >= 100L)
return 3;
if (l >= 10L)
return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map)
write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
// Fast Inputs
static class FastScanner {
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1)
return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] readArray(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] readArrayLong(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public int[][] readArrayMatrix(int N, int M, int Index) {
if (Index == 0) {
int[][] res = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
int[][] res = new int[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
public long[][] readArrayMatrixLong(int N, int M, int Index) {
if (Index == 0) {
long[][] res = new long[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = nextLong();
}
return res;
}
long[][] res = new long[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC)
c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-')
neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] readArrayDouble(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32)
return true;
while (true) {
c = getChar();
if (c == NC)
return false;
else if (c > 32)
return true;
}
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 313e0c8c217fd030058c5269a3adc8fc | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | // JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.*;
import java.lang.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.io.*;
public class Eshan {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static PrintWriter out = new PrintWriter(System.out);
static DecimalFormat df = new DecimalFormat("0.0000000");
final static int mod = (int) (1e9 + 7);
final static int MAX = Integer.MAX_VALUE;
final static int MIN = Integer.MIN_VALUE;
static Random rand = new Random();
// ======================= MAIN ==================================
public static void main(String[] args) throws IOException {
long time = System.currentTimeMillis();
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
// ==== start ====
int t = readInt();
// int t = 1;
preprocess();
while (t-- > 0) {
solve();
}
out.flush();
// ==== end ====
if (!oj)
System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
}
private static void solve() throws IOException {
int n = readInt();
long[] arr = readLongArray(n);
if (arr.length == 1) {
out.println(0);
return;
}
if (arr[n - 1] < arr[n - 2]) {
out.println(-1);
return;
}
List<int[]> list = new ArrayList<>();
for (int i = n - 3; i >= 0; i--) {
if (arr[i] > arr[i + 1] - arr[n - 1]) {
arr[i] = arr[i + 1] - arr[n - 1];
list.add(new int[] { i + 1, i + 2, n });
}
if (arr[i] > arr[i + 1]) {
out.println(-1);
return;
}
}
out.println(list.size());
for (int i = 0; i < list.size(); i++)
printIArray(list.get(i));
}
private static void preprocess() {
}
// ==================== CUSTOM CLASSES ================================
static class Pair implements Comparable<Pair> {
int first, second;
Pair(int first, int second) {
this.first = first;
this.second = second;
}
public int compareTo(Pair o) {
if (this.first != o.first)
return this.second - o.second;
return this.first - o.first;
}
}
static class DequeNode {
DequeNode prev, next;
int val;
DequeNode(int val) {
this.val = val;
}
DequeNode(int val, DequeNode prev, DequeNode next) {
this.val = val;
this.prev = prev;
this.next = next;
}
}
// ======================= FOR INPUT ==================================
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(readLine());
return st.nextToken();
}
static long readLong() throws IOException {
return Long.parseLong(next());
}
static int readInt() throws IOException {
return Integer.parseInt(next());
}
static double readDouble() throws IOException {
return Double.parseDouble(next());
}
static char readCharacter() throws IOException {
return next().charAt(0);
}
static String readString() throws IOException {
return next();
}
static String readLine() throws IOException {
return br.readLine().trim();
}
static int[] readIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = readInt();
return arr;
}
static int[][] read2DIntArray(int n, int m) throws IOException {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++)
arr[i] = readIntArray(m);
return arr;
}
static List<Integer> readIntList(int n) throws IOException {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(readInt());
return list;
}
static long[] readLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = readLong();
return arr;
}
static long[][] read2DLongArray(int n, int m) throws IOException {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++)
arr[i] = readLongArray(m);
return arr;
}
static List<Long> readLongList(int n) throws IOException {
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(readLong());
return list;
}
static char[] readCharArray(int n) throws IOException {
return readString().toCharArray();
}
static char[][] readMatrix(int n, int m) throws IOException {
char[][] mat = new char[n][m];
for (int i = 0; i < n; i++)
mat[i] = readCharArray(m);
return mat;
}
// ========================= FOR OUTPUT ==================================
private static void printIList(List<Integer> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printLList(List<Long> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printIArray(int[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DIArray(int[][] arr) {
for (int i = 0; i < arr.length; i++)
printIArray(arr[i]);
}
private static void printLArray(long[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DLArray(long[][] arr) {
for (int i = 0; i < arr.length; i++)
printLArray(arr[i]);
}
// ====================== TO CHECK IF STRING IS NUMBER ========================
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static boolean isLong(String s) {
try {
Long.parseLong(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
// ==================== FASTER SORT ================================
private static void sort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void reverseSort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void sort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void reverseSort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
// ==================== MATHEMATICAL FUNCTIONS ===========================
private static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static long power(long a, long b) {
if (b == 0)
return 1L;
long ans = power(a, b >> 1);
ans *= ans;
if ((b & 1) == 1)
ans *= a;
return ans;
}
private static int mod_power(int a, int b) {
if (b == 0)
return 1;
int temp = mod_power(a, b >> 1);
temp %= mod;
temp = (int) ((1L * temp * temp) % mod);
if ((b & 1) == 1)
temp = (int) ((1L * temp * a) % mod);
return temp;
}
private static int multiply(int a, int b) {
return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod);
}
private static boolean isPrime(long n) {
for (long i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
private static long nCr(long n, long r) {
if (n - r > r)
r = n - r;
long ans = 1L;
for (long i = r + 1; i <= n; i++)
ans *= i;
for (long i = 2; i <= n - r; i++)
ans /= i;
return ans;
}
// ==================== Primes using Seive =====================
private static List<Integer> getPrimes(int n) {
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, true);
for (int i = 2; i * i <= n; i++) {
if (prime[i]) {
for (int j = i * i; j <= n; j += i)
prime[j] = false;
}
}
// return prime;
List<Integer> list = new ArrayList<>();
for (int i = 2; i <= n; i++)
if (prime[i])
list.add(i);
return list;
}
private static int[] SeivePrime(int n) {
int[] primes = new int[n];
for (int i = 0; i < n; i++)
primes[i] = i;
for (int i = 2; i * i < n; i++) {
if (primes[i] != i)
continue;
for (int j = i * i; j < n; j += i) {
if (primes[j] == j)
primes[j] = i;
}
}
return primes;
}
// ==================== STRING FUNCTIONS ================================
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
private static String sortString(String str) {
int[] arr = new int[256];
for (char ch : str.toCharArray())
arr[ch]++;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 256; i++)
while (arr[i]-- > 0)
sb.append((char) i);
return sb.toString();
}
// ==================== LIS & LNDS ================================
private static int LIS(int arr[], int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find1(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find1(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) >= val) {
ret = mid;
j = mid - 1;
} else {
i = mid + 1;
}
}
return ret;
}
private static int LNDS(int[] arr, int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find2(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find2(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) <= val) {
i = mid + 1;
} else {
ret = mid;
j = mid - 1;
}
}
return ret;
}
// =============== Lower Bound & Upper Bound ===========
// less than or equal
private static int lower_bound(List<Integer> list, int val) {
int ans = -1, lo = 0, hi = list.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(int[] arr, int val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(long[] arr, long val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
// greater than or equal
private static int upper_bound(List<Integer> list, int val) {
int ans = list.size(), lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(int[] arr, int val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(long[] arr, long val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
// ==================== UNION FIND =====================
private static int find(int x, int[] parent) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x], parent);
}
private static boolean union(int x, int y, int[] parent, int[] rank) {
int lx = find(x, parent), ly = find(y, parent);
if (lx == ly)
return false;
if (rank[lx] > rank[ly])
parent[ly] = lx;
else if (rank[lx] < rank[ly])
parent[lx] = ly;
else {
parent[lx] = ly;
rank[ly]++;
}
return true;
}
// ==================== SEGMENT TREE (RANGE SUM) =====================
public static class SegmentTree {
int n;
int[] arr, tree, lazy;
SegmentTree(int arr[]) {
this.arr = arr;
this.n = arr.length;
this.tree = new int[(n << 2)];
this.lazy = new int[(n << 2)];
build(1, 0, n - 1);
}
void build(int id, int start, int end) {
if (start == end)
tree[id] = arr[start];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
build(left, start, mid);
build(right, mid + 1, end);
tree[id] = tree[left] + tree[right];
}
}
void update(int l, int r, int val) {
update(1, 0, n - 1, l, r, val);
}
void update(int id, int start, int end, int l, int r, int val) {
distribute(id, start, end);
if (end < l || r < start)
return;
if (start == end)
tree[id] += val;
else if (l <= start && end <= r) {
lazy[id] += val;
distribute(id, start, end);
} else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
update(left, start, mid, l, r, val);
update(right, mid + 1, end, l, r, val);
tree[id] = tree[left] + tree[right];
}
}
int query(int l, int r) {
return query(1, 0, n - 1, l, r);
}
int query(int id, int start, int end, int l, int r) {
if (end < l || r < start)
return 0;
distribute(id, start, end);
if (start == end)
return tree[id];
else if (l <= start && end <= r)
return tree[id];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r);
}
}
void distribute(int id, int start, int end) {
if (start == end)
tree[id] += lazy[id];
else {
tree[id] += lazy[id] * (end - start + 1);
lazy[(id << 1)] += lazy[id];
lazy[(id << 1) + 1] += lazy[id];
}
lazy[id] = 0;
}
}
// ==================== TRIE ================================
static class Trie {
class Node {
Node[] children;
boolean isEnd;
Node() {
children = new Node[26];
}
}
Node root;
Trie() {
root = new Node();
}
public void insert(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
curr.children[ch - 'a'] = new Node();
curr = curr.children[ch - 'a'];
}
curr.isEnd = true;
}
public boolean find(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
return false;
curr = curr.children[ch - 'a'];
}
return curr.isEnd;
}
}
// ==================== FENWICK TREE ================================
static class FT {
long[] tree;
int n;
FT(int[] arr, int n) {
this.n = n;
this.tree = new long[n + 1];
for (int i = 1; i <= n; i++) {
update(i, arr[i - 1]);
}
}
void update(int idx, int val) {
while (idx <= n) {
tree[idx] += val;
idx += idx & -idx;
}
}
long query(int l, int r) {
return getSum(r) - getSum(l - 1);
}
long getSum(int idx) {
long ans = 0L;
while (idx > 0) {
ans += tree[idx];
idx -= idx & -idx;
}
return ans;
}
}
// ==================== BINARY INDEX TREE ================================
static class BIT {
long[][] tree;
int n, m;
BIT(int[][] mat, int n, int m) {
this.n = n;
this.m = m;
tree = new long[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
update(i, j, mat[i - 1][j - 1]);
}
}
}
void update(int x, int y, int val) {
while (x <= n) {
int t = y;
while (t <= m) {
tree[x][t] += val;
t += t & -t;
}
x += x & -x;
}
}
long query(int x1, int y1, int x2, int y2) {
return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1);
}
long getSum(int x, int y) {
long ans = 0L;
while (x > 0) {
int t = y;
while (t > 0) {
ans += tree[x][t];
t -= t & -t;
}
x -= x & -x;
}
return ans;
}
}
} | Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 1f775e405d442fcf750f91602296e68f | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
public class pb6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0){
int n = sc.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n ; i++){
arr[i] = sc.nextLong();
}
if (arr[n-2] > arr[n-1]){
System.out.println(-1);
}
else if (arr[n-1] >= 0){
System.out.println(n-2);
for (int i = 0 ; i < n-2; i++){
System.out.println(i+1 + " " + (n-1) + " " + n);
}
}
else {
boolean flag = true;
for (int i = 1; i < n; i ++){
if (arr[i] < arr[i-1]){
flag = false;
break;
}
}
if (flag){
System.out.println(0);
}
else{
System.out.println(-1);
}
}
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | f010d199ee73966bb6b3cf78389ca9f0 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | //<———My cp————
//https://takeuforward.org/interview-experience/strivers-cp-sheet/?utm_source=youtube&utm_medium=striver&utm_campaign=yt_video
import java.util.*;
import java.io.*;
public class Solution{
static PrintWriter pw = new PrintWriter(System.out);
static FastReader fr = new FastReader(System.in);
private static long MOD = 1_000_000_007;
public static void main(String[] args) throws Exception{
int t = fr.nextInt();
while(t-->0){
int n = fr.nextInt();
int[] vals = new int[n];
for(int i = 0;i<n;i++){
vals[i]=fr.nextInt();
}
boolean sorted = true;
for(int i = 1;i<n;i++){
if(vals[i]<vals[i-1]){
sorted=false;
}
}
if(sorted){
pw.println(0);
}else if(vals[n-1]<vals[n-2]||(vals[n-1]<0&&vals[n-2]<0)){
pw.println(-1);
}else{
pw.println(n-2);
for(int i=n-2;i>=1;i--){
pw.println(i+" "+(i+1)+" "+n);
}
}
}
pw.close();
}
static class Pair{
int min;
int max;
public Pair(int min,int max){
this.min = min;
this.max = max;
}
@Override
public String toString() {
return "min: "+min+" max: "+max;
}
}
public static long opNeeded(long c,long[] vals){
long tempResult = 0;
for(int j = 0;j<vals.length;j++){
tempResult=tempResult+Math.abs((long)(vals[j]-Math.pow(c,j)));
}
if(tempResult<0){
tempResult=Long.MAX_VALUE;
}
return tempResult;
}
static int isPerfectSquare(int vals){
int lastPow=1;
while(lastPow*lastPow<vals){
lastPow++;
}
if(lastPow*lastPow==vals){
return lastPow*lastPow;
}else{
return -1;
}
}
public static int[] sort(int[] vals){
ArrayList<Integer> values = new ArrayList<>();
for(int i = 0;i<vals.length;i++){
values.add(vals[i]);
}
Collections.sort(values);
for(int i =0;i<values.size();i++){
vals[i] = values.get(i);
}
return vals;
}
public static long[] sort(long[] vals){
ArrayList<Long> values = new ArrayList<>();
for(int i = 0;i<vals.length;i++){
values.add(vals[i]);
}
Collections.sort(values);
for(int i =0;i<values.size();i++){
vals[i] = values.get(i);
}
return vals;
}
public static void reverseArray(long[] vals){
int startIndex = 0;
int endIndex = vals.length-1;
while(startIndex<=endIndex){
long temp = vals[startIndex];
vals[startIndex] = vals[endIndex];
vals[endIndex] = temp;
startIndex++;
endIndex--;
}
}
public static void reverseArray(int[] vals){
int startIndex = 0;
int endIndex = vals.length-1;
while(startIndex<=endIndex){
int temp = vals[startIndex];
vals[startIndex] = vals[endIndex];
vals[endIndex] = temp;
startIndex++;
endIndex--;
}
}
static class FastReader{
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastReader(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan());
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
}
public static int GCD(int numA, int numB){
if(numA==0){
return numB;
}else if(numB==0){
return numA;
}else{
if(numA>numB){
return GCD(numA%numB,numB);
}else{
return GCD(numA,numB%numA);
}
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output | |
PASSED | 0b67d3d101f8cce0e50e62eb430dd307 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. | 256 megabytes | import java.util.*;
public class DiffrentialSorting {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
long arr[]=new long[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextLong();
}
if(arr[n-2]>arr[n-1])
{
System.out.println("-1");
}
else if(arr[n-1]>=0)
{
System.out.println(n-2);
for(int i=0;i<n-2;i++)
{
System.out.println(i+1+" "+(n-1)+" "+n+" ");
}
}
else
{
boolean flag=true;
for(int i=0;i<n-2;i++)
{
if(arr[i]>arr[i+1])
{
flag=false;
break;
}
}
if(flag)
{
System.out.println("0");
}
else
{
System.out.println("-1");
}
}
}
}
}
| Java | ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"] | 2 seconds | ["2\n1 2 3\n3 4 5\n-1\n0"] | NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations. | Java 11 | standard input | [
"constructive algorithms",
"greedy"
] | c3ee6419adfc85c80f35ecfdea6b0d43 | Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$, the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,200 | For each test case, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \leq m \leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.