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
71d57045f88e2e0480e10a6b3f07931a
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Manav */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); CDifferentialSorting solver = new CDifferentialSorting(); solver.solve(1, in, out); out.close(); } static class CDifferentialSorting { public void solve(int testNumber, InputReader in, OutputWriter out) { int t = in.nextInt(); for (int it = 0; it < t; it++) { int n = in.nextInt(); int[] a = in.nextIntArray(n); // boolean decreasing = true; // for(int i = n-2; i >= 0; i--){ // if(a[i] <= a[i+1] // } int[] b = a.clone(); Arrays.sort(b); if (Arrays.compare(a, b) == 0) { out.println(0); } else if (a[n - 2] <= a[n - 1] && a[n - 1] >= 0) { out.println(n - 2); for (int i = 0; i < n - 2; i++) { out.println(i + 1, n - 1, n); } } else { out.println(-1); } } } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void println(int i) { writer.println(i); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } 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) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
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 &lt; y &lt; 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
b7c0b98ca3be36433889c59a4609d0de
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 &lt; y &lt; 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 { public static void main(String[] args) throws IOException { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] arr = sc.nextIntArray(n); if (arr[n - 2] > arr[n - 1]) { pw.println(-1); continue; } if (arr[n - 1] >= 0) { pw.println(n - 2); for (int i = 0; i < arr.length - 2; i++) { pw.println(toString(new int[] { i + 1, n - 1, n })); } continue; } boolean isSorted = true; for (int i = 0; i < arr.length - 1; i++) { if (arr[i] > arr[i + 1]) isSorted = false; } pw.println(isSorted ? 0 : -1); } pw.close(); } static int diffWords(char[] arr1, char[] arr2) { int diff = 0; for (int i = 0; i < arr2.length; i++) { diff += Math.abs(arr1[i] - arr2[i]); } return diff; } static int countOnes(String s) { int res = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '1') res++; } return res; } static long getMinDiff(long num, int[] arr) { long min = Integer.MAX_VALUE; for (int i = 1; i < arr.length - 1; i++) { min = Math.min(min, getdiff(num, arr[i])); } return min; } static long getdiff(long start, int end) { return Math.abs(start - end); } static HashMap Hash(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static HashMap Hash(char[] arr) { HashMap<Character, Integer> map = new HashMap<>(); for (char i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static boolean isPrime(int n) { if (n <= 1) return false; for (int i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } public static long combination(long n, long r) { return factorial(n) / (factorial(n - r) * factorial(r)); } static long factorial(Long n) { if (n == 0) return 1; return n * factorial(n - 1); } static boolean isPalindrome(char[] str, int i, int j) { // While there are characters to compare while (i < j) { // If there is a mismatch if (str[i] != str[j]) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } public static double log2(long l) { double result = (Math.log(l) / Math.log(2)); return result; } public static double log4(int N) { double result = (Math.log(N) / Math.log(4)); return result; } public static int setBit(int mask, int idx) { return mask | (1 << idx); } public static int clearBit(int mask, int idx) { return mask ^ (1 << idx); } public static boolean checkBit(int mask, int idx) { return (mask & (1 << idx)) != 0; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } public static String toString(int[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(long[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(ArrayList arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.size(); i++) { sb.append(arr.get(i) + " "); } return sb.toString().trim(); } public static String toString(int[][] arr) { StringBuilder sb = new StringBuilder(); for (int[] i : arr) { sb.append(toString(i) + "\n"); } return sb.toString(); } public static String toString(boolean[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(Integer[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(String[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(char[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
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 &lt; y &lt; 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
32024a6d231a77b700a8db18aec775f3
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 &lt; y &lt; 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 static java.lang.Math.*; public class CodeForces { final static String no = "NO"; final static String yes = "YES"; static public PrintWriter pw = new PrintWriter(System.out); static public IO sc = new IO(); static Scanner fc = new Scanner(System.in); //////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { int t = sc.nextInt(); in: while(t-->0) { int n = sc.nextInt(); long[]ar = sc.longArray(n); if(ar[n-2]>ar[n-1]) { pw.println(-1); continue in; } List<long[]> ls = new ArrayList(); int cnt =0; for(int i = n-3; i >= 0; i--) { if(ar[i] > ar[i+1]) { ar[i] = ar[i+1] -ar[n-1]; if(ar[i]>ar[i+1]) { cnt = -1; break; }else { cnt++; long a[] = {i+1,i+2,n}; ls.add(a); } } } pw.println(cnt); for(int p=0;p<cnt;p++) { pw.println(ls.get(p)[0]+" "+ls.get(p)[1]+" "+ls.get(p)[2]); } } pw.flush(); } ////////////////////////////////////////////////////////////////////////////////////////// static int floorPowerOf2(int n) { int p = (int)(Math.log(n) / Math.log(2)); return (int)Math.pow(2, p); } static int ceilPowerOf2(int n) { int p = (int)(Math.log(n) / Math.log(2)); return (int)Math.pow(2, p+1); } static List<Integer> printDivisors(int n) { // Note that this loop runs till square root List<Integer> sl = new ArrayList<>(); for (int i = 1; i <= Math.sqrt(n); i++) { if (n % i == 0) { // If divisors are equal, print only one if (n / i == i) sl.add(i); else // Otherwise print both { sl.add(i); sl.add(n / i); } } } return sl; } static class Pair { int x, y,z; Pair(int x,int y){ this.x = x; this.y = y; } Pair(int x, int y,int z) { this.x = x; this.y = y; this.z = z; } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } 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 / gcd(a, b)) * b; } static void sort(int[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 0; i < n; i++) { int idx = rand.nextInt(n); if (idx == i) continue; arr[i] ^= arr[idx]; arr[idx] ^= arr[i]; arr[i] ^= arr[idx]; } Arrays.sort(arr); } static void sort(long[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 0; i < n; i++) { int idx = rand.nextInt(n); if (idx == i) continue; arr[i] ^= arr[idx]; arr[idx] ^= arr[i]; arr[i] ^= arr[idx]; } Arrays.sort(arr); } static void sortDec(int[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 0; i < n; i++) { int idx = rand.nextInt(n); if (idx == i) continue; arr[i] ^= arr[idx]; arr[idx] ^= arr[i]; arr[i] ^= arr[idx]; } Arrays.sort(arr); int l = 0; int r = n - 1; while (l < r) { arr[l] ^= arr[r]; arr[r] ^= arr[l]; arr[l] ^= arr[r]; l++; r--; } } static void sortDec(long[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 0; i < n; i++) { int idx = rand.nextInt(n); if (idx == i) continue; arr[i] ^= arr[idx]; arr[idx] ^= arr[i]; arr[i] ^= arr[idx]; } Arrays.sort(arr); int l = 0; int r = n - 1; while (l < r) { arr[l] ^= arr[r]; arr[r] ^= arr[l]; arr[l] ^= arr[r]; l++; r--; } } static class IO { 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()); } long[] longArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } double nextDouble() { return Double.parseDouble(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 &lt; y &lt; 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
4645cc7c925d8f53db00cdc9269409c4
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 &lt; y &lt; 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.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Double.parseDouble; import static java.lang.Math.PI; import static java.lang.Math.min; import static java.lang.System.arraycopy; import static java.lang.System.exit; import static java.util.Arrays.copyOf; import java.util.LinkedList; import java.util.List; import java.util.Iterator; import java.io.FileReader; import java.io.FileWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.NoSuchElementException; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; import java.util.Comparator; import java.lang.StringBuilder; import java.util.Collections; import java.util.*; import java.text.DecimalFormat; public class Solution { static class Edge{ int u, v, w; Edge(int u, int v, int w){ this.u = u; this.v = v; this.w = w; } } static class Pair{ int first, second; Pair(int first, int second){ this.first = first; this.second = second; } } static class Point{ int x, y; Point(int x, int y){ this.x = x; this.y = y; } } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; static int dx[] = {-1,0,1,0}; static int dy[] = {0,-1,0,1}; private static void solve() throws IOException{ int n = scanInt(); int arr[] = inputArray(n); int count = 0; if(arr[n-2]>arr[n-1]){ out.println(-1); return; } if(arr[n-1] <0 ){ if(isSorted(arr, n)) out.println(0); else out.println(-1); return; } out.println(n-2); for(int i = 0; i<n-2; ++i){ out.println((i+1)+" "+(n-1)+" "+(n)); } } private static boolean isSorted(int arr[], int n){ for(int i = 1; i<n; ++i){ if(arr[i-1] > arr[i]){ return false; } } return true; } private static int[] inputArray(int n) throws IOException { int arr[] = new int[n]; for(int i=0; i<n; ++i) arr[i] = scanInt(); return arr; } public static void main(String[] args) { try { long startTime = System.currentTimeMillis(); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); //in = new BufferedReader(new FileReader("input.txt")); //out = new PrintWriter(new FileWriter("output.txt")); int test=scanInt(); for(int t=1; t<=test; t++){ // out.print("Case #"+t+": "); solve(); } long endTime = System.currentTimeMillis(); long totalTime = endTime - startTime; //out.println(totalTime+"---------- "+System.currentTimeMillis() ); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } static int scanInt() throws IOException { return parseInt(scanString()); } static long scanLong() throws IOException { return parseLong(scanString()); } static double scanDouble() throws IOException { return parseDouble(scanString()); } static String scanString() throws IOException { if (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static String scanLine() throws IOException { return in.readLine(); } }
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 &lt; y &lt; 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
1053f2524fcfd03661330607c228c642
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 &lt; y &lt; 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 final int INF = Integer.MAX_VALUE; static final int NINF = Integer.MIN_VALUE; static final long LINF = Long.MAX_VALUE; static final long LNINF = Long.MIN_VALUE; static Reader reader; static Writer writer; static PrintWriter out; static FastScanner fs; static void solve() { int n = fs.nextInt(); long [] a = fs.readArrayLong(n); if (isSorted(a)) { out.print(0 + "\n"); } else { long d = a[n-2] - a[n-1]; if ((a[n-2] > a[n-1]) || a[n-1] < 0){ out.print("-1\n" ); } else { out.print(n - 2 + "\n"); for (int i = 0; i < n - 2; i++) out.print(i+1 + " " + (n-1) + " " + n + "\n"); } } } static boolean isSorted(long[] a) { for (int i = 1; i < a.length; i++) if (a[i] < a[i-1]) return false; return true; } public static void main(String[] args) { setReaderWriter(); fs = new FastScanner(reader); testForTestcases(); // solve(); out.close(); } static void setReaderWriter() { reader = new InputStreamReader(System.in); writer = new OutputStreamWriter(System.out); out=new PrintWriter(writer); } static void testForTestcases() { int T = fs.nextInt(); while (T-- > 0) { solve(); } } static boolean isInteger(double val) { return !(val - (long)val > 0); } static void swap(int[] a , int i , int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } static int opposite(int n, int x) { return (n-1)^x; } static Pair print(int a, int b) { out.println(a+" "+b); return new Pair(a, b); } static class Pair { int a, b; public Pair(int a, int b) { this.a=a; this.b=b; } } static final Random random=new Random(); static final int mod=1_000_000_007; static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static long add(long a, long b) { return (a+b)%mod; } static long sub(long a, long b) { return ((a-b)%mod+mod)%mod; } static long mul(long a, long b) { return (a*b)%mod; } static long exp(long base, long exp) { if (exp==0) return 1; long half=exp(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials=new long[2_000_001]; static long[] invFactorials=new long[2_000_001]; static void precompFacts() { factorials[0]=invFactorials[0]=1; for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i); invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2); for (int i=invFactorials.length-2; i>=0; i--) invFactorials[i]=mul(invFactorials[i+1], i+1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k])); } 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 class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader reader) { br =new BufferedReader(reader); st =new StringTokenizer(""); } String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } int[] readArrayInt(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; } long nextLong() { return Long.parseLong(next()); } char[] readCharArray() { return next().toCharArray(); } } }
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 &lt; y &lt; 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
0d0c17735697a3fe565a2f5b54edfab4
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 &lt; y &lt; 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 C { public void prayGod() throws IOException { int t = nextInt(); while (t-- > 0) { int n = nextInt(); long[] a = nextLongArray(n); if (a[n - 2] > a[n - 1]) { out.println(-1); continue; } if (a[n - 1] < 0) { boolean possible = true; for (int i = 1; i < n; i++) { if (a[i] < a[i - 1]) { possible = false; break; } } if (possible) out.println(0); else out.println(-1); continue; } out.println(n - 2); for (int i = 0; i < n - 2; i++) { out.printf("%d %d %d\n", i + 1, n - 1, n); } } } public long binpow(long a, long b) { if (b < 0) return 0; long ret = 1, curr = a; while (b > 0) { if (b % 2 == 1) ret = (ret * curr) % mod; b /= 2; curr = (curr * curr) % mod; } return ret; } public long gcd(long x, long y) { if (y == 0) return x; return gcd(y, x % y); } public void printVerdict(boolean verdict) { if (verdict) out.println(VERDICT_YES); else out.println(VERDICT_NO); } static final String VERDICT_YES = "YES"; static final String VERDICT_NO = "NO"; static final boolean RUN_TIMING = true; static final boolean AUTOFLUSH = false; static final boolean FILE_INPUT = false; static final boolean FILE_OUTPUT = false; static int iinf = 0x3f3f3f3f; static long inf = (long) 1e18 + 10; static int mod = (int) 1e9 + 7; static char[] inputBuffer = new char[1 << 20]; static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1 << 20); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), AUTOFLUSH); // int data-type public int nextInt() throws IOException { return Integer.parseInt(next()); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } public void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } // long data-type public long nextLong() throws IOException { return Long.parseLong(next()); } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } public static void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } public void sort(long[] a) { shuffle(a); Arrays.sort(a); } // double data-type public double nextDouble() throws IOException { return Double.parseDouble(next()); } public double[] nextDoubleArray(int n) throws IOException { double[] arr = new double[n]; for (int i = 0; i < n; i++) arr[i] = nextDouble(); return arr; } public static void printArray(double[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } // Generic type public <T> void sort(T[] a) { shuffle(a); Arrays.sort(a); } public static <T> void printArray(T[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } public String next() throws IOException { int len = 0; int c; do { c = in.read(); } while (Character.isWhitespace(c) && c != -1); if (c == -1) { throw new NoSuchElementException("Reached EOF"); } do { inputBuffer[len] = (char) c; len++; c = in.read(); } while (!Character.isWhitespace(c) && c != -1); while (c != '\n' && Character.isWhitespace(c) && c != -1) { c = in.read(); } if (c != -1 && c != '\n') { in.unread(c); } return new String(inputBuffer, 0, len); } public String nextLine() throws IOException { int len = 0; int c; while ((c = in.read()) != '\n' && c != -1) { if (c == '\r') { continue; } inputBuffer[len] = (char) c; len++; } return new String(inputBuffer, 0, len); } public boolean hasNext() throws IOException { String line = nextLine(); if (line.isEmpty()) { return false; } in.unread('\n'); in.unread(line.toCharArray()); return true; } public void shuffle(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public void shuffle(long[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public void shuffle(Object[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); Object temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public static void main(String[] args) throws IOException { if (FILE_INPUT) in = new PushbackReader(new BufferedReader(new FileReader(new File("output.txt"))), 1 << 20); if (FILE_OUTPUT) out = new PrintWriter(new FileWriter(new File("output.txt"))); long time = 0; time -= System.nanoTime(); new C().prayGod(); time += System.nanoTime(); if (RUN_TIMING) System.err.printf("%.3f ms%n", time / 1000000.0); out.flush(); in.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 &lt; y &lt; 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
f85ad4a80977568199ea50c7762df692
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 &lt; y &lt; 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
/***** ---> :) Vijender Srivastava (: <--- *****/ import java.util.*; import java.lang.*; import java.lang.reflect.Array; import java.io.*; public class Main { static FastReader sc =new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=998244353; /* start */ public static void main(String [] args) { // int testcases = 1; int testcases = i(); while(testcases-->0) { solve(); } out.flush(); out.close(); } static void solve() { int n = i(); int a[] = input(n); if(isSorted(a)) { pl(0); } else { if(a[n-1]<a[n-2] || a[n-1]<0) pl(-1); else { pl(n-2); for(int i=0;i<n-2;i++) { pl((i+1)+" "+(n-1)+" "+(n)); } } } } static boolean isSorted(int a[]) { for(int i=0;i<a.length-1;i++) { if(a[i]>a[i+1]) return false; } return true; } /* end */ 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 p(Object o) { out.print(o); } static void pl(Object o) { out.println(o); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static long[] putL(long a[]) { long A[]=new long[a.length]; for(int i=0;i<a.length;i++) { A[i]=a[i]; } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x) ; y = y >> 1; x = (x * x); } return res; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long[] sort(long a[]) { ArrayList<Long> arr = new ArrayList<>(); for(long i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } static int[] sort(int a[]) { ArrayList<Integer> arr = new ArrayList<>(); for(Integer i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } //pair class private static class Pair implements Comparable<Pair> { int first, second; public Pair(int f, int s) { first = f; second = s; } @Override public int compareTo(Pair p) { if (first > p.first) return 1; else if (first < p.first) return -1; else { if (second < p.second) return 1; else if (second > p.second) return -1; else return 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 &lt; y &lt; 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
a82f990c5898caec67b4ec0821ae716c
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 &lt; y &lt; 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.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; import java.io.File; import java.io.FileInputStream; import java.util.*; public class Main { // 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) { // } // } static long MOD = (long) 1e9 + 7; public static void main(String[] args) { FastReader sc = new FastReader(); int test = 1; 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]) System.out.println(-1); else { if (a[n - 1] >= 0) { System.out.println(n - 2); for (int i = 1; i < n - 1; i++) { System.out.println(i + " " + (n - 1) + " " + (n)); } } else { int check = 1; for (int i = 1; i < n; i++) { if (a[i - 1] > a[i]) { check = 0; break; } } if (check == 1) System.out.println(0); else System.out.println(-1); } } } } static 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) != 0) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } public static double log2(long N) { double result = (double) (Math.log(N) / (double) Math.log(2)); return result; } public static int countSetBits(long number) { int count = 0; while (number > 0) { ++count; number &= number - 1; } return count; } static int lower_bound(long target, long[] a, int pos) { if (pos >= a.length) return -1; int low = pos, high = a.length - 1; while (low < high) { int mid = low + (high - low) / 2; if (a[mid] < target) low = mid + 1; else high = mid; } return a[low] >= target ? low : -1; } private static void swap(long[] a, int i, int j) { long tmp = a[i]; a[i] = a[j]; a[j] = tmp; } static class pair { long a; long b; pair(long x, long y) { this.a = x; this.b = y; } } static class first implements Comparator<pair> { public int compare(pair p1, pair p2) { if (p1.a > p2.a) return 1; else if (p1.a < p2.a) return -1; return 0; } } static class second implements Comparator<pair> { public int compare(pair p1, pair p2) { if (p1.b > p2.b) return 1; else if (p1.b < p2.b) return -1; return 0; } } private static long getSum(int[] array) { long sum = 0; for (int value : array) { sum += value; } return sum; } private static boolean isPrime(Long x) { if (x < 2) return false; for (long d = 2; d * d <= x; ++d) { if (x % d == 0) return false; } return true; } static long[] reverse(long a[]) { int n = a.length; int i; long t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } return a; } private static boolean isPrimeInt(int x) { if (x < 2) return false; for (int d = 2; d * d <= x; ++d) { if (x % d == 0) return false; } return true; } public static String reverse(String input) { StringBuilder str = new StringBuilder(""); for (int i = input.length() - 1; i >= 0; i--) { str.append(input.charAt(i)); } return str.toString(); } private static int[] getPrimes(int n) { boolean[] used = new boolean[n + 1]; used[0] = used[1] = true; // int size = 0; for (int i = 2; i <= n; ++i) { if (!used[i]) { // ++size; for (int j = 2 * i; j <= n; j += i) { used[j] = true; } } } int[] primes = new int[n + 1]; for (int i = 0; i <= n; ++i) { if (!used[i]) { primes[i] = 1; } } return primes; } static long expo(long a, long b, long mod) { long res = 1; while (b > 0) { if ((b & 1) != 0) res = (res * a) % mod; a = (a * a) % mod; b = b >> 1; } return res; } static long mminvprime(long a, long b) { return expo(a, b - 2, b); } static long mod_add(long a, long b, long m) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } static long mod_mul(long a, long b, long m) { a = a % m; b = b % m; return (((a * b) % m) + m) % m; } static long mod_sub(long a, long b, long m) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } static long mod_div(long a, long b, long m) { a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m; } private static long lcm(long a, long b) { return a / gcd(a, b) * b; } private static long gcd(long a, long b) { return (a == 0 ? b : gcd(b % a, a)); } static void sortI(int[] arr) { int n = arr.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } Arrays.sort(arr); } static void shuffleList(ArrayList<Long> arr) { int n = arr.size(); Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = arr.get(i); int randomPos = i + rnd.nextInt(n - i); arr.set(i, arr.get(randomPos)); arr.set(randomPos, tmp); } } static void factorize(long n) { int count = 0; while (!(n % 2 > 0)) { n >>= 1; count++; } if (count > 0) { // System.out.println("2" + " " + count); } long i = 0; for (i = 3; i <= (long) Math.sqrt(n); i += 2) { count = 0; while (n % i == 0) { count++; n = n / i; } if (count > 0) { // System.out.println(i + " " + count); } } if (n > 2) { // System.out.println(i + " " + count); } } static void sortL(long[] arr) { int n = arr.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } Arrays.sort(arr); } ////////////////////////////////// DSU START /////////////////////////// static class DSU { int[] parent, rank, total_Elements; DSU(int n) { parent = new int[n + 1]; rank = new int[n + 1]; total_Elements = new int[n + 1]; for (int i = 0; i <= n; i++) { parent[i] = i; rank[i] = 1; total_Elements[i] = 1; } } int find(int u) { if (parent[u] == u) return u; return parent[u] = find(parent[u]); } void unionByRank(int u, int v) { int pu = find(u); int pv = find(v); if (pu != pv) { if (rank[pu] > rank[pv]) { parent[pv] = pu; total_Elements[pu] += total_Elements[pv]; } else if (rank[pu] < rank[pv]) { parent[pu] = pv; total_Elements[pv] += total_Elements[pu]; } else { parent[pu] = pv; total_Elements[pv] += total_Elements[pu]; rank[pv]++; } } } boolean unionBySize(int u, int v) { u = find(u); v = find(v); if (u != v) { parent[u] = v; total_Elements[v] += total_Elements[u]; total_Elements[u] = 0; return true; } return false; } } ////////////////////////////////// DSU END ///////////////////////////// static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public boolean hasNext() { return false; } 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 &lt; y &lt; 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
8b3527cbaee0e76a59634b8035e57ade
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 &lt; y &lt; 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
// C. Differential Sorting // Codeforces Round #772 (Div. 2) // Codeforces // Solution by Anmol Sharma import java.util.*; import java.lang.*; import java.io.*; public class Main { public static long mod = 1000000007; public static long mod2 = 998244353; public static void main(String[] args) throws java.lang.Exception { Reader sc = new Reader(); FastNum in = new FastNum(System.in); Writer out = new Writer(System.out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int tests = in.nextInt(); for (int test = 1; test <= tests; test++) { int n = in.nextInt(); long[] a = in.nextLongArray(n); boolean isPossible = false; long max = a[n - 1]; long min = a[n - 2]; if(min > max) { out.println("-1"); } else { int count = 0; for (int i = n - 2; i >= 0; i--) { if(a[i] > a[i + 1]) { count++; } } if(count == 0) { out.println("0"); } else if(count > 0 && max < 0) { out.println("-1"); } else { out.println(n - 2); for (int i = 1; i < n - 1; i++) { out.println(i + " " + (n - 1) + " " + n); } } } } out.flush(); } static long modSum(long p, long q) { if (q > 0) { return (p + q) % mod; } else { return (p + q + mod) % mod; } } static long invMod(long p, long q, long m) { long expo = m - 2; while (expo != 0) { if ((expo & 1) == 1) { p = (p * q) % m; } q = (q * q) % m; expo >>= 1; } return p; } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } public static long power(long a, long b) { if (b == 0) return 1; long answer = power(a, b / 2) % mod; answer = (answer * answer) % mod; if (b % 2 != 0) answer = (answer * a) % mod; return answer; } public static void swap(int x, int y) { int t = x; x = y; y = t; } public static long min(long a, long b) { if (a < b) return a; return b; } public static long divide(long a, long b) { return (a % mod * (power(b, mod - 2) % mod)) % mod; } public static long nCr(long n, long r) { long answer = 1; long k = min(r, n - r); for (long i = 0; i < k; i++) { answer = (answer % mod * (n - i) % mod) % mod; answer = divide(answer, i + 1); } return answer % mod; } public static boolean plaindrome(String str) { StringBuilder sb = new StringBuilder(); sb.append(str); return (str.equals((sb.reverse()).toString())); } public static class Pair { int a; int b; Pair(int s, int e) { a = s; b = e; } static void sort(Pair[] a) { Arrays.sort(a, (o1, o2) -> { if (o1.a == o2.a) { return o1.b - o2.b; } else { return o1.a - o2.a; } }); } @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); } } static class Assert { static void check(boolean e) { if (!e) { throw new AssertionError(); } } } static class FastNum implements AutoCloseable { InputStream is; byte buffer[] = new byte[1 << 16]; int size = 0; int pos = 0; FastNum(InputStream is) { this.is = is; } int nextChar() { if (pos >= size) { try { size = is.read(buffer); } catch (IOException e) { throw new IOError(e); } pos = 0; if (size == -1) { return -1; } } Assert.check(pos < size); int c = buffer[pos] & 0xFF; pos++; return c; } int nextInt() { int c = nextChar(); while (c == ' ' || c == '\r' || c == '\n' || c == '\t') { c = nextChar(); } if (c == '-') { c = nextChar(); Assert.check('0' <= c && c <= '9'); int n = -(c - '0'); c = nextChar(); while ('0' <= c && c <= '9') { int d = c - '0'; c = nextChar(); Assert.check(n > Integer.MIN_VALUE / 10 || n == Integer.MIN_VALUE / 10 && d <= -(Integer.MIN_VALUE % 10)); n = n * 10 - d; } return n; } else { Assert.check('0' <= c && c <= '9'); int n = c - '0'; c = nextChar(); while ('0' <= c && c <= '9') { int d = c - '0'; c = nextChar(); Assert.check( n < Integer.MAX_VALUE / 10 || n == Integer.MAX_VALUE / 10 && d <= Integer.MAX_VALUE % 10); n = n * 10 + d; } return n; } } char nextCh() { int c = nextChar(); while (c == ' ' || c == '\r' || c == '\n' || c == '\t') { c = nextChar(); } return (char) c; } long nextLong() { int c = nextChar(); while (c == ' ' || c == '\r' || c == '\n' || c == '\t') { c = nextChar(); } if (c == '-') { c = nextChar(); Assert.check('0' <= c && c <= '9'); long n = -(c - '0'); c = nextChar(); while ('0' <= c && c <= '9') { int d = c - '0'; c = nextChar(); Assert.check(n > Long.MIN_VALUE / 10 || n == Long.MIN_VALUE / 10 && d <= -(Long.MIN_VALUE % 10)); n = n * 10 - d; } return n; } else { Assert.check('0' <= c && c <= '9'); long n = c - '0'; c = nextChar(); while ('0' <= c && c <= '9') { int d = c - '0'; c = nextChar(); Assert.check( n < Long.MAX_VALUE / 10 || n == Long.MAX_VALUE / 10 && d <= Long.MAX_VALUE % 10); n = n * 10 + d; } return n; } } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } char[] nextCharArray(int n) { char[] arr = new char[n]; for (int i = 0; i < n; i++) { arr[i] = nextCh(); } return arr; } @Override public void close() { } } static class Writer extends PrintWriter { public Writer(java.io.Writer out) { super(out); } public Writer(java.io.Writer out, boolean autoFlush) { super(out, autoFlush); } public Writer(OutputStream out) { super(out); } public Writer(OutputStream out, boolean autoFlush) { super(out, autoFlush); } public void printArray(int[] arr) { for (int j : arr) { print(j); print(' '); } println(); } public void printArray(long[] arr) { for (long j : arr) { print(j); print(' '); } println(); } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final 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') break; 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(); } } }
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 &lt; y &lt; 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
d2440029ca37b142636212e85d26fb82
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 &lt; y &lt; 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]; 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); } } } }
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 &lt; y &lt; 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
3e8811028825382d04ce63be793c2883
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 &lt; y &lt; 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.PrintWriter; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; public class C { static QuickReader fs = new QuickReader(); static PrintWriter out = new PrintWriter(System.out); static void output() { int n = fs.nextInt(); long[] a = fs.readLongArray(n); long[] b = a.clone(); Arrays.sort(b); if(Arrays.equals(a,b)){ out.println(0); }else if(a[n-2] > a[n-1] || (a[n-2] - a[n-1] > a[n-2])){ out.println(-1); }else{ 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 = 1; T = fs.nextInt(); for (int tt = 0; tt < T; tt++) { output(); out.flush(); } } static void printArray(int[] a) { for (int i : a) out.print(i + " "); out.println(); } static void printlnArray(int[] a) { for (int i : a) out.println(i); } static void printMatrix(int[][] a) { for (int[] i : a) printArray(i); } static void sortA(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 sortD(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l, (i, j) -> j - i); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class QuickReader { 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[] readIntArray(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; } long nextLong() { return Long.parseLong(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 &lt; y &lt; 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
98201cc9f708564f5d7e4ad165049028
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 &lt; y &lt; 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.PrintWriter; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; public class C { static QuickReader fs = new QuickReader(); static PrintWriter out = new PrintWriter(System.out); static void output() { int n = fs.nextInt(); long[] a = fs.readLongArray(n); long[] b = a.clone(); Arrays.sort(b); if(Arrays.equals(a,b)){ out.println(0); } else if(a[n-1] < a[n-2] || (a[n-2] - a[n-1] > a[n-2])){ out.println(-1); }else{ 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 = 1; T = fs.nextInt(); for (int tt = 0; tt < T; tt++) { output(); out.flush(); } } static void printArray(int[] a) { for (int i : a) out.print(i + " "); out.println(); } static void printlnArray(int[] a) { for (int i : a) out.println(i); } static void printMatrix(int[][] a) { for (int[] i : a) printArray(i); } static void sortA(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 sortD(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l, (i, j) -> j - i); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class QuickReader { 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[] readIntArray(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; } long nextLong() { return Long.parseLong(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 &lt; y &lt; 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
40f504668ac3ddf8daebae5d262595ea
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 &lt; y &lt; 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
// Java is like Alzheimer's, it starts off slow, but eventually, your memory is gone. import java.io.*; import java.util.*; public class Aqueous { static MyScanner sc = new MyScanner(); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) { 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.nextInt(); } if(a[n-2]>a[n-1]) { pw.println(-1); } else { ArrayList<String> al = new ArrayList<>(); boolean mila = false; for(int i=n-3; i>=0; i--) { if(a[i]<=a[i+1]) { continue; } else { long x = a[i+1]-a[n-1]; if(x<=a[i+1]) { a[i] = x; String key = (i+1)+" "+(i+2)+" "+n; al.add(key); } else { mila = true; break; } } } if(mila) { pw.println(-1); } else { pw.println(al.size()); for(String e : al){ pw.println(e); } } } } pw.close(); } static void ruffleSort(int a[]) { int n = a.length; Random r = new Random(); for (int i = 0; i < n; i++) { int oi = r.nextInt(n); int temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static void ruffleSort(long a[]) { int n = a.length; Random r = new Random(); for (int i = 0; i < n; i++) { int oi = r.nextInt(n); long temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } 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 &lt; y &lt; 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
63f6bd274ea699df55ccd3ef0ac6e026
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 &lt; y &lt; 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 Codeforces.Round772; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class C{ public static void main(String[] args) throws Exception {new C().run();} long mod = 1000000000 + 7; int ans=0; // int[][] ar; void solve() throws Exception { int t=ni(); while(t-->0){ int n = ni(); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i] = ni(); } boolean flag = false; for(int i=1;i<n;i++){ if(a[i]<a[i-1]) {flag=true;break;} } if(!flag){ out.println(0); continue; } if(a[n-1]<a[n-2]){ out.println(-1); continue; } List<int[]> l = new ArrayList<>(); if(a[n-2]-a[n-1]>a[n-2]){ out.println(-1); continue; } for(int i=n-3;i>=0;i--){ l.add(new int[]{i+1,n-1,n}); } out.println(l.size()); for(int[] i:l){ out.println(i[0]+" "+i[1]+" "+i[2]); } } } boolean isPalindrome(String s){ int i=0; int j = s.length()-1; while(i<j){ if(s.charAt(i)!=s.charAt(j)){ return false; } i++; j--; } return true; } // void buildMatrix(){ // // for(int i=1;i<=1000;i++){ // // ar[i][1] = (i*(i+1))/2; // // for(int j=2;j<=1000;j++){ // ar[i][j] = ar[i][j-1]+(j-1)+i-1; // } // } // } /* FAST INPUT OUTPUT & METHODS BELOW */ private byte[] buf = new byte[1024]; private int index; private InputStream in; private int total; private SpaceCharFilter filter; PrintWriter out; int min(int... ar) { int min = Integer.MAX_VALUE; for (int i : ar) min = Math.min(min, i); return min; } long min(long... ar) { long min = Long.MAX_VALUE; for (long i : ar) min = Math.min(min, i); return min; } int max(int... ar) { int max = Integer.MIN_VALUE; for (int i : ar) max = Math.max(max, i); return max; } long max(long... ar) { long max = Long.MIN_VALUE; for (long i : ar) max = Math.max(max, i); return max; } void shuffle(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i = 0; i < a.length; i++) al.add(a[i]); Collections.sort(al); for (int i = 0; i < a.length; i++) a[i] = al.get(i); } long lcm(long a, long b) { return (a * b) / (gcd(a, b)); } int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } /* * for (1/a)%mod = ( a^(mod-2) )%mod ----> use expo to calc -->(a^(mod-2)) */ long expo(long p, long q) /* (p^q)%mod */ { long z = 1; while (q > 0) { if (q % 2 == 1) { z = (z * p) % mod; } p = (p * p) % mod; q >>= 1; } return z; } void run() throws Exception { in = System.in; out = new PrintWriter(System.out); solve(); out.flush(); } private int scan() throws IOException { if (total < 0) throw new InputMismatchException(); if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) return -1; } return buf[index++]; } private int ni() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); int sgn = 1; if (c == '-') { sgn = -1; c = scan(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = scan(); } while (!isSpaceChar(c)); return res * sgn; } private long nl() throws IOException { long num = 0; int b; boolean minus = false; while ((b = scan()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = scan(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = scan(); } } private double nd() throws IOException { return Double.parseDouble(ns()); } private String ns() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = scan(); } while (!isSpaceChar(c)); return res.toString(); } private String nss() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } private char nc() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); return (char) c; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; return false; } private boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhiteSpace(c); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
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 &lt; y &lt; 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
cab5346815f4b0ee2d4bed7b0205a257
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 &lt; y &lt; 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
// 🔥🔥🔥🔥 Author: Aman Bhatt (Codeforces Handle: bhattaman0001) 🔥🔥🔥🔥 // import java.io.*; import java.util.*; public class Practice extends PrintWriter { Practice() { super(System.out); } static Scanner sc = new Scanner(System.in); class Pair { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public Pair() {} } int binary(int n) { if (n == 1) { return 1; } int temp = binary(n / 2); String str = temp + ""; if (n % 2 == 0) str += '0'; else str += '1'; return Integer.parseInt(str); } int isSorted(long[] arr, long n) { if (n == 1 || n == 0) { return 1; } if (arr[(int) (n - 1)] < arr[(int) (n - 2)]) { return 0; } return isSorted(arr, n - 1); } int findIndex(int arr[], int t) { if (arr == null) { return -1; } int len = arr.length; int i = 0; while (i < len) { if (arr[i] == t) { return i; } else { i = i + 1; } } return -1; } boolean isSorted(ArrayList<Integer> list) { int last = Integer.MIN_VALUE; for (int i = 0; i < list.size(); i++) { int ai = list.get(i); if (last > ai) return false; last = ai; } return true; } int distance(int x1, int y1, int x2, int y2) { return (int) Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); } boolean isSquare(int n) { int sqrt = (int) Math.sqrt(n); if (sqrt * sqrt == n) return true; else return false; } int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int lcm(int a, int b) { return (a / gcd(a, b)) * b; } void reverseAArray(int[] arr, int s, int e) { while (s < e) { int temp = arr[e]; arr[e] = arr[s]; arr[s] = temp; s++; e--; } } String sort(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } int ceil(int a, int b) { return (a + b - 1) / b; } String reverseAString(String s) { char[] ch = s.toCharArray(); String reverse = ""; for (int i = s.length() - 1; i >= 0; i--) { reverse += ch[i]; } return reverse; } public static void main(String[] $) { try (Practice o = new Practice()) { o.main(); o.flush(); } } boolean isDecreasing(long[] arr) { for (int i = 1; i < arr.length; i++) { if (arr[i] < arr[i - 1]) { return false; } } return true; } void main() { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); long[] arr = new long[n]; arr[0] = sc.nextLong(); boolean sort = true; for (int i = 1; i < n; i++) { arr[i] = sc.nextInt(); if (arr[i] < arr[i - 1]) sort = false; } if (sort == true) println(0); else if ( arr[n - 1] < arr[n - 2] || arr[n - 1] < 0 ) { println(-1); } else { println(n - 2); for (int i = 1; i <= n - 2; i++) { println(i + " " + (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 &lt; y &lt; 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
e2d6e65ef4ef1f283d9e7ebdfd6f4fcf
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 &lt; y &lt; 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 differential{ 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 B[]=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); // B[i]=arr[i]; } if(arraySortedOrNot(arr,n)==true) System.out.println(0); else if(arr[n-2]>arr[n-1]) System.out.println(-1); else if(arraySortedOrNot(arr,n)!=true&&arr[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)); } } } } public static boolean arraySortedOrNot(int 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; } }
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 &lt; y &lt; 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
dde04d110ee195e9be694183fb7c5e40
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 &lt; y &lt; 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.Scanner; import java.util.StringTokenizer; 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; } } private static class trip { int m; int n; int o; } 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]; a[0]=sc.nextInt(); int count=0; for(int i=1;i<n;i++) { a[i]=sc.nextInt(); if(a[i]<a[i-1]) count++; } if(a[n-2]>a[n-1]) { 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(n-2); for(int i=1;i<=n-2;i++) { System.out.println(i+" "+(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 &lt; y &lt; 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
933d960501fcfd57e21c32df4b2c5d51
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 &lt; y &lt; 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 { /*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/ public static void solve(int tCase) throws IOException { int n = sc.nextInt(); int [] arr = new int[n]; boolean isInc = true; for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); if(i>0 && arr[i] < arr[i-1])isInc = false; } if(isInc){ out.println(0);return; } int x = arr[n-2] - arr[n-1]; if(x > arr[n-2] || x> arr[n-1] || arr[n-2] > arr[n-1]){ out.println(-1);return; } out.println(n-2); for(int i=0;i<n-2;i++)out.println((i+1)+" "+(n-1)+" "+n); } public static void main(String[] args) throws IOException { openIO(); int testCase = 1; testCase = sc. nextInt(); for (int i = 1; i <= testCase; i++) solve(i); closeIO(); } /*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/ /*--------------------------------------HELPER FUNCTIONS STARTS HERE-----------------------------------------*/ public static int mod = (int) 1e9 + 7; // public static int mod = 998244353; public static int inf_int = (int) 2e9; public static long inf_long = (long) 2e18; public static void _sort(int[] arr, boolean isAscending) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } public static void _sort(long[] arr, boolean isAscending) { int n = arr.length; List<Long> list = new ArrayList<>(); for (long ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // time : O(1), space : O(1) public static int _digitCount(long num,int base){ // this will give the # of digits needed for a number num in format : base return (int)(1 + Math.log(num)/Math.log(base)); } // time : O(n), space: O(n) public static long _fact(int n){ // simple factorial calculator long ans = 1; for(int i=2;i<=n;i++) ans = ans * i % mod; return ans; } // time for pre-computation of factorial and inverse-factorial table : O(nlog(mod)) public static long[] factorial , inverseFact; public static void _ncr_precompute(int n){ factorial = new long[n+1]; inverseFact = new long[n+1]; factorial[0] = inverseFact[0] = 1; for (int i = 1; i <=n; i++) { factorial[i] = (factorial[i - 1] * i) % mod; inverseFact[i] = _modExpo(factorial[i], mod - 2); } } // time of factorial calculation after pre-computation is O(1) public static int _ncr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod); } public static int _npr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[n - r] % mod); } // euclidean algorithm time O(max (loga ,logb)) public static long _gcd(long a, long b) { while (a>0){ long x = a; a = b % a; b = x; } return b; // if (a == 0) // return b; // return _gcd(b % a, a); } // lcm(a,b) * gcd(a,b) = a * b public static long _lcm(long a, long b) { return (a / _gcd(a, b)) * b; } // binary exponentiation time O(logn) public static long _modExpo(long x, long n) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans *= x; ans %= mod; n--; } else { x *= x; x %= mod; n >>= 1; } } return ans; } // function to find a/b under modulo mod. time : O(logn) public static long _modInv(long a,long b){ return (a * _modExpo(b,mod-2)) % mod; } //sieve or first divisor time : O(mx * log ( log (mx) ) ) public static int[] _seive(int mx){ int[] firstDivisor = new int[mx+1]; for(int i=0;i<=mx;i++)firstDivisor[i] = i; for(int i=2;i*i<=mx;i++) if(firstDivisor[i] == i) for(int j = i*i;j<=mx;j+=i) firstDivisor[j] = i; return firstDivisor; } // check if x is a prime # of not. time : O( n ^ 1/2 ) private static boolean _isPrime(long x){ for(long i=2;i*i<=x;i++) if(x%i==0)return false; return true; } static class Pair<K, V>{ K ff; V ss; public Pair(K ff, V ss) { this.ff = ff; this.ss = ss; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || this.getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return ff.equals(pair.ff) && ss.equals(pair.ss); } @Override public int hashCode() { return Objects.hash(ff, ss); } @Override public String toString(){ return ff.toString()+" "+ss.toString(); } } /*--------------------------------------HELPER FUNCTIONS ENDS HERE-----------------------------------------*/ /*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/ static FastestReader sc; static PrintWriter out; private static void openIO() throws IOException { sc = new FastestReader(); out = new PrintWriter(System.out); } public static void closeIO() throws IOException { out.flush(); out.close(); sc.close(); } private static final class FastestReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public FastestReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastestReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() throws IOException { int b; //noinspection StatementWithEmptyBody while ((b = read()) != -1 && isSpaceChar(b)) { } return b; } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final 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(); } final 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(); } final 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 { din.close(); } } /*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/ }
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 &lt; y &lt; 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
4c5fd798bc5f588d410514db5a1a7a89
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 &lt; y &lt; 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]; 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); } } } }
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 &lt; y &lt; 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
ff32db9fd953717e532a96646ac5b2a2
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 &lt; y &lt; 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 C { public static void main (String[] args) throws IOException { Kattio io = new Kattio(); int t = io.nextInt(); outer: for (int ii=0; ii<t; ii++) { int n = io.nextInt(); long[] arr = new long[n]; for (int i=0; i<n; i++) { arr[i] = io.nextLong(); } boolean good = true; for (int i=0; i<n-1; i++) { if (arr[i] > arr[i+1]) { good = false; break; } } if (good) { System.out.println(0); continue outer; } int ops = 0; boolean foundPositive = false; int ind = -1; long pos = -1; StringBuilder ans = new StringBuilder(); for (int i=n-1; i>=0; i--) { if (i < n-1 && arr[i] > arr[i+1]) { if (i == n-2) { System.out.println(-1); continue outer; } if (!foundPositive) { System.out.println(-1); continue outer; } else { arr[i] = arr[i+1] - pos; ans.append((i+1) + " " + (i+2) + " " + (ind+1)); ans.append('\n'); ops++; } } if (arr[i] >= 0 && !foundPositive) { foundPositive = true; pos = arr[i]; ind = i; } } System.out.println(ops); System.out.print(ans); } } static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(new FileWriter(problemName + ".out")); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(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 &lt; y &lt; 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
970c0dfd73b271d6dcd422edf454030d
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 &lt; y &lt; 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 Codechef { static final PrintWriter out =new PrintWriter(System.out); static final FastReader sc = new FastReader(); static long m = 998244353; public static void main (String[] args) throws java.lang.Exception { // ArrayList<Long> arr = sieveOfEratosthenes(10000); long t = sc.nextLong(); while(t-- >0) { int n = sc.nextInt(); long arr[] = new long[n]; for(int i=0 ; i<n ; i++) { arr[i] = sc.nextInt(); } if(solve(arr)==true) { out.println(0); continue; } long count=n-2; long xx = arr[n-1]-arr[n-2]; long yy = arr[n-2]-arr[n-1]; xx = Math.min(xx, yy); for(int i=0 ; i<n-2 ; i++) { arr[i] = xx; } if(solve(arr)==true) { out.println(count); for(int i=0 ; i<n-2 ; i++) { out.println((i+1)+" "+(n-1)+" "+(n)); } } else out.println(-1); } out.flush(); } static boolean solve(long arr[]) { boolean ch = true; for(int i=0 ; i<arr.length-1 ; i++) { if(arr[i]>arr[i+1]) { ch = false ; break; } } return ch; } public static class Pair { long a; long b; long c ; Pair(long a , long b , long c) { this.a=a; this.b=b; this.c=c; } } static class Sort implements Comparator<Pair> { // Method // Sorting in ascending order of roll number public int compare(Pair a, Pair b) { return (int) (a.a - b.a); } } static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop 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 ArrayList<Long> sieveOfEratosthenes(long n) { boolean[] prime = new boolean[(int) (n + 1)]; for (int i = 0; i <= n; i++) prime[i] = true; prime[0]=false; if(1<=n) prime[1]=false; ArrayList<Long> arr = new ArrayList<>(); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { if(p!=2 && p!=3 ) arr.add((long) p); for (int i = p * p; i <= n; i += p) prime[i] = false; } } return arr; } static int count(String s) { int count=0; for(int i=0 ; i<s.length() ; i++) { if(s.charAt(i)=='1' ) count++; } return count; } 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*b)/gcd(a,b); } static long fastPower(long a , long b , long n ) { long res=1; while(b>0) { if((b&1)!=0) { res = (res%n * a%n)%n; } a = (a%n * a%n)%n; b=b>>1; } return res; } static long modexp(long x, long n) { if (n == 0) { return 1; } else if (n % 2 == 0) { return modexp((x * x) % m, n / 2); } else { return (x * modexp((x * x) % m, (n - 1) / 2) % m); } } static long getFractionModulo(long a, long b) { long c = gcd(a, b); a = a / c; b = b / c; long d = modexp(b, m - 2); long ans = ((a % m) * (d % m)) % m; return ans; } public static long power(long x, long y) { long temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return modMult(temp,temp); else { if (y > 0) return modMult(x,modMult(temp,temp)); else return (modMult(temp,temp)) / x; } } static long modMult(long a,long b) { return a*b%m; } 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 &lt; y &lt; 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
6b8fa46db2af0bbabb01b4343b55ddc9
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 &lt; y &lt; 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 C_Differential_Sorting { static final int INT_MOD = (int) 1e9 + 7; static final long LONG_MOD = (long) 1e9 + 7; static final int INT_POSITIVE_INFINITY = Integer.MAX_VALUE; static final long LONG_POSITIVE_INFINITY = Long.MAX_VALUE; static final int INT_NEGATIVE_INFINITY = Integer.MIN_VALUE; static final long LONG_NEGATIVE_INFINITY = Long.MIN_VALUE; static StringBuilder result = new StringBuilder(); public static void main(String args[]) throws IOException { FastReader fr = new FastReader(); FastWriter fw = new FastWriter(); // FastFileReader ffr = new FastFileReader("input.txt"); // FastFileWriter ffw = new FastFileWriter("output.txt"); int tc; tc = fr.nextInt(); // tc = ffr.nextInt(); while (tc-- > 0) { int n = fr.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = fr.nextInt(); } if (arr[n - 2] > arr[n - 1]) { result.append("-1\n"); } else { if (arr[n - 1] >= 0) { result.append((n - 2) + "\n"); for (int i = 1; i <= n - 2; i++) { result.append(i + " " + (n - 1) + " " + (n) + "\n"); } } else { if (isSorted(arr, n)) { result.append("0\n"); } else { result.append("-1\n"); } } } } fw.write(result.toString()); // ffw.write(result.toString()); } static boolean isSorted(int[] arr, int n) { for (int i = 1; i < n; i++) { if (arr[i] < arr[i - 1]) return false; } return true; } static void reverse(int[] nums, int i, int j) { while (i < j) { swap(nums, i++, j--); } } static void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } static boolean isPrime(long x) { if (x <= 1) return false; for (long i = 2; i * i <= x; i++) if (x % i == 0) return false; return true; } static boolean[] sieve(int n) { boolean[] sieve = new boolean[n + 1]; Arrays.fill(sieve, true); sieve[0] = sieve[1] = false; for (int i = 2; i * i <= n; i++) { if (sieve[i]) { for (int j = i * i; j <= n; j += i) { sieve[j] = false; } } } return sieve; } static boolean isFibonacci(long x) { return isPerfectSquare(5 * x * x + 4); } static boolean isPerfectSquare(long x) { if (x <= 1) return true; long low = 1; long high = x; long mid = 0; while (low <= high) { mid = low + (high - low) / 2l; if (mid * mid == x) return true; else if (mid * mid < x) low = mid + 1; else high = mid - 1; } return false; } static long lcm(long a, long b) { return a * b / gcd(a, b); } static long gcd(long a, long b) { if (b > a) return gcd(b, a); if (b == 0) return a; return gcd(b, a % b); } static long pow(long b, long e) { long curr = b; long res = 1; while (e != 0) { if ((e & 1) != 0) { res = (res * curr) % LONG_MOD; } curr = (curr * curr) % LONG_MOD; e >>= 1; } return res; } static double log(double x, double base) { return Math.log(x) / Math.log(base); } } /* user-defined data structures */ class Pair { long a; long b; public Pair(long a, long b) { this.a = a; this.b = b; } } class Tair { int a; int b; int c; public Tair(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } } class Fair { int a; int b; int c; int d; public Fair(int a, int b, int c, int d) { this.a = a; this.b = b; this.c = c; this.d = d; } } class Point { int x; int y; int z; public Point(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } } /* User defined data structures ends here */ /* IO class */ class FastReader { InputStreamReader isr; BufferedReader br; StringTokenizer st; public FastReader() { isr = new InputStreamReader(System.in); br = new BufferedReader(isr); } 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 FastWriter { OutputStreamWriter osw; BufferedWriter bw; public FastWriter() { osw = new OutputStreamWriter(System.out); bw = new BufferedWriter(osw); } void write(String text) { try { bw.write(text); bw.flush(); } catch (IOException e) { e.printStackTrace(); } } } class FastFileReader { FileInputStream fis; InputStreamReader isr; BufferedReader br; StringTokenizer st; public FastFileReader(String fileName) throws FileNotFoundException { fis = new FileInputStream(fileName); isr = new InputStreamReader(fis); br = new BufferedReader(isr); } 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 FastFileWriter { FileOutputStream fos; OutputStreamWriter osw; BufferedWriter bw; public FastFileWriter(String fileName) throws FileNotFoundException { fos = new FileOutputStream(fileName); osw = new OutputStreamWriter(fos); bw = new BufferedWriter(osw); } void write(String text) { try { bw.write(text); bw.flush(); } catch (IOException e) { e.printStackTrace(); } } } /* IO class ends here */
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 &lt; y &lt; 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
9d1501ad2deeeb7b2a0d8b536e01ebce
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 &lt; y &lt; 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.HashSet; import java.util.LinkedList; import java.util.Scanner; 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[] a=new long[n]; for(int i=0;i<n;i++) a[i]=sc.nextLong(); boolean flag=a[n-2]<=a[n-1]; if(!flag) { System.out.println(-1); continue; } LinkedList<pair> list=new LinkedList<>(); boolean f=false; for(int i=n-3;i>=0;i--) { if(a[i]>a[i+1]) { a[i]=a[i+1]-a[n-1]; if(a[i]<=a[i+1]) list.addLast(new pair(i+1,i+1+1,n-1+1)); else { f=true; break; } } } if(f) System.out.println(-1); else { System.out.println(list.size()); while(list.size()>0) System.out.println(list.removeFirst()); } } sc.close(); } } class pair { private final int x; private final int y; private final int z; public pair(int x,int y,int z) { this.x=x; this.y=y; this.z=z; } @Override public String toString() { return x+" "+y+" "+z; } }
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 8
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 &lt; y &lt; 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
9b8f712825c3869c88d268e0bf06edc8
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 &lt; y &lt; 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.HashSet; import java.util.LinkedList; import java.util.Scanner; 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[] a=new long[n]; for(int i=0;i<n;i++) a[i]=sc.nextLong(); boolean flag=a[n-2]<=a[n-1]; if(!flag) { System.out.println(-1); continue; } LinkedList<pair> list=new LinkedList<>(); int z=n-1; boolean f=false; for(int i=n-3;i>=0;i--) { if(a[i]>a[i+1]) { a[i]=a[i+1]-a[z]; if(a[i]<=a[i+1]) list.addLast(new pair(i+1,i+1+1,z+1)); else { f=true; break; } } } if(f) System.out.println(-1); else { System.out.println(list.size()); while(list.size()>0) System.out.println(list.removeFirst()); } } sc.close(); } } class pair { private final int x; private final int y; private final int z; public pair(int x,int y,int z) { this.x=x; this.y=y; this.z=z; } @Override public String toString() { return x+" "+y+" "+z; } }
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 8
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 &lt; y &lt; 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
63b7b827feff5c1c2a0e2c3dda8c5805
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 &lt; y &lt; 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.LinkedList; import java.util.Scanner; 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[] a=new long[n]; for(int i=0;i<n;i++) a[i]=sc.nextLong(); boolean flag=a[n-2]<=a[n-1]; if(!flag) { System.out.println(-1); continue; } LinkedList<pair> list=new LinkedList<>(); int z=n-1; boolean f=false; for(int i=n-3;i>=0;i--) { if(a[i]>a[i+1]) { a[i]=a[i+1]-a[z]; if(a[i]<=a[i+1]) list.addLast(new pair(i+1,i+1+1,z+1)); else { f=true; break; } } if(a[i+1]>=0) { z=i+1; } } if(f) System.out.println(-1); else { System.out.println(list.size()); while(list.size()>0) System.out.println(list.removeFirst()); } } sc.close(); } } class pair { private final int x; private final int y; private final int z; public pair(int x,int y,int z) { this.x=x; this.y=y; this.z=z; } @Override public String toString() { return x+" "+y+" "+z; } }
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 8
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 &lt; y &lt; 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
ada360198dcec6bb84d39aa238f02a0a
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 &lt; y &lt; 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 Data implements Comparable<Data>{ int ind, val; public Data(int a, int b) { ind = a; val = b; } public int compareTo(Data o) { if(val == o.val) return Integer.compare(ind, o.ind); return Integer.compare(val, o.val); } } static class ops{ int a, b, c; public ops(int d, int e, int f) { a = d; b = e; c = f; } public String toString() { return a + " " + b + ' ' + c; } } static class TreeMultiSet<T> { private final TreeMap<T,Integer> map; private int size; public TreeMultiSet(){map=new TreeMap<>(); size=0;} public TreeMultiSet(boolean reverse) { if(reverse) map=new TreeMap<>(Collections.reverseOrder()); else map=new TreeMap<>(); size=0; } public void clear(){map.clear(); size=0;} public int size(){return size;} public int setSize(){return map.size();} public boolean contains(T a){return map.containsKey(a);} public boolean isEmpty(){return size==0;} public Integer get(T a){return map.getOrDefault(a,0);} public void add(T a, int count) { int cur=get(a);map.put(a,cur+count); size+=count; if(cur+count==0) map.remove(a); } public void addOne(T a){add(a,1);} public void remove(T a, int count){add(a,Math.max(-get(a),-count));} public void removeOne(T a){remove(a,1);} public void removeAll(T a){remove(a,Integer.MAX_VALUE-10);} public T ceiling(T a){return map.ceilingKey(a);} public T floor(T a){return map.floorKey(a);} public T first(){return map.firstKey();} public T last(){return map.lastKey();} public T higher(T a){return map.higherKey(a);} public T lower(T a){return map.lowerKey(a);} public T pollFirst(){T a=first(); removeOne(a); return a;} public T pollLast(){T a=last(); removeOne(a); return a;} } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); Here: while(T-- > 0) { int N = Integer.parseInt(br.readLine()); int[] nums = new int [N]; StringTokenizer st = new StringTokenizer(br.readLine()); for(int i = 0;i < N;i++) { nums[i] = Integer.parseInt(st.nextToken()); } Queue<ops> ops = new LinkedList<>(); if(nums[N-1] >= 0) { TreeMultiSet<Data> ms = new TreeMultiSet<>(); for(int i = 1;i < N-1;i++) { ms.addOne(new Data(i, nums[i])); } for(int i = 0;i < N-2;i++) { nums[i] = ms.first().val - nums[N-1]; ops.add(new ops(i+1, ms.first().ind + 1, N)); ms.removeOne(new Data(i+1, nums[i+1])); } } else { int[] numsCopy = Arrays.copyOf(nums, N); Arrays.sort(numsCopy); for(int i = 0;i < N;i++) { if(nums[i] != numsCopy[i]) { System.out.println(-1); continue Here; } } } int[] numsCopy = Arrays.copyOf(nums, N); Arrays.sort(numsCopy); for(int i = 0;i < N;i++) { if(nums[i] != numsCopy[i]) { System.out.println(-1); continue Here; } } System.out.println(ops.size()); while(!ops.isEmpty()) { System.out.println(ops.remove()); } } } }
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 8
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 &lt; y &lt; 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
fdf91bf367f96322e9a779d97237bb8d
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 &lt; y &lt; 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 Data implements Comparable<Data>{ int ind, val; public Data(int a, int b) { ind = a; val = b; } public int compareTo(Data o) { if(val == o.val) return Integer.compare(ind, o.ind); return Integer.compare(val, o.val); } public String toString() { return ind + " " + val; } } static class ops{ int a, b, c; public ops(int d, int e, int f) { a = d; b = e; c = f; } public String toString() { return a + " " + b + ' ' + c; } } static class TreeMultiSet<T> { private final TreeMap<T,Integer> map; private int size; public TreeMultiSet(){map=new TreeMap<>(); size=0;} public TreeMultiSet(boolean reverse) { if(reverse) map=new TreeMap<>(Collections.reverseOrder()); else map=new TreeMap<>(); size=0; } public void clear(){map.clear(); size=0;} public int size(){return size;} public int setSize(){return map.size();} public boolean contains(T a){return map.containsKey(a);} public boolean isEmpty(){return size==0;} public Integer get(T a){return map.getOrDefault(a,0);} public void add(T a, int count) { int cur=get(a);map.put(a,cur+count); size+=count; if(cur+count==0) map.remove(a); } public void addOne(T a){add(a,1);} public void remove(T a, int count){add(a,Math.max(-get(a),-count));} public void removeOne(T a){remove(a,1);} public void removeAll(T a){remove(a,Integer.MAX_VALUE-10);} public T ceiling(T a){return map.ceilingKey(a);} public T floor(T a){return map.floorKey(a);} public T first(){return map.firstKey();} public T last(){return map.lastKey();} public T higher(T a){return map.higherKey(a);} public T lower(T a){return map.lowerKey(a);} public T pollFirst(){T a=first(); removeOne(a); return a;} public T pollLast(){T a=last(); removeOne(a); return a;} } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); Here: while(T-- > 0) { int N = Integer.parseInt(br.readLine()); int[] nums = new int [N]; StringTokenizer st = new StringTokenizer(br.readLine()); for(int i = 0;i < N;i++) { nums[i] = Integer.parseInt(st.nextToken()); } Data[] min = new Data[N]; min[N-1] = new Data(N-1, nums[N-1]); for(int i = N-2;i >= 0;i--) { if(min[i+1].val < nums[i]) { min[i] = min[i+1]; } else { min[i] = new Data(i, nums[i]); } } // System.out.println(Arrays.toString(min)); Queue<ops> ops = new LinkedList<>(); if(nums[N-1] >= 0) { // TreeMultiSet<Data> ms = new TreeMultiSet<>(); // for(int i = 1;i < N-1;i++) { // ms.addOne(new Data(i, nums[i])); // } for(int i = 0;i < N-2;i++) { nums[i] = min[i+1].val - nums[N-1]; ops.add(new ops(i+1, min[i+1].ind + 1, N)); // ms.removeOne(new Data(i+1, nums[i+1])); } } else { int[] numsCopy = Arrays.copyOf(nums, N); Arrays.sort(numsCopy); for(int i = 0;i < N;i++) { if(nums[i] != numsCopy[i]) { System.out.println(-1); continue Here; } } } int[] numsCopy = Arrays.copyOf(nums, N); Arrays.sort(numsCopy); for(int i = 0;i < N;i++) { if(nums[i] != numsCopy[i]) { System.out.println(-1); continue Here; } } System.out.println(ops.size()); while(!ops.isEmpty()) { System.out.println(ops.remove()); } } } }
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 8
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 &lt; y &lt; 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
783d29ffd13e444704081e4b19dd4634
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 &lt; y &lt; 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 javax.print.attribute.IntegerSyntax; import java.net.CookieHandler; import java.util.*; import java.io.*; //import static com.sun.tools.javac.jvm.ByteCodes.swap; // ?)(? public class fastTemp { static FastScanner fs = null; static ArrayList<Long> graph[] ; static int mod = 998244353; static int bit[]; static int arr[]; static int hhh; static int hmm; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); outer: while (t-- > 0) { int n = fs.nextInt(); long a[] = fs.readlongArray(n); boolean f = true; for(int i=1;i<n;i++){ if(a[i]>=a[i-1]){ continue ; }else{ f = false; break; } } if(f){ out.println("0"); }else if( (a[n-1]<a[n-2]) || (a[n-1]<0 && a[n-2]<0)){ out.println("-1"); }else{ out.println(n - 2); for (int i = n - 3; i >= 0; i--) { out.println((i + 1) + " " + (n-1) + " " + (n)); } } } out.close(); } static class Pair1 { String s; int x; int y; Pair1(String s, int x, int y) { this.s = s; this.x = x; this.y = y; } } 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) { return this.x - o.x; } } static void factorial(int n) { int res[] = new int[500]; // Initialize result res[0] = 1; int res_size = 1; // Apply simple factorial formula // n! = 1 * 2 * 3 * 4...*n for (int x = 2; x <= n; x++) res_size = multiply(x, res, res_size); } static int multiply(int x, int res[], int res_size) { int carry = 0; // Initialize carry // One by one multiply n with individual // digits of res[] for (int i = 0; i < res_size; i++) { int prod = res[i] * x + carry; res[i] = prod % 10; // Store last digit of // 'prod' in res[] carry = prod / 10; // Put rest in carry } // Put carry in res and increase result size while (carry != 0) { res[res_size] = carry % 10; carry = carry / 10; res_size++; } return res_size; } static long power(long x, long y, long p) { if (y == 0) return 1; if (x == 0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if (y == 0) return 1; if (x == 0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } static long fact[] = new long[200001]; public static void fact() { fact[1] = 1; fact[0] = 1; for (int i = 2; i <= 200000; i++) { fact[i] = (((fact[i - 1]) % mod) * ((i) % mod)) % mod; } } static long lower(long array[], long key, int i, int k) { long ans = -1; while (i <= k) { int mid = (i + k) / 2; if (array[mid] >= key) { ans = mid; k = mid - 1; } else { i = mid + 1; } } return ans; } static long upper(long array[], long key, int i, int k) { long ans = -1; while (i <= k) { int mid = (i + k) / 2; if (array[mid] <= key) { ans = array[mid]; i = mid + 1; } else { k = mid - 1; } } return ans; } public static class String1 implements Comparable<String1> { String str; int id; String1(String str, int id) { this.str = str; this.id = id; } public int compareTo(String1 o) { int i = 0; while (i < str.length() && str.charAt(i) == o.str.charAt(i)) { i++; } if (i < str.length()) { if (i % 2 == 1) { return o.str.compareTo(str); // descending order } else { return str.compareTo(o.str); // ascending order } } return str.compareTo(o.str); } } 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); } 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; } long nextLong() { return Long.parseLong(next()); } } static void swap(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static boolean prime[]; static void sieveOfEratosthenes(int n) { prime = new boolean[n + 1]; for (int i = 0; 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; } } } public static int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } public static Pair Euclid(int a, int b) { if (b == 0) { return new Pair(1, 0); // answer of x and y } Pair dash = Euclid(b, a % b); return new Pair(dash.y, dash.x - (a / b) * dash.y); } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static long nCk(int n, int k) { long res = 1; for (int i = n - k + 1; i <= n; ++i) res *= i; for (int i = 2; i <= k; ++i) res /= i; 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 8
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 &lt; y &lt; 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
593bfc371b9a6fa467d30d69b752c859
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 &lt; y &lt; 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 javax.print.attribute.IntegerSyntax; import java.net.CookieHandler; import java.util.*; import java.io.*; //import static com.sun.tools.javac.jvm.ByteCodes.swap; // ?)(? public class fastTemp { static FastScanner fs = null; static ArrayList<Long> graph[] ; static int mod = 998244353; static int bit[]; static int arr[]; static int hhh; static int hmm; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); outer: while (t-- > 0) { int n = fs.nextInt(); long a[] = fs.readlongArray(n); boolean f = true; for(int i=1;i<n;i++){ if(a[i]>=a[i-1]){ continue ; }else{ f = false; break; } } if(f){ out.println("0"); }else{ if(a[n-1]<a[n-2] || (a[n-1]<0 && a[n-2]<0)){ out.println(-1); }else{ out.println(n-2); for(int i=n-2;i>=1;i--){ out.println((i)+" "+(n-1)+" "+(n)); } } } } out.close(); } static class Pair1 { String s; int x; int y; Pair1(String s, int x, int y) { this.s = s; this.x = x; this.y = y; } } 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) { return this.x - o.x; } } static void factorial(int n) { int res[] = new int[500]; // Initialize result res[0] = 1; int res_size = 1; // Apply simple factorial formula // n! = 1 * 2 * 3 * 4...*n for (int x = 2; x <= n; x++) res_size = multiply(x, res, res_size); } static int multiply(int x, int res[], int res_size) { int carry = 0; // Initialize carry // One by one multiply n with individual // digits of res[] for (int i = 0; i < res_size; i++) { int prod = res[i] * x + carry; res[i] = prod % 10; // Store last digit of // 'prod' in res[] carry = prod / 10; // Put rest in carry } // Put carry in res and increase result size while (carry != 0) { res[res_size] = carry % 10; carry = carry / 10; res_size++; } return res_size; } static long power(long x, long y, long p) { if (y == 0) return 1; if (x == 0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if (y == 0) return 1; if (x == 0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } static long fact[] = new long[200001]; public static void fact() { fact[1] = 1; fact[0] = 1; for (int i = 2; i <= 200000; i++) { fact[i] = (((fact[i - 1]) % mod) * ((i) % mod)) % mod; } } static long lower(long array[], long key, int i, int k) { long ans = -1; while (i <= k) { int mid = (i + k) / 2; if (array[mid] >= key) { ans = mid; k = mid - 1; } else { i = mid + 1; } } return ans; } static long upper(long array[], long key, int i, int k) { long ans = -1; while (i <= k) { int mid = (i + k) / 2; if (array[mid] <= key) { ans = array[mid]; i = mid + 1; } else { k = mid - 1; } } return ans; } public static class String1 implements Comparable<String1> { String str; int id; String1(String str, int id) { this.str = str; this.id = id; } public int compareTo(String1 o) { int i = 0; while (i < str.length() && str.charAt(i) == o.str.charAt(i)) { i++; } if (i < str.length()) { if (i % 2 == 1) { return o.str.compareTo(str); // descending order } else { return str.compareTo(o.str); // ascending order } } return str.compareTo(o.str); } } 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); } 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; } long nextLong() { return Long.parseLong(next()); } } static void swap(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static boolean prime[]; static void sieveOfEratosthenes(int n) { prime = new boolean[n + 1]; for (int i = 0; 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; } } } public static int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } public static Pair Euclid(int a, int b) { if (b == 0) { return new Pair(1, 0); // answer of x and y } Pair dash = Euclid(b, a % b); return new Pair(dash.y, dash.x - (a / b) * dash.y); } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static long nCk(int n, int k) { long res = 1; for (int i = n - k + 1; i <= n; ++i) res *= i; for (int i = 2; i <= k; ++i) res /= i; 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 8
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 &lt; y &lt; 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
0910ac78f703938e15e006201fa38700
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 &lt; y &lt; 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 Hiking { static Scanner sc; static PrintWriter pw; public static void main(String[] args) throws IOException, InterruptedException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long []arr=sc.nextlongArray(n); if(arr[n-2]>arr[n-1]) { pw.println(-1); continue; } int c=0; StringBuilder ans=new StringBuilder(); int c1=n-1,c2=n-2; int minidx=n-1; for(int i=n-3;i>=0;i--) { if(arr[i]>arr[i+1]) { c++; arr[i]=arr[i+1]-arr[minidx]; ans.append((i+1)+" "+(i+2)+" "+(minidx+1)+'\n'); } if(arr[minidx]>arr[i+1] && arr[i+1]>=0) { minidx=i+1; } } // pw.println(Arrays.toString(arr)); boolean f=true; for(int i=1;i<n;i++) { f&=arr[i]>=arr[i-1]; } if(!f) { pw.println(-1); continue; } pw.println(c); pw.print(ans); } pw.flush(); } static class FenwickTree { // one-based DS int n; long[] ft; FenwickTree(int size) { n = size; ft = new long[n + 1]; } long rsq(int b) // O(log n) { b++; long sum = 0; while (b > 0) { sum += ft[b]; b -= b & -b; } // min? return sum; } long rsq(int a, int b) { b = Math.min(b, n - 1); return rsq(b) - rsq(a - 1); } void point_update(int k, long val) // O(log n), update = increment { k++; while (k <= n) { ft[k] += val; k += k & -k; } } } static class pair { // long x,y; int x, y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
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 8
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 &lt; y &lt; 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
349095f9fac193e52e1fd71e345d55b6
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 &lt; y &lt; 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 Hiking { static Scanner sc; static PrintWriter pw; public static void main(String[] args) throws IOException, InterruptedException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long []arr=sc.nextlongArray(n); boolean f=true; for(int i=1;i<n;i++) { f&=arr[i]>=arr[i-1]; } if(f) { pw.println(0); continue; } if(arr[n-2]>arr[n-1] || arr[n-1]<0) { pw.println(-1); continue; } int c=0; StringBuilder ans=new StringBuilder(); int c1=n-1,c2=n-2; int minidx=n-1; for(int i=n-3;i>=0;i--) { if(arr[i]>arr[i+1]) { c++; arr[i]=arr[i+1]-arr[minidx]; ans.append((i+1)+" "+(i+2)+" "+(minidx+1)+'\n'); } if(arr[minidx]>arr[i+1] && arr[i+1]>=0) { minidx=i+1; } } pw.println(c); pw.print(ans); } pw.flush(); } static class FenwickTree { // one-based DS int n; long[] ft; FenwickTree(int size) { n = size; ft = new long[n + 1]; } long rsq(int b) // O(log n) { b++; long sum = 0; while (b > 0) { sum += ft[b]; b -= b & -b; } // min? return sum; } long rsq(int a, int b) { b = Math.min(b, n - 1); return rsq(b) - rsq(a - 1); } void point_update(int k, long val) // O(log n), update = increment { k++; while (k <= n) { ft[k] += val; k += k & -k; } } } static class pair { // long x,y; int x, y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
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 8
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 &lt; y &lt; 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
08daa74bbeb496db6b42a669cfb99e04
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 &lt; y &lt; 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 Hiking { static Scanner sc; static PrintWriter pw; public static void main(String[] args) throws IOException, InterruptedException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int []arr=sc.nextIntArray(n); boolean f=true; for(int i=1;i<n;i++) f&=arr[i]>=arr[i-1]; if(f) { pw.println(0); continue; } if(arr[n-2]>arr[n-1] || arr[n-1]<0) { pw.println(-1); continue; } int c=0; StringBuilder ans=new StringBuilder(); int minidx=n-1; for(int i=n-3;i>=0;i--) { c++; arr[i]=arr[i+1]-arr[minidx]; ans.append((i+1)+" "+(i+2)+" "+(minidx+1)+'\n'); } // pw.println(Arrays.toString(arr)); pw.println(c); pw.print(ans); } pw.flush(); } static class FenwickTree { // one-based DS int n; long[] ft; FenwickTree(int size) { n = size; ft = new long[n + 1]; } long rsq(int b) // O(log n) { b++; long sum = 0; while (b > 0) { sum += ft[b]; b -= b & -b; } // min? return sum; } long rsq(int a, int b) { b = Math.min(b, n - 1); return rsq(b) - rsq(a - 1); } void point_update(int k, long val) // O(log n), update = increment { k++; while (k <= n) { ft[k] += val; k += k & -k; } } } static class pair { // long x,y; int x, y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
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 8
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 &lt; y &lt; 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
846f54eee9ea75c04134cdc9104f682f
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 &lt; y &lt; 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 Hiking { static Scanner sc; static PrintWriter pw; public static void main(String[] args) throws IOException, InterruptedException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] arr = sc.nextIntArray(n); boolean f=true; for(int i=1;i<n;i++) f&=arr[i]>=arr[i-1]; if(f) { pw.println(0); continue; } else if (arr[n - 2] > arr[n - 1] || arr[n-1]<0) { pw.println(-1); continue; } pw.println(n-2); for(int i=1;i<=n-2;i++) pw.println(i+" "+(n-1)+" "+n); } pw.flush(); } static class pair { // long x,y; int x, y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
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 8
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 &lt; y &lt; 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
f2b038fe8030db54bb3502f0f5622959
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 &lt; y &lt; 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 void main(String[] args) throws Exception { int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); long arr[] = sc.nextlongArray(n); long maxSum = 0; boolean f = true; int y = 0 , z=0; y= n-2; z=n-1; if(arr[y]>arr[z]) f=false; maxSum=arr[y]-arr[z]; int operations=0; StringBuilder ans = new StringBuilder(); boolean isSorted = true; for(int i=1;i<n;i++) { if(arr[i]<arr[i-1]) { isSorted = false; break; } } for(int i=0;i<n-2;i++) { operations++; ans.append((i+1)+" "+(y+1)+" "+(z+1)+"\n"); } if(isSorted) pw.println(0); else { if(!f || arr[n-1]<0) pw.println(-1); else { pw.println(operations); pw.print(ans); } } } pw.flush(); } public static void sort(int[] in) { shuffle(in); Arrays.sort(in); } public static void shuffle(int[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); int tmp = in[i]; in[i] = in[idx]; in[idx] = tmp; } } static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { if (x != o.x) return x - o.x; else return y - o.y; } public String toString() { return x + " " + y; } } static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } public static void display(char[][] a) { for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j]); } 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 8
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 &lt; y &lt; 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
b31c44d473cc0751309632acd9990fe3
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 &lt; y &lt; 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 final class C { public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int T = fs.nextInt(); for(int tt =0; tt < T; ++tt){ int n = fs.nextInt(); long[] a = new long[n]; for(int i = 0; i <n; ++i) a[i] = fs.nextLong(); long max = a[n - 1]; long min = 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(min - max <= min && min <= max){ System.out.println(n - 2); for(int i = 1; i <= n - 2; ++i){ System.out.println(i + " " + (n - 1) + " " + n); } continue; } System.out.println(-1); } } static final Random random = new Random(); static final int mod = 1_000_000_007; static long add(long a, long b) { return (a + b) % mod; } static long sub(long a, long b) { return ((a - b) % mod + mod) % mod; } 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 long mul(long a, long b) { return (a * b) % mod; } static long exp(long base, long exp) { if (exp == 0) return 1; long half = exp(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials = new long[2_000_001]; static long[] invFactorials = new long[2_000_001]; static void precompFacts() { factorials[0] = invFactorials[0] = 1; for (int i = 1; i < factorials.length; i++) factorials[i] = mul(factorials[i - 1], i); invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2); for (int i = invFactorials.length - 2; i >= 0; i--) invFactorials[i] = mul(invFactorials[i + 1], i + 1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k])); } 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 8
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 &lt; y &lt; 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
86ec76f627396f8149979e7cd33be8dc
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 &lt; y &lt; 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 scan = new Scanner(System.in); int n = scan.nextInt(); for (int i = 0; i < n; i++) { int m = scan.nextInt(); int[] arr = new int[m]; for (int j = 0; j < m; j++) { arr[j] = scan.nextInt(); } printOperation(arr); } } public static void printOperation(int[] arr) { int N = arr.length; 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 { for (int i = 1; i < N; i++) { if (arr[i - 1] > arr[i]) { System.out.println(-1); return; } } 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 8
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 &lt; y &lt; 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
dc7faf115a2b41853a02abae64f1965a
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 &lt; y &lt; 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.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); for (int i = 0; i < n; i++) { int m = scan.nextInt(); int[] arr = new int[m]; for (int j = 0; j < m; j++) { arr[j] = scan.nextInt(); } printOperation(arr); } } public static void printOperation(int[] arr) { List<List<Integer>> ans = new ArrayList<>(); int operation = 0; int N = arr.length; // 是否已经排好序 boolean sorted = true; for (int i = 0; i < N - 1; i++) { if (arr[i] > arr[i + 1]) { sorted = false; } } if (sorted) { System.out.println(0); return; } if (arr[N - 2] > arr[N - 1] || arr[N - 1] < 0) { System.out.println(-1); return; } // 每次都操作,把arr[i] 改为 arr[i+1] - arr[N-1] // 只有最后2个数不用排 System.out.println(N - 2); for (int i = N - 3; i >= 0; i--) { 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 8
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 &lt; y &lt; 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
d16d72884d1a3406330f090f5fa87a5b
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 &lt; y &lt; 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.ArrayList; import java.util.List; import java.util.Scanner; public class C1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int i = 0; i < t; i++){ int n = in.nextInt(); long[] arr = new long[n]; for(int j = 0; j < n; j++){ arr[j] = in.nextLong(); } long minNum = arr[n-1]; int minId = n-1; int newMinId = n-1; List<int[]> opts = new ArrayList<>(); boolean failed = false; for(int j = n-2; j >= 0; j--){ if(arr[j] <= arr[j+1]){ if(arr[j] >= 0) { minNum = arr[j]; minId = j; } continue; } if(j == n-2 || minNum < 0) { System.out.println(-1); failed = true; break; } if(j+1 == minId) { newMinId = j+2; } else { newMinId = minId; } if(Math.abs(arr[j+1]-arr[newMinId]) >= (long)(1e18)){ System.out.println(-1); failed = true; break; } arr[j] = arr[j+1]-arr[newMinId]; opts.add(new int[]{j, j+1, newMinId}); if(arr[j] >= 0) { minNum = arr[j]; minId = j; } } if(!failed){ System.out.println(opts.size()); StringBuilder sb = new StringBuilder(); for(int j = 0; j < opts.size(); j++){ for(int id : opts.get(j)){ sb.append(id+1); sb.append(' '); } sb.append('\n'); } System.out.println(sb.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 8
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 &lt; y &lt; 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
5b84b262a2b6da6182bf18c49703c287
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 &lt; y &lt; 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 C { 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 main(String[] args) { // FastReader in = new FastReader(); Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int i = 0; i < t; i++){ int n = in.nextInt(); int[] arr = new int[n]; boolean inc = true; int mx = Integer.MIN_VALUE; for(int j = 0; j < n; j++){ arr[j] = in.nextInt(); if(inc){ if(arr[j] < mx) inc = false; else mx = arr[j]; } } if(arr[n-2] > arr[n-1]) { System.out.println(-1); continue; } if(arr[n-1] < 0) { if(inc){ System.out.println(0); continue; } else { System.out.println(-1); continue; } } System.out.println(n-2); StringBuilder sb = new StringBuilder(); for(int j = 0; j < n-2; j++){ sb.append(j+1); sb.append(' '); sb.append(n-1); sb.append(' '); sb.append(n); sb.append('\n'); } System.out.print(sb.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 8
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 &lt; y &lt; 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
c4c3bc593cc76a07f46bf6b1e8311c6e
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 &lt; y &lt; 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 C { 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 main(String[] args) { FastReader in = new FastReader(); int t = in.nextInt(); for(int i = 0; i < t; i++){ int n = in.nextInt(); int[] arr = new int[n]; boolean inc = true; int mx = Integer.MIN_VALUE; for(int j = 0; j < n; j++){ arr[j] = in.nextInt(); if(inc){ if(arr[j] < mx) inc = false; else mx = arr[j]; } } if(arr[n-2] > arr[n-1]) { System.out.println(-1); continue; } if(arr[n-1] < 0) { if(inc){ System.out.println(0); continue; } else { System.out.println(-1); continue; } } System.out.println(n-2); StringBuilder sb = new StringBuilder(); for(int j = 0; j < n-2; j++){ sb.append(j+1); sb.append(' '); sb.append(n-1); sb.append(' '); sb.append(n); sb.append('\n'); } System.out.print(sb.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 8
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 &lt; y &lt; 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
b85fb8b4530659e6b3b27fb2d92367ed
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 &lt; y &lt; 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.math.BigDecimal; import java.math.*; public class Main{ public static void main(String[] args) { TaskA solver = new TaskA(); // boolean[]prime=seive(3*100001); int t = in.nextInt(); for (int i = 1; i <= t ; i++) { solver.solve(1, in, out); } // solver.solve(1, in, out); out.flush(); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n= in.nextInt(); int[]arr=new int[n]; for(int i=0;i<n;i++) { arr[i]= in.nextInt(); } if((arr[n-1]<arr[n-2])) { println(-1);return; } else if(arr[n-1]>=0) { println(n-2); for(int i=0;i<n-2;i++) { println((i+1)+" "+(n-1)+" "+n); } } else { for(int i=1;i<n;i++) { if(arr[i-1]>arr[i]) { println(-1);return; } } println(0); } } } static long ceil(long a,long b) { return (a/b + Math.min(a%b, 1)); } static char[] rev(char[]ans,int n) { for(int i=ans.length-1;i>=n;i--) { ans[i]=ans[ans.length-i-1]; } return ans; } static int countStep(int[]arr,long sum,int k,int count1) { int count=count1; int index=arr.length-1; while(sum>k&&index>0) { sum-=(arr[index]-arr[0]); count++; if(sum<=k) { break; } index--; // println("c"); } if(sum<=k) { return count; } else { return Integer.MAX_VALUE; } } static long pow(long b, long e) { long ans = 1; while (e > 0) { if (e % 2 == 1) ans = ans * b % mod; e >>= 1; b = b * b % mod; } return ans; } static void sortDiff(Pair arr[]) { // 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.first==p2.first) { return (p1.second-p1.first)-(p2.second-p1.first); } return (p1.second-p1.first)-(p2.second-p2.first); } }); } static void sortF(Pair arr[]) { // 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.first==p2.first) { return p1.second-p2.second; } return p1.first - p2.first; } }); } static int[] shift (int[]inp,int i,int j){ int[]arr=new int[j-i+1]; int n=j-i+1; for(int k=i;k<=j;k++) { arr[(k-i+1)%n]=inp[k]; } for(int k=0;k<n;k++ ) { inp[k+i]=arr[k]; } return inp; } static long[] fac; static long mod = (long) 1000000007; static void initFac(long n) { fac = new long[(int)n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = (fac[i - 1] * i) % mod; } } static long nck(int n, int k) { if (n < k) return 0; long den = inv((int) (fac[k] * fac[n - k] % mod)); return fac[n] * den % mod; } static long inv(long x) { return pow(x, mod - 2); } static void sort(int[] a) { ArrayList<Integer> q = new ArrayList<>(); for (int i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static void sort(long[] a) { ArrayList<Long> q = new ArrayList<>(); for (long i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } public static int bfsSize(int source,LinkedList<Integer>[]a,boolean[]visited) { Queue<Integer>q=new LinkedList<>(); q.add(source); visited[source]=true; int distance=0; while(!q.isEmpty()) { int curr=q.poll(); distance++; for(int neighbour:a[curr]) { if(!visited[neighbour]) { visited[neighbour]=true; q.add(neighbour); } } } return distance; } public static Set<Integer>factors(int n){ Set<Integer>ans=new HashSet<>(); ans.add(1); for(int i=2;i*i<=n;i++) { if(n%i==0) { ans.add(i); ans.add(n/i); } } return ans; } public static int bfsSp(int source,int destination,ArrayList<Integer>[]a) { boolean[]visited=new boolean[a.length]; int[]parent=new int[a.length]; Queue<Integer>q=new LinkedList<>(); int distance=0; q.add(source); visited[source]=true; parent[source]=-1; while(!q.isEmpty()) { int curr=q.poll(); if(curr==destination) { break; } for(int neighbour:a[curr]) { if(neighbour!=-1&&!visited[neighbour]) { visited[neighbour]=true; q.add(neighbour); parent[neighbour]=curr; } } } int cur=destination; while(parent[cur]!=-1) { distance++; cur=parent[cur]; } return distance; } static int bs(int size,int[]arr) { int x = -1; for (int b = size/2; b >= 1; b /= 2) { while (!ok(arr)); } int k = x+1; return k; } static boolean ok(int[]x) { return false; } public static int solve1(ArrayList<Integer> A) { long[]arr =new long[A.size()+1]; int n=A.size(); for(int i=1;i<=A.size();i++) { arr[i]=((i%2)*((n-i+1)%2))%2; arr[i]%=2; } int ans=0; for(int i=0;i<A.size();i++) { if(arr[i+1]==1) { ans^=A.get(i); } } return ans; } public static String printBinary(long a) { String str=""; for(int i=31;i>=0;i--) { if((a&(1<<i))!=0) { str+=1; } if((a&(1<<i))==0 && !str.isEmpty()) { str+=0; } } return str; } public static String reverse(long a) { long rev=0; String str=""; int x=(int)(Math.log(a)/Math.log(2))+1; for(int i=0;i<32;i++) { rev<<=1; if((a&(1<<i))!=0) { rev|=1; str+=1; } else { str+=0; } } return str; } //////////////////////////////////////////////////////// static void sortS(Pair arr[]) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return p1.second - p2.second; } }); } static class Pair implements Comparable<Pair> { int first ;int second ; public Pair(int x, int y) { this.first = x ;this.second = y ; } @Override public boolean equals(Object obj) { if(obj == this)return true ; if(obj == null)return false ; if(this.getClass() != obj.getClass()) return false ; Pair other = (Pair)(obj) ; if(this.first != other.first)return false ; if(this.second != other.second)return false ; return true ; } @Override public int hashCode() { return this.first^this.second ; } @Override public String toString() { String ans = "" ;ans += this.first ; ans += " "; ans += this.second ; return ans ; } @Override public int compareTo(Main.Pair o) { { if(this.first==o.first) { return this.second-o.second; } return this.first - o.first; } } } ////////////////////////////////////////////////////////////////////////// static int nD(long num) { String s=String.valueOf(num); return s.length(); } static int CommonDigits(int x,int y) { String s=String.valueOf(x); String s2=String.valueOf(y); return 0; } static int lcs(String str1, String str2, int m, int n) { int L[][] = new int[m + 1][n + 1]; int i, j; // Following steps build L[m+1][n+1] in // bottom up fashion. Note that L[i][j] // contains length of LCS of str1[0..i-1] // and str2[0..j-1] for (i = 0; i <= m; i++) { for (j = 0; j <= n; j++) { if (i == 0 || j == 0) L[i][j] = 0; else if (str1.charAt(i - 1) == str2.charAt(j - 1)) L[i][j] = L[i - 1][j - 1] + 1; else L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]); } } // L[m][n] contains length of LCS // for X[0..n-1] and Y[0..m-1] return L[m][n]; } ///////////////////////////////// boolean IsPowerOfTwo(int x) { return (x != 0) && ((x & (x - 1)) == 0); } //////////////////////////////// static long power(long a,long b,long m ) { long ans=1; while(b>0) { if(b%2==1) { ans=((ans%m)*(a%m))%m; b--; } else { a=(a*a)%m;b/=2; } } return ans%m; } /////////////////////////////// public static boolean repeatedSubString(String string) { return ((string + string).indexOf(string, 1) != string.length()); } static int search(char[]c,int start,int end,char x) { for(int i=start;i<end;i++) { if(c[i]==x) {return i;} } return -2; } //////////////////////////////// static long gcd(long a, long b) { while (b != 0) { long t = b; b = a % b; a = t; } return a; } static long fac(long a) { if(a== 0L || a==1L)return 1L ; return a*fac(a-1L) ; } static ArrayList al() { ArrayList<Integer>a =new ArrayList<>(); return a; } static HashSet h() { return new HashSet<Integer>(); } static void debug(int[][]a) { int n= a.length; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { out.print(a[i][j]+" "); } out.print("\n"); } } static void debug(int[]a) { out.println(Arrays.toString(a)); } static void debug(ArrayList<Integer>a) { out.println(a.toString()); } static boolean[]seive(int n){ boolean[]b=new boolean[n+1]; for (int i = 2; i <= n; i++) b[i] = true; for(int i=2;i*i<=n;i++) { if(b[i]) { for(int j=i*i;j<=n;j+=i) { b[j]=false; } } } return b; } static int longestIncreasingSubseq(int[]arr) { int[]sizes=new int[arr.length]; Arrays.fill(sizes, 1); int max=1; for(int i=1;i<arr.length;i++) { for(int j=0;j<i;j++) { if(arr[j]<arr[i]) { sizes[i]=Math.max(sizes[i],sizes[j]+1); max=Math.max(max, sizes[i]); } } } return max; } public static ArrayList primeFactors(long n) { ArrayList<Long>h= new ArrayList<>(); // Print the number of 2s that divide n if(n%2 ==0) {h.add(2L);} // n must be odd at this point. So we can // skip one element (Note i = i +2) for (long i = 3; i <= Math.sqrt(n); i+= 2) { if(n%i==0) {h.add(i);} } if (n > 2) h.add(n); return h; } static boolean Divisors(long n){ if(n%2==1) { return true; } for (long i=2; i<=Math.sqrt(n); i++){ if (n%i==0 && i%2==1){ return true; } } return false; } static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(outputStream); public static void superSet(int[]a,ArrayList<String>al,int i,String s) { if(i==a.length) { al.add(s); return; } superSet(a,al,i+1,s); superSet(a,al,i+1,s+a[i]+" "); } public static long[] makeArr() { long size=in.nextInt(); long []arr=new long[(int)size]; for(int i=0;i<size;i++) { arr[i]=in.nextInt(); } return arr; } public static long[] arr(int n) { long []arr=new long[n+1]; for(int i=1;i<n+1;i++) { arr[i]=in.nextLong(); } return arr; } public static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } static void println(long x) { out.println(x); } static void print(long c) { out.print(c); } static void print(int c) { out.print(c); } static void println(int x) { out.println(x); } static void print(String s) { out.print(s); } static void println(String s) { out.println(s); } public static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } public static void reverse(int[] array) { int n = array.length; for (int i = 0; i < n / 2; i++) { int temp = array[i]; array[i] = array[n - i - 1]; array[n - i - 1] = temp; } } public static long nCr(int n,int k) { long ans=1L; k=k>n-k?n-k:k; for( int j=1;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } static int searchMax(int index,long[]inp) { while(index+1<inp.length &&inp[index+1]>inp[index]) { index+=1; } return index; } static int searchMin(int index,long[]inp) { while(index+1<inp.length &&inp[index+1]<inp[index]) { index+=1; } return index; } public class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } static class Pairr implements Comparable<Pairr>{ private int index; private int cumsum; private ArrayList<Integer>indices; public Pairr(int index,int cumsum) { this.index=index; this.cumsum=cumsum; indices=new ArrayList<Integer>(); } public int compareTo(Pairr other) { return Integer.compare(cumsum, other.cumsum); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } 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 8
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 &lt; y &lt; 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
6ef0db85dab127c74a8c7ac2f8c7151f
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 &lt; y &lt; 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 codeforces.round772div2; import java.io.*; import java.util.*; import static java.lang.Math.*; public class C { static InputReader in; static PrintWriter out; public static void main(String[] args) { //initReaderPrinter(true); initReaderPrinter(false); solve(in.nextInt()); //solve(1); } /* General tips 1. It is ok to fail, but it is not ok to fail for the same mistakes over and over! 2. Train smarter, not harder! 3. If you find an answer and want to return immediately, don't forget to flush before return! */ /* Read before practice 1. Set a timer based on a problem's difficulty level: 45 minutes at your current target practice level; 2. During a problem solving session, focus! Do not switch problems or even worse switch to do something else; 3. If fail to solve within timer limit, read editorials to get as little help as possible to get yourself unblocked; 4. If after reading the entire editorial and other people's code but still can not solve, move this problem to to-do list and re-try in the future. 5. Keep a practice log about new thinking approaches, good tricks, bugs; Review regularly; 6. Also try this new approach suggested by um_nik: Solve with no intention to read editorial. If getting stuck, skip it and solve other similar level problems. Wait for 1 week then try to solve again. Only read editorial after you solved a problem. 7. Remember to also submit in the original problem link (if using gym) so that the 1 v 1 bot knows which problems I have solved already. 8. Form the habit of writing down an implementable solution idea before coding! You've taken enough hits during contests because you rushed to coding! */ /* Read before contests and lockout 1 v 1 Mistakes you've made in the past contests: 1. Tried to solve without going through given test examples -> wasting time on solving a different problem than asked; 2. Rushed to coding without getting a comprehensive sketch of your solution -> implementation bugs and WA; Write down your idea step by step, no need to rush. It is always better to have all the steps considered before hand! Think about all the past contests that you have failed because slow implementation and implementation bugs! This will be greatly reduced if you take your time to get a thorough idea steps! 3. Forgot about possible integer overflow; When stuck: 1. Understand problem statements? Walked through test examples? 2. Take a step back and think about other approaches? 3. Check rank board to see if you can skip to work on a possibly easier problem? 4. If none of the above works, take a guess? */ static void solve(int testCnt) { for (int testNumber = 0; testNumber < testCnt; testNumber++) { int n = in.nextInt(); long[] a = in.nextLongArrayPrimitive(n); if(a[n - 2] > a[n - 1]) { out.println(-1); } else { boolean sorted = true; for(int i = 0; i < n - 1; i++) { sorted &= a[i] <= a[i + 1]; } if(sorted) out.println(0); else { List<int[]> ans = new ArrayList<>(); for(int i = n - 3; i >= 0; i--) { a[i] = a[i + 1] - a[n - 1]; if(a[i] > a[i + 1]) { ans.clear(); break; } ans.add(new int[]{i + 1, i + 2, n}); } if(ans.size() == 0) out.println(-1); else { out.println(ans.size()); for(int[] e : ans) { out.println(e[0] + " " + e[1] + " " + e[2]); } } } } } out.close(); } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream("src/input.in")); out = new PrintWriter(new FileOutputStream("src/output.out")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } 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(); } catch (IOException e) { e.printStackTrace(); } return str; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitive(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitiveOneIndexed(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } Long[] nextLongArray(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitive(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitiveOneIndexed(int n) { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } String[] nextStringArray(int n) { String[] g = new String[n]; for (int i = 0; i < n; i++) g[i] = next(); return g; } List<Integer>[] readGraphOneIndexed(int n, int m) { List<Integer>[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); adj[u].add(v); adj[v].add(u); } return adj; } List<Integer>[] readGraphZeroIndexed(int n, int m) { List<Integer>[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; adj[u].add(v); adj[v].add(u); } return adj; } /* A more efficient way of building an undirected graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildUndirectedGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]] = end2[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]] = end1[i]; idxForEachNode[end2[i]]++; } return adj; } /* A more efficient way of building a directed graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildDirectedGraph(int nodeCnt, int edgeCnt) { int[] from = new int[edgeCnt], to = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; //from u to v: u -> v for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; from[i] = u; to[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[from[i]][idxForEachNode[from[i]]] = to[i]; idxForEachNode[from[i]]++; } return adj; } } }
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 8
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 &lt; y &lt; 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
1522fd05796b68859d5843d2d332973f
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 &lt; y &lt; 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.HashMap; import java.util.*; public class C { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static Scanner scn = new Scanner(System.in); public static void main(String[] args) throws Exception { int t = Integer.parseInt(br.readLine()); while (t-- > 0) { int n = Integer.parseInt(br.readLine()); String[] parts = br.readLine().split(" "); int[] arr = new int[n + 1]; for (int i = 0; i < n; i++) { arr[i + 1] = Integer.parseInt(parts[i]); } if(check(arr)) { System.out.println(0); continue; }else if(arr[n - 1] > arr[n] || arr[n] < 0) { System.out.println(-1); continue; }else { System.out.println(n - 2); for(int i = n - 2; i > 0; i--) { System.out.println(i + " " + (i + 1) + " " + n); } } } } private static boolean check(int[] arr) { for(int i = 2; i < arr.length; i++) { if(arr[i - 1] > arr[i]) { 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 8
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 &lt; y &lt; 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
1550827af2a7d116aea77c30d37af97e
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 &lt; y &lt; 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 { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer st = new StreamTokenizer(br); static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws Exception { int T = nextInt(); while(T-- != 0) { int n = nextInt(); int[] A = new int[n+1]; for(int i = 1; i <= n; i++) A[i] = nextInt(); if(A[n] < A[n-1]) { out.println(-1); continue; } if(A[n] < 0) { boolean flag = false; for(int i = 1; i < n; i++) { if(A[i] > A[i+1]) { flag = true; break; } } if(flag) out.println(-1); else out.println(0); continue; } out.println(n-2); for(int i = 1; i < n - 1; i++) { out.printf("%d %d %d\n", i, n-1, n); } } out.flush(); } public static int nextInt() throws Exception { st.nextToken(); return (int) st.nval; } public static String nextStr() throws Exception { st.nextToken(); return st.sval; } }
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 8
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 &lt; y &lt; 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
df4d6d375a3ea0ec3bd0c0770aefa862
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 &lt; y &lt; 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 { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer st = new StreamTokenizer(br); static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws Exception { int T = nextInt(); while(T-- != 0) { int n = nextInt(); int[] A = new int[n+1]; for(int i = 1; i <= n; i++) A[i] = nextInt(); if(A[n] < A[n-1]) { out.println(-1); continue; } if(A[n] < 0) { boolean flag = false; for(int i = 1; i < n; i++) { if(A[i] > A[i+1]) { flag = true; break; } } if(flag) out.println(-1); else out.println(0); continue; } out.println(n-2); for(int i = 1; i < n - 1; i++) { out.printf("%d %d %d\n", i, n-1, n); } } out.flush(); } public static int nextInt() throws Exception { st.nextToken(); return (int) st.nval; } public static String nextStr() throws Exception { st.nextToken(); return st.sval; } }
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 8
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 &lt; y &lt; 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
977c060e849cc60753d392eb0b53b67f
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 &lt; y &lt; 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 { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer st = new StreamTokenizer(br); static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws Exception { int T = nextInt(); while(T-- != 0) { int n = nextInt(); int[] A = new int[n+1]; for(int i = 1; i <= n; i++) A[i] = nextInt(); if(A[n] < A[n-1]) { out.println(-1); continue; } if(A[n] < 0) { boolean flag = false; for(int i = 1; i < n; i++) { if(A[i] > A[i+1]) { flag = true; break; } } if(flag) out.println(-1); else out.println(0); continue; } out.println(n-2); for(int i = 1; i < n - 1; i++) { out.printf("%d %d %d\n", i, n-1, n); } } out.flush(); } public static int nextInt() throws Exception { st.nextToken(); return (int) st.nval; } public static String nextStr() throws Exception { st.nextToken(); return st.sval; } }
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 8
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 &lt; y &lt; 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
264dffd4bd74f62c046661c7f1363d58
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 &lt; y &lt; 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.ArrayList; import java.util.Scanner; /** * https://codeforces.com/problemset/problem/1635/C * C. Differential Sorting * */ public class Main { static int N = 2 * 100010; static int[] a = new int[N]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { solve(sc); } } static void solve(Scanner sc) { int n = sc.nextInt(); boolean sorted = true; for(int i = 1; i <= n; i++) { a[i] = sc.nextInt(); if(i > 1 && i < n && a[i - 1] > a[i]) { sorted = false; } } if(a[n - 1] > a[n]) { System.out.println(-1); } else { if(a[n] < 0) { if(sorted) { System.out.println(0); } else { 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)); } } } } }
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 8
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 &lt; y &lt; 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
60d6da31e7ba898f7b2a9714a92ef1d6
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 &lt; y &lt; 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 C1635 { public static void main(String[] args) throws IOException { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(r.readLine()); for(int i = 0; i < T; i++) { int n=Integer.parseInt(r.readLine()); int arr[] = new int[n]; StringTokenizer st = new StringTokenizer(r.readLine()); for(int j = 0;j < n; j++) { arr[j] = Integer.parseInt(st.nextToken()); } int[] sorted = arr.clone(); Arrays.sort(sorted); if(Arrays.equals(sorted,arr)) { System.out.println(0); continue; } else if(arr[n-1] < 0 || arr[n-2]>arr[n-1]) { System.out.println(-1); continue; } else { solve(n, arr); } } } static int solve(int n, int[] arr) { //right to left see if possible System.out.println(n-2); for(int j = n-3; j >= 0; j--) { //if(arr[j] > arr[j+1]) { System.out.println((j+1) + " " + (n-1) +" " + n); //} } return 1; } static class Swap { int x; int y; int z; int swap; boolean possible; public Swap(int x_, int y_, int z_, int s, boolean p) { x=x_; y=y_; z=z_; swap=s; possible=p; } } /* @param: index is the index of the number needing a swap in the original array thus the loop starts at index+1 @return : highest value that can be swapped that is <= arr[index+1] */ static Swap bestSwap(int index, int[] arr) { //double check indexes int yIndex=-1; int zIndex=-1; int max=-(Integer.MAX_VALUE); for(int i = index+1; i < arr.length-1; i++) { for(int j = i+1; j < arr.length; j++) { if(arr[i]-arr[j] < arr[index+1]) { //if that number is less if(max < arr[i]-arr[j]) { max=arr[i]-arr[j]; zIndex = j; yIndex = i; } } } } if(max == -(Integer.MAX_VALUE)) { return new Swap(0,0,0,0,false); } return new Swap(index+1, yIndex+1, zIndex+1, max, 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 8
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 &lt; y &lt; 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
dea97b7f3c3e294a43bf84376a4f09aa
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 &lt; y &lt; 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.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.StringTokenizer; public class CodeForces { static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(){ br=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); } public FastScanner(File f){ try { br=new BufferedReader(new FileReader(f)); st=new StringTokenizer(""); } catch(FileNotFoundException e){ br=new BufferedReader(new InputStreamReader(System.in)); 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; } long nextLong() { return Long.parseLong(next()); } } public static long factorial(int n){ if(n==0)return 1; return (long)n*factorial(n-1); } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } static void sort (int[]a){ ArrayList<Integer> b = new ArrayList<>(); for(int i:a)b.add(i); Collections.sort(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static void sortReversed (int[]a){ ArrayList<Integer> b = new ArrayList<>(); for(int i:a)b.add(i); Collections.sort(b,new Comparator<Integer>(){ @Override public int compare(Integer o1, Integer o2) { return o2-o1; } }); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static void sort (long[]a){ ArrayList<Long> b = new ArrayList<>(); for(long i:a)b.add(i); Collections.sort(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static ArrayList<Integer> sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } ArrayList<Integer> ans = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (prime[i] == true) ans.add(i); } return ans; } static int binarySearchSmallerOrEqual(int arr[], int key) { int n = arr.length; int left = 0, right = n; int mid = 0; while (left < right) { mid = (right + left) >> 1; if (arr[mid] == key) { while (mid + 1 < n && arr[mid + 1] == key) mid++; break; } else if (arr[mid] > key) right = mid; else left = mid + 1; } while (mid > -1 && arr[mid] > key) mid--; return mid; } public static int binarySearchStrictlySmaller(int[] arr, int target) { int start = 0, end = arr.length-1; if(end == 0) return -1; if (target > arr[end]) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } static int binarySearch(int arr[], int x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } static int binarySearch(long arr[], long x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } static void init(int[]arr,int val){ for(int i=0;i<arr.length;i++){ arr[i]=val; } } static void init(int[][]arr,int val){ for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[i].length;j++){ arr[i][j]=val; } } } static void init(long[]arr,long val){ for(int i=0;i<arr.length;i++){ arr[i]=val; } } static<T> void init(ArrayList<ArrayList<T>>arr,int n){ for(int i=0;i<n;i++){ arr.add(new ArrayList()); } } static int binarySearchStrictlySmaller(ArrayList<Pair> arr, int target) { int start = 0, end = arr.size()-1; if(end == 0) return -1; if (target > arr.get(end).y) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr.get(mid).y >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } static int binarySearchStrictlyGreater(int[] arr, int target) { int start = 0, end = arr.length - 1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] <= target) { start = mid + 1; } else { ans = mid; end = mid - 1; } } return ans; } static long sum (int []a, int[][] vals){ long ans =0; int m=0; while(m<5){ for (int i=m+1;i<5;i+=2){ ans+=(long)vals[a[i]-1][a[i-1]-1]; ans+=(long)vals[a[i-1]-1][a[i]-1]; } m++; } return ans; } public static long pow(long n, long pow) { if (pow == 0) { return 1; } long retval = n; for (long i = 2; i <= pow; i++) { retval *= n; } return retval; } static String reverse(String s){ StringBuffer b = new StringBuffer(s); b.reverse(); return b.toString(); } static String charToString (char[] arr){ String t=""; for(char c :arr){ t+=c; } return t; } public static void main(String[] args) { // StringBuilder sbd = new StringBuilder(); // PrintWriter out = new PrintWriter("output.txt"); // File input = new File("input.txt"); // FastScanner fs = new FastScanner(input); // FastScanner fs = new FastScanner(); // Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int testNumber =fs.nextInt(); // ArrayList<Integer> arr = new ArrayList<>(); for (int T =0;T<testNumber;T++){ // StringBuffer sbf = new StringBuffer(); int n = fs.nextInt(); int []arr = fs.readArray(n); boolean res=true; for (int i=1;i<n;i++){ if(arr[i]<arr[i-1]){ res= false; } } if(res){ out.print("0\n"); }else { if(arr[n-2]>arr[n-1]){ out.print("-1\n"); }else if(arr[n-1]<0){ out.print("-1\n"); }else { out.print(n-2+"\n"); for (int i=1;i<=n-2;i++)out.print(i+" "+(n-1)+" "+(n)+"\n"); } } } out.flush(); } static class Pair { int x;//a int y;//k public Pair(int x,int y){ this.x=x; this.y=y; } @Override public boolean equals(Object o) { if(o instanceof Pair){ if(o.hashCode()!=hashCode()){ return false; } else { return x==((Pair)o).x&&y==((Pair)o).y; } } return false; } @Override public int hashCode() { return x+2*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 8
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 &lt; y &lt; 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
df5308e138328e6a897108758ccd0d9b
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 &lt; y &lt; 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.concurrent.ThreadLocalRandom; //3 6 6 1 //2 7 4 6 public class D { private static void sport(int[] a) { int n = a.length; List<int[]> ans = new ArrayList<>(); boolean f = false; for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) { f = true; break; } } if (!f) { System.out.println(0); return; } // x-y x y if (a[n - 2] > a[n - 1]) { System.out.println(-1); return; } for (int i = n - 3; i >= 0; i--) { a[i] = a[n - 2] - a[n - 1]; if (a[i] > a[i + 1]) { System.out.println(-1); return; } ans.add(new int[]{i + 1, n - 1, n}); } System.out.println(ans.size()); for (int[] an : ans) { System.out.println(an[0] + " " + an[1] + " " + an[2]); } } public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int[] a = sc.readArrayInt(n); sport(a); } } static void shuffleArrayA(int[][] ar) { // If running on Java 6 or older, use `new Random()` on RHS here 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; } } static void shuffleArray(int[] ar) { // If running on Java 6 or older, use `new Random()` on RHS here 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; } } 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()); } void nextLine() throws IOException { br.readLine(); } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } int[] readArrayInt(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()); } } }
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 8
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 &lt; y &lt; 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
e26e4a6cb66711b5c3673bd6161b8cfe
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 &lt; y &lt; 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 Temp { static int t,n,arr[]; static final PrintWriter out=new PrintWriter(System.out); static final StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); public static void main(String[] args) throws IOException { in.nextToken();t=(int)in.nval; while(t-->0) solve(); out.flush(); } static void solve() throws IOException{ in.nextToken();n=(int)in.nval;arr=new int[n]; getArray(); if((arr[n-1]<arr[n-2])) { out.println(-1);return; } else if(arr[n-1]>=0) { out.println(n-2); for(int i=0;i<n-2;i++) { out.println((i+1)+" "+(n-1)+" "+n); } } else { for(int i=1;i<n;i++) { if(arr[i-1]>arr[i]) { out.println(-1);return; } } out.println(0); } } static void getArray() throws IOException{ for(int i=0;i<n;++i){ in.nextToken();arr[i]=(int)in.nval; } } }
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 8
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 &lt; y &lt; 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
604d38a0b81ca5a32246cb0855b397c4
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 &lt; y &lt; 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 { // For fast input output static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader( new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { 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; } } // end of fast i/o code public static void main(String[] args) { FastReader reader = new FastReader(); StringBuilder sb = new StringBuilder(""); int t = reader.nextInt(); int ans = 0; while (t-- > 0) { int n = reader.nextInt(); int arr[] = new int[n]; for(int i=0; i<n; i++){ arr[i] = reader.nextInt(); } boolean unsorted = false; for(int i=1; i<n; i++){ if(arr[i] < arr[i-1]){ unsorted = true; } } if(!unsorted){ sb.append("0\n"); continue; } if(arr[n-2]>arr[n-1]){ sb.append("-1\n"); continue; } if(arr[n-2]-arr[n-1] > arr[n-2]){ sb.append("-1\n"); continue; } sb.append((n-2)+"\n"); for(int i=0; i<n-2; i++){ sb.append((i+1)+" "+(n-1)+" "+n+"\n"); } } System.out.println(sb); } }
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 8
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 &lt; y &lt; 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
b51005b25e51aca9eaffb68149ba115b
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 &lt; y &lt; 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.lang.*; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.File; import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; public class Main { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair ob) { return (int)(first - ob.first); } } static class Tuple implements Comparable<Tuple> { long first, second,third; public Tuple(long first, long second, long third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int count = 0; int[] parent; int[] rank; public DSU(int n) { count = n; parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public void union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return; if(rank[a] < rank[b]) { parent[a] = b; } else if(rank[a] > rank[b]) { parent[b] = a; } else { parent[b] = a; rank[a] = 1 + rank[a]; } count--; } public int countConnected() { return count; } } static class Reader { 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) throws IOException { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] longReadArray(int n) throws IOException { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static long lcm(long a, long b) { return (a / LongGCD(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long powMod(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; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return powMod(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)(n)] * modInverse(fac[(int)(r)], p) % p * modInverse(fac[(int)(n - r)], p) % p) % p; } public static long fact(long n) { long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (long i = 1; i <= n; i++) fac[(int)(i)] = fac[(int)(i - 1)] * i; return fac[(int)(n)]; } public static long nCr(long n, long k) { long ans = 1; for(long i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } //Modular Operations for Addition and Multiplication. public static long perfomMod(long x) { return ((x%M + M)%M); } public static long addMod(long a, long b) { return perfomMod(perfomMod(a)+perfomMod(b)); } public static long subMod(long a, long b) { return perfomMod(perfomMod(a)-perfomMod(b)); } public static long mulMod(long a, long b) { return perfomMod(perfomMod(a)*perfomMod(b)); } public static boolean isPrime(long n) { if(n == 1) { return false; } //check only for sqrt of the number as the divisors //keep repeating so only half of them are required. So,sqrt. for(int i = 2;i*i<=n;i++) { if(n%i == 0) { return false; } } return true; } public static List<Long> SieveList(int n) { boolean prime[] = new boolean[(int)(n+1)]; Arrays.fill(prime, true); List<Long> l = new ArrayList<>(); for (long p = 2; p*p<=n; p++) { if (prime[(int)(p)] == true) { for(long i = p*p; i<=n; i += p) { prime[(int)(i)] = false; } } } for (long p = 2; p<=n; p++) { if (prime[(int)(p)] == true) { l.add(p); } } return l; } public static int countDivisors(int x) { int c = 0; for(int i = 1;i*i<=x;i++) { if(x%i == 0) { if(x/i != i) { c+=2; } else { c++; } } } return c; } public static long log2(long n) { long ans = (long)(log(n)/log(2)); return ans; } public static boolean isPow2(long n) { return (n != 0 && ((n & (n-1))) == 0); } public static boolean isSq(int x) { long s = (long)Math.round(Math.sqrt(x)); return s*s==x; } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public 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; } public 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; } public static void Sort(int[] a) { List<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void ssort(char[] a) { List<Character> l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static boolean checkSorted(long[] a) { int n = a.length; boolean ok = true; for(int i = 0;i+1<n;i++) { if(a[i] > a[i+1]) { ok = false; } } return ok; } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int tt = sc.nextInt(); fr:while(tt-- > 0) { int n = sc.nextInt(); long[] a = sc.longReadArray(n); long r = a[n-1]; boolean ok = checkSorted(a); if(a[n-2] > r) { fout.println(-1); } else if(r >= 0) { fout.println(n-2); for(int i = 1;i+1<n;i++) fout.println(i + " " + (n - 1) + " " + n); } else { fout.println((ok == true) ? 0 : -1); } } fout.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 8
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 &lt; y &lt; 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
2a0d97c3419de63b2edf8029bf8d80c7
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 &lt; y &lt; 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.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; public class Main{ static PrintWriter out; static int [] tmp; static int ans; static int n; static Queue<Integer> solu; static boolean check; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while(t--!=0) { n = Integer.parseInt(br.readLine()); String [] line = br.readLine().split(" "); tmp = new int [line.length]; for(int i =0;i!=n;i++) tmp [i]=Integer.parseInt(line[i]); ans=0; check = true; solu = new LinkedList<>(); if(tmp[n-2]>tmp[n-1]) out.println(-1); else { if(tmp[n-1]>=0) { out.println(n-2); for(int i=0;i!=n-2;i++) { out.println((i+1)+" "+(n-1)+" "+(n)); } } else { boolean check =true; for(int i=0;i!=n-2;i++) if(tmp[i]>tmp[i+1]) { check=false; break; } if(check) out.println(0); 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 8
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 &lt; y &lt; 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
2ebb522efe8813633218d50d1106a6a8
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 &lt; y &lt; 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
// letsgoooooooooooooooooooooooooooooooo import java.util.*; import java.io.*; public class Solution{ static int MOD=1000000007; static PrintWriter pw; static FastReader sc; 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());} public char nextChar() throws IOException {return next().charAt(0);} String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int[] intArr(int a,int b,int n) throws IOException {int[]in=new int[n];for(int i=a;i<b;i++)in[i]=nextInt();return in;} } public static class pair{ int val=0,idx=0; pair(int v,int i){ this.val=v; this.idx=i; } } public static class helper{ int x=0,y=0,z=0; helper(int x,int y,int z){ this.x=x; this.y=y; this.z=z; } } static void Solve() throws Exception{ int n =sc.nextInt(); int [] arr=new int [n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } boolean check = isSorted(arr); if(check){ pw.println(0); return; } if( arr[n-2]<=arr[n-1] && arr[n-2]-arr[n-1]<=arr[n-2] ){ pw.println(n-2); for(int i=0;i<n-2;i++){ pw.println(i+1+" "+(n-1)+" "+n); } return; } pw.println("-1"); } public static boolean isSorted(int [] arr){ for(int i=1;i<arr.length;i++){ if(arr[i]<arr[i-1]){ return false; } } return true; } public static void parr(int [] arr){for(int i=0;i<arr.length;i++){pw.print(arr[i]+" ");}} public static void main(String[] args) throws Exception{ try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } sc= new FastReader(); pw = new PrintWriter(System.out); int t=1; t=sc.nextInt(); for(int ii=1;ii<=t;ii++) { Solve(); } 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 8
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 &lt; y &lt; 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
e347c8ff983ab5cd7d91d221e01075ca
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 &lt; y &lt; 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 Codechef { static long fans[] = new long[200001]; static long inv[] = new long[200001]; static long mod = 1000000007; static void init() { fans[0] = 1; inv[0] = 1; fans[1] = 1; inv[1] = 1; for (int i = 2; i < 200001; i++) { fans[i] = ((long) i * fans[i - 1]) % mod; inv[i] = power(fans[i], mod - 2); } } static long ncr(int n, int r) { return (((fans[n] * inv[n - r]) % mod) * (inv[r])) % mod; } public static void main(String[] args) throws java.lang.Exception { FastReader in = new FastReader(System.in); StringBuilder sb = new StringBuilder(); int t = 1; t = in.nextInt(); while (t > 0) { --t; int n = in.nextInt(); int arr[] = new int[n]; boolean isok = true; for(int i = 0;i<n;i++) { arr[i] = in.nextInt(); if(i-1>=0) { if(arr[i]<arr[i-1]) isok = false; } } if(isok) { sb.append("0\n"); continue; } if(arr[n-1]<arr[n-2]) { sb.append("-1\n"); continue; } int count = 0; StringBuilder tb = new StringBuilder(); for(int i = 0;i<n-2;i++) { ++count; arr[i] = arr[n-2] - arr[n-1]; tb.append((i+1)+" "+(n-1)+" "+n+"\n"); } boolean poss = true; for(int i = 0;i<n-1;i++) if(arr[i]>arr[i+1]) poss = false; if(poss) { sb.append(count+"\n"); sb.append(tb); } else sb.append("-1\n"); } System.out.print(sb); } 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 % mod) * (x % mod)) % mod; // y must be even now y = y >> 1; // y = y/2 x = ((x % mod) * (x % mod)) % mod; // Change x to x^2 } return res; } static long[] generateArray(FastReader in, int n) throws IOException { long arr[] = new long[n]; for (int i = 0; i < n; i++) arr[i] = in.nextLong(); return arr; } static long[][] generatematrix(FastReader in, int n, int m) throws IOException { long arr[][] = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = in.nextLong(); } } return arr; } static long gcd(long a, long b) { if (a == 0) return b; else return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } 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); } } 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(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } 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; } }
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 8
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 &lt; y &lt; 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
88402ea0e139fee9326096087827f4e2
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 &lt; y &lt; 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; public class R772C { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); while (t-- > 0) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); long[] arr = new long[n]; boolean needsOperation = false; for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(st.nextToken()); if (i > 0 && arr[i] < arr[i-1]) needsOperation = true; } if (!needsOperation){ System.out.println("0"); continue; } int[][] result = solve(arr); if (result == null) { System.out.println("-1"); continue; } StringBuilder builder = new StringBuilder(); builder.append(result.length); builder.append("\n"); for (int i = 0; i < result.length; i++) { builder.append(result[i][0] + 1); builder.append(" "); builder.append(result[i][1] + 1); builder.append(" "); builder.append(result[i][2] + 1); builder.append(" "); builder.append("\n"); } System.out.print(builder.toString()); } } private static int[][] solve(long[] arr) { int[][] operations = new int[arr.length - 2][3]; if (arr[arr.length-1] < arr[arr.length-2] || arr[arr.length-1] < 0) return null; for (int i = arr.length - 3; i >= 0; i--) { operations[arr.length - i - 3] = new int[] {i, i + 1, arr.length-1}; } return operations; } private static int[] getOperation(int i, int length) { return new int[] {length + 1 - i, length + 1 - (i+1), length + 1 - (length-1)}; } private static void reverse(long[] arr) { for (int i = 0; i < arr.length / 2; i++) { long t = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = t; } } } /* -3 -3 -4 -2 -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 8
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 &lt; y &lt; 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
a25510d13d578119680f02a3f9f29e63
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 &lt; y &lt; 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 { static PrintStream out = new PrintStream(System.out); static LinkedList<LinkedList<Integer>> adj; static boolean[] vis; static long ans; static int target; static HashMap<ArrayList<Integer>, Integer> seqs; //static ArrayList<ArrayList<Integer>> lists; public static void main(String[] args){ FastScanner sc = new FastScanner(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int[] arr = sc.nextIntArray(n); // if(arr[n-2] > arr[n-1]){ // out.println(-1); // continue; // } // check if already non-decreasing boolean ok = true; for(int i = 1; i < n; i++){ if(arr[i - 1] > arr[i]) { ok = false; break; } } if(ok){ out.println(0); continue; } if(arr[n-2] > arr[n-1] || arr[n-2] - arr[n-1] > arr[n-2]){ out.println(-1); continue; } out.println(n - 2); for(int i = 0; i < n - 2; i++){ out.println((i + 1) + " " + (n - 1) + " " + (n)); } } } public static void dfs(int v){ if(vis[v]) return; vis[v] = true; LinkedList<Integer> neighbors = adj.get(v); Iterator<Integer> it = neighbors.iterator(); while(it.hasNext()){ int node = it.next(); if(!vis[node]){ dfs(node); } } } public static void mergeSort(int[] inputArray){ int n = inputArray.length; // if input array is empty or contains only one element (meaning already sorted) if(n < 2){ return; } // split input array int mid = n / 2; int[] leftHalf = new int[mid]; int[] rightHalf = new int[n - mid]; for(int i = 0; i < mid; i++){ leftHalf[i] = inputArray[i]; } for(int i = mid; i < n; i++){ rightHalf[i - mid] = inputArray[i]; } // merge sort both halves mergeSort(leftHalf); mergeSort(rightHalf); // merge halves back into one array merge(inputArray, leftHalf, rightHalf); } public static void merge(int[] inputArray, int[] leftArray, int[] rightArray){ int leftSize = leftArray.length; int rightSize = rightArray.length; int i = 0, j = 0, k = 0; // get smaller element between two arrays while(i < leftSize && j < rightSize){ if(leftArray[i] <= rightArray[j]){ inputArray[k] = leftArray[i++]; } else{ for(int x = i ; x < leftSize; x++){ //out.print(leftArray[x] + " " + rightArray[j]); int u = leftArray[x]; int v = rightArray[j]; adj.get(u).add(v); adj.get(v).add(u); } inputArray[k] = rightArray[j++]; } k++; } // check left for leftovers while(i < leftSize){ inputArray[k++] = leftArray[i++]; } // check right for leftovers while(j < rightSize){ inputArray[k++] = rightArray[j++]; } } public static void addUndirectedEdge(int u, int v){ adj.get(u).add(v); adj.get(v).add(u); } } // custom I/O class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { 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[] nextIntArray(int length) { int[] arr = new int[length]; for (int i = 0; i < length; i++) arr[i] = nextInt(); return arr; } }
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 8
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 &lt; y &lt; 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
d35665cba2e6a5ad0adf9f2eef6508ad
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 &lt; y &lt; 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 { static PrintStream out = new PrintStream(System.out); static LinkedList<LinkedList<Integer>> adj; static boolean[] vis; static long ans; static int target; static HashMap<ArrayList<Integer>, Integer> seqs; //static ArrayList<ArrayList<Integer>> lists; public static void main(String[] args){ FastScanner sc = new FastScanner(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int[] arr = sc.nextIntArray(n); if(arr[n-2] > arr[n-1]){ out.println(-1); continue; } // check if already non-decreasing boolean ok = true; for(int i = 1; i < n; i++){ if(arr[i - 1] > arr[i]) { ok = false; break; } } if(ok){ out.println(0); continue; } if(arr[n-2] - arr[n-1] > arr[n-2]){ out.println(-1); continue; } out.println(n - 2); for(int i = 0; i < n - 2; i++){ out.println((i + 1) + " " + (n - 1) + " " + (n)); } } } public static void dfs(int v){ if(vis[v]) return; vis[v] = true; LinkedList<Integer> neighbors = adj.get(v); Iterator<Integer> it = neighbors.iterator(); while(it.hasNext()){ int node = it.next(); if(!vis[node]){ dfs(node); } } } public static void mergeSort(int[] inputArray){ int n = inputArray.length; // if input array is empty or contains only one element (meaning already sorted) if(n < 2){ return; } // split input array int mid = n / 2; int[] leftHalf = new int[mid]; int[] rightHalf = new int[n - mid]; for(int i = 0; i < mid; i++){ leftHalf[i] = inputArray[i]; } for(int i = mid; i < n; i++){ rightHalf[i - mid] = inputArray[i]; } // merge sort both halves mergeSort(leftHalf); mergeSort(rightHalf); // merge halves back into one array merge(inputArray, leftHalf, rightHalf); } public static void merge(int[] inputArray, int[] leftArray, int[] rightArray){ int leftSize = leftArray.length; int rightSize = rightArray.length; int i = 0, j = 0, k = 0; // get smaller element between two arrays while(i < leftSize && j < rightSize){ if(leftArray[i] <= rightArray[j]){ inputArray[k] = leftArray[i++]; } else{ for(int x = i ; x < leftSize; x++){ //out.print(leftArray[x] + " " + rightArray[j]); int u = leftArray[x]; int v = rightArray[j]; adj.get(u).add(v); adj.get(v).add(u); } inputArray[k] = rightArray[j++]; } k++; } // check left for leftovers while(i < leftSize){ inputArray[k++] = leftArray[i++]; } // check right for leftovers while(j < rightSize){ inputArray[k++] = rightArray[j++]; } } public static void addUndirectedEdge(int u, int v){ adj.get(u).add(v); adj.get(v).add(u); } } // custom I/O class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { 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[] nextIntArray(int length) { int[] arr = new int[length]; for (int i = 0; i < length; i++) arr[i] = nextInt(); return arr; } }
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 8
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 &lt; y &lt; 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
126fb4f4c10adf4794f3823d5cf5c3b2
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 &lt; y &lt; 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 class1 { public static void main(String[] args) { Scanner input=new Scanner(System.in); int t=input.nextInt(); while(t-->0) { int n=input.nextInt(); int a[]=new int[n]; int x=0,y=0; for(int i=0;i<n;i++){ a[i]=input.nextInt(); if(i>0) { if(a[i]<a[i-1]) { y++; } } if(i==n-1) { if(a[n-2]>a[n-1]) { x++; } } } if(a[n-1]<0) { if(y==0) { System.out.println(0); } else { System.out.println(-1); } } else { if(x==0){ System.out.println(n-2); for(int i=0;i<n-2;i++) { int t1=i+1,t2=n,t3=n-1; System.out.println(t1+" "+t3+" "+t2); } } 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 8
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 &lt; y &lt; 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
9b473e58152943e748d15bc4ba9cde58
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 &lt; y &lt; 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 cp{ public static void main(String[] args)throws Exception{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t-->0){ int n=Integer.parseInt(br.readLine()); long[] arr=new long[n]; String[] str=br.readLine().split(" "); for(int i=0;i<n;i++){ arr[i]=Long.parseLong(str[i]); } int id=0; if((arr[n-1]<arr[n-2])){ id=1; } boolean isSorted=false; if(arr[n-1]<0){ for(int i=0;i<n-1;i++){ if(arr[i]>arr[i+1]){ id=1;break; } } if(id==0) isSorted=true; } StringBuilder sb=new StringBuilder(); int cnt=0; for(int i=n-3;i>=0&&id==0&&isSorted==false;i--){ sb.append((i+1)+" "+(n-1)+" "+(n)+"\n");cnt++; } if(id==1){ System.out.println(-1); }else{ System.out.println(cnt); System.out.println(sb); } } } }
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 8
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 &lt; y &lt; 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
57c32c8d3b01f5ee0322e0d85faf6e6c
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 &lt; y &lt; 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.*; public class Codechef { static class Pair{ int i; int j; int z; public Pair(int i,int j,int z) { this.i=i; this.j=j; this.z=z; } } static int[] num_to_bits = new int[] { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; static int countSetBitsRec(int num) { int nibble = 0; if (0 == num) return num_to_bits[0]; // Find last nibble nibble = num & 0xf; // Use pre-stored values to find count // in last nibble plus recursively add // remaining nibbles. return num_to_bits[nibble] + countSetBitsRec(num >> 4); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br=new BufferedReader(new FileReader("input.txt")); PrintStream out= new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } // Catch block to handle the exceptions catch (Exception e) { 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 int computeXOR(int n) { // If n is a multiple of 4 if (n % 4 == 0) return n; // If n%4 gives remainder 1 if (n % 4 == 1) return 1; // If n%4 gives remainder 2 if (n % 4 == 2) return n + 1; // If n%4 gives remainder 3 return 0; } public static void main (String[] args) throws java.lang.Exception { try{ FastReader sc=new FastReader(); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(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.nextLong(); } boolean isSort = true; for(int i=1;i<n;i++) { if(arr[i-1]<=arr[i]) continue; else { isSort=false; break; } } List<Pair> ans = new ArrayList<>(); if(isSort) { System.out.println(0); } else { long value1 = arr[n-2]; long value2 = arr[n-1]; boolean yes = true; if(value1<=value2) { long vv = value1-value2; long count = 0; if(vv<=arr[n-2]&&Math.abs(vv)<1000000000000000000L) arr[n-3] = vv; else yes=false; if(yes) { for(int i=n-3;i>=0;i--) { ans.add(new Pair(i+1,(n-1),(n))); count++; } System.out.println(count); for(int i=ans.size()-1;i>=0;i--) { System.out.println(ans.get(i).i+" "+ans.get(i).j+" "+ans.get(i).z); } } else System.out.println(-1); } else System.out.println(-1); } } }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 8
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 &lt; y &lt; 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
8e6b242b8233c9b38dd112aa3371c967
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 &lt; y &lt; 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 { public static void main (String[] args) throws java.lang.Exception { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t= sc.nextInt(); while (t-- != 0) { int n = sc.nextInt(); int[] Y = new int[n]; for (int i = 0; i < n; i++) { Y[i] = sc.nextInt(); } boolean isSorted = true; for (int i = 0; i < n - 1; i++) { isSorted = isSorted && Y[i] <= Y[i + 1]; } if (isSorted) { out.println(0); } else { if (Y[Y.length - 2] > Y[Y.length - 1]) { out.println(-1); } else if (Y[Y.length - 1] < 0) { out.println(-1); } else { out.println(n - 2); for (int i = 0; i < n - 2; i++) { out.println((i + 1) + " " + (Y.length - 1) + " " + Y.length); } } } } out.close(); } public static PrintWriter out; 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 8
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 &lt; y &lt; 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
c580f8fe7937eecd65faf03267561921
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 &lt; y &lt; 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.ArrayList; import java.util.Scanner; public class A { public static void main(String[] args) { int inNum = 0; int n = 0; int[] num; Scanner in = new Scanner(System.in); inNum = in.nextInt(); while(inNum-->0) { n = in.nextInt(); int[] nums = new int[n]; for(int i=0; i<n; i++) { nums[i] = in.nextInt(); } if(nums[n-1] < nums[n-2]) { System.out.println("-1"); continue; } if(nums[n-1] < 0) { boolean b = false; for(int i=0; i<n-1; i++) { if(nums[i]>nums[i+1]) { b = true; break; } } if(b)System.out.println("-1"); else System.out.println("0"); continue; } ArrayList<int[]> al = new ArrayList<int[]>(); for(int i=n-3; i>=0; i--) { al.add(new int[] {i+1, i+2, n}); } System.out.println(al.size()); for(int i=0; i<al.size(); i++) { int[] t = al.get(i); System.out.println(t[0]+" "+t[1]+" "+t[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 8
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 &lt; y &lt; 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
d5eb88871a405a9936f80e1dc5d9e65f
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 &lt; y &lt; 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.Math; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; public class Main { static PrintWriter pw; static Scanner sc; static StringBuilder ans; static long mod = 1000000000+7; static void pn(final Object arg) { pw.print(arg); pw.flush(); } /*-------------- for input in an value ---------------------*/ static int ni() { return sc.nextInt(); } static long nl() { return sc.nextLong(); } static double nd() { return sc.nextDouble(); } static String ns() { return sc.next(); } static String nLine() { return sc.nextLine(); } static void ap(int arg) { ans.append(arg); } static void ap(long arg) { ans.append(arg); } static void ap(String arg) { ans.append(arg); } static void ap(StringBuilder arg) { ans.append(arg); } static void apn() { ans.append("\n"); } static void apn(int arg) { ans.append(arg+"\n"); } static void apn(long arg) { ans.append(arg+"\n"); } static void apn(String arg) { ans.append(arg+"\n"); } static void apn(StringBuilder arg) { ans.append(arg+"\n"); } static void yes() { ap("Yes\n"); } static void no() { ap("No\n"); } /* for Dubuggin */ static void printArr(int ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); } static void printArr(long ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); } static void printArr(String ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); } static void printIntegerList(List<Integer> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); } static void printLongList(List<Long> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); } static void printStringList(List<String> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); } /*-------------- for input in an array ---------------------*/ static void readArray(int arr[]){ for(int i=0; i<arr.length; i++)arr[i] = ni(); } static void readArray(long arr[]){ for(int i=0; i<arr.length; i++)arr[i] = nl(); } static void readArray(String arr[]){ for(int i=0; i<arr.length; i++)arr[i] = ns(); } static void readArray(double arr[]){ for(int i=0; i<arr.length; i++)arr[i] = nd(); } /*-------------- File vs Input ---------------------*/ static void runFile() throws Exception { sc = new Scanner(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); } static void runIo() throws Exception { pw =new PrintWriter(System.out); sc = new Scanner(System.in); } static int log2(int n) { return (int)(Math.log(n) / Math.log(2)); } static boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0);} static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long nCr(long n, long r) { // Combinations if (n < r) return 0; if (r > n - r) { // because nCr(n, r) == nCr(n, n - r) r = n - r; } long ans = 1L; for (long i = 0; i < r; i++) { ans *= (n - i); ans /= (i + 1); } return ans; } static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);} 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 boolean sv[] = new boolean[10002]; static void seive() { //true -> not prime // false->prime sv[0] = sv[1] = true; sv[2] = false; for(int i = 0; i< sv.length; i++) { if( !sv[i] && (long)i*(long)i < sv.length ) { for ( int j = i*i; j<sv.length ; j += i ) { sv[j] = true; } } } } static long kadensAlgo(long ar[]) { int n = ar.length; long pre = ar[0]; long ans = ar[0]; for(int i = 1; i<n; i++) { pre = Math.max(pre + ar[i], ar[i]); ans = Math.max(pre, ans); } return ans; } static long binpow( long a, long b) { long res = 1; while (b > 0) { if ( (b & 1) > 0){ res = (res * a); } a = (a * a); b >>= 1; } return res; } static long factorial(long n) { long res = 1, i; for (i = 2; i <= n; i++){ res = ((res%mod) * (i%mod))%mod; } return res; } static int getCountPrime(int n) { int ans = 0; while (n%2==0) { ans ++; n /= 2; } for (int i = 3; i *i<= n; i+= 2) { while (n%i == 0) { ans ++; n /= i; } } if(n > 1 ) ans++; return ans; } 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); } static void sort(long[] arr) { /* because Arrays.sort() uses quicksort which is dumb Collections.sort() uses merge sort */ 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 int lowerBound(ArrayList<Integer> ar, int k, int l, int r) { while (l <= r) { int m = (l + r) >> 1 ; if (ar.get(m) >= k) { r = m - 1 ; } else { l = m + 1 ; } } return l ; } static int upperBound(long ar[], long k, int l, int r) { while (l <= r) { int m = (l + r) >> 1 ; if (ar[m] > k) { r = m - 1 ; } else { l = m + 1 ; } } return l ; } static int lowerBound(int ar[], int k, int l, int r) { while (l <= r) { int m = (l + r) >> 1 ; if (ar[m] >= k) { r = m - 1 ; } else { l = m + 1 ; } } return l ; } static void findDivisor(int n, ArrayList<Integer> al) { for(int i = 1; i*i <= n; i++){ if( n % i == 0){ if(n/i==i){ al.add(i); } else{ al.add(n/i); al.add(i); } } } } static class Pair{ int a; int b; Pair( int a, int b ){ this.a = a; this.b = b; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Pair)) return false; Pair pair = (Pair) o; return a == pair.a && b == pair.b; } @Override public int hashCode() { int result = a; result = 31 * result + b; return result; } } public static void main(String[] args) throws Exception { // runFile(); runIo(); int t; t = 1; t = sc.nextInt(); ans = new StringBuilder(); while( t-- > 0 ) { solve(); } pn(ans+""); } public static void solve ( ) { int n = ni(); int ar[] = new int[n]; readArray(ar); boolean flag = false; for(int i = 1; i<n; i++) { if( ar[i-1] > ar[i] ) { flag = true; break; } } if( !flag) { apn("0"); return; } if( ar[n-2] > ar[n-1] || ar[n-1] < 0 ) { apn("-1"); return; } apn((n-2)); for(int i = 1; i<=n-2; i++) { apn(i+" "+ (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 8
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 &lt; y &lt; 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
addb55b36cb9274d4481a68ca76a4e18
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 &lt; y &lt; 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 C { static class Pair { int f;int s; // Pair(){} Pair(int f,int s){ this.f=f;this.s=s;} } static class Fast { BufferedReader br; StringTokenizer st; public Fast() { 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()); } int[] readArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArray1(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (IOException e) { e.printStackTrace(); } return str; } } /* static long noOfDivisor(long a) { long count=0; long t=a; for(long i=1;i<=(int)Math.sqrt(a);i++) { if(a%i==0) count+=2; } if(a==((long)Math.sqrt(a)*(long)Math.sqrt(a))) { count--; } return count; }*/ static boolean isPrime(long a) { for (long i = 2; i <= (long) Math.sqrt(a); i++) { if (a % i == 0) return false; } return true; } static void primeFact(int n) { int temp = n; HashMap<Integer, Integer> h = new HashMap<>(); for (int i = 2; i * i <= n; i++) { if (temp % i == 0) { int c = 0; while (temp % i == 0) { c++; temp /= i; } h.put(i, c); } } if (temp != 1) h.put(temp, 1); } static void reverseArray(int a[]) { int n = a.length; for (int i = 0; i < n / 2; i++) { a[i] = a[i] ^ a[n - i - 1]; a[n - i - 1] = a[i] ^ a[n - i - 1]; a[i] = a[i] ^ a[n - i - 1]; } } 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 class Tuple { int a;int b;int c; Tuple(){} Tuple(int a,int b,int c) { this.a=a;this.b=b;this.c=c; } } public static void main(String args[]) throws IOException { Fast sc = new Fast(); PrintWriter out = new PrintWriter(System.out); int t1 = sc.nextInt(); outer: while (t1-- > 0) { int n = sc.nextInt(); int a[] = sc.readArray(n); ArrayList<Tuple> ans=new ArrayList<>(); if(a[n-2]<=a[n-1]) { if(a[n-2]>=0) { for(int i=0;i<=n-3;i++) { a[i] = a[n - 2] - a[n - 1]; ans.add(new Tuple(i + 1, n - 1, n)); } } else { if(a[n-1]<0) { if(isSorted(a)) { out.println(0); continue outer; } else { out.println(-1); continue outer; } } else { for(int i=0;i<=n-3;i++) { a[i] = a[n - 2] - a[n - 1]; ans.add(new Tuple(i + 1, n - 1, n)); } } } } else { out.println(-1); continue outer; } /* for(int i=0;i<=n-3;i++) { if(a[i]<=a[i+1]) //increasing continue; else { if(a[i+1]>=0) //pos { if (a[i + 1] > a[i + 2]) //de { out.println(-1); continue outer; } else //ii { a[i] = a[i + 1] - a[i + 2]; ans.add(new Tuple(i + 1, i + 2, i + 3)); } } else // a(i+1)<0 { if (a[i+2]<0) //de { out.println(-1); continue outer; } else // a[i+2]>=0 { a[i] = a[i + 1] - a[i + 2]; ans.add(new Tuple(i + 1, i + 2, i + 3)); } } } /* for(int j=i;j<=i+3;j++) { if(a[j]>) }*/ out.println(ans.size()); for(Tuple t:ans) { out.println(t.a+" "+t.b+" "+t.c); } } 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 8
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 &lt; y &lt; 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
6ea521ad061df5b41d4aee6ea673d134
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 &lt; y &lt; 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 C { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); StringTokenizer st; int t = Integer.parseInt(br.readLine()); while (t --> 0) { int n = Integer.parseInt(br.readLine()); int[] a = new int[n]; st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) a[i] = Integer.parseInt(st.nextToken()); boolean sorted = true; for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) sorted = false; } if (sorted) { pw.println(0); continue; } boolean pos = true; if (a[n - 2] > a[n - 1] || a[n - 1] < 0) { pw.println(-1); continue; } pw.println(n - 2); for (int i = 0; i < n - 2; i++) { pw.println((i + 1) + " " + (n - 1) + " " + n); } } pw.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 8
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 &lt; y &lt; 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
a2272fa11263f0f90447e26216808617
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 &lt; y &lt; 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.Scanner; import java.util.StringTokenizer; import java.util.*; import java.io.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; public class C { public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); outer:while(t-- >0) { int n = sc.nextInt(); int a[] = sc.readArray(n); int c = 0; if(check(a)) { System.out.println(0); continue outer; } if((a[n-2]-a[n-1])>a[n-2]||a[n-2]>a[n-1]) { System.out.println(-1); continue outer; } //always possible 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[]) { for(int i=0;i<a.length-1;i++) { if(a[i]>a[i+1]) return false; } return true; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } int [] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] = Integer.parseInt(next()); } 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; } } static void debugInt(int[] arr) { for(int i=0;i<arr.length;++i) System.out.print(arr[i]+" "); System.out.println(); } static void debugIntInt(int[][] arr) { for(int i=0;i<arr.length;++i) { for(int j=0;j<arr[0].length;++j) System.out.print(arr[i][j]+" "); System.out.println(); } System.out.println(); } static void debugLong(long[] arr) { for(int i=0;i<arr.length;++i) System.out.print(arr[i]+" "); System.out.println(); } static void debugLongLong(long[][] arr) { for(int i=0;i<arr.length;++i) { for(int j=0;j<arr[0].length;++j) System.out.print(arr[i][j]+" "); System.out.println(); } System.out.println(); } static int MOD=(int)(1e9+7); static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } public static void debugArrayList(ArrayList<Integer> arr) { for(int i=0;i<arr.size();i++){ System.out.print(arr.get(i)+" "); } System.out.println(); } static void sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { //if (prime[i] == true) //add it to HashSet } } static boolean isPalindrome(String s) { for(int i = 0,j=s.length()-1;i<j;i++,j--) { if(s.charAt(i)!=s.charAt(j)) return false; } return true; } 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); } /* if RTE inside for loop: - check for increment/decrement in loop, (i++,i--) if after custom sorting an array using lamda , numbers are messed up - check the order in which input is taken */ }
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 8
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 &lt; y &lt; 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
917ef6009625248383143b4b1680f57b
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 &lt; y &lt; 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.*; public class Codechef { static FR sc = new FR(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) { int tc = 1; tc = sc.nextInt(); while(tc-->0) { int n = sc.nextInt(); long arr[] = sc.longArray(n); solve(arr, n); sb.append("\n"); } System.out.println(sb); } public static void solve(long[] arr, int n){ if(arr[n-1]<arr[n-2]){ sb.append("-1"); return; } int j=1; for(j=1; j<n; j++){ if(arr[j]<arr[j-1]) break; } if(j==n){ sb.append("0"); return; } if(arr[n-1]<0){ sb.append("-1"); return; } sb.append(n-2); String s=""+(n-1)+" "+n; for(int i=n-3; i>=0; i--){ sb.append("\n"+(i+1)+" "+s); } } /* ***************************************************************************************************************************************************/ static class FR{ BufferedReader br; StringTokenizer st; public FR() { 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()); } int[] intArray(int n){ int a[] = new int[n]; for(int i=0; i<n; i++){ a[i] = Integer.parseInt(next()); } return a; } long[] longArray(int n){ long a[] = new long[n]; for(int i=0; i<n; i++){ a[i] = Long.parseLong(next()); } return a; } String[] stringArray(int n){ String a[] = new String[n]; for(int i=0; i<n; i++){ a[i] = next(); } return a; } void printArry(int a[]){ for(int i=0; i<a.length; i++){ System.out.print(a[i] + " "); } System.out.println(); } void printArry(long a[]){ for(int i=0; i<a.length; i++){ System.out.print(a[i] + " "); } System.out.println(); } void printArry(String a[]){ for(int i=0; i<a.length; i++){ System.out.print(a[i] + " "); } System.out.println(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long mod = (long)(1e9 + 7); static void sort(long[] arr ) { ArrayList<Long> al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr ) { ArrayList<Integer> al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static int UB(long[] arr , long find , int l , int r) { while(l<=r) { int m = (l+r)/2; if(arr[m]<find) l = m+1; else r = m-1; } return l; } static int LB(long[] arr , long find,int l ,int r ) { while(l<=r) { int m = (l+r)/2; if(arr[m] > find) r = m-1; else l = m+1; } return r; } static int UB(int[] arr , long find , int l , int r) { while(l<=r) { int m = (l+r)/2; if(arr[m]<find) l = m+1; else r = m-1; } return l; } static int LB(int[] arr , long find,int l ,int r ) { while(l<=r) { int m = (l+r)/2; if(arr[m] > find) r = m-1; else l = m+1; } return r; } 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[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i); } static long[][] ncr(int n, int k) { long C[][] = new long[n + 1][k + 1]; int i, j; // Calculate value of Binomial // Coefficient in bottom up manner for (i = 0; i <= n; i++) { for (j = 0; j <= Math.min(i, k); j++) { // Base Cases if (j == 0 || j == i) C[i][j] = 1; // Calculate value using // previously stored values else C[i][j] = (C[i - 1][j - 1] + C[i - 1][j])%mod; } } return C; } static long modInverse(long a, long m) { long g = gcd(a, m); return power(a, m - 2, m); } static long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (int)((p * (long)p) % m); if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); } static void dfs(int[][] grid, int u, boolean[] visited){ visited[u] = true; for(int i=0; i<grid.length; i++){ if(!visited[i] && grid[u][i]==1){ dfs(grid, i, visited); } } } static boolean[] segSieve(int low, int high, List<Integer> prime){ if(low==1) low++; boolean isPrime[] = new boolean[high-low+1]; Arrays.fill(isPrime, true); for(int i: prime){ if(i*i>high) break; int smallest_multiple = (low/i)*i; if(smallest_multiple<low) smallest_multiple+=i; for(; smallest_multiple<=high; smallest_multiple+=i){ if(smallest_multiple!=i) isPrime[smallest_multiple-low] = false; } } return isPrime; } static List<Integer> sieve(int n){ List<Integer> prime = new ArrayList<>(); 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]){ for(int j=i*i; j<=n; j+=i){ isPrime[j] = false; } } } for(int i=0; i<=n; i++){ if(isPrime[i]) prime.add(i); } return prime; } } class Pair{ int b; int c; Pair(int b, int c){ this.b = b; this.c = 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 8
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 &lt; y &lt; 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
7b52128e11466b09c86406d80e414631
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 &lt; y &lt; 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.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class Main { static class Reader { BufferedReader r; StringTokenizer str; Reader() { r=new BufferedReader(new InputStreamReader(System.in)); } Reader(String fileName) throws FileNotFoundException { r=new BufferedReader(new FileReader(fileName)); } public String read() throws IOException { return r.readLine(); } public String getNextToken() throws IOException { if(str==null||!str.hasMoreTokens()) { str=new StringTokenizer(r.readLine()); } return str.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(getNextToken()); } public long nextLong() throws IOException { return Long.parseLong(getNextToken()); } public Double nextDouble() throws IOException { return Double.parseDouble(getNextToken()); } public String nextString() throws IOException { return getNextToken(); } public int[] intArray(int n) throws IOException { int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=nextInt(); return a; } public long[] longArray(int n) throws IOException { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } public String[] stringArray(int n) throws IOException { String a[]=new String[n]; for(int i=0;i<n;i++) a[i]=nextString(); return a; } } public static void main(String args[]) throws IOException{ Reader r=new Reader(); PrintWriter pr=new PrintWriter(System.out); int t=r.nextInt(); while(t-->0){ int n=r.nextInt(); int a[]=r.intArray(n); int min[]=new int[n]; int ind[][]=new int[n][2]; List<String> ans=new ArrayList<>(); min[n-1]=Integer.MAX_VALUE; int maxSoFar=n-1; ind[n-1][0]=n-1; ind[n-1][1]=n-1; for(int i=n-2;i>=0;i--){ if(a[i]-a[maxSoFar]<min[i+1]){ ind[i][0]=i; ind[i][1]=maxSoFar; min[i]=a[i]-a[maxSoFar]; }else { ind[i][0]=ind[i+1][0]; ind[i][1]=ind[i+1][1]; min[i]=min[i+1]; } if(a[maxSoFar]<a[i]){ maxSoFar=i; } } for(int i=0;i+2<n;i++){ int x=min[i+1]; if(a[i]<x) continue; a[i]=x; ans.add((i+1)+" "+(ind[i+1][0]+1)+" "+(ind[i+1][1]+1)); } boolean isAns=true; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1]){ isAns=false; break; } } if(!isAns){ pr.println("-1"); } else{ pr.println(ans.size()); for(int i=0;i<ans.size();i++){ pr.println(ans.get(i)); } } } pr.flush(); pr.close(); } static class Pair{ int val; int ind; public Pair(int val, int ind){ this.val=val; this.ind=ind; } } }
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 8
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 &lt; y &lt; 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
2b33787d9592479d514695cdf2d3104b
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 &lt; y &lt; 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 CodeForces { class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader(new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { 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; } } void shuffle(int a[], int n) { for (int i = 0; i < n; i++) { int t = (int) Math.random() * a.length; int x = a[t]; a[t] = a[i]; a[i] = x; } } long lcm(long a, long b) { long val = a * b; return val / gcd(a, b); } void swap(long a[], long[] b, int i) { long temp = a[i]; a[i] = b[i]; b[i] = temp; } long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } long mod = (long) 1e9 + 7, c; FastReader in; PrintWriter o; public static void main(String[] args) throws IOException { new CodeForces().run(); } void run() throws IOException { in = new FastReader(); OutputStream op = System.out; o = new PrintWriter(op); int t; t = 1; t = in.nextInt(); for (int i = 1; i <= t; i++) { helper(); o.println(); } o.flush(); o.close(); } public void helper() { int n = in.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=in.nextInt(); boolean f=false; for(int i=1;i<n;i++) { if(a[i]<a[i-1]) f=true; } if(!f){ o.print(0); return; } if(a[n-2]>a[n-1]||(a[n-2]<0 && a[n-1]<0)){ o.print(-1);return; } o.println(n-2); for(int i=0;i<n-2;i++) o.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 8
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 &lt; y &lt; 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
961bb45b263b00027efe2e821a91a170
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 &lt; y &lt; 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.ArrayList; import java.util.Collections; import java.util.Scanner; import static java.lang.Math.max; public class main { public static void main(String[] args){ Scanner input = new Scanner(System.in); int n = input.nextInt(); for (int i = 0; i<n;i++){ int m = input.nextInt(); int res = 0; ArrayList<Integer> arr = new ArrayList<Integer>(); int u = 0; arr.add(input.nextInt()); for (int j=1;j<m;j++){ arr.add(input.nextInt()); if (arr.get(j) < arr.get(j - 1)){ u++; } } if (u==0){ res = 0; System.out.println(res); } else if ((arr.get(m - 2) > arr.get(m - 1)) || arr.get(m - 2)<0 && arr.get(m - 1)<0){ res = -1; System.out.println(res); } else { System.out.println(m-2); for (int j=0;j<m-2;j++){ System.out.println(j+1 + " " + (m-1) + " " + m); } } /* for (int j=1;j<m-1;j++) { if (arr.get(j) > arr.get(j - 1)){ if (arr.get(j) > arr.get(j + 1)) { locmax.set(j, 1); } } }*/ /*for (int j = 1; j<m;j++){ } System.out.println(res); for (int j=0;j<m;j++){ System.out.print(arr.get(j) + " "); } 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 8
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 &lt; y &lt; 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
dedac00c05b39d2809d5524c6a30bbf8
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 &lt; y &lt; 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 a { public static void main(String[] args){ FastScanner sc = new FastScanner(); int t = sc.nextInt(); StringBuilder ans = new StringBuilder(); while(t-- > 0){ int n = sc.nextInt(); long arr[] = new long[n]; for(int i=0; i<n; i++){ arr[i] = sc.nextLong(); } boolean isSorted = true; for(int i=0; i<n-1; i++){ if(arr[i] > arr[i+1]){ isSorted = false; break; } } if(isSorted){ ans.append(0 + "\n"); continue; } if(arr[n-1] < 0){ ans.append("-1\n"); continue; } if(arr[n-2] <= arr[n-1]){ ans.append((n-2) + "\n"); for(int i=0; i<n-2; i++){ ans.append((i+1) + " " + (n-1) + " " + n + "\n"); } } else{ ans.append("-1\n"); } } System.out.println(ans); } } 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 8
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 &lt; y &lt; 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
545cbf3b48f71b1b1cb6bf68301032bf
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 &lt; y &lt; 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 Codechef { public static void main (String[] args) throws java.lang.Exception { try{ FastReader read=new FastReader(); StringBuilder sb = new StringBuilder(); long max = (long)Math.pow(10,18); int t=read.nextInt(); while(t>0) { int n = read.nextInt(); int ans = 0; long a[] = new long[n]; for(int i=0;i<n;i++) { a[i]=read.nextLong(); } if(a[n-1]<a[n-2]) { sb.append("-1"); sb.append('\n'); } else if(a[n-1]<0) { boolean flag = true; for(int i=0;i<n-1;i++) { if(a[i]>a[i+1]) { flag = false; break; } } if(flag) { sb.append("0"); sb.append('\n'); } else { sb.append("-1"); sb.append('\n'); } } else { int p = n, f = n-1; StringBuilder sb1 = new StringBuilder(); int count=0; for(int i=n-3;i>=0;i--) { if(a[i]>a[i+1]) { count++; sb1.append((i+1)+" "+(i+2)+" "+p); sb1.append('\n'); a[i] = a[i+1]-a[p-1]; if(a[i]>0) { p=i+1; } } } if(Math.abs(a[0])>=max) { sb.append("-1"); sb.append('\n'); } else { sb.append(count); sb.append('\n'); sb.append(sb1); } } t--; } System.out.println(sb); } catch(Exception e) {return; } } 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 n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(ArrayList<Long> A,char ch) { int n = A.size(); Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = A.get(i); int randomPos = i + rnd.nextInt(n - i); A.set(i,A.get(randomPos)); A.set(randomPos,tmp); } Collections.sort(A); } static void sort(ArrayList<Integer> A) { int n = A.size(); Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = A.get(i); int randomPos = i + rnd.nextInt(n - i); A.set(i,A.get(randomPos)); A.set(randomPos,tmp); } Collections.sort(A); } static String sort(String s) { Character ch[] = new Character[s.length()]; for (int i = 0; i < s.length(); i++) { ch[i] = s.charAt(i); } Arrays.sort(ch); StringBuffer st = new StringBuffer(""); for (int i = 0; i < s.length(); i++) { st.append(ch[i]); } return st.toString(); } } class pair implements Comparable<pair> { int X,Y,C; pair(int s,int e,int c) { X=s; Y=e; C=c; } public int compareTo(pair p ) { return this.C-p.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 8
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 &lt; y &lt; 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
046199d57d2fb6590fd21d8c714412ec
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 &lt; y &lt; 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 class Pair implements Comparable < Pair > { int d; int i; Pair(int d, int i) { this.d = d; this.i = i; } public int compareTo(Pair o) { return this.d - o.d; } } public static class SegmentTree { long[] st; long[] lazy; int n; SegmentTree(long[] arr, int n) { this.n = n; st = new long[4 * n]; lazy = new long[4 * n]; construct(arr, 0, n - 1, 0); } public long construct(long[] arr, int si, int ei, int node) { if (si == ei) { st[node] = arr[si]; return arr[si]; } int mid = (si + ei) / 2; long left = construct(arr, si, mid, 2 * node + 1); long right = construct(arr, mid + 1, ei, 2 * node + 2); st[node] = left + right; return st[node]; } public long get(int l, int r) { return get(0, n - 1, l, r, 0); } public long get(int si, int ei, int l, int r, int node) { if (r < si || l > ei) return 0; if (lazy[node] != 0) { st[node] += lazy[node] * (ei - si + 1); if (si != ei) { lazy[2 * node + 1] += lazy[node]; lazy[2 * node + 2] += lazy[node]; } lazy[node] = 0; } if (l <= si && r >= ei) return st[node]; int mid = (si + ei) / 2; return get(si, mid, l, r, 2 * node + 1) + get(mid + 1, ei, l, r, 2 * node + 2); } public void update(int index, int value) { update(0, n - 1, index, 0, value); } public void update(int si, int ei, int index, int node, int val) { if (si == ei) { st[node] = val; return; } int mid = (si + ei) / 2; if (index <= mid) { update(si, mid, index, 2 * node + 1, val); } else { update(mid + 1, ei, index, 2 * node + 2, val); } st[node] = st[2 * node + 1] + st[2 * node + 2]; } public void rangeUpdate(int l, int r, int val) { rangeUpdate(0, n - 1, l, r, 0, val); } public void rangeUpdate(int si, int ei, int l, int r, int node, int val) { if (r < si || l > ei) return; if (lazy[node] != 0) { st[node] += lazy[node] * (ei - si + 1); if (si != ei) { lazy[2 * node + 1] += lazy[node]; lazy[2 * node + 2] += lazy[node]; } lazy[node] = 0; } if (l <= si && r >= ei) { st[node] += val * (ei - si + 1); if (si != ei) { lazy[2 * node + 1] += val; lazy[2 * node + 2] += val; } return; } int mid = (si + ei) / 2; rangeUpdate(si, mid, l, r, 2 * node + 1, val); rangeUpdate(mid + 1, ei, l, r, 2 * node + 2, val); st[node] = st[2 * node + 1] + st[2 * node + 2]; } } 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 void main(String[] args) throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { try { System.setIn(new FileInputStream(new File("input.txt"))); System.setOut(new PrintStream(new File("output.txt"))); } catch (Exception e) { } } Reader sc = new Reader(); int tc = sc.nextInt(); StringBuilder sb = new StringBuilder(); while (tc-- > 0) { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } long[] maxi = new long[n]; long[] mini = new long[n]; long max = Integer.MIN_VALUE; int mxi = n; for (int i = n - 1; i >= 0; i--) { maxi[i] = mxi; if (arr[i] > max) { max = arr[i]; mxi = i; } } mxi = n; long min = Integer.MAX_VALUE; for (int i = n - 1; i >= 0; i--) { mini[i] = mxi; if (arr[i] < min) { min = arr[i]; mxi = i; } } boolean flag = true; for (int i = 1; i < n; i++) { if (arr[i] < arr[i - 1]) { flag = false; } } if (flag) { sb.append("0" + "\n"); } else if ((arr[n - 2] > arr[n - 1]) || ((arr[n - 2] - arr[n - 1]) > arr[n - 2]) || ((arr[n - 2] - arr[n - 1]) > arr[n - 1])) { sb.append("-1" + "\n"); } else { sb.append(n - 2 + "\n"); for (int i = 0; i < n - 2; i++) { sb.append((i + 1) + " " + (n - 1) + " " + (n)); sb.append("\n"); } } } System.out.println(sb); } static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop 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 gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a - b, b); return gcd(a, b - a); } public static int Xor(int n) { if (n % 4 == 0) return n; // If n%4 gives remainder 1 if (n % 4 == 1) return 1; // If n%4 gives remainder 2 if (n % 4 == 2) return n + 1; // If n%4 gives remainder 3 return 0; } public static long pow(long a, long b, long mod) { long res = 1; while (b > 0) { if ((b & 1) > 0) res = (res * a) % mod; b = b >> 1; a = ((a % mod) * (a % mod)) % mod; } return (res % mod + mod) % mod; } public static boolean isEqual(ArrayList<Integer> a, ArrayList<Integer> b, int n) { HashSet<Integer> hs = new HashSet<>(); if (a.size() < (n / 2) || b.size() < (n / 2)) return false; int s1 = 0; for (int ele : a) hs.add(ele); for (int ele : b) hs.add(ele); if (hs.size() == n) return true; return false; } public static double digit(long num) { return Math.floor(Math.log10(num) + 1); } public static int count(ArrayList<Integer> al, int num) { if (al.get(al.size() - 1) == num) return 0; int k = al.get(al.size() - 1); ArrayList<Integer> temp = new ArrayList<>(); for (int i = 0; i < al.size(); i++) { if (al.get(i) > k) temp.add(al.get(i)); } return 1 + count(temp, num); } public static String Util(String s) { for (int i = s.length() - 1; i >= 1; i--) { int l = s.charAt(i - 1) - '0'; int r = s.charAt(i) - '0'; if (l + r >= 10) { return s.substring(0, i - 1) + (l + r) + s.substring(i + 1); } } int l = s.charAt(0) - '0'; int r = s.charAt(1) - '0'; return (l + r) + s.substring(2); } public static boolean isPos(int idx, long[] arr, long[] diff) { if (idx == 0) { for (int i = 0; i <= Math.min(arr[0], arr[1]); i++) { diff[idx] = i; arr[0] -= i; arr[1] -= i; if (isPos(idx + 1, arr, diff)) { return true; } arr[0] += i; arr[1] += i; } } else if (idx == 1) { if (arr[2] - arr[1] >= 0) { long k = arr[1]; diff[idx] = k; arr[1] = 0; arr[2] -= k; if (isPos(idx + 1, arr, diff)) { return true; } arr[1] = k; arr[2] += k; } else return false; } else { if (arr[2] == arr[0] && arr[1] == 0) { diff[2] = arr[2]; return true; } else { return false; } } return false; } public static boolean isPal(String s) { for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != s.charAt(s.length() - 1 - i)) return false; } return true; } static int upperBound(ArrayList<Long> arr, long key) { int mid, N = arr.size(); // Initialise starting index and // ending index int low = 0; int high = N; // Till low is less than high while (low < high && low != N) { // Find the index of the middle element mid = low + (high - low) / 2; // If key is greater than or equal // to arr[mid], then find in // right subarray if (key >= arr.get(mid)) { low = mid + 1; } // If key is less than arr[mid] // then find in left subarray else { high = mid; } } // If key is greater than last element which is // array[n-1] then upper bound // does not exists in the array return low; } static int lowerBound(ArrayList<Long> array, long key) { // Initialize starting index and // ending index int low = 0, high = array.size(); int mid; // Till high does not crosses low while (low < high) { // Find the index of the middle element mid = low + (high - low) / 2; // If key is less than or equal // to array[mid], then find in // left subarray if (key <= array.get(mid)) { high = mid; } // If key is greater than array[mid], // then find in right subarray else { low = mid + 1; } } // If key is greater than last element which is // array[n-1] then lower bound // does not exists in the array if (low < array.size() && array.get(low) < key) { low++; } // Returning the lower_bound index return low; } }
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 8
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 &lt; y &lt; 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
8b821e0853ed51ff4409827d152f01fa
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 &lt; y &lt; 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.BufferedWriter; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; public class A { static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int [n1]; int R[] = new int [n2]; for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static void sort(int arr[], int l, int r) { if (l < r) { int m = (l+r)/2; sort(arr, l, m); sort(arr , m+1, r); merge(arr, l, m, r); } } static void revOrder(int[] arr) { int[] a = new int[arr.length]; for(int i=0;i<arr.length;i++) { a[i] = arr[arr.length-i-1]; } for(int i=0;i<arr.length;i++) arr[i] = a[i]; } static int next(long target, long[] arr) { int start = 0, end = arr.length - 1; int ans = -1; while (start < end) { int mid = start + (end-start) / 2; if (arr[mid] <=target) { start = mid + 1; ans = mid; } else { end = mid - 1; } } return ans; } static void pf(int n, ArrayList<Integer> al) { for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { if (n/i == i) { al.add(i); } else{ al.add(i); al.add(n/i); } } } } static 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; } static int gcd(int a, int b) { if (a == 0) return b; if (b == 0) return a; int k; for (k = 0; ((a | b) & 1) == 0; ++k) { a >>= 1; b >>= 1; } while ((a & 1) == 0) a >>= 1; do { while ((b & 1) == 0) b >>= 1; if (a > b) { int temp = a; a = b; b = temp; } b = (b - a); } while (b != 0); return a << k; } static long inv(long a, long m){ return power(a, m-2, m); } static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop 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 DecimalFormat df = new DecimalFormat("0.0000000000"); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { BufferInput in = new BufferInput(); try { int t = in.nextInt(); while(t-->0) { int n = in.nextInt(); int[] arr = in.nextIntArray(n); int ind = -1; for(int i=n-2;i>=0;i--) { if(arr[i+1]<arr[i]) { ind = i; break; } } if(ind==-1) { out.println("0"); } else if(ind==n-2) { out.println("-1"); } else { int min = arr[n-2]-arr[n-1]; Pair a = new Pair(n-1, n); for(int i=ind+1;i<n-1;i++) { if(arr[i]-arr[n-1]<min && arr[i]-arr[n-1]<=arr[i]) { min = arr[i]-arr[n-1]; a = new Pair(i+1, n); } } if(min>arr[n-2]) { out.println("-1"); } else { out.println(ind+1); for(int i=0;i<ind+1;i++) { out.println(i+1 + " " + a.x + " " + a.y); } } } } }catch(Exception e) {} out.close(); } static class Pair implements Comparable<Pair> { int x;int y; Pair(int i,int o) { x=i;y=o; } public int compareTo(Pair n) { if(this.x!=n.x) return this.x-n.x; return this.y-n.y; } } static class BufferInput { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public BufferInput() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public BufferInput(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]; int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public String nextString() throws IOException{ byte c = read(); while(Character.isWhitespace(c)){ c = read(); } StringBuilder builder = new StringBuilder(); builder.append((char)c); c = read(); while(!Character.isWhitespace(c)){ builder.append((char)c); c = read(); } return builder.toString(); } 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 int[] nextIntArray(int n) throws IOException { int arr[] = new int[n]; for(int i = 0; i < n; i++){ arr[i] = nextInt(); } return arr; } 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 long[] nextLongArray(int n) throws IOException { long arr[] = new long[n]; for(int i = 0; i < n; i++){ arr[i] = nextLong(); } return arr; } public char nextChar() throws IOException{ byte c = read(); while(Character.isWhitespace(c)){ c = read(); } return (char) c; } 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; } public double[] nextDoubleArray(int n) throws IOException { double arr[] = new double[n]; for(int i = 0; i < n; i++){ arr[i] = nextDouble(); } return arr; } 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 8
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 &lt; y &lt; 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
db0b546763c3ac113e05ac2967cb35e1
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 &lt; y &lt; 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 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader( new FileReader("input.rtf")); PrintStream out = new PrintStream(new FileOutputStream("output.rtf")); System.setOut(out); } catch (Exception e) { 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 read = new FastReader(); int t = read.nextInt(); while(t > 0){ int length = read.nextInt(); long[] arr = new long[length]; for(int i = 0; i < length; i++) arr[i] = read.nextLong(); boolean ind = true; for(int i = 0; i < arr.length - 1; i++){ if(arr[i+1] < arr[i]){ ind = false; break; } } if(ind) System.out.println(0); else if(arr[length-2] > arr[length-1] || arr[length-1] < 0) System.out.println(-1); else{ System.out.println(length-2); for(int i = 0; i < length-2; i++) System.out.println((i+1) + " " + (length - 1) + " " + length); } t--; } } }
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 8
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 &lt; y &lt; 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
1ab1de9bc31398481894f767f81f034a
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 &lt; y &lt; 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.util.stream.LongStream; import java.io.*; import java.math.*; public class Main{ public static void main(String[] args) { FastReader fr = new FastReader(); PrintWriter out = new PrintWriter(System.out); Scanner sc= new Scanner (System.in); //Code From Here---- StringBuilder answer = new StringBuilder(); int t = fr.nextInt(); for (int tt = 1; tt <=t; tt++) { int n = fr.nextInt(); long [] arr = new long [n]; fr.readArray(arr); if(arr[n-2]>arr[n-1]) answer.append("-1\n"); else if(isSorted(arr)) answer.append("0\n"); else if(arr[n-1]<0) answer.append("-1\n"); else{ answer.append((n-2)+"\n"); for (int i = 1; i < arr.length-1; i++) { answer.append(i+" "+(n-1)+" "+(n)+"\n"); } } } out.println(answer.toString()); out.flush(); sc.close(); } public static boolean isSorted(long [] arr ) { for (int i = 1; i < arr.length; i++) { if(arr[i]<arr[i-1]) return false; } return true; } public static TreeSet<Long> maxPrimeFactors(long n) { TreeSet <Long> set = new TreeSet<>(); long maxPrime = -1; while (n % 2 == 0) { maxPrime=2; n >>= 1; } while (n % 3 == 0) { maxPrime=3; n = n / 3; } for (long i = 5; i <= Math.sqrt(n); i += 6) { while (n % i == 0) { maxPrime=i; n = n / i; } while (n % (i + 2) == 0) { set.add((long)(i+2)); n = n / (i + 2); } } if (n > 4) { set.add((long)n); } return set; } public static int nSquaresFor(int n) { //Sum of Perfect Square Minimum ....... //Legendre's three-square theorem states that a natural number can be represented as the sum of three squares of integers //if and only if n is not of the form n = 4^a * (8b+7) int sqrt = (int)Math.sqrt(n); if(sqrt*sqrt==n) return 1; while ((n&3)==0) n>>=2; if((n&7)==7) return 4; int x = (int)Math.sqrt(n); for(int i =1;i<=x;i++) { int temp = n-(i*i); int tempsqrt = (int)Math.sqrt(temp); if(tempsqrt*tempsqrt == temp) return 2; } return 3; } public static BigInteger Factorial(BigInteger number){ if(number.compareTo(BigInteger.ONE)!=1) { return BigInteger.ONE; } return number.multiply(Factorial(number.subtract(BigInteger.ONE))); } private static boolean isPrime(int count) { if(count==1) return false; if(count==2 || count ==3) return true; if(count%2 == 0) return false; if(count%3 ==0) return false; for (int i = 5; i*i<=count; i+=6) { if(count%(i)==0 || count%(i+2)==0) return false;} return true; } static ArrayList<Integer> sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. boolean prime[] = new boolean[n + 1]; for (int i = 0; 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; } } ArrayList <Integer> list = new ArrayList<>(); // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) list.add(i); } return list; } //This RadixSort() is for long method public static long[] radixSort(long[] f){ return radixSort(f, f.length); } public static long[] radixSort(long[] f, int n) { long[] to = new long[n]; { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>16&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>16&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>32&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>32&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>48&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>48&0xffff)]++] = f[i]; long[] d = f; f = to; to = d; } return f; } 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 int lowerBound(long a[], long 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(long a[], long x) {// x is the key or target value int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1L; if (a[m] <= x) l = m; else r = m; } return l + 1; } static int upperBound(long[] arr, int start, int end, long k) { int N = end; while (start < end) { int mid = start + (end - start) / 2; if (k >= arr[mid]) { start = mid + 1; } else { end = mid; } } if (start < N && arr[start] <= k) { start++; } return start; } static long lowerBound(long[] arr, int start, int end, long k) { int N = end; while (start < end) { int mid = start + (end - start) / 2; if (k <= arr[mid]) { end = mid; } else { start = mid + 1; } } if (start < N && arr[start] < k) { start++; } return start; } // H.C.F. // For Fast Factorial public static long factorialStreams( long n ) { return LongStream.rangeClosed( 1, n ) .reduce(1, ( long a, long b ) -> a * b); } // Binary Exponentiation public static long FastExp(long base, long exp) { long ans=1; long mod=998244353; while (exp>0) { if (exp%2==1) ans*=base; exp/=2; base*=base; base%=mod; ans%=mod; } return ans; } // H.C.F. public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } // H.C.F. by Binary Method(Eucladian Algorithm) private static int gcdBinary(int x, int y) { int shift; /* GCD(0, y) == y; GCD(x, 0) == x, GCD(0, 0) == 0 */ if (x == 0) return y; if (y == 0) return x; int gcdX = Math.abs(x); int gcdY = Math.abs(y); if (gcdX == 1 || gcdY == 1) return 1; /* Let shift := lg K, where K is the greatest power of 2 dividing both x and y. */ for (shift = 0; ((gcdX | gcdY) & 1) == 0; ++shift) { gcdX >>= 1; gcdY >>= 1; } while ((gcdX & 1) == 0) gcdX >>= 1; /* From here on, gcdX is always odd. */ do { /* Remove all factors of 2 in gcdY -- they are not common */ /* Note: gcdY is not zero, so while will terminate */ while ((gcdY & 1) == 0) /* Loop X */ gcdY >>= 1; /* * Now gcdX and gcdY are both odd. Swap if necessary so gcdX <= gcdY, * then set gcdY = gcdY - gcdX (which is even). For bignums, the * swapping is just pointer movement, and the subtraction * can be done in-place. */ if (gcdX > gcdY) { final int t = gcdY; gcdY = gcdX; gcdX = t; } // Swap gcdX and gcdY. gcdY = gcdY - gcdX; // Here gcdY >= gcdX. }while (gcdY != 0); /* Restore common factors of 2 */ return gcdX << shift; } // Check For Palindrome public static boolean isPalindrome(String s) { return s.equals( new StringBuilder(s).reverse().toString()); } // For Fast Input ---- 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()); } int[] readArray(int n) { int [] arr = new int [n]; for (int i = 0; i < arr.length; i++) { arr[i]=nextInt(); } return arr; } long[] readArray(long []arr) { for (int i = 0; i < arr.length; i++) { arr[i]=nextLong(); } return arr; } 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 8
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 &lt; y &lt; 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
57a5277204ff0e7daf7d5f744ca85757
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 &lt; y &lt; 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 DifferentialSorting { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numTests = sc.nextInt(); for (int test = 0; test < numTests; test++) { int numElems = sc.nextInt(); int[] arr = new int[numElems]; arr[0] = sc.nextInt(); boolean alreadySorted = true; for (int i = 1; i < numElems; i++) { arr[i] = sc.nextInt(); if (arr[i] < arr[i - 1]) { alreadySorted = false; } } // no way we can sort this. print -1 if (arr[numElems - 2] > arr[numElems - 1]) { System.out.println(-1); } else if (arr[numElems - 2] - arr[numElems - 1] > arr[numElems - 2] && !alreadySorted) { System.out.println(-1); } else if (alreadySorted) { System.out.println(0); } else { System.out.println(numElems - 2); for (int i = 1; i <= numElems - 2; i++) { System.out.println(i + " " + (numElems - 1) + " " + numElems); } } } } }
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 8
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 &lt; y &lt; 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
1f9e2dd6e8333e334463207ff0a10612
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 &lt; y &lt; 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 DifferentialSorting { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine().split(" ")[0]); PrintWriter pr = new PrintWriter(System.out); while (t > 0) { t--; String[] ints = br.readLine().split(" "); int n = Integer.parseInt(ints[0]); ints = br.readLine().split(" "); List<Integer> a = new ArrayList<>(); List<Integer> b = new ArrayList<>(); for(int i = 0;i<n;i++){ int temp = Integer.parseInt(ints[i]); a.add(temp); b.add(temp); } boolean isSorted = true; for(int i = 0;i<n-1;i++){ if (a.get(i) > a.get(i+1)) isSorted = false; } if (isSorted){ pr.println(0); continue; } if (a.get(n-1) < 0 || a.get(n-1) < a.get(n-2)) { pr.println(-1); continue; } for(int i = n-3;i>=0;i--){ a.set(i, a.get(n-2) - a.get(n-1)); } pr.println(n-2); for(int i = 0;i<n-2;i++){ int n1 = n-1; int n0 = n; int ii = i+1; String s = ii + " " + n1 +" " + n0; pr.println(s); } } pr.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 8
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 &lt; y &lt; 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
9d33201160afb43e0ccbc2769b246dc8
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 &lt; y &lt; 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.text.DecimalFormat; import java.util.*; public class Codeforces { static long mod = 1000000007 ; public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); int t=fs.nextInt(); outer:while(t-->0) { int n=fs.nextInt(); long arr[]=new long[n+1]; for(int i=1;i<=n;i++) arr[i]=fs.nextInt(); // int arr[]=fs.readArray(n); if(arr[n-1]>arr[n]) { out.println(-1); continue outer; } List<Integer> ans=new ArrayList<>(n*3); int y=n-1, z=n; for(int i=n-2;i>=1;i--) { if(arr[i]>arr[i+1]) { arr[i]= arr[y]-arr[z]; ans.add(i); ans.add(y);ans.add(z); } if(arr[i]>arr[i+1]) { out.println(-1); continue outer; } if(arr[i]<arr[y]) { y=i; } } out.println(ans.size()/3); for(int i=0;i<ans.size();i+=3) { out.println(ans.get(i)+" "+ans.get(i+1)+" "+ans.get(i+2)); } } out.close(); } static long pow(long a,long b) { if(b<0) return 1; long res=1; while(b!=0) { if((b&1)!=0) { res*=a; res%=mod; } a*=a; a%=mod; b=b>>1; } return res; } static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } static long nck(int n,int k) { if(k>n) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { // return fact[(int)n]; long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle 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; } //then sort Arrays.sort(a); } static void sort(long[] a) { //suffle 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; } //then sort Arrays.sort(a); } // Use this to input code since it is faster than a Scanner 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()); } long[] readArrayL(int n) { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } 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 8
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 &lt; y &lt; 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
a17794d4a394c390a09200fd4b4198a2
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 &lt; y &lt; 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 long cr[][]=new long[1001][1001]; //static double EPS = 1e-7; static long mod=1000000007; static long val=0; public static void main(String[] args) { FScanner sc = new FScanner(); //Arrays.fill(prime, true); //sieve(); //ncr(); int t=sc.nextInt(); StringBuilder sb = new StringBuilder(); while(t-->0) { int n=sc.nextInt(); boolean flag=true; long prev=Integer.MIN_VALUE; long arr[]=new long[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); if(arr[i]<prev) { flag=false; } prev=arr[i]; } if(flag) { sb.append(0+"\n"); continue; } if(arr[n-2]>arr[n-1] || arr[n-2]-arr[n-1]>arr[n-2] ) { sb.append("-1"+"\n"); continue; } /* else { i=n-2; while(i>=1) { if(arr[i]-arr[i+1]<=arr[i]) { break; } i--; } } */ sb.append(n-2+"\n"); for(int j=0;j<n-2;j++) { sb.append( (j+1)+" "+(n-1)+" "+(n)+"\n" ); } // sb.append("\n"); } System.out.println(sb.toString()); } public static int check(String s1,String s2) { int diff=0; for(int i=0;i<7;i++) { if(s1.charAt(i)!=s2.charAt(i)) diff++; if(s1.charAt(i)=='0' && s2.charAt(i)=='1') return -1; } return diff; } public static long gcd(long a,long b) { return b==0 ? a:gcd(b,a%b); } /* public static void sieve() { prime[0]=prime[1]=false; int n=1000000; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } */ public static void ncr() { cr[0][0]=1; for(int i=1;i<=1000;i++) { cr[i][0]=1; for(int j=1;j<i;j++) { cr[i][j]=(cr[i-1][j-1]+cr[i-1][j])%mod; } cr[i][i]=1; } } } class pair //implements Comparable<pair> { int a;int b; pair(int a,int b) { this.b=b; this.a=a; } } class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(next()); } double nextDouble(){ return Double.parseDouble(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 8
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 &lt; y &lt; 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
c97aa68758a8ee85736b6714e5d33a25
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 &lt; y &lt; 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.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while(t-- > 0) { int n = scanner.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); } if(arr[arr.length-1] < arr[arr.length-2]) { System.out.println(-1); continue; } int num = arr[arr.length-2] - arr[arr.length-1]; boolean isSorted = true; for(int i = 0; i < n-1; i++) { if(arr[i] > arr[i+1]) { isSorted = false; break; } } if(isSorted) { System.out.println(0); continue; } if(num > arr[arr.length-2] || num > arr[arr.length-1]) { System.out.println(-1); continue; } System.out.println(arr.length-2); for(int i = 0; i < arr.length-2; i++) { System.out.println((i+1)+" "+(arr.length-1)+" "+(arr.length)); } } } }
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 8
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 &lt; y &lt; 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
71564a02bc8d1171f4d3b30951c09812
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 &lt; y &lt; 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 practise; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.*; public class A { static Reader sc; //static Scanner sc; static PrintWriter w; public static void main(String[] args) throws IOException { sc = new Reader(); // sc = new Scanner(System.in); w = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { solve(); } w.close(); } static void solve() throws IOException { 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] ) { w.println(-1); return; } long max=a[n-1]; List<List<Long>> ans= new ArrayList<>(); long ct=0; for(int i=n-3;i>=0;i--) { if(a[i]>a[i+1]) { List<Long>temp= new ArrayList<>(); a[i]=a[i+1]-max; temp.add(1L*i); temp.add(1L*i+1L); temp.add(1L*n-1L); ans.add(temp); ct++; } } if(max<0 && ct>0) { w.println(-1); return; } w.println(ct); if(ct>0) { for(List<Long> t:ans) { w.println( (t.get(0)+1)+" "+(t.get(1)+1)+" "+(t.get(2)+1) ); } } // System.out.println(Arrays.toString(a)); //w.println(); } 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 class FenwickTree{ int tree[]; void contructFenwickTree(int ar[],int n) { tree= new int[n+1]; for(int i=0;i<ar.length;i++) { update(i,ar[i],n); } } void update(int i,int delta,int n) { //delta means newValue - preValue. //i is index of array. i++; while(i<=n) { tree[i]+=delta; i=i+Integer.lowestOneBit(i); } } int getSum(int i ){ int sum=0; i++; while(i>0) { sum+=tree[i]; i-=Integer.lowestOneBit(i); } return sum; } int rangeOfSum(int i,int j) { return getSum(j)-getSum(i-1); } } 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; } 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); } static void sort(long[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Long> ls = new ArrayList<>(); for(long x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } 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; } 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; } static class Reader // here reader class is defined. { 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') break; 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(); } } }
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 8
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 &lt; y &lt; 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
47889e5a3d25e0a60df839ec459ec6e3
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 &lt; y &lt; 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.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); while(n-->0){ int m=sc.nextInt(); int[] a=new int[m]; for(int j=0;j<m;j++){ a[j]=sc.nextInt(); } if(m==1){ System.out.println("0"); } else if(a[m-1]<a[m-2]){ System.out.println("-1"); } else { List<String> list=new ArrayList<>(); int max=m-1,x = 0,y = 0,z = 0,j=m-3; for(;j>=0;j--){ if(a[j+2]>a[max])max=j+2; if(a[j]>a[j+1]){ if(a[j+1]-a[max]>a[j+1]){ System.out.println("-1"); break; } else{ System.out.println(j+1); x=j+1;y=j+1+1;z=max+1; break; // list.add((j+1)+" "+(j+1+1)+" "+(max+1)); } } } if(j==-1) System.out.println("0"); for(int i=1;i<=x;i++){ System.out.println(i+" "+y+" "+z); } } } } } /** * 3 * 5 * 5 -4 2 -1 2 * 3 * 4 3 2 * 3 * -3 -2 -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 8
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 &lt; y &lt; 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
ec802a9c8e6a77c490212c55d3642136
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 &lt; y &lt; 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 cfc { FastScanner scn; PrintWriter w; PrintStream fs; int MOD = 1000000007; int MAX = 200005; long mul(long x, long y) {long res = x * y; return (res >= MOD ? res % MOD : res);} long power(long x, long y) {if (y < 0) return 1; long res = 1; x %= MOD; while (y!=0) {if ((y & 1)==1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;} 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), temp=a[i];a[i]=a[oi];a[oi]=temp;}Arrays.sort(a);} void reverseSort(int[] arr){List<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);}} boolean LOCAL; void debug(Object... o){if(LOCAL)System.err.println(Arrays.deepToString(o));} //SPEED IS NOT THE CRITERIA, CODE SHOULD BE A NO BRAINER boolean is_sorted(int[] ar){ int n=ar.length; for(int i=1;i<n;i++){ if(ar[i]<ar[i-1]){ return false; } } return true; } void solve(){ int t=scn.nextInt(); while(t-->0) { int n=scn.nextInt(); int[] ar=scn.nextIntArray(n); if(ar[n-1]<ar[n-2]){ w.println(-1); continue; }else{ if(ar[n-1]<0&&ar[n-2]<0){ if(is_sorted(ar)){ w.println(0); }else{ w.println(-1); } }else{ w.println(n-2); for(int i=n-3;i>=0;i--){ w.println((i+1)+" "+(n-1)+" "+n); } } } } } void run() { try { long ct = System.currentTimeMillis(); scn = new FastScanner(new File("input.txt")); w = new PrintWriter(new File("output.txt")); fs=new PrintStream("error.txt"); System.setErr(fs); LOCAL=true; solve(); w.close(); System.err.println(System.currentTimeMillis() - ct); } catch (FileNotFoundException e) { e.printStackTrace(); } } void runIO() { scn = new FastScanner(System.in); w = new PrintWriter(System.out); LOCAL=false; solve(); w.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } int lowerBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid-1 >= 0 && arr[mid-1] == arr[mid]){ei = mid-1;}else{return mid;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; } int upperBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid+1 < n && arr[mid+1] == arr[mid]){si = mid+1;}else{return mid + 1;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; } int upperBound(ArrayList<Integer> list, int x){int n = list.size(), si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(list.get(mid) == x){if(mid+1 < n && list.get(mid+1) == list.get(mid)){si = mid+1;}else{return mid + 1;}}else if(list.get(mid) > x){ei = mid - 1; }else{si = mid+1;}}return si; } void swap(int[] arr, int i, int j){int temp = arr[i];arr[i] = arr[j];arr[j] = temp;} long nextPowerOf2(long v){if (v == 0) return 1;v--;v |= v >> 1;v |= v >> 2;v |= v >> 4;v |= v >> 8;v |= v >> 16;v |= v >> 32;v++;return v;} int gcd(int a, int b) {if(a == 0){return b;}return gcd(b%a, a);} // TC- O(logmax(a,b)) boolean nextPermutation(int[] arr) {if(arr == null || arr.length <= 1){return false;}int last = arr.length-2;while(last >= 0){if(arr[last] < arr[last+1]){break;}last--;}if (last < 0){return false;}if(last >= 0){int nextGreater = arr.length-1;for(int i=arr.length-1; i>last; i--){if(arr[i] > arr[last]){nextGreater = i;break;}}swap(arr, last, nextGreater);}int i = last + 1, j = arr.length - 1;while(i < j){swap(arr, i++, j--);}return true;} public static void main(String[] args) { new cfc().runIO(); } }
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 8
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 &lt; y &lt; 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
d58ebab807f8f8347cfe0200eed95b79
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 &lt; y &lt; 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
/*##################################################### ################ >>>> Diaa12360 <<<< ################## ################ Just Nothing ################## ############ If You Need it, Fight For IT; ############ ####################.-. 1 5 9 2 .-.#################### ######################################################*/ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringBuilder out = new StringBuilder(); StringTokenizer tk; int t = Integer.parseInt(in.readLine()); while(t-- > 0){ int n = Integer.parseInt(in.readLine()); int[] arr = new int[n]; tk = new StringTokenizer(in.readLine()); for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(tk.nextToken()); } if (isSorted(arr)) { out.append(0).append('\n'); continue; } if (arr[n - 1] < 0 || arr[n - 1] < arr[n - 2]) { out.append(-1).append('\n'); continue; } out.append(n - 2).append('\n'); for (int i = 1; i <= n - 2; i++) { out.append(i + " " + (n - 1) + " " + n).append('\n'); } } System.out.print(out); } static boolean isSorted(int[] arr) { for (int i = 1; i < arr.length; 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 8
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 &lt; y &lt; 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
6d94a48578e6d35cc138e7d76a50293f
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 &lt; y &lt; 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.util.*; import java.lang.*; import java.io.*; public class Rough_Work { public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-->0) { int n = sc.nextInt(); int [] arr = sc.nextIntArray(n); if (arr[n-2] > arr[n-1]) { out.println(-1); continue; } boolean flag = true; for (int i = 0; i < n-1; i++) { if (arr[i] > arr[i+1]) { flag = false; break; } } if (flag) out.println(0); else { if (arr[n - 2] >= arr[n - 2] - arr[n - 1]) { out.println(n - 2); for (int i = n - 3; i >= 0; i--) { out.println((i+1) + " " + (n - 1) + " " + (n)); } } else out.println(-1); } } sc.close(); out.close(); } static class FastReader { final private int BUFFER_SIZE = 1 << 17; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastReader(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[128]; // 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; } public int[] nextIntArray(int size) throws IOException { int[] arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int size) throws IOException { long[] arr = new long[size]; for (int i = 0; i < size; i++) { arr[i] = nextLong(); } return arr; } public Double[] nextDoubleArray(int size) throws IOException { Double[] arr = new Double[size]; for (int i = 0; i < size; i++) { arr[i] = nextDouble(); } return arr; } public int[][] nextIntMatrix(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { arr[i] = nextIntArray(m); } return arr; } public long[][] nextLongMatrix(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { arr[i] = nextLongArray(m); } return arr; } public ArrayList<Integer> nextIntList(int size) throws IOException { ArrayList<Integer> arrayList = new ArrayList<>(size); for (int i = 0; i < size; i++) { arrayList.add(nextInt()); } return arrayList; } public ArrayList<Long> nextLongList(int size) throws IOException { ArrayList<Long> arrayList = new ArrayList<>(size); for (int i = 0; i < size; i++) { arrayList.add(nextLong()); } return arrayList; } 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 8
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 &lt; y &lt; 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
566f579efd268606916b680a179d3f47
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 &lt; y &lt; 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 C { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = in.nextInt(); for (int tt = 0; tt < t; tt++) { int n = in.nextInt(); long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = in.nextLong(); if (isOk(a, n)) { pw.println(0); continue; } ArrayList<xyz> ans = solve(a, n); if (ans.size() == 0) pw.println(-1); else { pw.println(ans.size()); for (xyz x: ans) pw.println(x); } } pw.close(); } static boolean isOk(long[] a, int n) { for (int i = n; i >= 2; i--) { if (a[i] < a[i - 1]) return false; } return true; } static ArrayList<xyz> solve(long[] a, int n) { ArrayList<xyz> ans = new ArrayList<>(); int min = n - 1; int max = n; if (a[n - 1] > a[n]) return (new ArrayList<>()); for (int i = n - 2; i >= 1; i--) { if (a[min] > a[i + 1]) { min = i + 1; } if (a[max] < a[i + 2] && i != min) { max = i + 2; } long val = a[min] - a[max]; // debug(val); // debug(min, max, val); if (a[i] <= a[i + 1]) { if (a[i] >= val); else { if (val <= a[i + 1]) { a[i] = val; ans.add(new xyz(i, min, max)); } } } else { // debug(a[i], i, val); if (a[i + 1] < val) return (new ArrayList<>()); a[i] = val; ans.add(new xyz(i, min, max)); } } // debug(a); return ans; } static class xyz { int x, y, z; xyz(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } @Override public String toString() { return x + " " + y + " " + z; } } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
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 8
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 &lt; y &lt; 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
b3416229920d88491e9d89464fcdfde4
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 &lt; y &lt; 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 Competitve1; //package compete; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.Array; import java.nio.MappedByteBuffer; //import java.security.acl.LastOwnerException; import java.util.*; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Main { public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); 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[] nextSArray() { String sr[] = null; try { sr = br.readLine().trim().split(" "); } catch (IOException e) { e.printStackTrace(); } return sr; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int root[],size[],tree[],arr[]; static int mod=998244353; static int maxDepth=0; public static void main(String[] args) throws Exception{ FastReader sc=new FastReader(); int t=sc.nextInt(); //int t=1; while(t-->0) { solve(sc); } out.close(); } static long l,r; static long count=0l; static int N; static long dp[]; static int gmex[]; static void solve(FastReader sc) { 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]) { out.println(-1); return; } if(isSorted(arr)) { out.println(0); return; } if(arr[n-1]>=0) { out.println(n-2); for(int i=1;i<n-1;i++) out.println(i+" "+(n-1)+" "+n); }else out.println(-1); } static boolean isSorted(long arr[]) { for(int i=1;i<arr.length;i++) if(arr[i]<arr[i-1]) return false; return true; } static int find(int a,int b) { int mask=0; for(int i=0;i<32;i++) { int p=1<<i; if((a&p)!=0) mask|=p; if(mask>=b) { return mask; } } return a; } static String reverse(String a){ return new StringBuilder(a).reverse().toString(); } static int bs(ArrayList<Integer> arr,int k) { int lo=0,hi=arr.size()-1,res=hi+1; while(lo<=hi) { int m=lo+(hi-lo)/2; if(arr.get(m)>=k) { res=m; hi=m-1; }else lo=m+1; } return res; } static boolean check(long m, long arr[],long sum) { int l=arr.length; int start=(l-1)/2; long op=m*l-sum; if(arr[start]<=m) op-=(m-arr[start]); else return false; for(int i=start+1;i<l;i++) { if(m>=arr[i]) op-=(m-arr[i]); } return op>=0?true:false; } static long bit(long num,long i,long len) { if(i==len/2+1) return num%2; if(i<=len/2) return bit(num/2,i,len/2); else return bit(num/2,i-len/2-1,len/2); } static long f(long n) { if(n==1 || n==0) return 1; return 2*f(n/2)+1; } static void ftree(int n) { tree=new int[n+1]; } static int query(int r) { int ans=0; r+=1; while(r>0) { ans= (ans%mod + tree[r]%mod)%mod; r-=(r & -r); } return ans; } static void update(int i,int n) { i+=1; while(i<tree.length) { tree[i]= (tree[i]%mod + n%mod)%mod; i+= (i & -i); } } static void dsu(int n) { root=new int[n+1]; size=new int[n+1]; Arrays.fill(size, 1); for(int i=0;i<=n;i++) root[i]=i; } static int find_root(int i) { while(root[i]!=i) { root[i]=root[root[i]]; i=root[i]; } return i; } static void union(int a,int b) { int ra=find_root(a); int rb=find_root(b); if(ra==rb) return; if(size[ra]>size[rb]) { size[ra]+=size[rb]; root[rb]=ra; }else { size[rb]+=size[ra]; root[ra]=rb; } } static int bs(int arr[],int i, int j,int a,int b) { int lo=i,hi=j,index=lo; while(lo<=hi) { int m=lo+(hi-lo)/2; if(check(arr,m,a,b)) { index=m; lo=m+1; } else { hi=m-1; } } return arr[index]; } static boolean check(int arr[],int m,int a,int b) { if(m==0) return true; long x=(long)(arr[m]-a)*(long)(b-arr[m]),y=(long)(arr[m-1]-a)*(long)(b-arr[m-1]); return x>y; } static long pow(long a,long n) { if(n==0l) return 1l; long curr=pow(a,n/2)%mod; if(n%2l==0l) return (curr*curr)%mod; else return (((curr*a)%mod)*curr)%mod; } static void stree(int n,int[] arr) { tree=new int[4*n]; maketree(arr,0,arr.length-1,1); } static int maketree(int[] arr,int st, int en,int root) { if(st==en) { tree[root]=arr[st]; return arr[st]; } int m=st+(en-st)/2; int left=maketree(arr,st,m,root*2); int right=maketree(arr,m+1,en,root*2+1); tree[root]=left+right; return tree[root]; } static int update(int lq,int rq,int l,int r,int root,int val) { if(l==r) { tree[root]=val; arr[l]=val; return tree[root]; } else if(lq>r || rq<l) return 0; int m=l+(r-l)/2; tree[root]= update(lq,rq,l,m,root*2,val)+update(lq,rq,m+1,r,root*2+1,val); return tree[root]; } static void update(int i,int l,int r,int root,int val) { if(l==r) { tree[root]=val; arr[i]=val; return; } int m=l+(r-l)/2; if(i<=m) update(i,l,m,root*2,val); else update(i,m+1,r,root*2+1,val); tree[root]=tree[root*2]+tree[root*2+1]; } static int query(int lq,int rq,int l,int r,int root) { if(lq>r || rq<l) return 0; if(lq<=l && rq>=r) return tree[root]; int m=l+(r-l)/2; return query(lq,rq,l,m,root*2)+query(lq,rq,m+1,r,root*2+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 8
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 &lt; y &lt; 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
58bc9583393d054362ddda3e0a8b28f6
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 &lt; y &lt; 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 Codeforces; import java.io.*; import java.util.*; public class C { static class Pair { int first; int second; Pair(int m, int t) { first = m; second = t; } Pair() {} @Override public String toString() { return "Pair{" + "first=" + first + ", second=" + second + '}'; } } public static void main (String[] Z) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder op = new StringBuilder(); StringTokenizer stz; int T = Integer.parseInt(br.readLine()); while(T-- > 0) { int n = Integer.parseInt(br.readLine()); stz = new StringTokenizer(br.readLine()); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(stz.nextToken()); } boolean already = true; for (int i = 1 ; i < n ; i++) { if(arr[i-1] > arr[i]) { already = false; break; } } if(already) { op.append("0\n"); continue; } if(arr[n-1] < arr[n-2] || arr[n-1] < 0 && arr[n-2] < 0) { op.append("-1\n"); continue; } int ans = n-2; op.append(ans); op.append("\n"); for (int i = 0; i < (n-2); i++) { int x = i+1; int y = n-1; int z = n; String jj = String.format("%d %d %d", x, y, z); op.append(jj); op.append("\n"); } } System.out.println(op); // END OF CODE } }
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 8
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 &lt; y &lt; 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
7f1c687b5b2b42c9b8be0e8ac1c55fdd
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 &lt; y &lt; 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 javafx.util.*; public class Main { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<Integer> g[]; //static ArrayList<ArrayList<TASK>> t; static long mod=(long)(1e9+7); static boolean set[],post[][]; static int prime[],c[]; static int par[]; // static int dp[][]; static HashMap<String,Long> mp; static long max=1; static boolean temp[][]; static int K=0; static int size[],dp[][],iv_A[],iv_B[]; static long modulo = 998244353; public static void main(String args[])throws IOException { /* * star,rope,TPST * BS,LST,MS,MQ */ int t = i(); while(t-- > 0){ int n = i(); int[] arr = input(n); int temp1 = arr[n-1]; int temp2 = arr[n-2]; boolean flag = true; for(int i = 0;i < n-1;i++){ if(arr[i] > arr[i+1]){ flag = false; break; } } if(flag){ System.out.println(0); continue; } if(temp2 - temp1 <= temp2 && temp2 <= temp1){ 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); } } static Boolean isSubsetSum(int n, int arr[], int sum){ // code here boolean[][] dp = new boolean[n+1][sum+1]; for(int i = 0;i < n+1;i++){ for(int j = 0;j < sum + 1;j++){ if(i == 0){ dp[i][j] = false; } if(j == 0){ dp[i][j] = true; } } } for(int i = 1;i < n+1;i++){ for(int j = 1;j < sum + 1;j++){ if(arr[i-1] <= j){ dp[i][j] = dp[i-1][j - arr[i-1]] || dp[i-1][j]; }else{ dp[i][j] = dp[i-1][j]; } } } return dp[n][sum]; } static int getmax(ArrayList<Integer> arr){ int n = arr.size(); int max = Integer.MIN_VALUE; for(int i = 0;i < n;i++){ max = Math.max(max,arr.get(i)); } return max; } static boolean check(ArrayList<Integer> arr){ int n = arr.size(); boolean flag = true; for(int i = 0;i < n-1;i++){ if(arr.get(i) != arr.get(i+1)){ flag = false; break; } } return flag; } static int MinimumFlips(String s, int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = (s.charAt(i) == '1' ? 1 : 0); } // Initialize prefix arrays to store // number of changes required to put // 1s at either even or odd position int[] oddone = new int[n + 1]; int[] evenone = new int[n + 1]; oddone[0] = 0; evenone[0] = 0; for (int i = 0; i < n; i++) { // If i is odd if (i % 2 != 0) { // Update the oddone // and evenone count oddone[i + 1] = oddone[i] + (a[i] == 1 ? 1 : 0); evenone[i + 1] = evenone[i] + (a[i] == 0 ? 1 : 0); } // Else i is even else { // Update the oddone // and evenone count oddone[i + 1] = oddone[i] + (a[i] == 0 ? 1 : 0); evenone[i + 1] = evenone[i] + (a[i] == 1 ? 1 : 0); } } // Initialize minimum flips return Math.min(evenone[n],oddone[n]); } static int nextPowerOf2(int n) { int count = 0; // First n in the below // condition is for the // case where n is 0 if (n > 0 && (n & (n - 1)) == 0){ while(n != 1) { n >>= 1; count += 1; } return count; }else{ while(n != 0) { n >>= 1; count += 1; } return count; } } static int length(int n){ int count = 0; while(n > 0){ n = n/10; count++; } return count; } static boolean isPrimeInt(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 lcs(int[] nums) { int[] tails = new int[nums.length]; int size = 0; for (int x : nums) { int i = 0, j = size; while (i != j) { int m = (i + j) / 2; if (tails[m] <= x) i = m + 1; else j = m; } tails[i] = x; if (i == size) ++size; } return size; } static int CeilIndex(int A[], int l, int r, int key) { while (r - l > 1) { int m = l + (r - l) / 2; if (A[m] >= key) r = m; else l = m; } return r; } static int f(int A[], int size) { // Add boundary case, when array size is one int[] tailTable = new int[size]; int len; // always points empty slot tailTable[0] = A[0]; len = 1; for (int i = 1; i < size; i++) { if (A[i] < tailTable[0]) // new smallest value tailTable[0] = A[i]; else if (A[i] > tailTable[len - 1]) // A[i] wants to extend largest subsequence tailTable[len++] = A[i]; else // A[i] wants to be current end candidate of an existing // subsequence. It will replace ceil value in tailTable tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]; } return len; } static int containsBoth(char X[],int N) { for(int i=1; i<N; i++)if(X[i]!=X[i-1])return i-1; return -1; } static void f(char X[],int N,int A[]) { int c=0; for(int i=N-1; i>=0; i--) { if(X[i]=='1') { A[i]=c; } else c++; A[i]+=A[i+1]; } } static int f(int i,int j,char X[],char Y[],int zero) { if(i==X.length && j==Y.length)return 0; if(i==X.length)return iv_B[j]; //return inversion count here if(j==Y.length)return iv_A[i]; if(dp[i][j]==-1) { int cost_x=0,cost_y=0; if(X[i]=='1') { cost_x=zero+f(i+1,j,X,Y,zero); } else cost_x=f(i+1,j,X,Y,zero-1); if(Y[j]=='1') { cost_y=zero+f(i,j+1,X,Y,zero); } else cost_y=f(i,j+1,X,Y,zero-1); dp[i][j]=Math.min(cost_x, cost_y); } return dp[i][j]; } static boolean f(long last,long next,long l,long r,long A,long B) { while(l<=r) { long m=(l+r)/2; long s=((m)*(m-1))/2; long l1=(A*m)+s,r1=(m*B)-s; if(Math.min(next, r1)<Math.max(last, l1)) { if(l1>last)r=m-1; else l=m+1; } else return true; } return false; } static boolean isVowel(char x) { if(x=='a' || x=='e' || x=='i' ||x=='u' || x=='o')return true; return false; } static boolean f(int i,int j) { //i is no of one //j is no of ai>1 if(i==0 && j==0)return true; //this player has no pile to pick --> last move iska rha hoga if(dp[i][j]==-1) { boolean f=false; if(i>0) { if(!f(i-1,j))f=true; } if(j>0) { if(!f(i,j-1) && !f(i+1,j-1))f=true; } if(f)dp[i][j]=1; else dp[i][j]=0; } return dp[i][j]==1; } static int last=-1; static void dfs(int n,int p) { last=n; System.out.println("n--> "+n+" p--> "+p); for(int c:g[n]) { if(c!=p)dfs(c,n); } } static long abs(long a,long b) { return Math.abs(a-b); } static int lower(long A[],long x) { int l=0,r=A.length; while(r-l>1) { int m=(l+r)/2; if(A[m]<=x)l=m; else r=m; } return l; } static int f(int i,int s,int j,int N,int A[],HashMap<Integer,Integer> mp) { if(i==N) { return s; } if(dp[i][j]==-1) { if(mp.containsKey(A[i])) { int f=mp.get(A[i]); int c=0; if(f==1)c++; mp.put(A[i], f+1); HashMap<Integer,Integer> temp=new HashMap<>(); temp.put(A[i],1); return dp[i][j]=Math.min(f(i+1,s+1+c,j,N,A,mp), s+K+f(i+1,0,i,N,A,temp)); } else { mp.put(A[i],1); return dp[i][j]=f(i+1,s,j,N,A,mp); } } return dp[i][j]; } static boolean inRange(int a,int l,int r) { if(l<=a && r>=a)return true; return false; } static long f(long a,long b) { if(a%b==0)return a/b; else return (a/b)+f(b,a%b); } static void f(int index,long A[],int i,long xor) { if(index+1==A.length) { if(valid(xor^A[index],i)) { xor=xor^A[index]; max=Math.max(max, i); } return; } if(dp[index][i]==0) { dp[index][i]=1; if(valid(xor^A[index],i)) { f(index+1,A,i+1,0); f(index+1,A,i,xor^A[index]); } else { f(index+1,A,i,xor^A[index]); } } } static boolean valid(long xor,int i) { if(xor==0)return true; while(xor%2==0 ) { xor/=2; i--; } return i<=0; } static int next(int i,long pre[],long S) { int l=i,r=pre.length; while(r-l>1) { int m=(l+r)/2; if(pre[m]-pre[i-1]>S)r=m; else l=m; } return r; } static boolean lexo(long A[],long B[]) { for(int i=0; i<A.length; i++) { if(B[i]>A[i])return true; if(A[i]>B[i])return false; } return false; } static long [] f(long A[],long B[],int j) { int N=A.length; long X[]=new long[N]; for(int i=0; i<N; i++) { X[i]=(B[j]+A[i])%N; j++; j%=N; } return X; } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { b=find(b); a=find(a); if(a!=b) { par[b]=a; } } static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } static long lower_Bound(long A[],int low,int high, long x) { if (low > high) if (x >= A[high]) return A[high]; int mid = (low + high) / 2; if (A[mid] == x) return A[mid]; if (mid > 0 && A[mid - 1] <= x && x < A[mid]) return A[mid - 1]; if (x < A[mid]) return lower_Bound( A, low, mid - 1, x); return lower_Bound(A, mid + 1, high, x); } static void sort(long[] a) //check for long { 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); } static void setGraph(int N) { size=new int[N+1]; // D=new int[N+1]; g=new ArrayList[N+1]; for(int i=0; i<=N; i++) { g[i]=new ArrayList<>(); } } static long pow(long a,long b) { long pow=1L; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(int x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } 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 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 long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader 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 8
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 &lt; y &lt; 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
40a20c9fa25f78e367c048149cad0963
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 &lt; y &lt; 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 q3 { static FastScanner fs = new FastScanner(); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) { int T = fs.nextInt(); for (int t=0;t<T;t++){ int n = fs.nextInt(); long[] pp = new long[n]; for (int i=0;i<n;i++) pp[i] = fs.nextLong(); if (pp[n - 2] > pp[n - 1]){ pw.println(-1); continue; } List<int[]> ans = new LinkedList<>(); boolean flag = false, ff = false; int index = -1; for (int i=n-3;i>=0;i--){ if (!flag && pp[i + 2] >= 0){ flag = true; index = i + 2; } if (pp[i] > pp[i + 1]){ if (flag){ pp[i] = pp[i + 1] - pp[index]; ans.add(new int[]{i, i + 1, index}); } else{ pw.println(-1); ff = true; break; } } } if (ff) continue; pw.println(ans.size()); for (int[] a : ans){ pw.println((a[0] + 1) + " " + (a[1] + 1) + " " + (a[2] + 1)); } } pw.close(); } // ----------input function---------- 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 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()); } } }
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 8
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 &lt; y &lt; 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
521c53cb566d14078c66168b5966039b
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 &lt; y &lt; 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.ArrayList; import java.util.Collections; import java.util.Scanner; public class cfContest1635 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); StringBuilder sb = new StringBuilder(); k: while (t-- > 0) { int n = scan.nextInt(); int[] a = new int[n]; ArrayList<Integer> ar = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { a[i] = scan.nextInt(); } if (a[n - 1] < 0) { for (int i = 0; i < n - 1; i++) { if (a[i + 1] < a[i]) { sb.append("-1\n"); continue k; } } sb.append("0\n"); continue k; } if (a[n - 2] > a[n - 1]) { sb.append("-1\n"); } else { sb.append(n - 2 + "\n"); for (int i = 0; i < n - 2; i++) { sb.append((i + 1) + " " + (n - 1) + " " + n + "\n"); } } } System.out.println(sb); } }
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 8
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 &lt; y &lt; 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
d7bd66237e9609fbb6cf1ed57ac2823e
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 &lt; y &lt; 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 C { public static void main(String args[]){ FScanner in = new FScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while(t-->0) { int n=in.nextInt(); int arr[]=in.readArray(n); int f=0; for(int i=1;i<n;i++) { if(arr[i]<arr[i-1]) { f=1;break; } } if(f==0) out.println(0); else { if(arr[n-2]>arr[n-1]||(arr[n-2]<0&&arr[n-1]<0)) out.println(-1); else { out.println(n-2); for(int i=0;i<n-2;i++) { arr[i]=arr[n-2]-arr[n-1]; out.println((i+1)+" "+(n-1)+" "+(n)); } } } } out.close(); } 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 boolean checkprime(int n1) { if(n1%2==0||n1%3==0) return false; else { for(int i=5;i*i<=n1;i+=6) { if(n1%i==0||n1%(i+2)==0) return false; } return true; } } static class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(next()); } double nextDouble(){ return Double.parseDouble(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 8
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 &lt; y &lt; 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
d7683e6d2245a33e6f71eadcad3dd62b
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 &lt; y &lt; 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.reflect.Array; import java.util.*; import java.lang.reflect.Array; import javax.print.DocFlavor.STRING; import javax.swing.Popup; import javax.swing.plaf.synth.SynthStyleFactory; public class Simple{ public static class Pair implements Comparable<Pair>{ int val; int freq = 0; Pair prev ; Pair next; boolean bool = false; public Pair(int val,int freq){ this.val = val; this.freq= freq; } public int compareTo(Pair p){ // if(p.freq == this.freq){ // return this.val - p.freq; // }; // if(this.freq > p.freq)return -1; // return 1; return (p.freq-p.val) - (this.freq - this.val); } } public static long factorial(long n) { // single line to find factorial return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } static long m = 998244353; // Function to return the GCD of given numbers static long gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // Recursive function to return (x ^ n) % m static long modexp(long x, long n) { if (n == 0) { return 1; } else if (n % 2 == 0) { return modexp((x * x) % m, n / 2); } else { return (x * modexp((x * x) % m, (n - 1) / 2) % m); } } // Function to return the fraction modulo mod // static long getFractionModulo(long a, long b) // { // long c = gcd(a, b); // a = a / c; // b = b / c; // // (b ^ m-2) % m // long d = modexp(b, m - 2); // // Final answer // long ans = ((a % m) * (d % m)) % m; // return ans; // } // public static long bs(long lo,long hi,long fact,long num){ // long help = num/fact; // long ans = hi; // while(lo<=hi){ // long mid = (lo+hi)/2; // if(mid/) // } // } // public 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 <= sqrt(n); i += 2) // { // if (n % i == 0) // return false; // } // return true; // } // public static int countDigit(long n) // { // int count = 0; // while (n != 0) { // n = n / 10; // ++count; // } // return count; // } // static ArrayList<Long> al ; // static boolean checkperfectsquare(long n) // { // // If ceil and floor are equal // // the number is a perfect // // square // if (processesh.ceil((double)processesh.sqrt(n)) == // processesh.floor((double)processesh.sqrt(n))) // { // return true; // } // else // { // return false; // } // } public static void decToBinary(int n,int arr[],int j) { // Size of an integer is assumed to be 32 bits for (int i = 31; i >= 0; i--) { int k = n >> i; if ((k & 1) > 0){ arr[j]++; } j++; } } public static class Node{ //int u; long v; long w; public Node(long v,long w){ //this.u = u; this.v=v; this.w=w; } } 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; } 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 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; } // static int modInverse(int a, int p) // { // return power(a, p - 2, p); // } public static void bfs(ArrayList<ArrayList<Integer>> adj,boolean vis[],int dis[]){ vis[0] = true; Queue<Integer> q = new LinkedList<>(); q.add(0); int count = 0; while(!q.isEmpty()){ int size = q.size(); for(int j = 0;j<size;j++){ int poll = q.poll(); dis[poll] = count; vis[poll] = true; for(Integer x : adj.get(poll)){ if(!vis[x]){ q.add(x); } } } count++; } } static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static boolean isSorted(int arr[],int[] copy,int n){ for(int i=0;i<n;i++){ if(arr[i]!=copy[i])return false; } return true; } public static class DSU{ int par[]; int rank[];//rank denotes the height of graph public DSU(int n){ par = new int[n]; for(int i=0;i<n;i++){ par[i] = i; } rank = new int[n]; } public int findPar(int node){ if(par[node]==node)return node; par[node] = findPar(par[node]); return par[node]; } public void union(int u,int v){ u = findPar(u); v = findPar(v); if(rank[v]>rank[u]){ par[u] = v; } else if(rank[v]<rank[u]){ par[v] = u; } else{ rank[u]++; par[v] = u; } } } public static long modFact(int n,int k) { long M = 998244353; long f = 1; for (int i = 1; i <= n; i++){ if(i==k+1)continue; f = (f%M*i%m) % M; // Now f never can } // exceed 10^9+7 return f; } public static class Item{ int val; int index; public Item(int val,int index){ this.val = val; this.index = index; } } public static void mergeSortCount(Item[] itemArr,int count[],int n,int i,int j){ if(i>=j)return; int mid = (i+j)/2; mergeSortCount(itemArr, count, n, i, mid); mergeSortCount(itemArr, count, n, mid+1, j); mergeCount(itemArr,count,i,mid+1,mid,j); } public static void mergeCount(Item[] itemArr,int count[],int i1,int i2,int j1,int j2){ int size = j2 - i1+1; Item[] sortedItems = new Item[size]; int index = 0; int i11 = i1; //int j11 = j2; int i22 = i2; //int j22 = j2; while(i11<=j1 && i22 <=j2 ){ if(itemArr[i11].val < itemArr[i22].val){ count[itemArr[i11].index]+= (j2-i22+1); sortedItems[index++] = itemArr[i11++]; } else{ sortedItems[index++] = itemArr[i22++]; } } while(i11<=j1){ sortedItems[index++] = itemArr[i11++]; } while(i22<=j2){ sortedItems[index++] = itemArr[i22++]; } index=0; for(int i=i1;i<=j2;i++){ itemArr[i] = sortedItems[index++]; } } public static void main(String args[]){ //System.out.println("Hello Java"); Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int t1 = 1;t1<=t;t1++){ int n =s.nextInt(); long arr[]= new long[n]; for(int i=0;i<n;i++){ arr[i] = s.nextLong(); } if(arr[n-1]<arr[n-2]){ System.out.println(-1); continue; } boolean isSort = true; for(int i=1;i<n;i++){ if(arr[i]<arr[i-1]){ isSort=false; break; } } if(isSort){ System.out.println(0); continue; } if(arr[n-1]<0){ System.out.println(-1); continue; } int x = n-1; int y = n; int m= n-2; System.out.println(m); for(int i=0;i<n-2;i++){ int j = i+1; System.out.println(j+" "+x+" "+y); } } } } /* 1 2 3 4 5 6 */
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 8
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 &lt; y &lt; 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
632f1d2e8065c04ba16f1ff8c8290b9d
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 &lt; y &lt; 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 CodeForces{ public static void main(String[] args) throws FileNotFoundException { FastScanner fs = new FastScanner(); int t = fs.nextInt(); while(t-- > 0) { int n = fs.nextInt(); long[] a = fs.readArray(n); boolean flag = true; StringBuilder sb = new StringBuilder(); if(a[n - 2] > a[n - 1]) { flag = false; System.out.println(-1); continue; } int counter = 0; long lo = a[n - 2], hi = a[n - 1]; for(int i = n - 3; i >= 0; i--) { if(a[i] <= a[i + 1]) { lo = Math.min(a[i], lo); continue; } if(lo - hi <= a[i + 1]) { a[i] = lo - hi; lo = Math.min(lo, a[i]); if(counter != 0) { sb.append("\n"); } counter++; sb.append((i+1) + " " + (i + 2) + " " + (n)); }else { flag = false; break; } } if(!flag) { System.out.println(-1); }else { System.out.println(counter); System.out.println(sb.toString()); } } } 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 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[] readArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(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 8
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 &lt; y &lt; 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
50782a10cdfffce0110a68e242654a66
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 &lt; y &lt; 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 C { static InputReader in; static PrintWriter out; static int mod = 1000_000_007; public static void main(String args[]) { new Thread(null, new Runnable() { public void run() { try { solve(); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } public static void solve() { in = new InputReader(System.in); out = new PrintWriter(System.out); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int a[] = in.nextIntArray(n); int f = 0; for (int i = 0; i < n-1; i++) { if(a[i] > a[i+1] ) { f=1; break; } } if(f == 0) { out.println(0); continue; } if(a[n-2] < a[n-2]-a[n-1] || a[n-2] > a[n-1]) { out.println(-1); continue; } out.println(n-2); for (int i = 0; i < n-2; i++) { out.println((i+1)+" "+(n-1)+" "+n); } // for(int x:a) // out.print(x+" "); // out.println(); } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.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 readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } 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 int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static long inv(long a, long b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; } public static int[] sort(int[] arr) { List<Integer> temp = new ArrayList(); for (int i : arr) temp.add(i); Collections.sort(temp); int start = 0; for (int i : temp) arr[start++] = i; return arr; } public static long[] sort(long[] arr) { List<Long> temp = new ArrayList(); for (long i : arr) temp.add(i); Collections.sort(temp); int start = 0; for (long i : temp) arr[start++] = i; return arr; } }
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 8
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 &lt; y &lt; 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
f30f4bc27435dd4ae25bd1ad9b3fac2d
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 &lt; y &lt; 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 tr0 { static PrintWriter out; static StringBuilder sb; static long mod = (long) 998244353; static long inf = (long) 1e16; static int n, l, k; static TreeSet<Integer>[] ad, ad1, ad2; static ArrayList<int[]>[] quer; static int[][] remove, add; static int[][] memo; static boolean vis[]; static long[] inv, f, ncr[]; static HashMap<Integer, Integer> hm; static int[] pre, suf, Smax[], Smin[]; static int idmax, idmin; static ArrayList<Integer> av; static HashMap<Integer, Integer> mm; static boolean[] msks; static int[] lazy[], lazyCount; static int[] a, b; static long ans, tem, c; static ArrayList<Integer> gl; static int[] ar; static String s; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] a = sc.nextArrInt(n); if (a[n - 1] < a[n - 2]) { out.println(-1); continue; } boolean f = true; for (int i = 0; i < n - 1; i++) f &= a[i] <= a[i + 1]; if (f) { out.println(0); continue; } Queue<int[]> ans = new LinkedList<int[]>(); TreeMap<Integer, int[]> tm1 = new TreeMap<>(); tm1.put(a[n - 2] - a[n - 1], new int[] { n - 2, n - 1 }); TreeMap<Integer, Integer> tm = new TreeMap<>(); tm.put(a[n - 1], n - 1); tm.put(a[n - 2], n - 2); for (int i = n - 3; i >= 0; i--) { if (a[i] > a[i + 1]) { a[i] = tm1.firstKey(); ans.add(new int[] { i, tm1.firstEntry().getValue()[0], tm1.firstEntry().getValue()[1] }); } if (a[i] <= tm.lastKey()) tm1.put(a[i] - tm.lastKey(), new int[] { i, tm.lastEntry().getValue() }); tm.put(a[i], i); // System.out.println(tm1+" "+tm); } // System.out.println(Arrays.toString(a)); f = true; for (int i = 0; i < n - 1; i++) f &= a[i] <= a[i + 1]; if (!f) { out.println(-1); continue; } out.println(ans.size()); for (int[] u : ans) out.println((u[0] + 1) + " " + (u[1] + 1) + " " + (u[2] + 1)); } out.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public int[] nextArrInt(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextArrLong(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextArrIntSorted(int n) throws IOException { int[] a = new int[n]; Integer[] a1 = new Integer[n]; for (int i = 0; i < n; i++) a1[i] = nextInt(); Arrays.sort(a1); for (int i = 0; i < n; i++) a[i] = a1[i].intValue(); return a; } public long[] nextArrLongSorted(int n) throws IOException { long[] a = new long[n]; Long[] a1 = new Long[n]; for (int i = 0; i < n; i++) a1[i] = nextLong(); Arrays.sort(a1); for (int i = 0; i < n; i++) a[i] = a1[i].longValue(); return a; } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
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 8
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 &lt; y &lt; 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
6ccdd065d8ac403c7608e15b2e130b75
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 &lt; y &lt; 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 cp { static PrintWriter w = new PrintWriter(System.out); static FastScanner s = new FastScanner(); static int mod = 1000000007; static class Edge { int src; int wt; int nbr; Edge(int src, int nbr, int et) { this.src = src; this.wt = et; this.nbr = nbr; } } class EdgeComparator implements Comparator<Edge> { @Override //return samllest elemnt on polling public int compare(Edge s1, Edge s2) { if (s1.wt < s2.wt) { return -1; } else if (s1.wt > s2.wt) { return 1; } return 0; } } static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static void prime_till_n(boolean[] prime) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. for (int p = 2; p * p < prime.length; 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 < prime.length; i += p) { prime[i] = false; } } } // int l = 1; // for (int i = 2; i <= n; i++) { // if (prime[i]) { // w.print(i+","); // arr[i] = l; // l++; // } // } //Time complexit Nlog(log(N)) } static int noofdivisors(int n) { //it counts no of divisors of every number upto number n int arr[] = new int[n + 1]; for (int i = 1; i <= (n); i++) { for (int j = i; j <= (n); j = j + i) { arr[j]++; } } return arr[0]; } static char[] reverse(char arr[]) { char[] b = new char[arr.length]; int j = arr.length; for (int i = 0; i < arr.length; i++) { b[j - 1] = arr[i]; j = j - 1; } return b; } static long factorial(int n) { if (n == 0) { return 1; } long su = 1; for (int i = 1; i <= n; i++) { su *= (long) i; } return su; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } long[] readArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } static class Vertex { int x; int y; int wt; public Vertex(int x, int y) { this.x = x; this.y = y; } public Vertex(int x, int y, int wt) { this.x = x; this.y = y; this.wt = wt; } } public static long power(long x, int n) { if (n == 0) { return 1l; } long pow = power(x, n / 2) % mod; if ((n & 1) == 1) // if `y` is odd { return ((((x % mod) * (pow % mod)) % mod) * (pow % mod)) % mod; } // otherwise, `y` is even return ((pow % mod) * (pow % mod)) % mod; } public static void main(String[] args) { { int t = s.nextInt(); // int t = 1; while (t-- > 0) { solve(); } w.close(); } } public static void solve() { int n = s.nextInt(); int arr[]= new int[n]; for(int i=0;i<n;i++) { arr[i]=s.nextInt(); } int p=0; for(int i=1;i<n;i++) if(arr[i-1]<=arr[i]) p++; if(p==n-1) { w.println(0);return; } if(arr[n-2]>arr[n-1] || arr[n-2]-arr[n-1]>arr[n-2] || arr[n-2]-arr[n-1]>arr[n-1]) { w.println(-1);return; } w.println(n-2); for(int i=0;i<n-2;i++) { w.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 8
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 &lt; y &lt; 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