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
9644d0b70219ffdcc1d1152b1a70860a
train_108.jsonl
1645367700
You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \leq x < y < z \leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well.
256 megabytes
import java.util.*; import java.io.*; public class practice { public static void solve() { Reader sc = new Reader(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0;i < n;i++) arr[i] = sc.nextInt(); boolean asc = true; for(int i = 0;i < n - 1;i++) if(arr[i] > arr[i + 1]) asc = false; if(asc) out.println(0); else { if(arr[n - 1] < 0 && arr[n - 2] < 0) out.println(-1); else if(arr[n - 2] > arr[n - 1]) out.println(-1); else { List<Pair> ans = new ArrayList<>(); int last_pos_idx = n - 1; for(int i = n - 3;i >= 0;i--) { arr[i] = arr[i + 1] - arr[last_pos_idx]; ans.add(new Pair(i + 1,last_pos_idx,last_pos_idx + 1)); } out.println(ans.size()); for(Pair p : ans) out.println(p.a + " " + p.b + " " + p.c); } } } out.flush(); } public static void main(String[] args) throws IOException { solve(); } static int[] sort(int[] arr,int n) { List<Integer> list = new ArrayList<>(); for(int i = 0;i < n;i++) list.add(arr[i]); Collections.sort(list); for(int i = 0;i < n;i++) arr[i] = list.get(i); return arr; } static class Reader { BufferedReader br; StringTokenizer st; public Reader() { 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 { if(st.hasMoreTokens()) str = st.nextToken("\n"); else str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class Pair { int a; int b; int c; public Pair(int a,int b,int c) { this.a = a; 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
8f540a3f13fe69d1ca63bda81a9e7546
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.nio.Buffer; import java.util.*; public class Main2 { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int testCases = sc.nextInt(); while (testCases -- > 0) { int n = sc.nextInt(); Long[] numbers = new Long[n]; for(int i = 0; i < numbers.length; i++) numbers[i] = sc.nextLong(); if(numbers[numbers.length - 2] > numbers[numbers.length - 1]) { System.out.println(-1); } else { ArrayList<String> ans = new ArrayList<>(); if(numbers[numbers.length - 1] >= 0) { for(int i = 0; i < numbers.length - 2; i++) { ans.add((i + 1) + " " + (numbers.length - 1) + " " + (numbers.length)); } System.out.println(ans.size()); for(String element : ans) System.out.println(element); } else { boolean can = true; for(int i = 0; i < numbers.length - 1; i++) { if(numbers[i] > numbers[i + 1]) can = false; } if(can) System.out.println(0); else System.out.println(-1); } } } } }
Java
["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"]
2 seconds
["2\n1 2 3\n3 4 5\n-1\n0"]
NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations.
Java 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
a4cc808098317831d2e868fe6c7db5bb
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 Ac { static int MOD = 998244353; static int MAX = (int)1e8; static Random rand = new Random(); static FastReader in = new FastReader(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int t = i(); out.println(); out.println(); out.println(); // randArr(10, 10, 10); // out.println(arr[1] + " " + arr[5]); while (t-- > 0){ sol(); // out.println(sol(arr)); // int[] ans = sol(); // for (int i = 0; i < ans.length; i++){ // out.print(ans[i] + " "); // } // out.println(); // boolean ans = sol(); // if (ans){ // out.println("YES"); // }else{ // out.println("NO"); // } } out.flush(); out.close(); } static void sol(){ int n = i(); int[] arr = inputI(n); if (arr[n - 1] < arr[n - 2]){ out.println(-1); return; } int i = n - 2, pos = -1; for (; i > -1; i--){ if (pos == - 1 && arr[i + 1] >= 0){ pos = i + 1; } if (arr[i] > arr[i + 1]){ break; } } if (i == -1){ out.println(0); return; } if (pos == -1 || pos == i + 1){ out.println(-1); return; } if (i == -1){ out.println(0); return; } out.println(i + 1); for (; i > -1; i--){ arr[i] = arr[i + 1] - arr[pos]; out.print((i + 1) + " " + (i + 2) + " " + (pos + 1)); out.println(); } } static void randArr(int num, int n, int val){ for (int i = 0; i < num; i++){ int len = rand.nextInt(n + 1); System.out.println(len); for (int j = 0; j < len; j++){ System.out.print(rand.nextInt(val) + 1 + " "); } System.out.println(); } } static void shuffle(long a[], int n){ for (int i = 0; i < n; i++) { int t = (int)Math.random() * a.length; long x = a[t]; a[t] = a[i]; a[i] = x; } } static int gcd(int a, int b){ return a % b == 0 ? a : gcd(b, a % b); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static double d() { return in.nextDouble(); } static String s(){ return in.nextLine(); } static int[] inputI(int n) { int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = i(); } return nums; } static long[] inputL(int n) { long[] nums = new long[n]; for (int i = 0; i < n; i++) { nums[i] = l(); } return nums; } } 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
8760e0333ff0d6497f84f758b91b6e1e
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 javafx.scene.layout.Priority; public class Main { public static boolean check(int a[]) { int ar[] = a.clone(); Arrays.sort(ar); for (int i = 0; i <a.length; i++) { if(a[i]!=ar[i]) { return false; } } return true; } public static void main(String args[]) { FastScanner input = new FastScanner(); int tc = input.nextInt(); work: while (tc-- > 0) { int n = input.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = input.nextInt(); } if(check(a)) { System.out.println("0"); } else if(a[n-2]>a[n-1]||(a[n-2]-a[n-1])>a[n-2]) { System.out.println("-1"); } else { System.out.println(n-2); for (int i = 0; i <n-2; i++) { System.out.println((i+1)+" "+(n-1)+" "+n); } } } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.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 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
8707229950bf8045a756eeac2aeb019a
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{ static FastScanner fs = null; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); while (t-->0) { int n = fs.nextInt(); int a[] = new int[n]; int b[] = new int[n]; for(int i=0;i<n;i++){ a[i] = fs.nextInt(); b[i] = a[i]; } sort(b); int co = 0; for(int i=0;i<n;i++){ if(a[i]!=b[i]) co+=1; } int d = -a[n-1]+a[n-2]; boolean res = true; if(a[n-1] < a[n-2]){ res = false; } else if(co==0){ out.println("0"); } else if(d>a[n-2]){ res = false; } else{ out.println((n-2)); for(int i=1;i<n-1;i++){ out.println(i+" "+(n-1)+" "+(n)); } } if(!res){ out.println("-1"); } } 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 class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
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
fd7e06b4038d6879d33cbec91c027858
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.Collectors; import java.io.*; import java.math.*; public class R772_C{ public static FastScanner sc; public static PrintWriter pw; public static StringBuilder sb; public static int MOD= 1000000007; public 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) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static void printList(List<Long> al) { System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" "))); } public static void printList(Deque<Long> al) { System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" "))); } public static void printArr(long[] arr) { System.out.println(Arrays.toString(arr).trim().replace("[", "").replace("]","").replace(","," ")); } public static long gcd(long a, long b) { if(b==0) return a; return gcd(b,a%b); } public static long lcm(long a, long b) { return((a*b)/gcd(a,b)); } public static void decreasingOrder(ArrayList<Long> al) { Comparator<Long> c = Collections.reverseOrder(); Collections.sort(al,c); } public static boolean[] sieveOfEratosthenes(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++) { for(int j=2*i;j<n;j+=i) { isPrime[j]=false; } } return isPrime; } public static long nCr(long N, long R) { double result=1; for(int i=1;i<=R;i++) result=((result*(N-R+i))/i); return (long) result; } public static long fastPow(long a, long b, int n) { long result=1; while(b>0) { if((b&1)==1) result=(result*a %n)%n; a=(a%n * a%n)%n; b>>=1; } return result; } public static int BinarySearch(long[] arr, long key) { int low=0; int high=arr.length-1; while(low<=high) { int mid=(low+high)/2; if(arr[mid]==key) return mid; else if(arr[mid]>key) high=mid-1; else low=mid+1; } return low; //High=low-1 } public static void solve(int t) { int n=sc.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); boolean ans=true; for(int i=0;i<n-1;i++) { if(arr[i]>arr[i+1]) { ans=false; break; } } if(ans) { System.out.println(0); return; } else if(arr[n-2]>arr[n-1] || arr[n-1]<0) { 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); } System.out.println(); } } public static void main(String[] args) { sc = new FastScanner(); pw = new PrintWriter(new OutputStreamWriter(System.out)); sb= new StringBuilder(""); int t=sc.nextInt(); for(int i=1;i<=t;i++) solve(i); 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
46dfa22a8557d47f421ae25b0e6e2fde
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; import java.util.*; public class Main { public static void main(String[] args){ InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); solve(in, out); out.close(); } static String reverse(String s) { return (new StringBuilder(s)).reverse().toString(); } static void sieveOfEratosthenes(int n, int factors[], ArrayList<Integer> ar) { factors[1]=1; int p; for(p = 2; p*p <=n; p++) { if(factors[p] == 0) { ar.add(p); factors[p]=p; for(int i = p*p; i <= n; i += p) if(factors[i]==0) factors[i] = p; } } for(;p<=n;p++){ if(factors[p] == 0) { factors[p] = p; ar.add(p); } } } static void sort(int ar[]) { int n = ar.length; ArrayList<Integer> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static void sort1(long ar[]) { int n = ar.length; ArrayList<Long> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static long ncr(long n, long r, long mod) { if (r == 0) return 1; long val = ncr(n - 1, r - 1, mod); val = (n * val) % mod; val = (val * modInverse(r, mod)) % mod; return val; } static int findMax(int a[], int n, int vis[], int i, int d){ if(i>=n) return 0; if(vis[i]==1) return findMax(a, n, vis, i+1, d); int max = 0; for(int j=i+1;j<n;j++){ if(Math.abs(a[i]-a[j])>d||vis[j]==1) continue; vis[j] = 1; max = Math.max(max, 1 + findMax(a, n, vis, i+1, d)); vis[j] = 0; } return max; } static void findSub(ArrayList<ArrayList<Integer>> ar, int n, ArrayList<Integer> a, int i){ if(i==n){ ArrayList<Integer> b = new ArrayList<Integer>(); for(int y:a){ b.add(y); } ar.add(b); return; } for(int j=0;j<n;j++){ if(j==i) continue; a.set(i,j); findSub(ar, n, a, i+1); } } // *-------------------code starts here--------------------------------------------------* public static void solve(InputReader sc, PrintWriter pw){ long mod=(long)1e9+7; int t=sc.nextInt(); // int t=1; L : while(--t>=0){ int n=sc.nextInt(); int a[]=sc.readArray(n); if(a[n-2]>a[n-1]){ pw.println(-1); } else{ if(a[n-1]<0){ pw.println(isSorted(a)?0:-1); } else{ pw.println(n-2); for(int i=1;i<=n-2;i++){ pw.println(i+" "+(n-1)+" "+n); } } } } } public static boolean isSorted(int a[]){ for(int i=0;i<a.length-1;i++){ if(a[i]>a[i+1]){ return false; } } return true; } // *-------------------code ends here-----------------------------------------------------* static void assignAnc(ArrayList<Integer> ar[],int sz[], int pa[] ,int curr, int par){ sz[curr] = 1; pa[curr] = par; for(int v:ar[curr]){ if(par==v) continue; assignAnc(ar, sz, pa, v, curr); sz[curr] += sz[v]; } } static int findLCA(int a, int b, int par[][], int depth[]){ if(depth[a]>depth[b]){ a = a^b; b = a^b; a = a^b; } int diff = depth[b] - depth[a]; for(int i=19;i>=0;i--){ if((diff&(1<<i))>0){ b = par[b][i]; } } if(a==b) return a; for(int i=19;i>=0;i--){ if(par[b][i]!=par[a][i]){ b = par[b][i]; a = par[a][i]; } } return par[a][0]; } static void formArrayForBinaryLifting(int n, int par[][]){ for(int j=1;j<20;j++){ for(int i=0;i<n;i++){ if(par[i][j-1]==-1) continue; par[i][j] = par[par[i][j-1]][j-1]; } } } static long lcm(int a, int b){ return a*b/gcd(a,b); } static class Pair1 { long a; long b; Pair1(long a, long b) { this.a = a; this.b = b; } } static class Pair implements Comparable<Pair> { int a; int b; // int c; Pair(int a, int b) { this.a = a; this.b = b; // this.c = c; } public int compareTo(Pair p) { return Integer.compare(a,p.a); } } static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long fast_pow(long base, long n, long M) { if (n == 0) return 1; if (n == 1) return base % M; long halfn = fast_pow(base, n / 2, M); if (n % 2 == 0) return (halfn * halfn) % M; else return (((halfn * halfn) % M) * base) % M; } static long modInverse(long n, long M) { return fast_pow(n, M - 2, M); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 9992768); 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[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } public long[] readarray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } 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
d861fbfd0cdc74434977cb504c32b1e7
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 DifferentialSorting{ static long mod = 1000000007L; static MyScanner sc = new MyScanner(); static void solve(){ int n = sc.nextInt(); int arr[] = sc.readIntArray(n); 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"); return; } if(arr[n-2]>arr[n-1] || (arr[n-2]-arr[n-1])>arr[n-1] || (arr[n-2]-arr[n-1])>arr[n-2]){ out.println("-1"); return; } out.println(n-2); for(int i = 1;i<=n-2;i++){ out.println(i+" "+(n-1)+" "+(n)); } } static String fun(int n){ String str = ""; for(int i = 0;i<n;i++) str+= '0'; return str; } static String fun2(int n){ String str = ""; for(int i = 0;i<n;i++) str+= '1'; return str; } static long pow(long a, long b) { if (b == 0) return 1; long res = pow(a, b / 2); res = (res * res) % 1_000_000_007; if (b % 2 == 1) { res = (res * a) % 1_000_000_007; } return res; } static int lis(int arr[],int n){ int lis[] = new int[n]; lis[0] = 1; for(int i = 1;i<n;i++){ lis[i] = 1; for(int j = 0;j<i;j++){ if(arr[i]>arr[j]){ lis[i] = Math.max(lis[i],lis[j]+1); } } } int max = Integer.MIN_VALUE; for(int i = 0;i<n;i++){ max = Math.max(lis[i],max); } return max; } static boolean isPali(String str){ int i = 0; int j = str.length()-1; while(i<j){ if(str.charAt(i)!=str.charAt(j)){ return false; } i++; j--; } return true; } static long gcd(long a,long b){ if(b==0) return a; return gcd(b,a%b); } static String reverse(String str){ char arr[] = str.toCharArray(); int i = 0; int j = arr.length-1; while(i<j){ char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } String st = new String(arr); return st; } static boolean isprime(int n){ if(n==1) return false; if(n==3 || n==2) return true; if(n%2==0 || n%3==0) return false; for(int i = 5;i*i<=n;i+= 6){ if(n%i== 0 || n%(i+2)==0){ return false; } } return true; } static class Pair implements Comparable<Pair>{ int val; int ind; int ans; Pair(int v,int f){ val = v; ind = f; } public int compareTo(Pair p){ return p.val - this.val; } } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); // int t= 1; while(t-- >0){ // solve(); solve(); } // Stop writing your solution here. ------------------------------------- out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int[] readIntArray(int n){ int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(next()); } return arr; } int[] reverse(int arr[]){ int n= arr.length; int i = 0; int j = n-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; j--;i++; } return arr; } long[] readLongArray(int n){ long arr[] = new long[n]; for(int i = 0;i<n;i++){ arr[i] = Long.parseLong(next()); } return arr; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } }
Java
["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"]
2 seconds
["2\n1 2 3\n3 4 5\n-1\n0"]
NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations.
Java 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
39ab28b35c54060d4942745a4b65e423
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.io.*; import java.util.*; import java.math.*; import static java.lang.Math.sqrt; import static java.lang.Math.floor; public class topcoder { public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; } static int x = -1; static int y = -1; public static int first_search(TreeNode root, TreeNode main_root) { if(root == null) return 0; int a = first_search(root.left,main_root); int b = first_search(root.right,main_root); if(a > main_root.val) x = a; if(b < main_root.val) y = b; return root.val; } public static void fix(TreeNode root, TreeNode main_root) { if(root == null) return; fix(root.left,main_root); if(root.val > main_root.val) { root.val = y; } fix(root.right,main_root); if(root.val < main_root.val); root.val = x; } public static int max(int []nums, int s, int e) { int max = Integer.MIN_VALUE; for(int i = s; i <= e; i++) { max = Math.max(max, nums[i]); } return max; } public static TreeNode new_node(int []nums, int s, int e) { int max = max(nums,s,e); TreeNode node = new TreeNode(max); return node; } public static TreeNode root; public static void res(int []nums, int s, int e) { if(s > e)return; if(root == null) { root = new_node(nums,s,e); } root.left = new_node(nums,s,e); root.right = new_node(nums,s,e); return; } static int depth(TreeNode root) { if(root == null)return 0; int a = 1+ depth(root.left); int b = 1+ depth(root.right); return Math.max(a,b); } static HashSet<Integer>set = new HashSet<>(); static void deepestLeaves(TreeNode root, int cur_depth, int depth) { if(root == null)return; if(cur_depth == depth)set.add(root.val); deepestLeaves(root.left,cur_depth+1,depth); deepestLeaves(root.right,cur_depth+1,depth); } public static void print(TreeNode root) { if(root == null)return; System.out.print(root.val+" "); System.out.println("er"); print(root.left); print(root.right); } public static HashSet<Integer>original(TreeNode root){ int d = depth(root); deepestLeaves(root,0,d); return set; } static HashSet<Integer>set1 = new HashSet<>(); static void leaves(TreeNode root) { if(root == null)return; if(root.left == null && root.right == null)set1.add(root.val); leaves(root.left); leaves(root.right); } public static boolean check(HashSet<Integer>s, HashSet<Integer>s1) { if(s.size() != s1.size())return false; for(int a : s) { if(!s1.contains(a))return false; } return true; } static TreeNode subTree; public static void smallest_subTree(TreeNode root) { if(root == null)return; smallest_subTree(root.left); smallest_subTree(root.right); set1 = new HashSet<>(); leaves(root); boolean smallest = check(set,set1); if(smallest) { subTree = root; return; } } public static TreeNode answer(TreeNode root) { smallest_subTree(root); return subTree; } } static class pair{ long first; long second; public pair(long first, long second) { this.first = first; this.second = second; } public long compareTo(pair p) { if(first == p.first)return second-p.second; return first-p.first; } } static class Compare{ static void compare(ArrayList<pair>arr) { Collections.sort(arr,new Comparator<pair>() { public int compare(pair p1, pair p2) { return (int) (p1.first-p2.first); } }); } } public static HashMap<Integer,Integer>sortByValue(HashMap<Integer,Integer>hm){ List<Map.Entry<Integer,Integer>>list = new LinkedList<Map.Entry<Integer,Integer>>(hm.entrySet()); Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>(){ public int compare(Map.Entry<Integer,Integer>o1, Map.Entry<Integer,Integer>o2) { return (o1.getValue()).compareTo(o2.getValue()); }}); HashMap<Integer,Integer>temp = new LinkedHashMap<Integer,Integer>(); for(Map.Entry<Integer,Integer>aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static class pairr implements Comparable<pairr>{ static Long value; Long index; public pairr(Long value, Long index) { this.value = value; this.index = index; } public int compareTo(pairr o) { return (int)(value-o.value); } } static class Key<K1, K2> { public K1 key1; public K2 key2; public Key(K1 key1, K2 key2) { this.key1 = key1; this.key2 = key2; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Key key = (Key) o; if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) { return false; } if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) { return false; } return true; } @Override public int hashCode() { int result = key1 != null ? key1.hashCode() : 0; result = 31 * result + (key2 != null ? key2.hashCode() : 0); return result; } @Override public String toString() { return "[" + key1 + ", " + key2 + "]"; } } public static int sumOfDigits (long n) { int sum = 0; while(n > 0) { sum += n%10; n /= 10; } return sum; } public static long binary_search(int s, int e, long num, long []ar) { if(s > e) { return -1; } int mid = (s+e)/2; if(s == e && ar[s] >= num) { return ar[s]; }else if(s == e && ar[s] < num) { return -1; }else if(ar[mid] < num) { return binary_search(mid+1,e,num,ar); }else if(ar[mid] >= num) { return binary_search(s,mid,num,ar); } return -1; } public static int index_search(int s, int e, long num, long []ar) { if(s > e) { return -1; } int mid = (s+e)/2; if(s == e && ar[s] >= num) { return s; }else if(s == e && ar[s] < num) { return -1; }else if(ar[mid] < num) { return index_search(mid+1,e,num,ar); }else if(ar[mid] >= num) { return index_search(s,mid,num,ar); } return -1; } public static void swap(int []ar, int i, int j) { for(int k= j; k >= i; k--) { int temp = ar[k]; ar[k] = ar[k+1]; ar[k+1] = temp; } } public static boolean digit_exists(long n) { while(n > 0) { if(n%10 == 9) return true; n = n/10; } return false; } public static int log(int n) { int c = 0; while(n > 0) { c++; n /=2; } return c; } public static int findOr(int[]bits){ int or=0; for(int i=0;i<32;i++){ or=or<<1; if(bits[i]>0) or=or+1; } return or; } static void simpleSieve(int limit, Vector<Integer> prime) { // Create a boolean array "mark[0..n-1]" and initialize // all entries of it as true. A value in mark[p] will // finally be false if 'p' is Not a prime, else true. boolean mark[] = new boolean[limit+1]; for (int i = 0; i < mark.length; i++) mark[i] = true; for (int p=2; p*p<limit; p++) { // If p is not changed, then it is a prime if (mark[p] == true) { // Update all multiples of p for (int i=p*p; i<limit; i+=p) mark[i] = false; } } // Print all prime numbers and store them in prime for (int p=2; p<limit; p++) { if (mark[p] == true) { prime.add(p); } } } // Prints all prime numbers smaller than 'n' public static void segmentedSieve(int n, ArrayList<Integer>l) { // Compute all primes smaller than or equal // to square root of n using simple sieve int limit = (int) (floor(sqrt(n))+1); Vector<Integer> prime = new Vector<>(); simpleSieve(limit, prime); // Divide the range [0..n-1] in different segments // We have chosen segment size as sqrt(n). int low = limit; int high = 2*limit; // While all segments of range [0..n-1] are not processed, // process one segment at a time while (low < n) { if (high >= n) high = n; // To mark primes in current range. A value in mark[i] // will finally be false if 'i-low' is Not a prime, // else true. boolean mark[] = new boolean[limit+1]; for (int i = 0; i < mark.length; i++) mark[i] = true; // Use the found primes by simpleSieve() to find // primes in current range for (int i = 0; i < prime.size(); i++) { // Find the minimum number in [low..high] that is // a multiple of prime.get(i) (divisible by prime.get(i)) // For example, if low is 31 and prime.get(i) is 3, // we start with 33. int loLim = (int) (floor(low/prime.get(i)) * prime.get(i)); if (loLim < low) loLim += prime.get(i); /* Mark multiples of prime.get(i) in [low..high]: We are marking j - low for j, i.e. each number in range [low, high] is mapped to [0, high-low] so if range is [50, 100] marking 50 corresponds to marking 0, marking 51 corresponds to 1 and so on. In this way we need to allocate space only for range */ for (int j=loLim; j<high; j+=prime.get(i)) mark[j-low] = false; } // Numbers which are not marked as false are prime for (int i = low; i<high; i++) if (mark[i - low] == true) l.add(i); // Update low and high for next segment low = low + limit; high = high + limit; } } public static int find_indexNum(long k) { long k1 = k; int power = 0; while(k > 0) { power++; k /=2 ; } long check = (long)Math.pow(2, power-1); if(k1 == check) { return power; } // System.out.println(power); long f = (long)Math.pow(2, power-1); long rem = k1-f; return find_indexNum(rem); } public static void shuffle(int []array, int num,int t_index, boolean []vis, int m ) { for(int i = 0; i < m; i++) { if(vis[i] == false) { int temp = array[i]; if(i < t_index) { vis[i] = true; } array[i] = num; array[t_index] = temp; // System.out.println(array[t_index]+" "+array[i]); break; } } } public static void rotate(int []arr,int j, int times, int m) { if(j == 0) { int temp1 = arr[0]; arr[0] = arr[times]; arr[times] = temp1; }else { int temp = arr[j]; int z = arr[0]; arr[0] = arr[times]; arr[j] = z; arr[times] = temp; } } public static void recur(int i,int A, int B,int []dp,int []metal, int n, boolean took,int ind) { if(i-A <= 0 && i-B <= 0)return; int count = 0; for(int j = 1; j <= n; j++) { if(dp[j] >= metal[j]) { count++; } } if(count == n)return; if(i-A >= 0 && i-B >= 0 && dp[i] > 0 && dp[i] > metal[i]) { dp[i]--; dp[i-A]++; dp[i-B]++; } if(ind == 6) { // System.out.println(Arrays.toString(dp)); } recur(i-A,A,B,dp,metal,n,took,ind); recur(i-B,A,B,dp,metal,n,took,ind); } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static boolean[] getSieve(int n) { boolean[] isPrime = new boolean[n+1]; for (int i = 2; i <= n; i++) isPrime[i] = true; for (int i = 2; i*i <= n; i++) if (isPrime[i]) for (int j = i; i*j <= n; j++) isPrime[i*j] = false; return isPrime; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } public static void dfs(LinkedList<Integer>[]list, HashMap<Integer,Integer>map, int parent, int n) { Stack<Integer>st = new Stack<>(); } public static boolean pos(int n) { int i = 1; boolean pos = false; while(i*i <= n) { if(i*i*2 == n || i*i*4 == n) { pos = true; break; } i++; } if(pos)return true; return false; } static long count = 0; public static void pairs (int []ar, int s, int e) { if(e <= s)return; // System.out.println(ar[s]+" "+ar[e]+" "+s+" "+e); if(ar[e]-ar[s] == e-s) { count++; //System.out.println("sdf"); } pairs(ar,s+1,e); pairs(ar,s,e-1); } public static class Pair1 implements Comparable <Pair1>{ int value; int index; public Pair1(int value, int index) { this.value = value; this.index = index; } public int compareTo(Pair1 o) { return o.value-value; } } public static long ways(long n) { return (n*(n-1))/2; } 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+= 6) { if(n%i == 0 || n%(i+2) == 0) return false; } return true; } static long nextPrime(long n) { boolean found = false; long prime = n; while(!found) { prime++; if(isPrime(prime)) found = true; } return prime; } public static boolean isValid(int h, int m, int hour, int minute) { int a = flip(hour / 10); if (a == -1) return false; int b = flip(hour % 10); if (b == -1) return false; int c = flip(minute / 10); if (c == -1) return false; int d = flip(minute % 10); if (d == -1) return false; if (10 * d + c >= h) return false; if (10 * b + a >= m) return false; return true; } public static int flip(int x) { if (x == 0) return 0; if (x == 1) return 1; if (x == 2) return 5; if (x == 5) return 2; if (x == 8) return 8; return -1; } static long maximum(long a, long b, long c, long d) { long m = Math.max(a, b); long m1 = Math.max(c, d); return Math.max(m1, m1); } static long minimum(long a, long b, long c,long d) { long m = Math.min(a, b); long m1 = Math.min(c, d); return Math.min(m, m1); } static long ans = 0; public static void solve1(boolean [][]vis,long [][]mat, int r, int c, int r2, int c2, int r1, int c1, int r3, int c3) { if(r > r1 || c > c1 || r > r2 || c > c2 || r1 > r3 || c1 > c3 || r3 < r2 || c3 < c2 || vis[r][c] || vis[r1][c1]|| vis[r2][c2] || vis[r3][c3]) return; vis[r][c] = true; vis[r1][c1] = true; vis[r2][c2] = true; vis[r3][c3] = true; long max = maximum(mat[r][c],mat[r1][c1],mat[r2][c2],mat[r3][c3]); long min = minimum(mat[r][c],mat[r1][c1],mat[r2][c2],mat[r3][c3]); long a = mat[r][c]; long b = mat[r1][c1]; long c4 = mat[r2][c2]; long d =mat[r3][c3]; long []p = {a,b,c4,d}; Arrays.sort(p); long temp = (p[2]+p[3]-p[0]-p[1]); if(r == r1 && r == r2 && r2 == r3 && r1 == r3) temp /= 2; System.out.println(Arrays.toString(p)); ans += temp; solve1(vis,mat,r+1,c,r2+1,c2,r1-1,c1,r3-1,c3); solve1(vis,mat,r,c+1,r2,c2-1,r1,c1+1,r3,c3-1); solve1 (vis,mat,r+1,c+1,r2+1,c2-1,r1-1,c1+1,r3-1,c3-1); } private static int solve(int[][] mat, int i, int c, int j, int c2, int k, int c1, int l, int c3) { // TODO Auto-generated method stub return 0; } public static int dfs(int parent, LinkedList<Integer>[]list) { for(int i : list[parent]) { if(list[parent].size() == 0) { return 0; }else { return 1 + dfs(i,list); } } return 0; } public static long answer = Integer.MAX_VALUE; public static void min_Time(int [][]dp, int i, HashSet<Integer>set, int min, int r, int c) { if(i > r) { answer = Math.min(answer, min); return; } if(min > answer)return; for(int j = i; j <= c; j++) { if(!set.contains(j)) { set.add(j); min += dp[i][j]; min_Time(dp,i+1,set,min,r,c); min -= dp[i][j]; set.remove(j); } } } public static void dp(int [][]dp, int r, int c, int o, int z, long sum) { if(r > o) { answer = Math.min(answer, sum); } if(r > o || c > z) { return; } if(sum > answer)return; sum += dp[r][c]; dp(dp,r+1,c+1,o,z,sum); sum -= dp[r][c]; dp(dp,r,c+1,o,z,sum); } static HashSet<ArrayList<Integer>>l = new HashSet<>(); public static void fourSum(Deque<Integer>ll, int i, int target, int []ar, int n) { if(ll.size() == 4) { int sum = 0; ArrayList<Integer>list = new ArrayList<>(); for(int a : ll) { sum += a; list.add(a); } if(sum == target) { Collections.sort(list); l.add(list); // System.out.println(ll); } return; } for(int j = i; j < n; j++) { ll.add(ar[j]); fourSum(ll,j+1,target,ar,n); ll.removeLast(); } } static int max_bottles(int cur, int exchange, int n){ if(cur == exchange){ cur = 0; n++; } if(n == 0)return 0; return 1+ max_bottles(cur+1,exchange,n-1); } public static void fill(int [][]mat, List<Integer>ans, int row_start, int row_end, int col_start, int col_end) { for(int i = col_start; i <= col_end; i++) { ans.add(mat[row_start][i]); } for(int i = row_start+1; i <= row_end; i++) { ans.add(mat[i][col_end]); } if(col_start == col_end)return; if(row_start == row_end)return; for(int i = col_end-1; i >= col_start; i--) { ans.add(mat[row_end][i]); } for(int i = row_end-1; i >= row_start+1; i--) { ans.add(mat[i][col_start]); } } public static void create(int [][]mat, int j, int i, int k) { if(i < 1 || j >= mat.length)return; mat[j][i] = k; create(mat,j+1,i-1,k+1); } public static long sum(int [][]mat, int x1, int y1, int x2, int y2) { long sum = 0; while(x1 <= x2) { sum += mat[x1][y1]; // System.out.println(mat[x1][y1]); x1++; } y1++; while(y1 <= y2) { sum += mat[x2][y1]; y1++; } return sum; } public static boolean allneg(int []ar, int n) { for(int i = 0; i < n; i++) { if(ar[i] >= 0)return false; } return true; } public static boolean allpos(int []ar, int n) { for(int i = 0; i < n; i++) { if(ar[i] <= 0)return false; } return true; } public static int max_pos(int []ar, int n) { int min = Integer.MAX_VALUE; for(int i = 1; i < n; i++) { if(ar[i] > 0) { break; } int a = Math.abs(ar[i]-ar[i-1]); min = Math.min(min, a); } int c = 0; boolean zero = false; TreeSet<Integer>set = new TreeSet<>(); int neg = 0; for(int i = 0; i < n; i++) { if(ar[i] <= 0) {neg++; if(ar[i] == 0) zero = true; continue;} if(ar[i] <= min) { c = 1; } } neg += c; return neg; } static final int MAX = 10000000; // prefix[i] is going to store count // of primes till i (including i). static int prefix[] = new int[MAX + 1]; static void buildPrefix() { // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. boolean prime[] = new boolean[MAX + 1]; Arrays.fill(prime, true); for (int p = 2; p * p <= MAX; 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 * 2; i <= MAX; i += p) prime[i] = false; } } // Build prefix array prefix[0] = prefix[1] = 0; for (int p = 2; p <= MAX; p++) { prefix[p] = prefix[p - 1]; if (prime[p]) prefix[p]++; } } static int query(int L, int R) { return prefix[R] - prefix[L - 1]; } static void alter(int n) { int ans = 0; boolean []vis = new boolean[n+1]; for(int i = 2; i <= n; i++){ boolean p = false; if(vis[i] == false) { for(int j = i; j <= n; j+=i) { if(vis[j] == true) { p = true; }else { vis[j] = true; } } if(!p)ans++; } } System.out.println(ans); } public static void solveDK(int []dp, int i, int D, int K) { int d = D/K; int ans = -1; int ind = d+1; while(ind < i) { int temp = i/ind; temp--; if(dp[temp*ind] == temp) { ans = dp[temp*ind]+1; dp[i] = ans; break; } ind = ind*2; } if(ans == -1) dp[i] = 1; } public static void solveKD(int []dp, int i, int D, int K) { int d = K/D; int ans = -1; int ind = d+1; while(ind < i) { int temp = i/ind; temp--; if(dp[temp*ind] == temp) { ans = dp[temp*ind]+1; dp[i] = ans; break; } ind = ind*2; } if(ans == -1) dp[i] = 1; } static int countGreater(int arr[], int n, int k) { int l = 0; int r = n - 1; // Stores the index of the left most element // from the array which is greater than k int leftGreater = n; // Finds number of elements greater than k while (l <= r) { int m = l + (r - l) / 2; // If mid element is greater than // k update leftGreater and r if (arr[m] > k) { leftGreater = m; r = m - 1; } // If mid element is less than // or equal to k update l else l = m + 1; } // Return the count of elements greater than k return (n - leftGreater); } static ArrayList<Integer>printDivisors(int n) { // Note that this loop runs till square root ArrayList<Integer>list = 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) list.add(i); else // Otherwise print both list.add(i); list.add(n/i); } } return list; } static boolean isPossible(String s, String str, int i, int j) { // System.out.println(i+" "+j); int x = i; int y = j; while(i >= 0 && j < str.length()) { if(s.charAt(i) != str.charAt(j)) { break; } i--; j++; } if(j == str.length()) { System.out.println(x+" "+y); return true; } return false; } static void leftRotate(int l, int r,int arr[], int d) { for (int i = 0; i < d; i++) leftRotatebyOne(l,r,arr); } static void leftRotatebyOne(int l, int r,int arr[]) { int i, temp; temp = arr[l]; for (i = l; i < r; i++) arr[i] = arr[i + 1]; arr[r] = temp; } static long modInverse(long a, long m) { long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient long q = a / m; long t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long solvetheproblem(long n, long k) { long mod = 1000000007; long ansss = 0; while(n > 0) { // Nearest power of 2<=N long p = (long)(Math.log(n) / Math.log(2));; // Now insert k^p in the answer long temp = (long)power(k,p,mod); ansss += temp; ansss %= mod; // update n n %= (long)Math.pow(2, p); } // Print the ans in sorted order return ansss%mod ; } static boolean pos (int [][]mat, int r, int c, int n, boolean [][]vis) { if(r <= 0 || c <= 0 || r > 2 || c > n )return false; if(r == 2 && c == n)return true; if(vis[r][c])return false; vis[r][c] = true; if(mat[r][c] == 1)return false; boolean a = pos(mat,r+1,c,n,vis); boolean b = pos(mat,r,c+1,n,vis); boolean d = pos(mat,r+1,c+1,n,vis); boolean e = pos(mat,r-1,c+1,n,vis); return a || b || d || e; } static long sameremdiv(long x, long y) { if(x <= y) { y = x-1; } long sq = (long)Math.sqrt(x); if(y <= sq) { y--; long ans = (y*(y+1))/2; return ans; }else { long ans = 0; long dif = y-sq; sq -= 2; if(sq > 1) ans += (sq*(sq+1))/2; long d = x/y; sq+=2; for(int i = (int)sq; i <= y; i++) { if(i > 1 ) { long temp = x/i; if(x%i < temp)temp--; ans += temp; } } return ans; } } static int binary(long []ar, long element, int s, int e) { int mid = (s+e)/2; // System.out.println(mid); if(s > e)return mid; if(ar[mid] == element) { return mid; }else if(ar[mid] > element) { return binary(ar,element,s,mid-1); }else { return binary(ar,element,mid+1,e); } } static boolean isGibbrish(HashSet<String>set, String str, int j) { StringBuilder sb = new StringBuilder(); if(j >= str.length()) { return true; } for(int i = j; i < str.length(); i++) { sb.append(str.charAt(i)); String temp = sb.toString(); if(set.contains(temp)) { boolean test = isGibbrish(set,str,i+1); if(test)return true; } } return false; } static TreeNode buildTree(TreeNode root,int []ar, int l, int r){ if(l > r)return null; int len = l+r; if(len%2 != 0)len++; int mid = (len)/2; int v = ar[mid]; TreeNode temp = new TreeNode(v); root = temp; root.left = buildTree(root.left,ar,l,mid-1); root.right = buildTree(root.right,ar,mid+1,r); return root; } public static int getClosest(int val1, int val2, int target) { if (target - val1 >= val2 - target) return val2; else return val1; } public static int findClosest(int start, int end, int arr[], int target) { int n = arr.length; // Corner cases if (target <= arr[start]) return arr[start]; if (target >= arr[end]) return arr[end]; // Doing binary search int i = start, j = end+1, mid = 0; while (i < j) { mid = (i + j) / 2; if (arr[mid] == target) return arr[mid]; /* If target is less than array element, then search in left */ if (target < arr[mid]) { // If target is greater than previous // to mid, return closest of two if (mid > 0 && target > arr[mid - 1]) return getClosest(arr[mid - 1], arr[mid], target); /* Repeat for left half */ j = mid; } // If target is greater than mid else { if (mid < n-1 && target < arr[mid + 1]) return getClosest(arr[mid], arr[mid + 1], target); i = mid + 1; // update i } } // Only single element left after search return arr[mid]; } static int lis(int arr[], int n) { int lis[] = new int[n]; int i, j, max = 0; /* Initialize LIS values for all indexes */ for (i = 0; i < n; i++) lis[i] = 1; /* Compute optimized LIS values in bottom up manner */ for (i = 1; i < n; i++) for (j = 0; j < i; j++) if (arr[i] >= arr[j] && lis[i] < lis[j] + 1) lis[i] = lis[j] + 1; /* Pick maximum of all LIS values */ for (i = 0; i < n; i++) if (max < lis[i]) max = lis[i]; return max; } static List<List<Integer>>res = new ArrayList<>(); public static void lists(List<Integer>list, List<Integer>temp, int target, int j) { int sum = 0; for(int i = 0; i < temp.size(); i++) { sum += temp.get(i); } if(sum > target) { return; }else if(sum == target) { Collections.sort(temp); boolean exist = false; for(List<Integer>l : res) { if(l.size() == temp.size()) { int c = 0; for(int i = 0; i < l.size(); i++) { if(l.get(i) == temp.get(i))c++; } if(c == l.size()) {exist = true; break;} } } if(!exist) { res.add(new ArrayList<>(temp)); } }else if(j == list.size())return; for(int i = j; i < list.size(); i++){ temp.add(list.get(i)); lists(list,temp,target,i+1); temp.remove(temp.size()-1); } return; } static ArrayList<Integer>dfs(int p, int c, ArrayList<Integer>l,HashMap<Integer,Integer>map, LinkedList<Integer>[]list, int v) { l.add(map.get(c)); System.out.println(c); if(c == v) { //System.out.println(c); return l; } for(int j : list[c]) { if(c == 1) { // System.out.println("Yes"+" "+j); // System.out.println("yes"+" "+list[c]); } if(j != p) { dfs(c,j,l,map,list,v); } } return new ArrayList<>(); } static boolean pos(char []a, int j) { char []ar = new char[a.length]; for(int i = 0; i < a.length; i++) { ar[i] = a[i]; } ar[j] = 'c'; ar[j-1] = 'a'; ar[j-2] = 'b'; ar[j-3] = 'a'; ar[j+1] = 'a'; ar[j+2] = 'b'; ar[j+3] = 'a'; int count = 0; for(int i = 3; i < ar.length-3; i++) { if(ar[i] == 'c' && ar[i-1] == 'a' && ar[i-2] == 'b' && ar[i-3] == 'a' && ar[i+1] == 'a' && ar[i+2] == 'b' && ar[i+3] == 'a') { count++; } } if(count == 1)return true; return false; } static void bruteforce(String s) { String ans = s.charAt(0)+""; ans += ans; StringBuilder sb = new StringBuilder(); sb.append(s.charAt(0)); for(int i = 1;i < s.length(); i++){ String d = sb.toString(); sb.reverse(); d += sb.toString(); boolean con = true; System.out.println(d+" "+s); for(int j = 0; j < Math.min(d.length(), s.length()); j++) { if(s.charAt(j) > d.charAt(j)) { con = false; break; } } sb.reverse(); sb.append(s.charAt(i)); if(con) { ans = d; break; } } System.out.println(ans+" "+"yes"); } static void permute(String s , String answer) { if (s.length() == 0) { System.out.print(answer + " "); return; } for(int i = 0 ;i < s.length(); i++) { char ch = s.charAt(i); String left_substr = s.substring(0, i); String right_substr = s.substring(i + 1); String rest = left_substr + right_substr; permute(rest, answer + ch); } } static List<List<Integer>>result = new ArrayList<>(); public static void comb(List<Integer>list, int n,int []ar, HashSet<Integer>set) { if(list.size() == n) { boolean exist = false; for(List<Integer>l : result) { int c = 0; for(int i = 0; i < l.size(); i++) { if(l.get(i) == list.get(i))c++; } if(c == n) { exist = true; break;} } if(!exist)result.add(new ArrayList<>(list)); } for(int j = 0; j < n; j++) { if(!set.contains(j)) { list.add(ar[j]); set.add(j); comb(list,n,ar,set); set.remove(j); list.remove(list.size()-1); } } return; } static void pinkSeat(int [][]mat, int i, int j, int n, int m, boolean vis[][], int [][]res) { if(i <= 0 || j <= 0 || i > n || j > m || vis[i][j])return; int a = Math.abs(i-1); int b = Math.abs(j-1); int c = Math.abs(i-1); int d = Math.abs(j-m); int e = Math.abs(i-n); int f = Math.abs(j-m); int x = Math.abs(i-n); int y = Math.abs(j-1); vis[i][j] = true; int max = Math.max(a+b,c+d); max = Math.max(max, e+f); max = Math.max(max, x+y); res[i][j] = max; pinkSeat(mat,i-1,j-1,n,m,vis,res); pinkSeat(mat,i+1,j-1,n,m,vis,res); pinkSeat(mat,i-1,j+1,n,m,vis,res); pinkSeat(mat,i+1,j+1,n,m,vis,res); pinkSeat(mat,i,j-1,n,m,vis,res); pinkSeat(mat,i,j+1,n,m,vis,res); pinkSeat(mat,i-1,j,n,m,vis,res); pinkSeat(mat,i+1,j,n,m,vis,res); } static boolean isPowerOfTwo(long n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static long ksearch(long s,long e, long []ar, long ans, long h, int n, long k) { if(s > e)return k; long mid = (s+e)/2; long count = 0; for(int i = 0; i < n; i++) { count += (ar[i])/mid; if(ar[i]%mid > 0)count++; } if(count <= h) { if(mid <= k) { ans = count; k = mid; } return ksearch(s, mid-1,ar,ans,h,n,k); }else { return ksearch(mid+1,e,ar,ans,h,n,k); } } static long mod = 1000000000; static long ans2 = 0; static void shirtCombo(long x, HashSet<Integer>set,int j, ArrayList<List<Integer>>list) { if(set.size() == list.size()) { ans2 = (ans2+x)%mod; } if(j == list.size()) { return; } int c = 0; List<Integer>temp = list.get(j); for(int a : temp ) { if(!set.contains(a)) { c++; set.add(a); shirtCombo(1,set,j+1,list); set.remove(a); } } x = (x*c)%mod; } public static class Pair { int x; int y; // Constructor public Pair(int x, int y) { this.x = x; this.y = y; } } static class Compare1{ static void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return p1.x - p2.x; } }); } } static long gcd1(long a, long b) { if (a == 0) return b; return gcd1(b % a, a); } // method to return LCM of two numbers static long lcm(long a, long b) { return (a / gcd1(a, b)) * b; } static ArrayList<List<Integer>>res1 = new ArrayList<>(); static void permuteNumbers(LinkedHashSet<Integer>set, int n) { if(res1.size() == n)return; if(set.size() == n) { List<Integer>l = new ArrayList<>(); for(int a : set) { l.add(a); } boolean pos = true; for(int i = 2; i < l.size(); i++) { if(l.get(i-1) + l.get(i-2) == l.get(i)) { pos = false; break; } } if(pos) { res1.add(new ArrayList(l)); } return; } for(int i = 1; i <= n; i++) { if(!set.contains(i)) { set.add(i); permuteNumbers(set,n); set.remove(i); } } } public static void main(String args[])throws IOException { BufferedReader ob = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(ob.readLine()); while(t --> 0) { /* StringTokenizer st = new StringTokenizer(ob.readLine()); int n = Integer.parseInt(st.nextToken()); int n = Integer.parseInt(ob.readLine()); int []ar = new int[n]; */ int n = Integer.parseInt(ob.readLine()); long []ar = new long[n]; StringTokenizer st = new StringTokenizer(ob.readLine()); for(int i = 0; i < n; i++) { ar[i] = Long.parseLong(st.nextToken()); } if(ar[n-2] > ar[n-1]) { System.out.println(-1); }else if(ar[n-1] < 0 && ar[n-2] < 0){ boolean pos = true; for(int i = 1; i < n; i++) { if(ar[i] < ar[i-1]) { pos = false; break; } } System.out.println(pos == false?-1:0); }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
56952965dc734d634658e1596ecd2f45
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.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * * @author pttrung */ public class C_Round_772_Div2 { public static int MOD = 998244353; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int T = in.nextInt(); mainLoop: for (int z = 0; z < T; z++) { int n = in.nextInt(); long[] data = new long[n]; for (int i = 0; i < n; i++) { data[i] = in.nextInt(); } if (data[n - 1] < data[n - 2]) { out.println(-1); continue; } ArrayList<Move> list = new ArrayList<>(); for (int i = n - 3; i >= 0; i--) { long v = data[i + 1] - data[n - 1]; if (data[i] > v) { list.add(new Move(i + 1, i + 2, n)); data[i] = v; } if (data[i] > data[i + 1]) { out.println(-1); continue mainLoop; } } out.println(list.size()); for (Move move : list) { out.println(move.x + " " + move.y + " " + move.z); } } out.close(); } static class Move { int x, y, z; Move(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } } static int abs(int a) { return a < 0 ? -a : a; } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point { int x; int y; public Point(int start, int end) { this.x = start; this.y = end; } public String toString() { return x + " " + y; } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return (val * val); } else { return (val * ((val * a))); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
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
4676f3dfdd92275e64e4f5087cc804c4
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
/* * Created by cravuri on 2/21/22 */ import java.util.Scanner; public class C { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int t = 1; t <= T; t++) { int N = sc.nextInt(); int[] a = new int[N]; for (int i = 0; i < N; i++) { a[i] = sc.nextInt(); } if (a[N - 1] < a[N - 2]) { System.out.println(-1); continue; } if (a[N - 1] >= 0) { System.out.println(N - 2); for (int i = 1; i <= N - 2; i++) { System.out.println(i + " " + (N - 1) + " " + N); } continue; } boolean sorted = true; for (int i = 0; i < N - 1; i++) { if (a[i] > a[i + 1]) { sorted = false; break; } } if (sorted) { System.out.println(0); } else { System.out.println(-1); } } } }
Java
["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"]
2 seconds
["2\n1 2 3\n3 4 5\n-1\n0"]
NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations.
Java 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
511996cc154374a961539a4a4044fbab
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 faltu; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.Map.Entry; public class Main { static long LowerBound(long[] a2, long x) { // x is the target value or key int l=-1,r=a2.length; while(l+1<r) { int m=(l+r)>>>1; if(a2[m]>=x) r=m; else l=m; } return r; } static int UpperBound(int a[], int x) {// x is the key or target value 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 long getClosest(long val1, long val2,long target) { if (target - val1 >= val2 - target) return val2; else return val1; } static void ruffleSort(long[] a) { int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { long oi=r.nextInt(n), temp=a[i]; a[i]=a[(int)oi]; a[(int)oi]=temp; } Arrays.sort(a); } static void ruffleSort(int[] a){ int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n), temp=a[i]; a[i]=a[oi]; a[oi]=temp; } Arrays.sort(a); } int ceilIndex(int input[], int T[], int end, int s){ int start = 0; int middle; int len = end; while(start <= end){ middle = (start + end)/2; if(middle < len && input[T[middle]] < s && s <= input[T[middle+1]]){ return middle+1; }else if(input[T[middle]] < s){ start = middle+1; }else{ end = middle-1; } } return -1; } public static int findIndex(long arr[], long 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; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } public static int[] swap(int a[], int left, int right) { int temp = a[left]; a[left] = a[right]; a[right] = temp; return a; } public static void swap(long x,long max1) { long temp=x; x=max1; max1=temp; } public static int[] reverse(int a[], int left, int right) { // Reverse the sub-array while (left < right) { int temp = a[left]; a[left++] = a[right]; a[right--] = temp; } return a; } static int lowerLimitBinarySearch(ArrayList<Integer> A,int B) { int n =A.size(); int first = 0,second = n; while(first <second) { int mid = first + (second-first)/2; if(A.get(mid) > B) { second = mid; }else { first = mid+1; } } if(first < n && A.get(first) < B) { first++; } return first; //1 index } 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; } } // *******----segement tree implement---***** // -------------START-------------------------- void buildTree (int[] arr,int[] tree,int start,int end,int treeNode) { if(start==end) { tree[treeNode]=arr[start]; return; } buildTree(arr,tree,start,end,2*treeNode); buildTree(arr,tree,start,end,2*treeNode+1); tree[treeNode]=tree[treeNode*2]+tree[2*treeNode+1]; } void updateTree(int[] arr,int[] tree,int start,int end,int treeNode,int idx,int value) { if(start==end) { arr[idx]=value; tree[treeNode]=value; return; } int mid=(start+end)/2; if(idx>mid) { updateTree(arr,tree,mid+1,end,2*treeNode+1,idx,value); } else { updateTree(arr,tree,start,mid,2*treeNode,idx,value); } tree[treeNode]=tree[2*treeNode]+tree[2*treeNode+1]; } // disjoint set implementation --start static void makeSet(int n) { parent=new int[n]; rank=new int[n]; for(int i=0;i<n;i++) { parent[i]=i; rank[i]=0; } } static void union(int u,int v) { u=findpar(u); v=findpar(v); if(rank[u]<rank[v])parent[u]=v; else if(rank[v]<rank[u])parent[v]=u; else { parent[v]=u; rank[u]++; } } private static int findpar(int node) { if(node==parent[node])return node; return parent[node]=findpar(parent[node]); } static int parent[]; static int rank[]; // *************end static Long MOD=(long) (1e9+7); static boolean coprime(int a, long l){ return (gcd(a, l) == 1); } static long[][] dp; static long[]dpp; static ArrayList<Integer>arr; static boolean[] vis; static ArrayList<ArrayList<Integer>>adj; public static void main(String[] args) throws IOException { // sieve(); FastReader s = new FastReader(); long tt = s.nextLong(); while(tt-->0) { int n=s.nextInt(); long[]a=new long[n]; for(int i=0;i<n;i++)a[i]=s.nextLong(); long[]b=a.clone(); Arrays.sort(b); if(Arrays.equals(a, b))System.out.println(0); else if(a[n-1]<a[n-2])System.out.println(-1); else { a[n-3]=a[n-2]-a[n-1]; if(a[n-3]<=a[n-2]&&a[n-3]<=a[n-1]) { System.out.println((n-2)); for(int i=0;i<=n-3;i++) { System.out.println((i+1)+" "+(n-1)+" "+(n)); } } else System.out.println(-1); } } } public static int swap(int[] arr, boolean op1) { int count = 0; while(!isSorted(arr)) { if(op1) { for(int i = 0; i < arr.length; i += 2) { int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } } else { int n = arr.length/2; for(int i = 0; i < n; i++) { int temp = arr[i]; arr[i] = arr[n + i]; arr[n + i] = temp; } } count++; op1 = !op1; if(arr[0] == 1 && !isSorted(arr)) { return Integer.MIN_VALUE; } } return count; } public static boolean isSorted(int[] arr) { int prev = arr[0]; for(int i = 1; i < arr.length; i++) { if(arr[i] < prev) return false; prev = arr[i]; } return true; } static void DFSUtil(int v, boolean[] visited) { visited[v] = true; Iterator<Integer> it = adj.get(v).iterator(); while (it.hasNext()) { int n = it.next(); if (!visited[n]) DFSUtil(n, visited); } } static long DFS(int n) { boolean[] visited = new boolean[n+1]; long cnt=0; for (int i = 1; i <= n; i++) { if (!visited[i]) { DFSUtil(i, visited); cnt++; } } return cnt; } public static String revStr(String str){ String input = str; StringBuilder input1 = new StringBuilder(); input1.append(input); input1.reverse(); return input1.toString(); } public static String sortString(String inputString){ char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } static long myPow(long n, long i){ if(i==0) return 1; if(i%2==0) return (myPow(n,i/2)%MOD * myPow(n,i/2)%MOD)%MOD; return (n%MOD* myPow(n,i-i)%MOD)%MOD; } static void palindromeSubStrs(String str) { HashSet<String>set=new HashSet<>(); char[]a =str.toCharArray(); int n=str.length(); int[][]dp=new int[n][n]; for(int g=0;g<n;g++){ for(int i=0,j=g;j<n;j++,i++){ if(!set.contains(str.substring(i,i+1))&&g==0) { dp[i][j]=1; set.add(str.substring(i,i+1)); } else { if(!set.contains(str.substring(i,j+1))&&isPalindrome(str,i,j)) { dp[i][j]=1; set.add(str.substring(i,j+1)); } } } } int ans=0; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { System.out.print(dp[i][j]+" "); if(dp[i][j]==1)ans++; } System.out.println(); } System.out.println(ans); } static boolean isPalindrome(String str,int i,int j) { while (i < j) { if (str.charAt(i) != str.charAt(j)) return false; i++; j--; } return true; } static boolean sign(long num) { return num>0; } static boolean isSquare(long x){ if(x==1)return true; long y=(long) Math.sqrt(x); return y*y==x; } static long power1(long a,long b) { if(b == 0){ return 1; } long ans = power(a,b/2); ans *= ans; if(b % 2!=0){ ans *= a; } return ans; } static void swap(StringBuilder sb,int l,int r) { char temp = sb.charAt(l); sb.setCharAt(l,sb.charAt(r)); sb.setCharAt(r,temp); } // function to reverse the string between index l and r static void reverse(StringBuilder sb,int l,int r) { while(l < r) { swap(sb,l,r); l++; r--; } } // function to search a character lying between index l and r // which is closest greater (just greater) than val // and return it's index static int binarySearch(StringBuilder sb,int l,int r,char val) { int index = -1; while (l <= r) { int mid = (l+r)/2; if (sb.charAt(mid) <= val) { r = mid - 1; } else { l = mid + 1; if (index == -1 || sb.charAt(index) >= sb.charAt(mid)) index = mid; } } return index; } // this function generates next permutation (if there exists any such permutation) from the given string // and returns True // Else returns false static boolean nextPermutation(StringBuilder sb) { int len = sb.length(); int i = len-2; while (i >= 0 && sb.charAt(i) >= sb.charAt(i+1)) i--; if (i < 0) return false; else { int index = binarySearch(sb,i+1,len-1,sb.charAt(i)); swap(sb,i,index); reverse(sb,i+1,len-1); return true; } } private static int lps(int m ,int n,String s1,String s2,int[][]mat) { for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { if(s1.charAt(i-1)==s2.charAt(j-1))mat[i][j]=1+mat[i-1][j-1]; else mat[i][j]=Math.max(mat[i-1][j],mat[i][j-1]); } } return mat[m][n]; } static String lcs(String X, String Y, int m, int n) { int[][] L = new int[m+1][n+1]; // Following steps build L[m+1][n+1] in bottom up fashion. Note // that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] for (int i=0; i<=m; i++) { for (int j=0; j<=n; j++) { if (i == 0 || j == 0) L[i][j] = 0; else if (X.charAt(i-1) == Y.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]); } } // Following code is used to print LCS int index = L[m][n]; int temp = index; // Create a character array to store the lcs string char[] lcs = new char[index+1]; lcs[index] = '\u0000'; // Set the terminating character // Start from the right-most-bottom-most corner and // one by one store characters in lcs[] int i = m; int j = n; while (i > 0 && j > 0) { // If current character in X[] and Y are same, then // current character is part of LCS if (X.charAt(i-1) == Y.charAt(j-1)) { // Put current character in result lcs[index-1] = X.charAt(i-1); // reduce values of i, j and index i--; j--; index--; } // If not same, then find the larger of two and // go in the direction of larger value else if (L[i-1][j] > L[i][j-1]) i--; else j--; } return String.valueOf(lcs); // Print the lcs // System.out.print("LCS of "+X+" and "+Y+" is "); // for(int k=0;k<=temp;k++) // System.out.print(lcs[k]); } static long lis(long[] aa2, int n) { long lis[] = new long[n]; int i, j; long max = 0; for (i = 0; i < n; i++) lis[i] = 1; for (i = 1; i < n; i++) for (j = 0; j < i; j++) if (aa2[i] >= aa2[j] && lis[i] <= lis[j] + 1) lis[i] = lis[j] + 1; for (i = 0; i < n; i++) if (max < lis[i]) max = lis[i]; return max; } static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) { if (str.charAt(i) != str.charAt(j)) return false; i++; j--; } return true; } static boolean issafe(int i, int j, int r,int c, char ch) { if (i < 0 || j < 0 || i >= r || j >= c|| ch!= '1')return false; else return true; } static long power(long a, long b) { a %=MOD; long out = 1; while (b > 0) { if((b&1)!=0)out = out * a % MOD; a = a * a % MOD; b >>= 1; a*=a; } return out; } static long[] sieve; public static void sieve() { int nnn=(int) 1e6+1; long nn=(int) 1e6; sieve=new long[(int) nnn]; int[] freq=new int[(int) nnn]; sieve[0]=0; sieve[1]=1; for(int i=2;i<=nn;i++) { sieve[i]=1; freq[i]=1; } for(int i=2;i*i<=nn;i++) { if(sieve[i]==1) { for(int j=i*i;j<=nn;j+=i) { if(sieve[j]==1) { sieve[j]=0; } } } } } } class decrease implements Comparator<Long> { // Used for sorting in ascending order of // roll number public int compare(long a, long b) { return (int) (b - a); } @Override public int compare(Long o1, Long o2) { // TODO Auto-generated method stub return (int) (o2-o1); } } class pair{ long x; long y; long c; public pair(long x,long y) { this.x=x; this.y=y; } public pair(long x,long y,long c) { this.x=x; this.y=y; this.c=c; } } class Pair { int idx; String str; public Pair(String str,int idx) { this.str=str; this.idx=idx; } }
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
a30021811e3b8c6ae6927279fd73328a
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 FastReader sc=null; public static void main(String[] args) { sc=new FastReader(); int t=sc.nextInt(); PrintWriter out=new PrintWriter(System.out); for(int tt=0;tt<t;tt++) { int n=sc.nextInt(); int a[]=sc.readArray(n); if(isSorted(a)) { out.println(0); continue; } if(a[n-2]>a[n-1] || (a[n-2]-a[n-1]>a[n-2])) { out.println(-1); continue; } out.println(n-2); for(int i=1;i+2<=n;i++) { out.println(i+" "+(n-1)+" "+n); } } out.close(); } static boolean isSorted(int a[]) { int n=a.length; for(int i=0;i+1<n;i++) { if(a[i]>a[i+1])return false; } return true; } static int[] ruffleSort(int a[]) { ArrayList<Integer> al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i<a.length;i++)a[i]=al.get(i); return a; } static void print(int a[]) { for(int e:a) { System.out.print(e+" "); } System.out.println(); } static class FastReader{ StringTokenizer st=new StringTokenizer(""); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String next() { while(!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); return a; } } }
Java
["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"]
2 seconds
["2\n1 2 3\n3 4 5\n-1\n0"]
NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations.
Java 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
f052fe438e077853e7c1587dee9bce21
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) { new C().run(); } BufferedReader br; PrintWriter out; long mod = (long) (1e9 + 7), inf = (long) (3e18); class pair { int F, S; pair(int f, int s) { F = f; S = s; } } void solve() { int t = ni(); while(t-- > 0) { //TODO: int n= ni(); long[] a= new long[n+1]; for(int i=1; i<n+1; i++){ a[i]=nl(); } if(a[n-1]>a[n]){ out.println(-1); continue; } if(a[n]<0){ if(isSorted(a)){ out.println(0); } else{ out.println(-1); } } else{ out.println(n-2); for(int i=1; i<=n-2; i++){ out.println(i+" "+(n-1)+" "+(n)); } } } } boolean isSorted(long[] a){ for(int i=1; i<a.length-1; i++){ if(a[i]>a[i+1]){ return false; } } return true; } // -------- I/O Template ------------- char nc() { return ns().charAt(0); } String nLine() { try { return br.readLine(); } catch(IOException e) { return "-1"; } } double nd() { return Double.parseDouble(ns()); } long nl() { return Long.parseLong(ns()); } int ni() { return Integer.parseInt(ns()); } int[] na(int n) { int a[] = new int[n]; for(int i = 0; i < n; i++) a[i] = ni(); return a; } StringTokenizer ip; String ns() { if(ip == null || !ip.hasMoreTokens()) { try { ip = new StringTokenizer(br.readLine()); if(ip == null || !ip.hasMoreTokens()) ip = new StringTokenizer(br.readLine()); } catch(IOException e) { throw new InputMismatchException(); } } return ip.nextToken(); } void run() { try { if (System.getProperty("ONLINE_JUDGE") == null) { br = new BufferedReader(new FileReader("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/input.txt")); out = new PrintWriter("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/output.txt"); } else { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } catch (FileNotFoundException e) { System.out.println(e); } solve(); out.flush(); } }
Java
["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"]
2 seconds
["2\n1 2 3\n3 4 5\n-1\n0"]
NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations.
Java 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
30715cf17f9e3d59bae75182a9c5e4b0
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 C_Differential_Sorting { static Scanner in = new Scanner(System.in); static int testCases, n; static long a[]; static void solve() { boolean sorted = true; for (int i = 1; i < n; i++) { if (a[i] < a[i - 1]) { sorted = false; break; } } if (sorted) { System.out.println(0); return; } if(a[n-1]<0 ){ for(int i=1;i<n;i++){ if(a[i-1]<a[i] ){ System.out.println(-1); return; } } } if (a[n - 1] < a[n - 2]) { System.out.println(-1); return; } System.out.println(n - 2); for (int i = 0; i < n - 2; i++) { System.out.println((i + 1) + " " + (n - 1) + " " + n); } } public static void main(String[] amit) { testCases = in.nextInt(); for (int t = 0; t < testCases; t++) { n = in.nextInt(); a = new long[n]; for (int i = 0; i < n; i++) a[i] = in.nextLong(); solve(); } } } /* 3 5 5 -4 2 -1 2 3 4 3 2 3 -3 -2 -1 */ /* 1 5 5 -4 2 -1 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
ffcba611be591281f30c8f7ccd42c5e7
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
4f6bcf8a7f1a6aeb24121e0fe89a2055
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 javax.management.Query; import java.io.*; import java.math.BigInteger; public class Contest1 { public static void main(String[] args) throws Exception { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long[] a=sc.nextlongArray(n); int idx=a.length-1; int c=0; boolean f=true; boolean idxx=false; Queue<tuble>q=new LinkedList<tuble>(); for (int i = a.length-1; i >0; i--) { if(a[i]>=a[i-1]) { if(a[i]>=0) { idx=i; idxx=true;} continue; } else { if(a[i]-a[idx]<=a[i]&&idxx) { a[i-1]=a[i]-a[idx]; c++; q.add(new tuble(i,i+1,idx+1)); } else { f=false; break; } } } if(f) { pw.println(c); for (int i = 0; i <c; i++) { pw.println(q.peek().x+" "+q.peek().y+" "+q.peek().z); q.remove(); } } else { pw.println(-1); } } pw.close(); } //static int n; //static int w; //static int[] weight; //static int[] val; //static long[][] memo; // public static long dp(int idx,int rem) { // if(idx==n) // return 0; // if(memo[idx][rem]!=-1) // return memo[idx][rem]; // long res=dp(idx+1,rem); // if(weight[idx]<=rem) // res=Math.max(res, val[idx]+dp(idx+1,rem-weight[idx])); // return memo[idx][rem]= res; // } public static double ceil(Long n, Long m) { if(n%m==0) return n/m; else return n/m +1; } public static int gcd(int a, int b) { if(b==0) return a; else return gcd(b,a%b); } 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; } } } 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 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
5fd425a94adb38a6905a13f0601d686d
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.StringTokenizer; public class Main { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(in.readLine()); while (t-- > 0) { int n = Integer.parseInt(in.readLine()); long[] a = new long[n]; StringTokenizer st = new StringTokenizer(in.readLine()); boolean isSorted = true; for (int i=0; i<n; i++) { a[i] = Integer.parseInt(st.nextToken()); if (i>0 && a[i]<a[i-1]) isSorted = false; } if(isSorted) out.println(0); else { if (a[n-1] < 0 || 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); } } } in.close(); 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
24d808f21c666c662b69c138b251a3ab
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 int c = 0; public static void sort(int[] arr, int l, int r) { if (l < r) { int mid = (r + l) / 2; sort(arr, l, mid); sort(arr, mid + 1, r); merge(arr, l, mid, mid + 1, r); } } public static void merge(int[] arr, int l1, int r1, int l2, int r2) { int[] temp = new int[r2 - l1 + 1]; int p = 0; int l = l1; while (l1 <= r1 && l2 <= r2) { if (arr[l1] <= arr[l2]) temp[p++] = arr[l1++]; else { temp[p++] = arr[l2++]; c += r1 - l1 + 1; } } while (l1 <= r1) { temp[p++] = arr[l1++]; } while (l2 <= r2) { temp[p++] = arr[l2++]; } for (int i = 0; i < p; i++) { arr[i + l] = temp[i]; } } static void swap(int[] a, int i, int j) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } public static int weakness(int[] arr, int i) { if (i <= 1) return 0; if (arr[i] == 2) return arr[1]; return weakness(arr, i - 1) + arr[i - 1]; } public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextLong(); if (arr[n - 1] < arr[n - 2]) pw.println(-1); else { if (arr[n - 1] < 0 && arr[n - 2] < 0) { boolean f = true; for (int i = 0; i < n - 2 && f; i++) { if(arr[i]>arr[i+1]) f=false; } if(f) pw.println(0); else pw.println(-1); } else { StringBuilder sb = new StringBuilder(); for (int p = 0; p < n - 2; p++) { sb.append((p + 1) + " " + (n - 1) + " " + (n) + "\n"); } pw.println(n - 2); pw.print(sb); } } } pw.flush(); } // static class Pair implements Comparable<Pair> { String x; int y; public Pair(String x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public int compareTo(Pair p) { if (x.charAt(0) == p.x.charAt(0)) { String s1 = x + p.x; String s2 = p.x + x; return s1.compareTo(s2); } else return x.charAt(0) - p.x.charAt(0); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } 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 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
c56abbc440cc9e6ba4e1e58efcb38263
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.lang.*; // import java.math.*; public class Main { static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=1000000007; // static long mod=998244353; static int MAX=Integer.MAX_VALUE; static int MIN=Integer.MIN_VALUE; static long MAXL=Long.MAX_VALUE; static long MINL=Long.MIN_VALUE; static ArrayList<Integer> graph[]; static long fact[]; static int seg[]; static int lazy[];//lazy propagation is used in case of range updates. static int dp[][]; // static long dp[][][]; public static void main (String[] args) throws java.lang.Exception { // code goes here int t=I(); outer:while(t-->0) { int n=I(); long a[]=IL(n); int ans=0; if(a[n-2]>a[n-1]){ out.println("-1"); continue outer; } long N=(long)(1e18); StringBuilder sb=new StringBuilder(); for(int i=n-3;i>=0;i--){ if(a[i]>a[i+1]){ a[i]=a[i+1]-a[n-1]; if(Math.abs(a[i])>=N || a[i]>a[i+1]){ out.println("-1"); continue outer; } ans++; sb.append((i+1)+" "+(i+2)+" "+(n)+"\n"); } } out.println(ans); out.print(sb); } out.close(); } public static class pair { int a; int b; public pair(int aa,int bb) { a=aa; b=bb; } } public static class myComp implements Comparator<pair> { //sort in ascending order. public int compare(pair p1,pair p2) { if(p1.a==p2.a) return 0; else if(p1.a<p2.a) return -1; else return 1; } // sort in descending order. // public int compare(pair p1,pair p2) // { // if(p1.a==p2.a) // return 0; // else if(p1.a<p2.a) // return 1; // else // return -1; // } } public static void DFS(int s,boolean visited[],int dis) { visited[s]=true; for(int i:graph[s]){ if(!visited[i]){ DFS(i,visited,dis); } } } public static void setGraph(int n,int m)throws IOException { graph=new ArrayList[n+1]; for(int i=0;i<=n;i++){ graph[i]=new ArrayList<>(); } for(int i=0;i<m;i++){ int u=I(),v=I(); graph[u].add(v); graph[v].add(u); } } //LOWER_BOUND and UPPER_BOUND functions //It returns answer according to zero based indexing. public static int lower_bound(long arr[],long X,int start, int end) //start=0,end=n-1 { if(start>end)return -1; if(arr[arr.length-1]<X)return end; if(arr[0]>X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; // if(arr[mid]==X){ //Returns last index of lower bound value. // if(mid<end && arr[mid+1]==X){ // left=mid+1; // }else{ // return mid; // } // } if(arr[mid]==X){ //Returns first index of lower bound value. if(mid>start && arr[mid-1]==X){ right=mid-1; }else{ return mid; } } else if(arr[mid]>X){ if(mid>start && arr[mid-1]<X){ return mid-1; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1]>X){ return mid; }else{ left=mid+1; } } } return left; } //It returns answer according to zero based indexing. public static int upper_bound(long arr[],long X,int start,int end) //start=0,end=n-1 { if(arr[0]>=X)return start; if(arr[arr.length-1]<X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; if(arr[mid]==X){ //returns first index of upper bound value. if(mid>start && arr[mid-1]==X){ right=mid-1; }else{ return mid; } } // if(arr[mid]==X){ //returns last index of upper bound value. // if(mid<end && arr[mid+1]==X){ // left=mid+1; // }else{ // return mid; // } // } else if(arr[mid]>X){ if(mid>start && arr[mid-1]<X){ return mid; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1]>X){ return mid+1; }else{ left=mid+1; } } } return left; } //END // File file = new File("C:\\Users\\Dell\\Desktop\\JAVA\\testcase.txt"); // FileWriter fw = new FileWriter("output.txt"); // BufferedReader br= new BufferedReader(new FileReader(file)); //Segment Tree Code public static void buildTree(int si,int ss,int se) { if(ss==se){ seg[si]=0; return; } int mid=(ss+se)/2; buildTree(2*si+1,ss,mid); buildTree(2*si+2,mid+1,se); seg[si]=max(seg[2*si+1],seg[2*si+2]); } public static void merge(ArrayList<Integer> f,ArrayList<Integer> a,ArrayList<Integer> b) { int i=0,j=0; while(i<a.size() && j<b.size()){ if(a.get(i)<=b.get(j)){ f.add(a.get(i)); i++; }else{ f.add(b.get(j)); j++; } } while(i<a.size()){ f.add(a.get(i)); i++; } while(j<b.size()){ f.add(b.get(j)); j++; } } public static void update(int si,int ss,int se,int pos,int val) { if(ss==se){ seg[si]=(int)add(seg[si],val); return; } int mid=(ss+se)/2; if(pos<=mid){ update(2*si+1,ss,mid,pos,val); }else{ update(2*si+2,mid+1,se,pos,val); } seg[si]=(int)add(seg[2*si+1],seg[2*si+2]); } public static int query(int si,int ss,int se,int qs,int qe) { if(qs>se || qe<ss)return 0; if(ss>=qs && se<=qe)return seg[si]; int mid=(ss+se)/2; return max(query(2*si+1,ss,mid,qs,qe),query(2*si+2,mid+1,se,qs,qe)); } //Segment Tree Code end //Prefix Function of KMP Algorithm public static int[] KMP(char c[],int n) { int pi[]=new int[n]; for(int i=1;i<n;i++){ int j=pi[i-1]; while(j>0 && c[i]!=c[j]){ j=pi[j-1]; } if(c[i]==c[j])j++; pi[i]=j; } return pi; } public static long nPr(int n,int r) { long ans=divide(fact(n),fact(n-r),mod); return ans; } public static long nCr(int n,int r) { long ans=divide(fact[n],mul(fact[n-r],fact[r]),mod); return ans; } public static long kadane(long a[],int n) //largest sum subarray { long max_sum=Long.MIN_VALUE,max_end=0; for(int i=0;i<n;i++){ max_end+=a[i]; if(max_sum<max_end){max_sum=max_end;} if(max_end<0){max_end=0;} } return max_sum; } public static boolean isSorted(int a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static boolean isSorted(long a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static int computeXOR(int n) //compute XOR of all numbers between 1 to n. { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } public static int np2(int x) { x--; x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x++; return x; } public static int hp2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static long hp2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static ArrayList<Integer> primeSieve(int n) { ArrayList<Integer> arr=new ArrayList<>(); 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) arr.add(i); } return arr; } // Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n); public static class FenwickTree { int farr[]; int n; public FenwickTree(int c) { n=c+1; farr=new int[n]; } // public void update_range(int l,int r,long p) // { // update(l,p); // update(r+1,(-1)*p); // } public void update(int x,int p) { for(;x<n;x+=x&(-x)) { farr[x]+=p; } } public int get(int x) { int ans=0; for(;x>0;x-=x&(-x)) { ans=ans+farr[x]; } return ans; } } //Disjoint Set Union public static class DSU { int par[],rank[]; public DSU(int c) { par=new int[c+1]; rank=new int[c+1]; for(int i=0;i<=c;i++) { par[i]=i; rank[i]=0; } } public int find(int a) { if(a==par[a]) return a; return par[a]=find(par[a]); } public void union(int a,int b) { int a_rep=find(a),b_rep=find(b); if(a_rep==b_rep) return; if(rank[a_rep]<rank[b_rep]) par[a_rep]=b_rep; else if(rank[a_rep]>rank[b_rep]) par[b_rep]=a_rep; else { par[b_rep]=a_rep; rank[a_rep]++; } } } public static HashMap<Integer,Integer> primeFact(int a) { // HashSet<Long> arr=new HashSet<>(); HashMap<Integer,Integer> hm=new HashMap<>(); int p=0; while(a%2==0){ // arr.add(2L); p++; a=a/2; } hm.put(2,hm.getOrDefault(2,0)+p); for(int i=3;i*i<=a;i+=2){ p=0; while(a%i==0){ // arr.add(i); p++; a=a/i; } hm.put(i,hm.getOrDefault(i,0)+p); } if(a>2){ // arr.add(a); hm.put(a,hm.getOrDefault(a,0)+1); } // return arr; return hm; } public static boolean isInteger(double N) { int X = (int)N; double temp2 = N - X; if (temp2 > 0) { return false; } return true; } public static boolean isPalindrome(String s) { int n=s.length(); for(int i=0;i<=n/2;i++){ if(s.charAt(i)!=s.charAt(n-i-1)){ return false; } } return true; } public static int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static long fact(long n) { long fact=1; for(long i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static long fact(int n) { long fact=1; for(int i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; 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; } public static boolean isPrime(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; } public static void printArray(long a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(int a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(char a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]); } out.println(); } public static void printArray(String a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(boolean a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(pair a[]) { for(pair p:a){ out.println(p.a+"->"+p.b); } } public static void printArray(int a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(long a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(char a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(ArrayList<?> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printMapI(HashMap<?,?> hm){ for(Map.Entry<?,?> e:hm.entrySet()){ out.println(e.getKey()+"->"+e.getValue()); }out.println(); } public static void printMap(HashMap<Long,ArrayList<Integer>> hm){ for(Map.Entry<Long,ArrayList<Integer>> e:hm.entrySet()){ out.print(e.getKey()+"->"); ArrayList<Integer> arr=e.getValue(); for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); }out.println(); } } //Modular Arithmetic public static long add(long a,long b) { a+=b; if(a>=mod)a-=mod; return a; } public static long sub(long a,long b) { a-=b; if(a<0)a+=mod; return a; } public static long mul(long a,long b) { return ((a%mod)*(b%mod))%mod; } public static long divide(long a,long b,long m) { a=mul(a,modInverse(b,m)); return a; } public static long modInverse(long a,long m) { int x=0,y=0; own p=new own(x,y); long g=gcdExt(a,m,p); if(g!=1){ out.println("inverse does not exists"); return -1; }else{ long res=((p.a%m)+m)%m; return res; } } public static long gcdExt(long a,long b,own p) { if(b==0){ p.a=1; p.b=0; return a; } int x1=0,y1=0; own p1=new own(x1,y1); long gcd=gcdExt(b,a%b,p1); p.b=p1.a - (a/b) * p1.b; p.a=p1.b; return gcd; } public static long pwr(long m,long n) { long res=1; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m); } n=n>>1; m=(m*m); } return res; } public static long modpwr(long m,long n) { long res=1; m=m%mod; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m)%mod; } n=n>>1; m=(m*m)%mod; } return res; } public static class own { long a; long b; public own(long val,long index) { a=val; b=index; } } //Modular Airthmetic public 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); } public static void sort(char[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { char tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public 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); } //max & min public static int max(int a,int b){return Math.max(a,b);} public static int min(int a,int b){return Math.min(a,b);} public static int max(int a,int b,int c){return Math.max(a,Math.max(b,c));} public static int min(int a,int b,int c){return Math.min(a,Math.min(b,c));} public static long max(long a,long b){return Math.max(a,b);} public static long min(long a,long b){return Math.min(a,b);} public static long max(long a,long b,long c){return Math.max(a,Math.max(b,c));} public static long min(long a,long b,long c){return Math.min(a,Math.min(b,c));} public static int maxinA(int a[]){int n=a.length;int mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static long maxinA(long a[]){int n=a.length;long mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static int mininA(int a[]){int n=a.length;int mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long mininA(long a[]){int n=a.length;long mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long suminA(int a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} public static long suminA(long a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} //end public static int[] I(int n) { int a[]=new int[n]; for(int i=0;i<n;i++){ a[i]=I(); } return a; } public static long[] IL(int n) { long a[]=new long[n]; for(int i=0;i<n;i++){ a[i]=L(); } return a; } public static long[] prefix(int a[]) { int n=a.length; long pre[]=new long[n]; pre[0]=a[0]; for(int i=1;i<n;i++){ pre[i]=pre[i-1]+a[i]; } return pre; } public static long[] prefix(long a[]) { int n=a.length; long pre[]=new long[n]; pre[0]=a[0]; for(int i=1;i<n;i++){ pre[i]=pre[i-1]+a[i]; } return pre; } public static int I(){return sc.I();} public static long L(){return sc.L();} public static String S(){return sc.S();} public static double D(){return sc.D();} } 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 I(){ return Integer.parseInt(next()); } long L(){ return Long.parseLong(next()); } double D(){ return Double.parseDouble(next()); } String S(){ 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
020c2bef6a3468bd93cecbbd082c9ba1
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.BigInteger; public class CodeChef2 { static boolean lazy[]; static int val[]; static boolean colupd[]; public static void main(String args[]) throws Exception { FastReader fr = new FastReader(); int t = fr.nextInt(); // int t = 1; PrintWriter out = new PrintWriter(System.out); while (t-- > 0) { int n = fr.nextInt(); int arr[] = new int[n]; for(int i = 0; i < n; i++) { arr[i] = fr.nextInt(); } if(arr[n-1] < arr[n-2]) { out.println(-1); continue; } else { int j = n-1; if(arr[n-1] >= 0) { long d = (long)arr[n-2] - (long)arr[n-1]; out.println(n-2); for(int i = 0; i < n-2; i++) out.println((i+1) + " " +( n-1) + " " + n); } else { boolean f = false; for(; j > 0; j--) { if(arr[j] < arr[j-1]) { f = true; } } if(f) out.println(-1); else out.println(0); } } } out.close(); } static int[] buildST(int arr[]) { int n = arr.length; int p = (int)(Math.ceil(Math.log(n)/Math.log(2))); n = (int)Math.pow(2, p); int size = 2*n - 1; int st[] = new int[size]; for(int i = 0; i < arr.length; i++) { st[n-1 + i] = arr[i]; } return st; } static long query(int col[], long val[], int l, int i, int s, int e) { if(l > e || l < s) return 0; if(s == e) { return val[i]; } int m = (s+e)/2; // if(val[i] != 0) { // updateV(val, col, 2*i + 1, s, m, col[2*i + 1], val[i]); // updateV(val, col, 2*i + 2, m+1, e, col[2*i + 2], val[i]); // val[i] = 0; // } long v = 0; if(l <= m) v = query(col, val, l, 2*i+1, s, m); else v = query(col, val, l, 2*i+2, m+1, e); return val[i] + v; } static void updateC(int col[], int l, int r, int i, int s, int e, int c) { if(l > r || l > e || r < s || col[i] == c) return; if(l <= s && e <= r) { col[i] = c; return; } int m = (s + e)/2; if(col[i] != 0 && col[i] != c) { updateC(col, s, m, 2*i + 1, s, m, col[i]); updateC(col, m+1, e, 2*i + 2, m+1, e, col[i]); col[i] = 0; } updateC(col, l, Math.min(m, r), 2*i + 1, s, m, c); updateC(col, Math.max(l, m+1), r, 2*i + 2, m+1, e, c); col[i] = (col[2*i + 1] != col[2*i + 2] || col[2*i + 1] == 0? 0 : c); } static void updateV(long val[], int col[], int i, int s, int e, int c, long v) { if(col[i] != 0 && col[i] != c) return; if(col[i] == c) { val[i] += v; return; } int m = (s + e)/2; // if(val[i] != 0) { // updateV(val, col, 2*i + 1, s, m, col[2*i + 1], val[i]); // updateV(val, col, 2*i + 2, m+1, e, col[2*i + 2], val[i]); // val[i] = 0; // } updateV(val, col,2*i + 1, s, m, c, v); updateV(val, col,2*i + 2, m+1, e, c, v); } static class Pair implements Comparable<Pair>{ int i; int v; Pair(int i, int j) { this.i = i; v = j; } public int compareTo(Pair o) { if(this.v < o.v) return -1; else if(this.v > o.v) return 1; else if(this.i < o.i) return -1; return 1; } } 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; } boolean hasMore() { try { return br.ready(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } } }
Java
["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"]
2 seconds
["2\n1 2 3\n3 4 5\n-1\n0"]
NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations.
Java 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
4caad4cc397a36eccc9fad1d170b820c
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 A { static class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new FileReader("test.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[]) { Reader r = new Reader(); // int tcs = r.nextInt(); // for (int tc = 0; tc < tcs; tc++) { // } int tcs = r.nextInt(); for (int tc = 0; tc < tcs; tc++) { int n = r.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = r.nextLong(); } boolean pos = true; if (a[n - 2] > a[n - 1]) pos = false; int ops = 0; LinkedList<Integer> o = new LinkedList<Integer>(); // int min = n - 2; // int max = n - 1; for (int i = n - 3; i >= 0; i--) { if (a[i] > a[i + 1]) { if (a[i + 1] - a[n - 1] <= a[i + 1]) { ops++; a[i] = a[i + 1] - a[n - 1]; o.add(i + 1); o.add(i + 2); o.add(n); } else { pos = false; break; } } // if (a[i + 1] - a[n - 1] <= a[i + 1]) { // ops++; // o.add(i + 1); // o.add(i + 2); // o.add(n); // } else if (a[i] <= a[i + 1]) { // pos = false; // break; // } } // if (a[0] < ) // System.out.println("==============="); System.out.println(pos ? ops : -1); if (pos) { while (!o.isEmpty()) { System.out.println(o.pop() + " " + o.pop() + " " + o.pop()); } } } } }
Java
["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"]
2 seconds
["2\n1 2 3\n3 4 5\n-1\n0"]
NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations.
Java 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
9630b932a5264c000d2562cdc9f45e01
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 Feb20P3 { public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); long[] o = new long[n]; boolean tf = true; for (int j = 0; j < n; j++) { o[j] = sc.nextLong(); if(j > 0 && o[j] < o[j-1]) tf = false; } String str = ""; if (o[o.length - 1] < o[o.length - 2]) out.println(-1); else if(tf) out.println(0); else { if (o[o.length - 1] - o[o.length - 2] <= o[o.length - 2] || o[o.length - 2] - o[o.length - 1] <= o[o.length - 2]) { out.println(o.length - 2); for (int j = 0; j < o.length - 2; j++) { out.println((j + 1) + " " + (o.length - 1) + " " + (o.length)); } } else { out.println(-1); } } } // Start writing your solution here. ------------------------------------- /* * int n = sc.nextInt(); // read input as integer long k = sc.nextLong(); // * read input as long double d = sc.nextDouble(); // read input as double String * str = sc.next(); // read input as String String s = sc.nextLine(); // read * whole line as String * * int result = 3*n; out.println(result); // print via PrintWriter */ // Stop writing your solution here. ------------------------------------- out.close(); } // -----------PrintWriter for faster output--------------------------------- public static PrintWriter out; // -----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // -------------------------------------------------------- }
Java
["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"]
2 seconds
["2\n1 2 3\n3 4 5\n-1\n0"]
NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations.
Java 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
4a49987f9e7ca99cc2845d094e863d3e
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.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.stream.Collector; import java.util.stream.Collectors; import javax.print.DocFlavor.INPUT_STREAM; public class Main { public static void main(String[] args) throws Exception { Sol obj=new Sol(); obj.runner(); } } class Sol{ FastScanner fs=new FastScanner(); Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); List<Integer> sieve; Set<Integer> sieveSet; void runner() throws Exception{ int T=1; //sieve=sieve(); //sieveSet=new HashSet<>(sieve); //T=sc.nextInt(); T=fs.nextInt(); while(T-->0) { solve(T); } out.close(); System.gc(); } private void solve(int T) throws Exception { int n=fs.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++) { arr[i]=fs.nextLong(); } if( arr[n-2]>arr[n-1] ) { System.out.println(-1); return ; } boolean flag=true; for(int i=1;i<n;i++) { if( arr[i-1]>arr[i] ) { flag=false; break; } } if( flag ) { System.out.println(0); return ; }else if( arr[n-1]<0 ) { System.out.println(-1); return; } System.out.println(n-2); for( int i=0;i<n-2;i++ ) { System.out.println( (i+1) +" "+(n-1)+" "+n ); } /**/ } static class FastScanner { BufferedReader br; StringTokenizer st ; FastScanner(){ br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } FastScanner(String file) { try { br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); st = new StringTokenizer(""); } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("file not found"); e.printStackTrace(); } } String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } String readLine() throws IOException{ return br.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 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
d117f8d816ac755a417dc35a0c1e4338
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.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Double.parseDouble; public class Main { public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int t = nextInt(); while (t-- > 0) { int n = nextInt(); long a[] = new long[n]; int m = 0; for (int i = 0; i < n; i++) { a[i] = nextLong(); } long curDelta = a[n - 2] - a[n - 1]; long curMax = a[n - 1]; long curMin = a[n - 2]; int y = n - 2; int z = n - 1; ArrayList<Triple> ans = new ArrayList<>(); for (int i = n - 3; i >= 0; i--) { curDelta = curMin - curMax; if (a[i] >= a[i + 1] && a[i] > curDelta) { a[i] = curDelta; ans.add(new Triple(i + 1, y + 1, z + 1)); if (curMin > curDelta) { curMin = curDelta; y = i; } } else { if (curMin > a[i]) { curMin = a[i]; y = i; } } } boolean sol = true; for (int i = 1; i < n; i++) { if (a[i] < a[i - 1]) { sol = false; break; } } if (sol) { out.println(ans.size()); for (int i = 0; i < ans.size(); i++) { Triple get = ans.get(i); out.println(get.x + " " + get.y + " " + get.z); } } else { out.println(-1); } } in.close(); out.close(); } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; static int nextInt() throws IOException { return parseInt(next()); } static long nextLong() throws IOException { return parseLong(next()); } static double nextDouble() throws IOException { return parseDouble(next()); } static String next() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } } class Triple { int x; int y; int z; public Triple(int x, int y, int z) { this.x = x; this.y = y; this.z = 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
132d4ba9ec59156baefd22e1318ad734
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
/* Rating: 1367 Date: 20-02-2022 Time: 20-33-31 Author: Kartik Papney Linkedin: https://www.linkedin.com/in/kartik-papney-4951161a6/ Leetcode: https://leetcode.com/kartikpapney/ Codechef: https://www.codechef.com/users/kartikpapney */ import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class C_Differential_Sorting { public static boolean debug = false; static void debug(String st) { if(debug) p.writeln(st); } public static void s() { int n = sc.nextInt(); int[] arr = sc.readArray(n); boolean isIncreasing = true; for(int i=0; i<arr.length-1; i++) { if(arr[i] > arr[i+1]) { isIncreasing = false; break; } } if(isIncreasing) { p.writeln(0); return; } if(arr[n-2] > arr[n-1] || arr[n-1] < 0) { p.writeln(-1); return; } p.writeln(n-2); for(int i=n-3; i>=0; i--) { p.writeln((i+1) + " " + (i+2)+ " " + (n)); } } public static void main(String[] args) { int t = 1; t = sc.nextInt(); while (t-- != 0) { s(); } p.print(); } static final Integer MOD = (int) 1e9 + 7; static final FastReader sc = new FastReader(); static final Print p = new Print(); static class Functions { static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void 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 int max(int[] a) { int max = Integer.MIN_VALUE; for (int val : a) max = Math.max(val, max); return max; } static int min(int[] a) { int min = Integer.MAX_VALUE; for (int val : a) min = Math.min(val, min); return min; } static long min(long[] a) { long min = Long.MAX_VALUE; for (long val : a) min = Math.min(val, min); return min; } static long max(long[] a) { long max = Long.MIN_VALUE; for (long val : a) max = Math.max(val, max); return max; } static long sum(long[] a) { long sum = 0; for (long val : a) sum += val; return sum; } static int sum(int[] a) { int sum = 0; for (int val : a) sum += val; return sum; } public static long mod_add(long a, long b) { return (a % MOD + b % MOD + MOD) % MOD; } public static long pow(long a, long b) { long res = 1; while (b > 0) { if ((b & 1) != 0) res = mod_mul(res, a); a = mod_mul(a, a); b >>= 1; } return res; } public static long mod_mul(long a, long b) { long res = 0; a %= MOD; while (b > 0) { if ((b & 1) > 0) { res = mod_add(res, a); } a = (2 * a) % MOD; b >>= 1; } return res; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long factorial(long n) { long res = 1; for (int i = 1; i <= n; i++) { res = (i % MOD * res % MOD) % MOD; } return res; } public static int count(int[] arr, int x) { int count = 0; for (int val : arr) if (val == x) count++; return count; } public static ArrayList<Integer> generatePrimes(int n) { boolean[] primes = new boolean[n]; for (int i = 2; i < primes.length; i++) primes[i] = true; for (int i = 2; i < primes.length; i++) { if (primes[i]) { for (int j = i * i; j < primes.length; j += i) { primes[j] = false; } } } ArrayList<Integer> arr = new ArrayList<>(); for (int i = 0; i < primes.length; i++) { if (primes[i]) arr.add(i); } return arr; } } static class Print { StringBuffer strb = new StringBuffer(); public void write(Object str) { strb.append(str); } public void writes(Object str) { char c = ' '; strb.append(str).append(c); } public void writeln(Object str) { char c = '\n'; strb.append(str).append(c); } public void writeln() { char c = '\n'; strb.append(c); } public void yes() { char c = '\n'; writeln("YES"); } public void no() { writeln("NO"); } public void writes(int[] arr) { for (int val : arr) { write(val); write(' '); } } public void writes(long[] arr) { for (long val : arr) { write(val); write(' '); } } public void writeln(int[] arr) { for (int val : arr) { writeln(val); } } public void print() { System.out.print(strb); } public void println() { System.out.println(strb); } } 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()); } 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; } double[] readArrayDouble(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = new String(); 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
4291134096f38d70189f9e684a059c1a
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.swing.*; import java.lang.reflect.Array; import java.text.DecimalFormat; import java.util.*; import java.lang.*; import java.io.*; import java.math.*; import java.util.stream.Stream; // Please name your class Main public class Main { static FastScanner fs=new FastScanner(); 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(); } int Int() { return Integer.parseInt(next()); } long Long() { return Long.parseLong(next()); } String Str(){ return next(); } } public static void main (String[] args) throws java.lang.Exception { PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); //reading /writing file //Scanner in=new Scanner(System.in); //Scanner in=new Scanner(new File("input.txt")); //PrintWriter pr=new PrintWriter("output.txt") int T=1; for(int t=0;t<T;t++){ Solution sol1=new Solution(out,fs); sol1.solution(T,t); } out.flush(); } public static int[] Arr(int n){ int A[]=new int[n]; for(int i=0;i<n;i++)A[i]=Int(); return A; } public static int Int(){ return fs.Int(); } public static long Long(){ return fs.Long(); } public static String Str(){ return fs.Str(); } } class Solution { PrintWriter out; int INF = Integer.MAX_VALUE; int NINF = Integer.MIN_VALUE; int MOD = 998244353; int mod = 1000000007; //int mod = 998244353; Main.FastScanner fs; public Solution(PrintWriter out, Main.FastScanner fs) { this.out = out; this.fs = fs; } public int[] arr(int n) { int A[] = new int[n]; for(int i = 0; i < n; i++) { A[i] = fs.Int(); } return A; } public void msg(String s) { System.out.println(s); } public void print(int A[]){ StringBuilder str = new StringBuilder(); for(int i: A)str.append(i).append(" "); out.println(str.toString()); } public boolean check(long A[]) { for(int i = 1; i < A.length; i++) { if(A[i] < A[i - 1]) { return false; } } return true; } public void solution(int all, int testcase) { int t = fs.Int(); while(t > 0) { t--; int n = fs.Int(); long A[] = new long[n]; for(int i = 0; i < n; i++){ A[i] = fs.Int(); } List<int[]>res = new ArrayList<>(); long max = A[n - 1]; long min = Long.MAX_VALUE; int curz = n - 1; int z = n - 1; int y = n - 2; long dp[][] = new long[n][3]; for(int i = n - 2; i >= 0; i--) { long a = A[i] - max; if(a < min) { min = a; y = i; z = curz; } dp[i][0] = min; dp[i][1] = y; dp[i][2] = z; if(A[i] > max) { max = A[i]; curz = i; } } //for(long p[]:dp){ //System.out.println(Arrays.toString(p)); // } for(int i = 0; i < n - 2; i++) { long cur = A[i]; long next = dp[i + 1][0]; if(next < cur) { A[i] = next; int yy = (int)(dp[i + 1][1]); int zz = (int)(dp[i + 1][2]); res.add(new int[]{i, yy, zz}); } } //System.out.println(Arrays.toString(A)); if(!check(A)) { out.println(-1); } else { out.println(res.size()); for(int p[]:res){ out.println((p[0] + 1) + " " + (p[1] + 1) + " " + (p[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
ff75c73982c2082e2d047c28b19d4f8d
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.Arrays; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.StringTokenizer; public class Main { static FastReader fr; static int arrForIndexSort[]; static Integer map1[]; static Integer map2[]; static int globalVal; 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 class Pair{ int first; int second; Pair(int first, int second){ this.first = first; this.second = second; } @Override public boolean equals(Object b) { Pair a = (Pair)b; if(this.first == a.first && this.second==a.second) { return true; } return false; } } class PairSorter implements Comparator<Main.Pair>{ public int compare(Pair a, Pair b) { if(a.first!=b.first) { return a.first-b.first; } return a.second-b.second; } } static class DoublePair{ double first; double second; DoublePair(double first, double second){ this.first = first; this.second = second; } @Override public boolean equals(Object b) { Pair a = (Pair)b; if(this.first == a.first && this.second==a.second) { return true; } return false; } } class DoublePairSorter implements Comparator<Main.DoublePair>{ public int compare(DoublePair a, DoublePair b) { if(a.second>b.second) { return 1; } else if(a.second<b.second) { return -1; } return 0; } } class IndexSorter implements Comparator<Integer>{ public int compare(Integer a, Integer b) { //desc if(arrForIndexSort[b]==arrForIndexSort[a]) { return b-a; } return arrForIndexSort[b]-arrForIndexSort[a]; } } class ListSorter implements Comparator<List>{ public int compare(List a, List b) { return b.size()-a.size(); } } static class DisjointSet{ int[] dsu; public DisjointSet(int n) { makeSet(n); } public void makeSet(int n) { dsu = new int[n+1]; //*** 1 Based indexing *** for(int i=1;i<=n;i++) { dsu[i] = -1; } } public int find(int i) { while(dsu[i] > 0) { i = dsu[i]; } return i; } public void union(int i, int j) { int iRep = find(i); int jRep = find(j); if(iRep == jRep) { return; } if(dsu[iRep]>dsu[jRep]) { dsu[jRep] += dsu[iRep]; dsu[iRep] = jRep; } else { dsu[iRep] += dsu[jRep]; dsu[jRep] = iRep; } } } static class SegmentTree{ int[] tree; int[] originalArr; public SegmentTree(int[] a) { int n = a.length; tree = new int[4*n]; build(1, a, 0, n-1); originalArr = a; } public void build(int node, int[] a, int start, int end){ if(start == end) { tree[node] = a[start]; return; } int mid = (start+end)/2; build(2*node, a, start, mid); build(2*node+1, a, mid+1, end); tree[node] = Math.min(tree[2*node], tree[2*node+1]); } public void update(int node, int start, int end, int idx, int val) { if(start == end) { tree[node] = originalArr[idx] = val; return; } int mid = (start+ end)/2; if(idx<=mid) { update(2*node, start, mid, idx, val); } else { update(2*node+1, mid+1, end, idx, val); } tree[node] = Math.min(tree[2*node], tree[2*node+1]); } public int query(int node, int start, int end, int l, int r) { if(r<start || l>end) { return Integer.MAX_VALUE; } if(start>=l && end<=r) { return tree[node]; } int mid = (start+end)/2; int p1 = query(2*node, start, mid, l, r); int p2 = query(2*node+1, mid+1, end, l, r); return Math.min(p1, p2); } } //1 Based Indexing static class BIT{ long[] tree; public BIT(int n) { tree = new long[n]; } public BIT(int[] a) { this(a.length); for(int i=1;i<tree.length;i++) { add(i, a[i]); } } public void add(int i, int val) { for(int j=i;j<tree.length;j += j&(-j)) { tree[j] += val; } } public long sum(int i) { long ret = 0; for(int j=i;j>0;j -= j&(-j)) { ret += tree[j]; } return ret; } public long query(int l, int r) { long lSum = 0; if(l>1) { lSum = sum(l-1); } long rSum = sum(r); return rSum-lSum; } } public static void main(String[] args) { fr = new FastReader(); int T = 1; T = fr.nextInt(); int t1 = T; while (T-- > 0) { solve(t1-T); } } /* Things to remember * Keep it Simple (Golden Rule) * Think Reverse * Don't get stuck on one approach * Check corner case * On error, check->edge case, implementation and question, * On error, check constraints, check if long needed, * On error, which one to choose when two values are equal for greedy */ public static void solve(int testcase) { int n = fr.nextInt(); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] = fr.nextInt(); } int index = n-1; if(a[index]<0) { while(index > 0 && a[index]<0 && a[index]>=a[index-1]) { index--; } if(index==0) { System.out.println(0); } else { System.out.println(-1); } } else { if(a[n-2]>a[n-1]) { System.out.println(-1); } else { StringBuilder sb = new StringBuilder(); System.out.println(n-2); for(int i=1;i<n-1;i++) { sb.append(i).append(" ").append(n-1).append(" ").append(n).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
91360b9d0ba3c8acc5b27a0497562baa
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 AACFCC { public static long mod = (long) Math.pow(10, 9) + 7; public static long mod2 = 998244353; public static int oo = 0; public static ArrayList<Integer> prime; public static int zz = -1; public static int ttt = 0; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { String[] str = br.readLine().split(" "); int n = (int) Long.parseLong(str[0]); int ans = 0; long[] arr = new long[n]; String[] s1 = br.readLine().split(" "); for (int i = 0; i < n; i++) { arr[i] = Integer.valueOf(s1[i]); } StringBuilder ttt = new StringBuilder(); if (arr[n - 2] > arr[n - 1]) { pw.println(-1); continue; } long cc = arr[n - 2] - arr[n - 1]; if (cc <= arr[n - 2]) { for (int i = 0; i < n - 2; i++) { ans++; ttt.append((i + 1) + " " + (n - 1) + " " + (n)); if (i != n - 3) { ttt.append("\n"); } } pw.println(ans); pw.println(ttt.toString()); }else { int a=0; for(int i=1;i<n;i++) { if(arr[i-1]<=arr[i]) { }else { a=-1; break; } } pw.println(a); } } pw.close(); } } // private static void putBit(int ind, int val, int[] bit) { // // TODO Auto-generated method stub // for (int i = ind; i < bit.length; i += (i & -i)) { // bit[i] += val; // } // } // // private static int getSum(int ind, int[] bit) { // // TODO Auto-generated method stub // int ans = 0; // for (int i = ind; i > 0; i -= (i & -i)) { // ans += bit[i]; // } // return ans; // } // private static void product(long[] bin, int ind, int currIt, long[] power) { // // TODO Auto-generated method stub // long pre = 1; // if (ind > 1) { // pre = power(power[ind - 1] - 1, mod2 - 1); // } // long cc = power[ind] - 1; // // System.out.println(pre + " " + cc); // for (int i = ind; i < bin.length; i += (i & (-i))) { // bin[i] = (bin[i] * pre) % mod2; // bin[i] = (bin[i] * cc) % mod2; // } // } // // private static void add(long[] bin, int ind, int val) { // // TODO Auto-generated method stub // for (int i = ind; i < bin.length; i += (i & (-i))) { // bin[i] += val; // } // } // // private static long sum(long[] bin, int ind) { // // TODO Auto-generated method stub // long ans = 0; // for (int i = ind; i > 0; i -= (i & (-i))) { // ans += bin[i]; // } // return ans; // } // // private static long power(long a, long p) { // TODO Auto-generated method stub // long res = 1;while(p>0) // { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // }return res; // }} // private static void getFac(long n, PrintWriter pw) { // // TODO Auto-generated method stub // int a = 0; // while (n % 2 == 0) { // a++; // n = n / 2; // } // if (n == 1) { // a--; // } // for (int i = 3; i <= Math.sqrt(n); i += 2) { // while (n % i == 0) { // n = n / i; // a++; // } // } // if (n > 1) { // a++; // } // if (a % 2 == 0) { // pw.println("Bob"); // } else { // pw.println("Alice"); // } // //System.out.println(a); // return; // } // private static long power(long a, long p) { // // TODO Auto-generated method stub // long res = 1; // while (p > 0) { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // } // return res; // } // // private static void fac() { // fac[0] = 1; // // TODO Auto-generated method stub // for (int i = 1; i < fac.length; i++) { // if (i == 1) { // fac[i] = 1; // } else { // fac[i] = i * fac[i - 1]; // } // if (fac[i] > mod) { // fac[i] = fac[i] % mod; // } // } // } // // private static int getLower(Long long1, Long[] st) { // // TODO Auto-generated method stub // int left = 0, right = st.length - 1; // int ans = -1; // while (left <= right) { // int mid = (left + right) / 2; // if (st[mid] <= long1) { // ans = mid; // left = mid + 1; // } else { // right = mid - 1; // } // } // return ans; // } // private static long getGCD(long l, long m) { // // long t1 = Math.min(l, m); // long t2 = Math.max(l, m); // while (true) { // long temp = t2 % t1; // if (temp == 0) { // return t1; // } // t2 = t1; // t1 = temp; // } // } // private static int kmp(String str) { // // TODO Auto-generated method stub // // System.out.println(str); // int[] pi = new int[str.length()]; // pi[0] = 0; // for (int i = 1; i < str.length(); i++) { // int j = pi[i - 1]; // while (j > 0 && str.charAt(i) != str.charAt(j)) { // j = pi[j - 1]; // } // if (str.charAt(j) == str.charAt(i)) { // j++; // } // pi[i] = j; // System.out.print(pi[i]); // } // System.out.println(); // return pi[str.length() - 1]; // } // private static void getFac(long n, PrintWriter pw) { // // TODO Auto-generated method stub // int a = 0; // while (n % 2 == 0) { // a++; // n = n / 2; // } // if (n == 1) { // a--; // } // for (int i = 3; i <= Math.sqrt(n); i += 2) { // while (n % i == 0) { // n = n / i; // a++; // } // } // if (n > 1) { // a++; // } // if (a % 2 == 0) { // pw.println("Bob"); // } else { // pw.println("Alice"); // } // //System.out.println(a); // return; // } // private static long power(long a, long p) { // // TODO Auto-generated method stub // long res = 1; // while (p > 0) { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // } // return res; // } // // private static void fac() { // fac[0] = 1; // // TODO Auto-generated method stub // for (int i = 1; i < fac.length; i++) { // if (i == 1) { // fac[i] = 1; // } else { // fac[i] = i * fac[i - 1]; // } // if (fac[i] > mod) { // fac[i] = fac[i] % mod; // } // } // } // // private static int getLower(Long long1, Long[] st) { // // TODO Auto-generated method stub // int left = 0, right = st.length - 1; // int ans = -1; // while (left <= right) { // int mid = (left + right) / 2; // if (st[mid] <= long1) { // ans = mid; // left = mid + 1; // } else { // right = mid - 1; // } // } // return ans; // } // private static long getGCD(long l, long m) { // // long t1 = Math.min(l, m); // long t2 = Math.max(l, m); // while (true) { // long temp = t2 % t1; // if (temp == 0) { // return t1; // } // t2 = t1; // t1 = temp; // } // }
Java
["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"]
2 seconds
["2\n1 2 3\n3 4 5\n-1\n0"]
NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations.
Java 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
53f85016e9a85aef26a211b2c9e60625
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); // int a = 1; int t; t = in.nextInt(); //t = 1; while (t > 0) { // out.print("Case #"+(a++)+": "); solver.call(in,out); t--; } out.close(); } static class TaskA { public void call(InputReader in, PrintWriter out) { int n; n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } if(arr[n - 2] > arr[n - 1]){ out.println(-1); return; } if(arr[n-1] < 0 && arr[n - 2] < 0){ for (int i = 0; i < n - 1; i++) { if(arr[i] > arr[i + 1]){ out.println(-1); return; } } out.println(0); return; } out.println(n - 2); for (int i = 0; i < n - 2; i++) { out.println((i + 1)+" "+(n - 1) +" "+(n)); } } } static int gcd(int a, int 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 class answer implements Comparable<answer>{ int a, b; public answer(int a, int b, int c) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return Integer.compare(this.b, o.b); } } static class answer1 implements Comparable<answer1>{ int a, b, c; public answer1(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } @Override public int compareTo(answer1 o) { return (o.a - this.a); } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (Long i:a) l.add(i); l.sort(Collections.reverseOrder()); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static final Random random=new Random(); static void shuffleSort(int[] a) { int n = a.length; 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 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()); } public 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
7c433827e1e38149234d698d03fea2f8
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[]a=sc.nextlongArray(n); long max=a[n-1]; long min=a[n-2]; boolean can=true; for(int i=n-2;i>-1;i--){ if(a[i]>a[i+1]){ can=false; break; } } if(can){ pw.println(0); }else { if(max<0||max<min){ pw.println(-1); }else{ pw.println(n-2); for(int i=0;i<n-2;i++)pw.println((i+1)+" "+(n-1)+" "+(n)); } } } pw.close(); } 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; } } } 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 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
ed75cfd6df016459840cfe2857f1ced7
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_Differential_Sorting { static int M = 1_000_000_007; static final PrintWriter out =new PrintWriter(System.out); static final FastReader fs = new FastReader(); static boolean prime[]; public static void main (String[] args) throws java.lang.Exception { int t= fs.nextInt(); aaa: for(int i=0;i<t;i++) { int n=fs.nextInt(); int a[]=fs.arrayIn(n); if(a[n-2]>a[n-1]){ out.println(-1); continue; } if(a[n-1]<0){ int j=n-1; while(j>=1){ if(a[j]<a[j-1]){ out.println(-1); continue aaa; } j--; } out.println(0); continue aaa; } out.println(n-2); for(int j=1;j<=n-2;j++ ){ out.println(j+" "+(n-1)+" "+n); } } out.flush(); } 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 void sieveOfEratosthenes(int n) { prime = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; prime[0]=false; if(1<=n) prime[1]=false; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int [] arrayIn(int n) throws IOException { int arr[] = new int[n]; for(int i=0; i<n; i++) { arr[i] = nextInt(); } return arr; } } public static class Pairs implements Comparable<Pairs> { int value,index; Pairs(int value, int index) { this.value = value; this.index = index; } public int compareTo(Pairs p) { return Integer.compare(this.value, p.value); } } static final Random random = new Random(); static void ruffleSort(int arr[]) { int n = arr.length; for(int i=0; i<n; i++) { int j = random.nextInt(n),temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static long nCk(int n, int k) { return (modMult(fact(n),fastexp(modMult(fact(n-k),fact(k)),M-2))); } static long fact (long n) { long fact =1; for(int i=1; i<=n; i++) { fact = modMult(fact,i); } return fact%M; } static long modMult(long a,long b) { return a*b%M; } static long fastexp(long x, int y){ if(y==1) return x; long ans = fastexp(x,y/2); if(y%2 == 0) return modMult(ans,ans); else return modMult(ans,modMult(ans,x)); } }
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
1fcabcca2af5d8f77c8b916980dd2fa8
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ankur.y */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CDifferentialSorting solver = new CDifferentialSorting(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class CDifferentialSorting { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); long ar[] = in.getLongArray(n); if (ar[n - 2] > ar[n - 1]) { out.println(-1); return; } boolean isSorted = true; for (int i = 0; i < n - 1; i++) { if (ar[i] > ar[i + 1]) { isSorted = false; break; } } if (isSorted) { out.println(0); return; } long val = ar[n - 2] - ar[n - 1]; if (val <= ar[n - 2]) { 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 InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar; private int snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { //*-*------clare-----anjlika--- //remeber while comparing 2 non primitive data type not to use == //remember Arrays.sort for primitive data has worst time case complexity of 0(n^2) bcoz it uses quick sort //again silly mistakes ,yr kb tk krta rhega ye mistakes //try to write simple codes ,break it into simple things //knowledge>rating /* public class Main implements Runnable{ public static void main(String[] args) { new Thread(null,new Main(),"Main",1<<26).start(); } public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; libraries.InputReader in = new libraries.InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC();//chenge the name of task solver.solve(1, in, out); out.close(); } */ if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 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 { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long[] getLongArray(int n) { long[] ar = new long[n]; for (int i = 0; i < n; i++) { ar[i] = nextLong(); } return ar; } 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 String next() { return readString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -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
8112302a83d6ccb58363de90bf842eee
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.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.function.BiFunction; public class Solution { // VM options: -Xmx1024m -Xss1024m private void solve() throws IOException { int n = nextInt(); int[] a = nextIntArray(n); if (a[n - 2] > a[n - 1]) { out.println(-1); } else if (a[n - 1] < 0) { int res = 0; for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) { res = -1; break; } } out.println(res); } else { out.println(n - 2); for (int i = 0; i < n - 2; i++) { println(i + 1, n - 1, n); } } } private static final String inputFilename = "input.txt"; private static final String outputFilename = "output.txt"; private BufferedReader in; private StringTokenizer line; private PrintWriter out; private final boolean isDebug; private Solution(boolean isDebug) { this.isDebug = isDebug; } public static void main(String[] args) throws IOException { new Solution(Arrays.asList(args).contains("DEBUG_MODE")).run(args); } private void run(String[] args) throws IOException { if (isDebug) { in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilename))); // in = new BufferedReader(new InputStreamReader(System.in)); } else { in = new BufferedReader(new InputStreamReader(System.in)); } out = new PrintWriter(System.out); // out = new PrintWriter(outputFilename); // int t = isDebug ? nextInt() : 1; int t = nextInt(); // int t = 1; for (int i = 0; i < t; i++) { // out.print("Case #" + (i + 1) + ": "); solve(); out.flush(); } in.close(); out.flush(); out.close(); } private void println(Object... objects) { boolean isFirst = true; for (Object o : objects) { if (!isFirst) { out.print(" "); } else { isFirst = false; } out.print(o.toString()); } out.println(); } private int[] nextIntArray(int n) throws IOException { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } private long[] nextLongArray(int n) throws IOException { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } private String nextToken() throws IOException { while (line == null || !line.hasMoreTokens()) { line = new StringTokenizer(in.readLine()); } return line.nextToken(); } private static void assertPredicate(boolean p) { if (!p) { throw new RuntimeException(); } } private static void assertPredicate(boolean p, String message) { if (!p) { throw new RuntimeException(message); } } private static void assertNotEqual(int unexpected, int actual) { if (actual == unexpected) { throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual); } } private static void assertEqual(int expected, int actual) { if (expected != actual) { throw new RuntimeException("assertEqual: " + expected + " != " + actual); } } }
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
7990926675a5104113bc5a5d82bd7906
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 { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int test =in.nextInt(); for(int t =1;t<=test;t++) { ArrayList<ArrayList> list = new ArrayList<>(); ArrayList<Integer> l = new ArrayList<>(); int n = in.nextInt(); int a[] = new int[n]; for (int i =0;i<n;i++){ a[i] = in.nextInt(); } if(isSorted(a)){ pw.println(0); } else if(a[n-2]>a[n-1]||a[n-2]-a[n-1]>a[n-2] || a[n-2]-a[n-1]>a[n-1]) { pw.println(-1); } else { pw.println(n-2); for(int i=1;i<=n-2;i++){ pw.println(i+" "+(n-1)+" "+n); } } } pw.close(); } private static boolean isSorted(int[] arr) { for (int i = 1; i < arr.length; i++) if (arr[i] < arr[i - 1]) return false; return true; } 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
f88d1e26e9ae36fa3b57dd93de99a1bf
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.BigInteger; public class Main { public static FastReader cin; public static PrintWriter out; public static void main(String[] args) throws Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); cin = new FastReader(); int qq = cin.nextInt(); // int qq = 1; label:while (qq-- > 0) { int n = cin.nextInt(); int []arr = new int [n + 1]; arr[0] = Integer.MIN_VALUE; for(int i = 1 ; i <= n ; i++){ arr[i] = cin.nextInt(); } if(arr[n - 1] > arr[n] ){ out.println("-1"); continue; } int times = 0; // ArrayList<Integer>list = new ArrayList<>(); for(int i = 1 ; i <= n - 1 ; i++){ if(arr[i] > arr[i + 1]){ times++; } } if(arr[n ]<0){ if(times==0){ out.println(0); continue label; }else{ out.println(-1); continue label; } }else{ times = Math.max(n - 2 ,0); out.println(times); for(int i = 1 ; i <= times ; i++){ out.printf("%d ",i); out.printf("%d ",n - 1); out.printf("%d ",n ); out.println(); } } } out.close(); } static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } return str; } } }
Java
["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"]
2 seconds
["2\n1 2 3\n3 4 5\n-1\n0"]
NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations.
Java 17
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
63f9b0b93df7f0e4225c2e9353732d1b
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.Arrays; import java.util.Scanner; public class DifferentialSort { public static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int cases = sc.nextInt(); for(int i = 0; i<cases;i++){ int n = sc.nextInt(); int count = 0; int bin = 0; int[] arr = new int[n]; for(int j = 0; j<n;j++) { arr[j] = sc.nextInt(); //temp[j] = arr[j]; } long[][] sol = new long[3][n]; if(arr[arr.length-1]<arr[arr.length-2]) { System.out.println(-1); continue; } if(arr[n-1]<0){ boolean flag = true; for(int k = 0; k< arr.length-1;k++){ if(arr[k]>arr[k+1]) flag = false; } if(!flag) System.out.println(-1); else System.out.println(0); continue; } System.out.println(n-2); for(int j = 0; j<n-2;j++){ System.out.println((j+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 17
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
298e2483a2e3698c8f3ae2caa2cb5b94
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 cf; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.*; import java.util.StringTokenizer; public class cftt { 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) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static void debug(int mat[][]) { for(int i=0; i<mat.length; i++) { System.out.print(mat[i][0]+" "+mat[i][1]); System.out.println(); } } public static void main(String[] args) { FastScanner r = new FastScanner(); PrintWriter out=new PrintWriter(System.out); // System.out.println("input"); int tc=r.nextInt(); while(tc-->0) { int n=r.nextInt(); int x[]=new int[n]; for(int i=0; i<n; i++) x[i]=r.nextInt(); if(x[n-2]>x[n-1])System.out.println(-1); else if(already(x)) { System.out.println(0); } else if(x[n-2]-x[n-1]>x[n-2]){ System.out.println(-1); } else { System.out.println(n-2); for(int i=0; i<n-2; i++) { System.out.println((i+1)+" "+(n-1)+" "+(n)); } } //out.close(); } } public static boolean already(int x[]) { for(int i=0; i<x.length-1; i++) { if(x[i]>x[i+1])return false; } return true; } } class pair{ int x; int y; int cost; public pair(int x,int y,int cost) { this.x=x; this.y=y; this.cost=cost; } }
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 17
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
ed35ba7f63905d26b687c00dd3cf3a06
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.math.*; import java.io.*; public class A1{ static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); static StringTokenizer st; static final long mod=1000000007; static final int mod1=998244353; static class Pair{ int a,b; Pair(int a,int b){ this.a=a; this.b=b; } } public static void Solve() throws IOException{ st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()); int[] ar=getArrIn(n); if(ar[n-2]>ar[n-1]){ bw.write("-1\n"); return ; } int[] br=ar.clone(); Arrays.sort(br); if(Arrays.equals(ar,br)){ bw.write("0\n"); return; } if(ar[n-2]<=ar[n-1] && ar[n-1]<0){ bw.write("-1\n"); return ; } bw.write((n-2)+"\n"); for(int i=0;i<n-2;i++){ bw.write((i+1)+" "+(n-1)+" "+n+"\n"); } } /** Main Method**/ public static void main(String[] YDSV) throws IOException{ //int t=1; int t=Integer.parseInt(br.readLine()); while(t-->0) Solve(); bw.flush(); } /** Helpers**/ private static char[] getStr()throws IOException{ return br.readLine().toCharArray(); } private static int Gcd(int a,int b){ if(b==0) return a; return Gcd(b,a%b); } private static long Gcd(long a,long b){ if(b==0) return a; return Gcd(b,a%b); } private static int[] getArrIn(int n) throws IOException{ st=new StringTokenizer(br.readLine()); int[] ar=new int[n]; for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken()); return ar; } private static Integer[] getArrInP(int n) throws IOException{ st=new StringTokenizer(br.readLine()); Integer[] ar=new Integer[n]; for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken()); return ar; } private static long[] getArrLo(int n) throws IOException{ st=new StringTokenizer(br.readLine()); long[] ar=new long[n]; for(int i=0;i<n;i++) ar[i]=Long.parseLong(st.nextToken()); return ar; } private static List<Integer> getListIn(int n) throws IOException{ st=new StringTokenizer(br.readLine()); List<Integer> al=new ArrayList<>(); for(int i=0;i<n;i++) al.add(Integer.parseInt(st.nextToken())); return al; } private static List<Long> getListLo(int n) throws IOException{ st=new StringTokenizer(br.readLine()); List<Long> al=new ArrayList<>(); for(int i=0;i<n;i++) al.add(Long.parseLong(st.nextToken())); return al; } private static long pow_mod(long a,long b) { long result=1; while(b!=0){ if((b&1)!=0) result=(result*a)%mod; a=(a*a)%mod; b>>=1; } return result; } private static int pow_mod(int a,int b) { int result=1; int mod1=(int)mod; while(b!=0){ if((b&1)!=0) result=(result*a)%mod1; a=(a*a)%mod1; b>>=1; } return result; } private static int lower_bound(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+1; } private static long lower_bound(long a[],long 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+1; } private static int upper_bound(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; } private static long upper_bound(long a[],long 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; } private static boolean Sqrt(int x){ int a=(int)Math.sqrt(x); return a*a==x; } private static boolean Sqrt(long x){ long a=(long)Math.sqrt(x); return a*a==x; } }
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 17
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
b767d5d473f8b46c2123ec7f65ae0c37
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; import java.util.*; public class differentialSorting { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++){ int n=sc.nextInt(); int[] arr=new int[n]; int sorted=0; for(int j=0;j<n;j++){ arr[j]=sc.nextInt(); } for(int j=1;j<n;j++){ if(arr[j]>=arr[j-1]) sorted++; } if(n==1) System.out.println("0"); else if(sorted==n-1) System.out.println(0); else if(arr[n-2]>arr[n-1]||arr[n-2]-arr[n-1]>arr[n-2]) System.out.println("-1"); else{ System.out.println(n-2); for(int j=0;j<n-2;j++){ System.out.println(j+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 17
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
90544105f763308eaa10f78c2938e55d
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
//package cf; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.*; import java.util.StringTokenizer; public class cftt { 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) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static void debug(int mat[][]) { for(int i=0; i<mat.length; i++) { System.out.print(mat[i][0]+" "+mat[i][1]); System.out.println(); } } public static void main(String[] args) { FastScanner r = new FastScanner(); PrintWriter out=new PrintWriter(System.out); // System.out.println("input"); int tc=r.nextInt(); while(tc-->0) { int n=r.nextInt(); int x[]=new int[n]; for(int i=0; i<n; i++) { x[i]=r.nextInt(); } int ans=0; for(int i=1; i<n-1; i++) { if(x[i]>x[i+1]&&x[i]>x[i-1]) { if(i<n-2) x[i+1]=Math.max(x[i], x[i+2]); else { x[i+1]=x[i]; } ans++; } } System.out.println(ans); for(int i=0; i<n; i++)System.out.print(x[i]+" "); System.out.println(); //out.close(); } } } class pair{ int x; int y; int cost; public pair(int x,int y,int cost) { this.x=x; this.y=y; this.cost=cost; } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 17
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
ff3c63b8afc8ef816f86e828e8dbb9c9
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.Scanner; public class AvoidLocalMax { public static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int cases = sc.nextInt(); for(int i = 0; i<cases;i++){ int size = sc.nextInt(); int[] arr = new int[size]; for(int j=0;j<size;j++) arr[j] = sc.nextInt(); int op = 0; if(size==2) { System.out.println(0); System.out.println(arr[0]+" "+arr[1]); } else if (size==3) { if(arr[1]>arr[0]&&arr[1]>arr[2]){ System.out.println(1); System.out.println(arr[0]+" "+arr[0]+" "+arr[2]); }else{ System.out.println(0); System.out.println(arr[0]+" "+arr[1]+" "+arr[2]); } } else{ for(int j=size-2;j>=1;j--){ if(arr[j]>arr[j-1]&&arr[j]>arr[j+1]){ if(j>1) { arr[j - 1] = Math.max(arr[j], arr[j - 2]); }else arr[j - 1] = arr[j]; op++; } } System.out.println(op); for(int k=0;k<size;k++){ System.out.print(arr[k]+" "); } System.out.println(); } } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 17
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
234339ba0ef751d96d31434db0cbb2df
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); while(tc-- > 0){ int n = sc.nextInt(); int[] arr =new int[n]; for(int i = 0; i < n; i++){ arr[i] = sc.nextInt(); } int ans = 0; int i = 1; while(i < n-1){ if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){ ans++; if(i != (n-2)){ if(arr[i] > arr[i+2]){ arr[i+1] = arr[i]; }else{ arr[i+1] = arr[i+2]; } i += 2; }else { arr[i+1] = arr[i]; i++; } }else{ i++; } } System.out.println(ans); for(int val: arr){ System.out.print(val+ " "); } System.out.println(); } sc.close(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 17
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
4e0b3c735b7280b22cfe83a9f1d2b347
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); // int a = 1; int t; t = in.nextInt(); //t = 1; while (t > 0) { // out.print("Case #"+(a++)+": "); solver.call(in,out); t--; } out.close(); } static class TaskA { public void call(InputReader in, PrintWriter out) { int n; n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } ArrayList<Integer> arrayList = new ArrayList<>(); int ans = 0; for (int i = 1; i < n - 1; i++) { if(arr[i] > arr[i - 1] && arr[i] > arr[i + 1]){ ans++; if(i + 2 < n) arr[i + 1] = Math.max(arr[i], arr[i + 2]); else{ arr[i + 1] = arr[i]; } } } out.println(ans); for (int i = 0; i < n; i++) { out.print(arr[i]+" "); } out.println(); } } static int gcd(int a, int 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 class answer implements Comparable<answer>{ int a, b; public answer(int a, int b, int c) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return Integer.compare(this.b, o.b); } } static class answer1 implements Comparable<answer1>{ int a, b, c; public answer1(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } @Override public int compareTo(answer1 o) { return (o.a - this.a); } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (Long i:a) l.add(i); l.sort(Collections.reverseOrder()); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static final Random random=new Random(); static void shuffleSort(int[] a) { int n = a.length; 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 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()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
837fce3ea319e5d8f81cda7dc0072d85
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
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); if(n <= 2){ System.out.println(0); for(int i = 0;i < n;i++){ System.out.print(arr[i] + " "); } System.out.println(); continue; } int count = 0; for(int i = 1;i < n-1;i++){ if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){ int temp1 = arr[i]; int temp2 = 0; if(i + 2 < n){ temp2 = arr[i+2]; }else{ temp2 = Integer.MIN_VALUE; } int ans = Math.max(temp1,temp2); arr[i+1] = ans; count++; } } System.out.println(count); for(int i = 0;i < n;i++){ System.out.print(arr[i] + " "); } System.out.println(); } } 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
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
d47dab8fc3c278a1909b12ea7ca253a7
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
//package com.company; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class ProblemSolving { static Scanner scanner = new Scanner(System.in); static BufferedReader reader =new BufferedReader(new InputStreamReader(System.in)); //start main public static void main(String[] args) throws Exception{ int t = 1; t = Integer.parseInt(reader.readLine()); while(t-- > 0){ solve(); } }//end main //start solve public static void solve() throws IOException { //int n = Integer.parseInt(reader.readLine()); //String str = reader.readLine(); //StringTokenizer st = new StringTokenizer("this is a test"); //st.hasMoreTokens(); //st.nextToken(); int n = Integer.parseInt(reader.readLine()); String str = reader.readLine(); StringTokenizer st = new StringTokenizer(str); List<Integer> numbers = new ArrayList<>(); while (st.hasMoreTokens()){ numbers.add(Integer.parseInt(st.nextToken())); } int operations = 0; for(int i=1; i<n-1;i++){ //System.out.println(numbers.get(i) + " " + numbers.get(i-1) + " " + numbers.get(i+1)); if( isLocalMaximum(numbers.get(i), numbers.get(i-1), numbers.get(i+1) ) ) { operations++; if(i != n-2) numbers.set(i+1, (numbers.get(i) > numbers.get(i+2)) ? numbers.get(i) : numbers.get(i+2)); else numbers.set(i+1, numbers.get(i)); } } System.out.println(operations); for (int number: numbers) { System.out.print(number + " "); } System.out.println(); }//end solve public static boolean isLocalMaximum(int number, int numberMinus1, int numberPlus1){ return number > numberMinus1 && number > numberPlus1; } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
1a33ab1fca8f483a28316dd7a82af9f1
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
//package com.company; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class ProblemSolving { static Scanner scanner = new Scanner(System.in); static BufferedReader reader =new BufferedReader(new InputStreamReader(System.in)); //start main public static void main(String[] args) throws Exception{ int t = 1; t = Integer.parseInt(reader.readLine()); while(t-- > 0){ solve(); } }//end main //start solve public static void solve() throws IOException { //StringTokenizer st = new StringTokenizer("this is a test"); //st.hasMoreTokens(); //st.nextToken(); int n = Integer.parseInt(reader.readLine()); String str = reader.readLine(); StringTokenizer st = new StringTokenizer(str); List<Integer> numbers = new ArrayList<>(); while (st.hasMoreTokens()){ numbers.add(Integer.parseInt(st.nextToken())); } int operations = 0; for(int i=1; i<n-1;i++){ //System.out.println(numbers.get(i) + " " + numbers.get(i-1) + " " + numbers.get(i+1)); if( isLocalMaximum(numbers.get(i), numbers.get(i-1), numbers.get(i+1) ) ) { operations++; if (i < n - 3 && isLocalMaximum(numbers.get(i + 2), numbers.get(i + 2 - 1), numbers.get((i + 2 + 1)))) { int deemedValue = numbers.get(i) > numbers.get(i + 2) ? numbers.get(i) : numbers.get(i + 2); numbers.set(i + 1, deemedValue); } else { numbers.set(i + 1, numbers.get(i)); } } } System.out.println(operations); for (int number: numbers) { System.out.print(number + " "); } System.out.println(); }//end solve public static boolean isLocalMaximum(int number, int numberMinus1, int numberPlus1){ return number > numberMinus1 && number > numberPlus1; } public static String intToBinary(int number){ String binaryCode = ""; while (number!=0){ if(number%2==0) binaryCode += "0"; else binaryCode += "1"; number = number/2; } while (binaryCode.length()<31) binaryCode += "0"; StringBuilder sb=new StringBuilder(binaryCode); sb.reverse(); return sb.toString(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
fdea1ff25d56f0361f4003d815d91b9e
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
//package codeforces_contest; // Working program with FastReader // shivam import java.io.*; import java.util.*; import java.math.*; public class cr2_B_1635 { public static void main(String[] args)throws Exception { FastReader sc = new FastReader(); BufferedWriter output=new BufferedWriter(new OutputStreamWriter(System.out)); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long[] arr=new long[n]; long max=0; for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); if(max<arr[i]) max=arr[i]; } int c=0; for(int i=1;i<n-1;i++) { if(arr[i]>arr[i-1] && arr[i]>arr[i+1]) { try { arr[i+1]=Math.max(arr[i],arr[i+2]); } catch(Exception e) { arr[i+1]=arr[i]; } c++; } } output.write(c+"\n"); for(int i=0;i<n;i++) output.write(arr[i]+" "); output.write("\n"); } output.flush(); } 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
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
4c3feac5059813b431d3090246d14784
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class AvoidLocalMaximums2 { 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<>(); for(int i = 0;i<n;i++){ a.add(Integer.parseInt(ints[i])); } int count = 0; for(int i = 1;i<n-1;i++){ if (a.get(i) > a.get(i-1) && a.get(i) > a.get(i+1)){ int temp = a.get(i); if (i + 2 < n) temp = Integer.max(a.get(i+2), temp); a.set(i+1, temp); count++; } } pr.println(count); for(int i = 0;i<n;i++){ pr.print(a.get(i)); if (i!=n-1) pr.print(' '); } pr.println(); } pr.close(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
beb39e797130c52b41242b9e5361cb6d
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class AvoidLocalMaximums { 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<>(); for(int i = 0 ;i<n;i++){ int temp = Integer.parseInt(ints[i]); a.add(temp); } int count = 0; for(int i =1;i<n-1;i++){ //logical error: this will remove atmost one max // if (a.get(i) > a.get(i-1) && a.get(i) > a.get(i+1)){ //// a.set(i, Integer.max(a.get(i-1), a.get(i+1))); //// count++; //// } if (a.get(i) > a.get(i-1) && a.get(i) > a.get(i+1)) { //bug: you set temp as a.get(i+1) int temp = a.get(i); if (i+2 < n){ temp = Integer.max(temp, a.get(i+2)); } a.set(i+1, temp); count++; } } pr.println(count); for(int i = 0;i<n;i++){ pr.print(a.get(i)); if (i!=n-1) pr.print(' '); } pr.println(); } pr.close(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
06d5f222524c8882aa7c623bfea33723
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int count = 0; int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } for(int i=1;i<n-1;i++){ if(a[i]>a[i-1]&&a[i]>a[i+1]){ if(i<n-2){ if(a[i]>a[i+2]){ a[i+1]=a[i]; } else if(a[i]<a[i+2]) { a[i+1]=a[i+2]; } else{ a[i+1]=a[i+2]; } } else{ a[i+1]=a[i]; } count++; } } System.out.println(count); for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } System.out.println(); } sc.close(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
437f87a54841ce0ab3f288324a0b2eff
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
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=new int[n]; for (int i=0;i<n;i++){ int num = fs.nextInt(); arr[i]=num; } int ans=0; for (int i=1;i<n-1;i++){ if(arr[i]>arr[i-1]&&arr[i]>arr[i+1]){ ans++; if(i<n-2) arr[i+1]=Math.max(arr[i],arr[i+2]); else arr[i+1]=arr[i]; } } out.print(ans+"\n"); for (int i:arr)out.print(i+" "); out.print("\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
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
89f440bbdf4154ce1199fa206d599d5b
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; public class AvoidLocalMaximums { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t= sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++) a[i]= sc.nextInt(); int c=0; for(int i=1;i<n-1;i++){ if(a[i]>a[i-1] && a[i+1]<a[i]){ if(i<n-3&&a[i+2]>a[i+1]&& a[i+2]>a[i]) a[i+1]=a[i+2]; else a[i+1]=a[i]; c++; } } System.out.println(c); for(int i=0;i<n;i++){ System.out.print(a[i]+" "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
d158c826ae5cc9c83a74e9891dff100d
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.util.*; public class q1 { public static void main(String[] args) throws Exception { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); for(int i=1;i<=t;i++){ int num_op = 0; int n = scn.nextInt(); int[] arr = new int[n]; int[] new_arr = new int[n]; for(int j=0;j<n;j++){ arr[j] = scn.nextInt(); } for(int j=0;j<n;j++){ new_arr[j] = arr[j]; } for(int j=1;j<n-1;j++){ if(new_arr[j]>new_arr[j-1] && new_arr[j]>new_arr[j+1]){ if(j+2 < n-1){ new_arr[j+1] = Math.max(new_arr[j], new_arr[j+2]); } else{ new_arr[j+1] = new_arr[j]; } num_op++; } } System.out.println(num_op); for(int x=0;x<new_arr.length;x++){ System.out.print(new_arr[x] + " "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
5ff6ff9f59022f39edcdf85317b2e5c5
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class test{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); for (int i = 0; i < n; i++) { int size = scan.nextInt(); int count = 0; int[] arr = new int[size]; for (int j = 0; j < arr.length; j++) { arr[j] = scan.nextInt(); } int max = Integer.MIN_VALUE; for (int j = 0; j < arr.length; j++) { max = Math.max(max, arr[j]); } for (int j = 1; j < arr.length - 1; j++) { if (arr[j] > arr[j - 1] && arr[j] > arr[j + 1]) { count++; if (j + 2 <= size - 1 && arr[j + 2] > arr[j + 1]) {// 2 1 3 1 3 1 3 1 3 if (arr[j + 2] > arr[j]) { arr[j + 1] = arr[j + 2]; } else { arr[j + 1] = arr[j]; } } else { arr[j + 1] = arr[j]; } } } System.out.println(count); for (int j = 0; j < arr.length; j++) { System.out.print(arr[j] + " "); } } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
29a511560a93c3302520eb7586589ae0
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.*; // import java.lang.*; // import java.math.*; public class Main { static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=1000000007; // static long mod=998244353; static int MAX=Integer.MAX_VALUE; static int MIN=Integer.MIN_VALUE; static long MAXL=Long.MAX_VALUE; static long MINL=Long.MIN_VALUE; static ArrayList<Integer> graph[]; static long fact[]; static int seg[]; static int lazy[];//lazy propagation is used in case of range updates. static int dp[][]; // static long dp[][][]; public static void main (String[] args) throws java.lang.Exception { // code goes here int t=I(); outer:while(t-->0) { int n=I(); int a[]=I(n); ArrayList<Integer> arr=new ArrayList<>(); for(int i=1;i<n-1;i++){ if(a[i]>a[i-1] && a[i]>a[i+1]){ arr.add(i); } } // printArray(arr); int ans=0; int i; HashSet<Integer> hs=new HashSet<>(); for(i=0;i<arr.size()-1;i++){ if(arr.get(i)+2==arr.get(i+1)){ hs.add(arr.get(i)); hs.add(arr.get(i+1)); ans++; a[arr.get(i)+1]=max(a[arr.get(i)],a[arr.get(i)+2]); i++; }else{ hs.add(arr.get(i)); ans++; a[arr.get(i)]=max(a[arr.get(i)-1],a[arr.get(i)+1]); } } // printArray(a); if(arr.size()>0 && !hs.contains(arr.get(arr.size()-1))){ ans++; a[arr.get(arr.size()-1)-1]=a[arr.get(arr.size()-1)]; } out.println(ans); printArray(a); } out.close(); } public static class pair { int a; int b; public pair(int aa,int bb) { a=aa; b=bb; } } public static class myComp implements Comparator<pair> { //sort in ascending order. public int compare(pair p1,pair p2) { if(p1.a==p2.a) return 0; else if(p1.a<p2.a) return -1; else return 1; } // sort in descending order. // public int compare(pair p1,pair p2) // { // if(p1.a==p2.a) // return 0; // else if(p1.a<p2.a) // return 1; // else // return -1; // } } public static void DFS(int s,boolean visited[],int dis) { visited[s]=true; for(int i:graph[s]){ if(!visited[i]){ DFS(i,visited,dis); } } } public static void setGraph(int n,int m)throws IOException { graph=new ArrayList[n+1]; for(int i=0;i<=n;i++){ graph[i]=new ArrayList<>(); } for(int i=0;i<m;i++){ int u=I(),v=I(); graph[u].add(v); graph[v].add(u); } } //LOWER_BOUND and UPPER_BOUND functions //It returns answer according to zero based indexing. public static int lower_bound(long arr[],long X,int start, int end) //start=0,end=n-1 { if(start>end)return -1; if(arr[arr.length-1]<X)return end; if(arr[0]>X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; // if(arr[mid]==X){ //Returns last index of lower bound value. // if(mid<end && arr[mid+1]==X){ // left=mid+1; // }else{ // return mid; // } // } if(arr[mid]==X){ //Returns first index of lower bound value. if(mid>start && arr[mid-1]==X){ right=mid-1; }else{ return mid; } } else if(arr[mid]>X){ if(mid>start && arr[mid-1]<X){ return mid-1; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1]>X){ return mid; }else{ left=mid+1; } } } return left; } //It returns answer according to zero based indexing. public static int upper_bound(long arr[],long X,int start,int end) //start=0,end=n-1 { if(arr[0]>=X)return start; if(arr[arr.length-1]<X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; if(arr[mid]==X){ //returns first index of upper bound value. if(mid>start && arr[mid-1]==X){ right=mid-1; }else{ return mid; } } // if(arr[mid]==X){ //returns last index of upper bound value. // if(mid<end && arr[mid+1]==X){ // left=mid+1; // }else{ // return mid; // } // } else if(arr[mid]>X){ if(mid>start && arr[mid-1]<X){ return mid; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1]>X){ return mid+1; }else{ left=mid+1; } } } return left; } //END // File file = new File("C:\\Users\\Dell\\Desktop\\JAVA\\testcase.txt"); // FileWriter fw = new FileWriter("output.txt"); // BufferedReader br= new BufferedReader(new FileReader(file)); //Segment Tree Code public static void buildTree(int si,int ss,int se) { if(ss==se){ seg[si]=0; return; } int mid=(ss+se)/2; buildTree(2*si+1,ss,mid); buildTree(2*si+2,mid+1,se); seg[si]=max(seg[2*si+1],seg[2*si+2]); } public static void merge(ArrayList<Integer> f,ArrayList<Integer> a,ArrayList<Integer> b) { int i=0,j=0; while(i<a.size() && j<b.size()){ if(a.get(i)<=b.get(j)){ f.add(a.get(i)); i++; }else{ f.add(b.get(j)); j++; } } while(i<a.size()){ f.add(a.get(i)); i++; } while(j<b.size()){ f.add(b.get(j)); j++; } } public static void update(int si,int ss,int se,int pos,int val) { if(ss==se){ seg[si]=(int)add(seg[si],val); return; } int mid=(ss+se)/2; if(pos<=mid){ update(2*si+1,ss,mid,pos,val); }else{ update(2*si+2,mid+1,se,pos,val); } seg[si]=(int)add(seg[2*si+1],seg[2*si+2]); } public static int query(int si,int ss,int se,int qs,int qe) { if(qs>se || qe<ss)return 0; if(ss>=qs && se<=qe)return seg[si]; int mid=(ss+se)/2; return max(query(2*si+1,ss,mid,qs,qe),query(2*si+2,mid+1,se,qs,qe)); } //Segment Tree Code end //Prefix Function of KMP Algorithm public static int[] KMP(char c[],int n) { int pi[]=new int[n]; for(int i=1;i<n;i++){ int j=pi[i-1]; while(j>0 && c[i]!=c[j]){ j=pi[j-1]; } if(c[i]==c[j])j++; pi[i]=j; } return pi; } public static long nPr(int n,int r) { long ans=divide(fact(n),fact(n-r),mod); return ans; } public static long nCr(int n,int r) { long ans=divide(fact[n],mul(fact[n-r],fact[r]),mod); return ans; } public static long kadane(long a[],int n) //largest sum subarray { long max_sum=Long.MIN_VALUE,max_end=0; for(int i=0;i<n;i++){ max_end+=a[i]; if(max_sum<max_end){max_sum=max_end;} if(max_end<0){max_end=0;} } return max_sum; } public static boolean isSorted(int a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static boolean isSorted(long a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static int computeXOR(int n) //compute XOR of all numbers between 1 to n. { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } public static int np2(int x) { x--; x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x++; return x; } public static int hp2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static long hp2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static ArrayList<Integer> primeSieve(int n) { ArrayList<Integer> arr=new ArrayList<>(); 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) arr.add(i); } return arr; } // Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n); public static class FenwickTree { int farr[]; int n; public FenwickTree(int c) { n=c+1; farr=new int[n]; } // public void update_range(int l,int r,long p) // { // update(l,p); // update(r+1,(-1)*p); // } public void update(int x,int p) { for(;x<n;x+=x&(-x)) { farr[x]+=p; } } public int get(int x) { int ans=0; for(;x>0;x-=x&(-x)) { ans=ans+farr[x]; } return ans; } } //Disjoint Set Union public static class DSU { int par[],rank[]; public DSU(int c) { par=new int[c+1]; rank=new int[c+1]; for(int i=0;i<=c;i++) { par[i]=i; rank[i]=0; } } public int find(int a) { if(a==par[a]) return a; return par[a]=find(par[a]); } public void union(int a,int b) { int a_rep=find(a),b_rep=find(b); if(a_rep==b_rep) return; if(rank[a_rep]<rank[b_rep]) par[a_rep]=b_rep; else if(rank[a_rep]>rank[b_rep]) par[b_rep]=a_rep; else { par[b_rep]=a_rep; rank[a_rep]++; } } } public static HashMap<Integer,Integer> primeFact(int a) { // HashSet<Long> arr=new HashSet<>(); HashMap<Integer,Integer> hm=new HashMap<>(); int p=0; while(a%2==0){ // arr.add(2L); p++; a=a/2; } hm.put(2,hm.getOrDefault(2,0)+p); for(int i=3;i*i<=a;i+=2){ p=0; while(a%i==0){ // arr.add(i); p++; a=a/i; } hm.put(i,hm.getOrDefault(i,0)+p); } if(a>2){ // arr.add(a); hm.put(a,hm.getOrDefault(a,0)+1); } // return arr; return hm; } public static boolean isInteger(double N) { int X = (int)N; double temp2 = N - X; if (temp2 > 0) { return false; } return true; } public static boolean isPalindrome(String s) { int n=s.length(); for(int i=0;i<=n/2;i++){ if(s.charAt(i)!=s.charAt(n-i-1)){ return false; } } return true; } public static int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static long fact(long n) { long fact=1; for(long i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static long fact(int n) { long fact=1; for(int i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; 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; } public static boolean isPrime(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; } public static void printArray(long a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(int a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(char a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]); } out.println(); } public static void printArray(String a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(boolean a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(pair a[]) { for(pair p:a){ out.println(p.a+"->"+p.b); } } public static void printArray(int a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(long a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(char a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(ArrayList<?> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printMapI(HashMap<?,?> hm){ for(Map.Entry<?,?> e:hm.entrySet()){ out.println(e.getKey()+"->"+e.getValue()); }out.println(); } public static void printMap(HashMap<Long,ArrayList<Integer>> hm){ for(Map.Entry<Long,ArrayList<Integer>> e:hm.entrySet()){ out.print(e.getKey()+"->"); ArrayList<Integer> arr=e.getValue(); for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); }out.println(); } } //Modular Arithmetic public static long add(long a,long b) { a+=b; if(a>=mod)a-=mod; return a; } public static long sub(long a,long b) { a-=b; if(a<0)a+=mod; return a; } public static long mul(long a,long b) { return ((a%mod)*(b%mod))%mod; } public static long divide(long a,long b,long m) { a=mul(a,modInverse(b,m)); return a; } public static long modInverse(long a,long m) { int x=0,y=0; own p=new own(x,y); long g=gcdExt(a,m,p); if(g!=1){ out.println("inverse does not exists"); return -1; }else{ long res=((p.a%m)+m)%m; return res; } } public static long gcdExt(long a,long b,own p) { if(b==0){ p.a=1; p.b=0; return a; } int x1=0,y1=0; own p1=new own(x1,y1); long gcd=gcdExt(b,a%b,p1); p.b=p1.a - (a/b) * p1.b; p.a=p1.b; return gcd; } public static long pwr(long m,long n) { long res=1; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m); } n=n>>1; m=(m*m); } return res; } public static long modpwr(long m,long n) { long res=1; m=m%mod; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m)%mod; } n=n>>1; m=(m*m)%mod; } return res; } public static class own { long a; long b; public own(long val,long index) { a=val; b=index; } } //Modular Airthmetic public 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); } public static void sort(char[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { char tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public 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); } //max & min public static int max(int a,int b){return Math.max(a,b);} public static int min(int a,int b){return Math.min(a,b);} public static int max(int a,int b,int c){return Math.max(a,Math.max(b,c));} public static int min(int a,int b,int c){return Math.min(a,Math.min(b,c));} public static long max(long a,long b){return Math.max(a,b);} public static long min(long a,long b){return Math.min(a,b);} public static long max(long a,long b,long c){return Math.max(a,Math.max(b,c));} public static long min(long a,long b,long c){return Math.min(a,Math.min(b,c));} public static int maxinA(int a[]){int n=a.length;int mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static long maxinA(long a[]){int n=a.length;long mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static int mininA(int a[]){int n=a.length;int mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long mininA(long a[]){int n=a.length;long mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long suminA(int a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} public static long suminA(long a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} //end public static int[] I(int n) { int a[]=new int[n]; for(int i=0;i<n;i++){ a[i]=I(); } return a; } public static long[] IL(int n) { long a[]=new long[n]; for(int i=0;i<n;i++){ a[i]=L(); } return a; } public static long[] prefix(int a[]) { int n=a.length; long pre[]=new long[n]; pre[0]=a[0]; for(int i=1;i<n;i++){ pre[i]=pre[i-1]+a[i]; } return pre; } public static long[] prefix(long a[]) { int n=a.length; long pre[]=new long[n]; pre[0]=a[0]; for(int i=1;i<n;i++){ pre[i]=pre[i-1]+a[i]; } return pre; } public static int I(){return sc.I();} public static long L(){return sc.L();} public static String S(){return sc.S();} public static double D(){return sc.D();} } 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 I(){ return Integer.parseInt(next()); } long L(){ return Long.parseLong(next()); } double D(){ return Double.parseDouble(next()); } String S(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
57015934709da9e3720271419b97fa1c
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
/* /$$$$$ /$$$$$$ /$$ /$$ /$$$$$$ |__ $$ /$$__ $$ |$$ |$$ /$$__ $$ | $$| $$ \ $$| $$|$$| $$ \ $$ | $$| $$$$$$$$| $$ / $$/| $$$$$$$$ / $$ | $$| $$__ $$ \ $$ $$/ | $$__ $$ | $$ | $$| $$ | $$ \ $$$/ | $$ | $$ | $$$$$$/| $$ | $$ \ $/ | $$ | $$ \______/ |__/ |__/ \_/ |__/ |__/ /$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$$$ /$$$$$$$ | $$__ $$| $$__ $$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$$ /$$$| $$$ /$$$| $$_____/| $$__ $$ | $$ \ $$| $$ \ $$| $$ \ $$| $$ \__/| $$ \ $$| $$ \ $$| $$$$ /$$$$| $$$$ /$$$$| $$ | $$ \ $$ | $$$$$$$/| $$$$$$$/| $$ | $$| $$ /$$$$| $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$ $$/$$ $$| $$$$$ | $$$$$$$/ | $$____/ | $$__ $$| $$ | $$| $$|_ $$| $$__ $$| $$__ $$| $$ $$$| $$| $$ $$$| $$| $$__/ | $$__ $$ | $$ | $$ \ $$| $$ | $$| $$ \ $$| $$ \ $$| $$ | $$| $$\ $ | $$| $$\ $ | $$| $$ | $$ \ $$ | $$ | $$ | $$| $$$$$$/| $$$$$$/| $$ | $$| $$ | $$| $$ \/ | $$| $$ \/ | $$| $$$$$$$$| $$ | $$ |__/ |__/ |__/ \______/ \______/ |__/ |__/|__/ |__/|__/ |__/|__/ |__/|________/|__/ |__/ */ import java.io.BufferedReader; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; import java.io.*; public class abc { static PrintWriter pw; static long x = 1, y = 1; /* * static long inv[]=new long[1000001]; static long dp[]=new long[1000001]; */ /// MAIN FUNCTION/// public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); pw = new PrintWriter(System.out); // use arraylist as it uses the concept of dynamic table(amortized analysis) // Arrays.stream(array).forEach(a -> Arrays.fill(a, 0)); /* List<Integer> l1 = new ArrayList<Integer>(); */ // Random rand = new Random(); int tst = sc.nextInt(); while (tst-- > 0) { int n=sc.nextInt(); int a[]=sc.readArray(n); int count=0; int map[]=new int[n]; for(int i=1;i<n-1;i++) { if(a[i]>a[i+1] && a[i]>a[i-1]) map[i]=1; } for(int i=1;i<n-1;i++) { if(map[i-1]==1 && map[i+1]==1) { a[i]=(int) max(a[i-1], a[i+1]); map[i-1]=0; map[i+1]=0; count++; } } for(int i=1;i<n-1;i++) { if(map[i]==1) { count++; a[i]=Math.max(a[i+1], a[i-1]); } } pw.println(count); for(int i=0;i<n;i++) { pw.print(a[i]+" "); } pw.println(); } pw.flush(); } public static int perfectSum(int arr[],int n, int sum) { int dp[][]=new int[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]=0; if(j==0) dp[i][j]=1; } } 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]+dp[i-1][j-arr[i-1]]; else dp[i][j]=dp[i-1][j]; dp[i][j]=dp[i][j]; } } return dp[n][sum]; } public static long eculidean_gcd(long a, long b) { if (a == 0) { x = 0; y = 1; return b; } long ans = eculidean_gcd(b % a, a); long x1 = x; x = y - (b / a) * x; y = x1; return ans; } public static boolean isLsbOne(int n) { if ((n & 1) != 0) return true; return false; } public static pair helper(int arr[], int start, int end, int k, pair dp[][]) { if (start >= end) { if (start == end) return (new pair(arr[start], 0)); else return (new pair(0, 0)); } if (dp[start][end].x != -1 && dp[start][end].y != -1) { return dp[start][end]; } pair ans = new pair(0, Integer.MAX_VALUE); for (int i = start; i < end; i++) { pair x1 = helper(arr, start, i, k, dp); pair x2 = helper(arr, i + 1, end, k, dp); long tip = k * (x1.x + x2.x) + x1.y + x2.y; if (tip < ans.y) ans = new pair(x1.x + x2.x, tip); } return dp[start][end] = ans; } public static void debugger() { Random rand = new Random(); int tst = (int) (Math.abs(rand.nextInt()) % 2 + 1); pw.println(tst); while (tst-- > 0) { int n = (int) (Math.abs(rand.nextInt()) % 5 + 1); pw.println(n); for (int i = 0; i < n; i++) { pw.print((int) (Math.abs(rand.nextInt()) % 6 + 1) + " "); } pw.println(); } } 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) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } 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 void recursion(int n) { if (n == 1) { pw.print(n + " "); return; } // pw.print(n+" "); gives us n to 1 recursion(n - 1); // pw.print(n+" "); gives us 1 to n } // ch.charAt(i)+"" converts into a char sequence 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; } /* CREATED BY ME */ 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; } } public static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public static boolean isPrime(long n) { if (n == 2) return true; long i = 2; while (i * i <= n) { if (n % i == 0) return false; i++; } return true; } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static double max(double x, double y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int abs(int x) { return Math.abs(x); } static long abs(long x) { return Math.abs(x); } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } public static class pair { long x; long y; public pair(long a, long b) { x = a; y = b; } } public static class Comp implements Comparator<pair> { public int compare(pair a, pair b) { long ans = a.y - b.y; if (ans > 0) return 1; if (ans < 0) return -1; return 0; // if(a.x!=b.x) // return Integer.compare((int)a.x, (int)b.x); // else // return Integer.compare((int)a.y, (int)b.y); } } // modular exponentiation public static long fastExpo(long a, int n, int mod) { if (n == 0) return 1; else { if ((n & 1) == 1) { long x = fastExpo(a, n / 2, mod); return (((a * x) % mod) * x) % mod; } else { long x = fastExpo(a, n / 2, mod); return (((x % mod) * (x % mod)) % mod) % mod; } } } public static long modInverse(long n, int p) { return fastExpo(n, p - 2, p); } /* * public static void extract(ArrayList<Integer> ar, int k, int d) { int c = 0; * for (int i = 1; i < k; i++) { int x = 0; boolean dm = false; while (x > 0) { * long dig = x % 10; x = x / 10; if (dig == d) { dm = true; break; } } if (dm) * ar.add(i); } } */ public static int[] prefixfuntion(String s) { int n = s.length(); int z[] = new int[n]; for (int i = 1; i < n; i++) { int j = z[i - 1]; while (j > 0 && s.charAt(i) != s.charAt(j)) j = z[j - 1]; if (s.charAt(i) == s.charAt(j)) j++; z[i] = j; } return z; } // counts the set(1) bit of a number public static long countSetBitsUtil(long x) { if (x <= 0) return 0; return (x % 2 == 0 ? 0 : 1) + countSetBitsUtil(x / 2); } //tells whether a particular index has which bit of a number public static int getIthBitsUtil(int x, int y) { return (x & (1 << y)) != 0 ? 1 : 0; } public static void swaper(long x, long y) { x = x ^ y; y = y ^ x; x = x ^ y; } public static double decimalPlaces(double sum) { DecimalFormat df = new DecimalFormat("#.00"); String angleFormated = df.format(sum); double fin = Double.parseDouble(angleFormated); return fin; } //use collections.swap for swapping static boolean isSubSequence(String str1, String str2, int m, int n) { int j = 0; for (int i = 0; i < n && j < m; i++) if (str1.charAt(j) == str2.charAt(i)) j++; return (j == m); } static long sum(long n) { long s2 = 0, max = -1, min = 10; while (n > 0) { s2 = (n % 10); min = min(s2, min); max = max(s2, max); n = n / 10; } return max * min; } static long pow(long base, long power) { if (power == 0) { return 1; } long result = pow(base, power / 2); if (power % 2 == 1) { return result * result * base; } return result * result; } // return the hash value of a string static long compute_hash(String s) { long val = 0; long p = 31; long mod = (long) (1000000007); long pow = 1; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); val = (val + (int) (ch - 'a' + 1) * pow) % mod; pow = (pow * p) % mod; } return val; } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
e4897733f549ff7b6f068411c747704f
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
/* * Everything is Hard * Before Easy * Jai Mata Dii */ import java.util.*; import java.io.*; 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; }} static long mod = (long)(1e9+7); // static long mod = 998244353; // static Scanner sc = new Scanner(System.in); static FastReader sc = new FastReader(); static PrintWriter out = new PrintWriter(System.out); public static void main (String[] args) { int ttt = 1; ttt = sc.nextInt(); z :for(int tc=1;tc<=ttt;tc++){ int n = sc.nextInt(); long a[] = new long[n]; for(int i=0;i<n;i++) { a[i] = sc.nextLong(); } int cnt = 0; ArrayList<Integer> l = new ArrayList<>(); for(int i=1;i<n-1;i++) { if(a[i]>a[i-1] && a[i]>a[i+1]) { l.add(i); } } int i = 0; while(i<l.size()) { if(i == l.size()-1) { cnt++; a[l.get(i)] = Math.max(a[l.get(i)-1], a[l.get(i)+1]); break; } else { if(l.get(i)+1 == l.get(i+1)-1){ cnt++; a[l.get(i)+1] = Math.max(a[l.get(i)], a[l.get(i+1)]); i += 2; } else { cnt++; a[l.get(i)] = Math.max(a[l.get(i)-1], a[l.get(i)+1]); i++; } } } out.write(cnt+"\n"); for(long val : a) out.write(val+" "); out.write("\n"); } out.close(); } static long pow(long a, long b){long ret = 1;while(b>0){if(b%2 == 0){a = (a*a)%mod;b /= 2;}else{ret = (ret*a)%mod;b--;}}return ret%mod;} static long gcd(long a,long b){if(b==0) return a; return gcd(b,a%b); } private static void sort(int[] a) {List<Integer> k = new ArrayList<>();for(int val : a) k.add(val);Collections.sort(k);for(int i=0;i<a.length;i++) a[i] = k.get(i);} private static void ini(List<Integer>[] tre2){for(int i=0;i<tre2.length;i++){tre2[i] = new ArrayList<>();}} private static void init(List<int[]>[] tre2){for(int i=0;i<tre2.length;i++){tre2[i] = new ArrayList<>();}} private static void sort(long[] a) {List<Long> k = new ArrayList<>();for(long val : a) k.add(val);Collections.sort(k);for(int i=0;i<a.length;i++) a[i] = k.get(i);} }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
345de7e2623f00821b6c59d1de86e535
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.*; import java.lang.Math; import java.util.StringTokenizer; public class ten { public static void main(String[] args){ int a[], b, t; String str; FastReader in = new FastReader(); t = in.nextInt(); while(t-- > 0){ int n = in.nextInt(); a = new int[n]; int max = Integer.MIN_VALUE; int op = 0; for(int i = 0;i < n;i++){ a[i] = in.nextInt(); if(a[i] > max){ max = a[i]; } } for(int i = 1; i < n - 1;i++){ if(a[i] > a[i + 1] && a[i] > a[i - 1]){ if(i + 2 < n){ a[i + 1] = Math.max(a[i] , a[i + 2]); op++; }else{ a[i + 1] = a[i]; op++; } } } System.out.println(op); for(int i = 0; i < n;i++){ System.out.print(a[i] + " "); } System.out.println(); } } 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 { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
f1df58e329ec9f4082865712abc048ae
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.awt.Desktop; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URI; import java.net.URISyntaxException; import java.sql.Array; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.Vector; import org.w3c.dom.Node; public class codechef3 { static class comp implements Comparator<String> { @Override public int compare(String o1, String o2) { if(o1.length()>o2.length()) return 1; else if(o1.length()<o2.length()) return -1; else return o1.compareTo(o2); } } static class Pair<Integer,Intetger> { int k=0; int v=0; public Pair(int a,int b) { k=a; v=b; } public int getKey() { return k; } } 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 s=new FastReader(); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++) a[i]=s.nextInt(); int count=0; for(int i=1;i<n-1;i++) { if(a[i]>a[i+1]&&a[i]>a[i-1]) { int max=a[i]; if(i+2<n) max=Math.max(a[i], a[i+2]); a[i+1]=max; count++; } } System.out.println(count); for(int i=0;i<n;i++) System.out.print(a[i]+" "); System.out.println(); } } //1 1 1 1 1 1 1 1 1 1 1 1 }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
225830cdeb7be86bb76de4b7b0e2d0ce
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; import java.io.PrintWriter; public class Template { public static void main(String[] args){ FastReader in = new FastReader(); int t = in.nextInt(); // test cases PrintWriter out = new PrintWriter(System.out); while (t-- > 0){ int n = in.nextInt(); int[] arr = new int[n]; for(int i=0; i<n; i++){ arr[i] = in.nextInt(); } // boolean[] isMax = isMaxArr(arr,n); int count = 0; for (int i = 1; i<n-1;i++){ if(i < n-3 && (arr[i] > arr[i+1] && arr[i] > arr[i-1]) && (arr[i+2] > arr[i+1] && arr[i+2] > arr[i+3])){ if (arr[i]>arr[i+2]){ arr[i+1] = arr[i]; }else{ arr[i+1] = arr[i+2]; } count++; }else if((arr[i] > arr[i+1] && arr[i] > arr[i-1])){ arr[i+1] = arr[i]; count++; } // isMax = isMaxArr(arr,n); } out.println(count); for (int i = 0; i<n;i++){ out.print(arr[i] + " "); } out.println(); } out.flush(); } // public static boolean[] isMaxArr(int[] arr, int n){ // boolean[] isMax = new boolean[n]; // for (int i = 1; i<n-1;i++){ // if(arr[i] > arr[i+1] && arr[i] > arr[i-1]){ // isMax[i] = true; // }else{ // isMax[i] = false; // } // } // return isMax; // } // FastReader Class 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; } } // Count Frequencies // If Sorted returns the element static HashMap <Integer, Integer> hm = new HashMap<Integer, Integer>(); static void countFreq(int a[], int n) { for (int i=0; i<n; i++) if (hm.containsKey(a[i]) ) hm.put(a[i], hm.get(a[i]) + 1); else hm.put(a[i] , 1); } static int query(int x) { if (hm.containsKey(x)) return hm.get(x); return 0; } // returns all elements static void countFreqAll(int arr[], int n) { Map<Integer, Integer> mp = new HashMap<>(); for (int i = 0; i < n; i++) { if (mp.containsKey(arr[i])) { mp.put(arr[i], mp.get(arr[i]) + 1); } else { mp.put(arr[i], 1); } } for (Map.Entry<Integer, Integer> entry : mp.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } } public static void inserstionSort(int a[]) { for (int i = 1; i < a.length; i++ ) { int value = a[i]; int j; for ( j = i - 1; j >= 0 && a[j] > value; j-- ){ a[j+1] = a[j]; } a[j+1] = value; } } static void printArray(int[] arr) // for testing only { int size = arr.length; for(int i = 0; i < size; i++){ System.out.print(arr[i] + " "); } System.out.println(); } static int numDigits(int num){ return (int) Math.floor(Math.log10(num)) + 1; } static boolean isPrime(int n){ if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static int minArr(int[] array){ int min = Arrays.stream(array).min().getAsInt(); return min; } public static int maxArr(int[] array){ int max = Arrays.stream(array).max().getAsInt(); return max; } public static int getArrayIndex(int[] arr,int value) { int k=-1; for(int i=0;i<arr.length;i++){ if(arr[i]==value){ k=i; break; } } return k; } public static boolean isSorted(int[] array) { for (int i = 0; i < array.length - 1; i++) { if (array[i] > array[i + 1]) return false; } return true; } public static void reverseSub(int[] array, int start, int end){ int tmp; if (start>end){ return; } while(start<end){ tmp = array[start]; array[start] = array[end]; array[end] = tmp; start++; end--; } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
141e2ad3b76e28eb719a12521a8798d4
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.awt.image.AreaAveragingScaleFilter; import java.io.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; public class Pratice { static final long mod = 1000000007; static StringBuilder sb = new StringBuilder(); static int xn = (int) (2e5 + 10); static long ans; static boolean prime[] = new boolean[1000001]; // calculate sqrt and cuberoot // static Set<Long> set=new TreeSet<>(); // static // { // long n=1000000001; // // // for(int i=1;i*i<=n;i++) // { // long x=i*i; // set.add(x); // } // for(int i=1;i*i*i<=n;i++) // { // long x=i*i*i; // set.add(x); // } // } static void sieveOfEratosthenes() { for (int i = 0; i <= 1000000; i++) prime[i] = true; prime[0] = false; prime[1] = false; for (int p = 2; p * p <= 1000000; p++) { if (prime[p] == true) { for (int i = p * p; i <= 1000000; i += p) prime[i] = false; } } } public static void main(String[] args) throws IOException { Reader reader = new Reader(); int t = reader.nextInt(); while (t-- > 0) { int n=reader.nextInt(); int arr[]=new int[n]; int max=-1; int min=Integer.MAX_VALUE; int or=0; for (int i = 0; i < n; i++) { arr[i]=reader.nextInt(); max=Math.max(max,arr[i]); min=Math.min(min,arr[i]); } int count=0; if(n<=2) { System.out.println(0); for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } continue; } for(int i=1;i<n-2;i++) { if(arr[i-1]<arr[i] && arr[i+1]<arr[i]) { count++; if(arr[i+2]>arr[i+1] && arr[i+2]>=arr[i]) arr[i+1]=arr[i+2]; else arr[i+1]=arr[i]; } } if(arr[n-2]>arr[n-1] && arr[n-3]<arr[n-2]) { count++; arr[n-1]=max; } System.out.println(count); for (int i = 0; i < n; i++) { System.out.print(arr[i]+" "); } System.out.println(); } } // static void SieveOfEratosthenes(int n, boolean prime[], // boolean primesquare[], int a[]) // { // // 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 i = 2; i <= n; i++) // prime[i] = true; // // /* Create a boolean array "primesquare[0..n*n+1]" // and initialize all entries it as false. // A value in squareprime[i] will finally // be true if i is square of prime, // else false.*/ // for (int i = 0; i < ((n * n) + 1); i++) // primesquare[i] = false; // // // 1 is not a prime number // prime[1] = false; // // 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 * 2; i <= n; i += p) // prime[i] = false; // } // } // // int j = 0; // for (int p = 2; p <= n; p++) { // if (prime[p]) { // // a[j] = p; // // // primesquare[p * p] = true; // j++; // } // } // } // // // static int countDivisors(int n) // { // // if (n == 1) // return 1; // // boolean prime[] = new boolean[n + 1]; // boolean primesquare[] = new boolean[(n * n) + 1]; // // int a[] = new int[n]; // // // SieveOfEratosthenes(n, prime, primesquare, a); // // // int ans = 1; // // // Loop for counting factors of n // for (int i = 0;; i++) { // // a[i] is not less than cube root n // if (a[i] * a[i] * a[i] > n) // break; // // int cnt = 1; // // // if a[i] is a factor of n // while (n % a[i] == 0) { // n = n / a[i]; // // // incrementing power // cnt = cnt + 1; // } // // // ans = ans * cnt; // } // // // if (prime[n]) // ans = ans * 2; // // // Second case // else if (primesquare[n]) // ans = ans * 3; // // // Third case // else if (n != 1) // ans = ans * 4; // // return ans; // Total divisors // } public static long[] inarr(long n) throws IOException { Reader reader = new Reader(); long arr[]=new long[(int) n]; for (long i = 0; i < n; i++) { arr[(int) i]=reader.nextLong(); } return arr; } public static boolean checkPerfectSquare(int number) { int x=number % 10; if (x==2 || x==3 || x==7 || x==8) { return false; } for (int i=0; i<=number/2 + 1; i++) { if (i*i==number) { return true; } } return false; } // check number is prime or not public static boolean isPrime(int n) { return BigInteger.valueOf(n).isProbablePrime(1); } // return the gcd of two numbers public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } // return lcm of number static long lcm(int a, int b) { return (a / gcd(a, b)) * b; } // number of digits in the given number public static long numberOfDigits(long n) { long ans= (long) (Math.floor((Math.log10(n)))+1); return ans; } // return most significant bit in the number public static long mostSignificantNumber(long n) { double k=Math.log10(n); k=k-Math.floor(k); int ans=(int)Math.pow(10,k); return ans; } 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') 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
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
e3e8e975a0906259a3381ce8925cb007
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
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 { public static void main (String[] args) throws java.lang.Exception { // your code goes here try { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); } long res=0; for(int i=1;i<n-1;i++) { if(arr[i]>arr[i-1] && arr[i]>arr[i+1]) { if((i+2)<n && arr[i+2]>arr[i] ) { arr[i+1]=arr[i+2]; } else{ arr[i+1]=arr[i]; } res++; } } System.out.println(res); String s=""; for(long x:arr) { System.out.print(x+" "); } System.out.println(); } } catch(Exception e) { } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
0463c9884bb79537d1bb7d60e6dde9ae
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int test = in.nextInt(); for(int ii=1;ii<=test;ii++) { int n = in.nextInt(), c = 0,max= 0; int a[] = new int[n]; for(int i = 0;i<n;i++) { a[i] = in.nextInt(); max=Math.max(max, a[i]); } for(int i = 1;i<n-1;i++) { if(a[i]>a[i-1]&& a[i]>a[i+1]) { if(i+2<n) { a[i+1]=Math.max(a[i],a[i+2]); } else { a[i+1]=a[i]; } c++; } } System.out.println(c); for(int i :a) { System.out.print(i+" "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
7d1b19996a1c8d0466e71024cf9fa54c
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.LinkedBlockingDeque; public class Codeforces2 { static String ab,b; static class Node { int val; Node left; Node right; public Node(int x) { // TODO Auto-generated constructor stub this.val=x; this.left=null; this.right=null; } } static class Pair<U, V> implements Comparable<Pair<U, V>> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } public int compareTo(Pair<U, V> o) { int value = ((Comparable<U>) x).compareTo(o.x); if (value != 0) return value; return ((Comparable<V>) y).compareTo(o.y); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return x.equals(pair.x) && y.equals(pair.y); } public int hashCode() { return Objects.hash(x, y); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextArray(int n) { int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=nextInt(); return arr; } } static String string; 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); } static int gcd(int a, int b,boolean[] prime) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; if(prime[a]&&prime[b]) return 1; // a is greater if (a > b) return gcd(a%b, b); return gcd(a, b%a); } static long gcd(long a, long b) { // Everything divides 0 for(long i=2;i<=b;i++) { if(a%i==0&&b%i==0) return i; } return 1; } static int fac(int n) { int c=1; for(int i=2;i<n;i++) if(n%i==0) c=i; return c; } static int lcm(int a,int b) { for(int i=Math.min(a, b);i<=a*b;i++) if(i%a==0&&i%b==0) return i; return 0; } static int cal(long n,long k) { System.out.println(n+","+k); if(n==k) return 2; if(n<k) return 1; if(k==1) return 1+cal(n, k+1); if(k>=32) return 1+cal(n/k, k); return 1+Math.min(cal(n/k, k),cal(n, k+1)); } static void shuffle(int a[], int n) { for (int i = 0; i < n; i++) { // getting the random index int t = (int)Math.random() * a.length; // and swapping values a random index // with the current index int x = a[t]; a[t] = a[i]; a[i] = x; } } static void sort(int[] arr ) { shuffle(arr, arr.length); Arrays.sort(arr); } static int largestPower(int n, int p) { // Initialize result int x = 0; // Calculate x = n/p + n/(p^2) + n/(p^3) + .... while (n > 0) { n /= p; x += n; } return x; } // Utility function to do modular exponentiation. // It returns (x^y) % p static int power(int x, int y, int p) { int 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 % 2 == 1) { res = (res * x) % p; } // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n! % p static int modFact(int n, int p) { if (n >= p) { return 0; } int res = 1; // Use Sieve of Eratosthenes to find all primes // smaller than n boolean isPrime[] = new boolean[n + 1]; Arrays.fill(isPrime, true); for (int i = 2; i * i <= n; i++) { if (isPrime[i]) { for (int j = 2 * i; j <= n; j += i) { isPrime[j] = false; } } } // Consider all primes found by Sieve for (int i = 2; i <= n; i++) { if (isPrime[i]) { // Find the largest power of prime 'i' that divides n int k = largestPower(n, i); // Multiply result with (i^k) % p res = (res * power(i, k, p)) % p; } } return res; } static boolean[] seiveOfErathnos(int n2) { boolean isPrime[] = new boolean[n2 + 1]; Arrays.fill(isPrime, true); for (int i = 2; i * i <= n2; i++) { if (isPrime[i]) { for (int j = 2 * i; j <= n2; j += i) { isPrime[j] = false; } } } return isPrime; } static boolean[] seiveOfErathnos2(int n2,int[] ans) { boolean isPrime[] = new boolean[n2 + 1]; Arrays.fill(isPrime, true); for (int i = 2; i * i <= n2; i++) { if (isPrime[i]) { for (int j = 2 * i; j <= n2; j += i) { if(isPrime[j]) ans[i]++; isPrime[j] = false; } } } return isPrime; } static void qprint(int a) { System.out.println(a); } static void qprint(int a,int b) { System.out.println(a+","+b); } static void level(int i,int curr,List<Integer>[] list,int[] level) { // System.out.println(i); level[i]=curr; for(int j:list[i]) level(j, curr+1, list, level); } static int fac(int n,int m) { int fac=1; for(int j=2;j<=n;j++) fac*=j; return fac; } static int helper(int st,int end,int k,int[] arr,int[] limit,int[][][] dp) { if(st==end) return 0; if(dp[st][end][k]>0) return dp[st][end][k]; int ans=0; for(int i=st;i<end-1;i++) { ans+=limit[arr[i]]*(arr[i+1]-arr[i]); } qprint(st,end,ans); if(k==0) return dp[st][end][k]=ans; for(int i=st+1;i<end-1;i++) { int curr=(arr[i+1]-arr[i-1])*limit[arr[i-1]]+helper(st, i, k-1, arr, limit, dp)+helper(i, end, k-1, arr, limit, dp); ans=Math.min(ans,curr); } return dp[st][end][k]=ans; } private static void qprint(int st, int end, int ans) { // TODO Auto-generated method stub System.out.println(st+","+end+","+ans); } public static void main(String[] args)throws IOException { BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in)); FastReader fs=new FastReader(); // int[] ans=new int[1000001]; int T=fs.nextInt(); // seiveOfErathnos2(1000000, ans); StringBuilder sb=new StringBuilder(); // int c=1; while(T-->0) { int n=fs.nextInt(); int[] arr=fs.nextArray(n); List<Integer> list=new ArrayList<>(); for(int i=1;i<n-1;i++) { if(arr[i]>arr[i-1]&&arr[i]>arr[i+1]) list.add(i); } // System.out.println(list); List<Integer> temp=new ArrayList<>(); int ans=0; for(int i=0;i<list.size();i++) { if(i==list.size()-1) temp.add(list.get(i)); else { if(list.get(i)+2==list.get(i+1)) { arr[list.get(i)+1]=Math.max(arr[list.get(i)],arr[list.get(i)+2]); ans++; i++; } else { temp.add(list.get(i)); } } } for(int i:temp) { ans++; arr[i]=Math.max(arr[i-1],arr[i+1]); } sb.append(ans).append("\n"); for(int i:arr) sb.append(i).append(" "); sb.append("\n"); } System.out.println(sb); // FileWriter fr=new FileWriter("output.txt"); // BufferedWriter br=new BufferedWriter(fr); // br.append(sb.toString()); // br.close(); // fr.close(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
c42c931b2b5ea35e1cda9f37c05b8b09
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
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(); int a[]= new int [n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); int strt=0,end=0; int ct=0; for(int i=1;i<n-1;i++) { if( a[i]>a[i+1] && a[i]>a[i-1]) { strt=i; end=i+2; //System.out.println(strt+" "+end); if( end<n-1 && ( a[end]>a[end-1] && a[end]>a[end+1]) ) { ct++; //w.println(strt+" "+end); a[strt+1]=Math.max(a[strt],a[end] ); //w.println(a[strt+1]); } else { a[strt]=Math.max(a[strt-1], a[strt+1]); ct++; } i=end; } } w.println(ct); for(int i:a) { w.print(i+" "); } 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
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
394b6266989e64322038006fcad67808
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class _02_Avoid_Local_Maximums { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int test=Integer.parseInt(sc.next());sc.nextLine(); for(int i=0;i<test;i++) { int n=Integer.parseInt(sc.next());sc.nextLine(); int ar[]=new int[n]; String inputSplit[]=sc.nextLine().split(" "); for(int j=0;j<n;j++) { ar[j]=Integer.parseInt(inputSplit[j]); } int res[]=calc(ar,n); for(int j=0;j<n;j++) { System.out.print(ar[j]+" "); } System.out.println(); } } public static int[] calc(int ar[],int n) { //ArrayList<Integer> list=new ArrayList<>(); int b[] = Arrays.copyOf(ar, n); int count=0; for(int i=1;i<n-1;i++) { if(ar[i-1]<ar[i] && ar[i]>ar[i+1]) { if(b[i-1]!=ar[i-1]) { ar[i-1]=ar[i]; } /*if(list.contains(i-1)) { ar[i-1]=ar[i]; }*/ else { ar[i+1]=ar[i]; //list.add(i+1); count++; } } } System.out.println(count); return ar; } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
469027100f1fef8ab6ee810598878f03
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
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 ans = 0; for(int i = 1;i+1<n;i++) { if(a[i] > a[i-1] && a[i] > a[i+1]) { a[i+1] = (i < n-2) ? max(a[i],a[i+2]) : a[i]; ans++; } } fout.println(ans); for(long it : a) fout.print(it + " "); fout.println(); } fout.close(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
c8a510c96496f36a2a36f8efb38116f3
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
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> { int first, second,third; public Tuple(int first, int second, int 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 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 count = 0; for(int i = 1;i+1<n;i++) { if(a[i] > a[i-1] && a[i] > a[i+1]) { count++; a[i+1] = max(a[i+1],a[i]); if(i + 2 < n) a[i+1] = max(a[i+1],a[i+2]); } } fout.println(count); for(long it : a) fout.print(it + " "); fout.println(); } fout.close(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
845ab518ea7669484f6df9da154fcb14
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.Scanner; public class AvoidLocalMaximums { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(),c=0; int[] arr=new int[n+1]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } for(int i=1;i+1<n;i++) { if(arr[i]>arr[i-1] && arr[i]>arr[i+1]) { if(i+2<n) { arr[i+1]=Math.max(arr[i],arr[i+2]); }else { arr[i+1]=arr[i]; } c++; } } System.out.println(c); for(int k=0;k<n;k++) { System.out.print(arr[k]+" "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
ab9a4eec74d7eb2b30b5dde4b84c2058
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.util.*; public class Program { static int currMin = Integer.MAX_VALUE; static String result = ""; static Map<String, Integer> mem = new HashMap(); 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){ try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } // code FastReader sc = new FastReader(); int t = sc.nextInt(); for(int tt=0; tt<t; tt++) { int n = sc.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) { arr[i] = sc.nextInt(); } find(arr, n); } return; } public static int find(int[] arr, int n) { int count = 0; for(int i=1;i<n-1;i++) { if(arr[i]>arr[i-1] && arr[i]>arr[i+1]) { if(i+2<n){ arr[i+1] = Math.max(arr[i],arr[i+2]); } else { arr[i+1] = arr[i]; } count++; } } System.out.println(count); print(arr); return 0; } public static void print(int[] arr) { for(int i=0;i<arr.length;i++) { System.out.print(arr[i]+" "); } System.out.println(""); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
b29e185fd57f3a49ea66c1d48ad490b8
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
//package CodeForces; import java.util.*; public class Main { public static void main(String[] arg) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t > 0) { int n = sc.nextInt(); List<Integer> l = new ArrayList<>(); int[] arr = new int[n]; for(int i = 0;i < n;i++) { arr[i] = sc.nextInt(); l.add(arr[i]); } Collections.sort(l); int op = 0; for(int i = 1;i < arr.length - 1;i++) { if(arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) { if(i + 2 < arr.length) arr[i + 1] = Math.max(arr[i] , arr[i + 2]); else arr[i + 1] = arr[i]; op++; } } System.out.println(op); for(int a : arr) System.out.print(a+" "); System.out.println(); t--; } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
ef56574847c1723b62fccdc9c942719f
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
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(),cnt=0; int[] a=new int[m+1],b=new int[m+1]; for(int j=0;j<m;j++){ a[j]=sc.nextInt(); } for(int j=1;j<m-1;j++){ if(a[j]>a[j-1]&&a[j]>a[j+1])b[j]=1; } for(int j=1;j<m-1;j++){ if(b[j]==1){ a[j+1]=Math.max(a[j],a[j+2]); j=j+2; cnt++; } } System.out.println(cnt); for(int x=0;x<m;x++){ System.out.print(a[x]); if(x!=m-1) System.out.print(" "); } if(n!=0)System.out.println("\n"); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
4fa9542e3058e1f916b464f350803659
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class B{ public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int[] arr = readArr(N, infile, st); int count = 0; for(int i = 1; i < N - 2; i++){ if(arr[i] > arr[i+1] && arr[i] > arr[i-1] && arr[i+1] < arr[i+2]){ count++; arr[i+1] = max(arr[i],arr[i+2]); } } for(int i = 1; i < N - 1; i++) { if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) { arr[i] = max(arr[i-1],arr[i+1]); count++; } } out.println(count); print(arr); } } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for (int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } public static void print(int[] arr) { //for debugging only for (int x : arr) out.print(x + " "); out.println(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
34a438cb78f57dfa5d7d99af66e78d1a
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class exe_B1635 { public static void main(String[] args) { //Input Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); int[] n = new int[t]; List<int[]> list = new LinkedList<int[]>(); for (int i = 0; i < t; i++) { n[i] = scanner.nextInt(); int[] a = new int[n[i]]; for (int j = 0; j < n[i]; j++) a[j] = scanner.nextInt(); list.add(a); } scanner.close(); //Output for (int i = 0; i < t; i++) { int ans = 0; int[] a = list.get(i); for (int j = 1; j < n[i]-1; j++) { if(a[j] > a[j-1] && a[j] > a[j+1]) { if (j+1 == n[i]-1) { a[j+1] = a[j]; }else { a[j+1] = Math.max(a[j], a[j+2]); } ans++; } } System.out.println(ans); for (int j : a) System.out.print(j+ " "); System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
3b928312d6af2184a907d0d695c30366
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.lang.*; import java.math.BigInteger; import java.io.*; @SuppressWarnings("unchecked") public class Main { static FastReader in; static PrintWriter out; static int bit(long n) { return (n == 0) ? 0 : (1 + bit(n & (n - 1))); } static void p(Object o) { out.print(o); } static void pn(Object o) { out.println(o); } static void pni(Object o) { out.println(o); out.flush(); } static String n() throws Exception { return in.next(); } static String nln() throws Exception { return in.nextLine(); } static int ni() throws Exception { return Integer.parseInt(in.next()); } static long nl() throws Exception { return Long.parseLong(in.next()); } static double nd() throws Exception { return Double.parseDouble(in.next()); } static class FastReader { static BufferedReader br; static StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception { br = new BufferedReader(new FileReader(s)); } String next() throws Exception { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception { String str = ""; try { str = br.readLine(); } catch (IOException e) { throw new Exception(e.toString()); } return str; } } static long power(long a, long b) { if (a == 0L) return 0L; if (b == 0) return 1; long val = power(a, b / 2); val = val * val; if ((b % 2) != 0) val = val * a; return val; } static long power(long a, long b, long mod) { if (a == 0L) return 0L; if (b == 0) return 1; long val = power(a, b / 2L, mod) % mod; val = (val * val) % mod; if ((b % 2) != 0) val = (val * a) % mod; return val; } static ArrayList<Long> prime_factors(long n) { ArrayList<Long> ans = new ArrayList<Long>(); while (n % 2 == 0) { ans.add(2L); n /= 2L; } for (long i = 3; i <= Math.sqrt(n); i++) { while (n % i == 0) { ans.add(i); n /= i; } } if (n > 2) { ans.add(n); } return ans; } static void sort(ArrayList<Long> a) { Collections.sort(a); } static void reverse_sort(ArrayList<Long> a) { Collections.sort(a, Collections.reverseOrder()); } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(List<Long> a, int i, int j) { long temp = a.get(i); a.set(j, a.get(i)); a.set(j, temp); } static void sieve(boolean[] prime) { int n = prime.length - 1; Arrays.fill(prime, true); for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = i * i; j <= n; j += i) { prime[j] = false; } } } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static void sort(long[] arr, int l, int r) { if (l >= r) return; int mid = (l + r) / 2; sort(arr, l, mid); sort(arr, mid + 1, r); merge(arr, l, mid, r); } public static void sort(int[] arr, int l, int r) { if (l >= r) return; int mid = (l + r) / 2; sort(arr, l, mid); sort(arr, mid + 1, r); merge(arr, l, mid, r); } static void merge(int[] arr, int l, int mid, int r) { int[] left = new int[mid - l + 1]; int[] right = new int[r - mid]; for (int i = l; i <= mid; i++) { left[i - l] = arr[i]; } for (int i = mid + 1; i <= r; i++) { right[i - (mid + 1)] = arr[i]; } int left_start = 0; int right_start = 0; int left_length = mid - l + 1; int right_length = r - mid; int temp = l; while (left_start < left_length && right_start < right_length) { if (left[left_start] < right[right_start]) { arr[temp] = left[left_start++]; } else { arr[temp] = right[right_start++]; } temp++; } while (left_start < left_length) { arr[temp++] = left[left_start++]; } while (right_start < right_length) { arr[temp++] = right[right_start++]; } } static void merge(long[] arr, int l, int mid, int r) { long[] left = new long[mid - l + 1]; long[] right = new long[r - mid]; for (int i = l; i <= mid; i++) { left[i - l] = arr[i]; } for (int i = mid + 1; i <= r; i++) { right[i - (mid + 1)] = arr[i]; } int left_start = 0; int right_start = 0; int left_length = mid - l + 1; int right_length = r - mid; int temp = l; while (left_start < left_length && right_start < right_length) { if (left[left_start] < right[right_start]) { arr[temp] = left[left_start++]; } else { arr[temp] = right[right_start++]; } temp++; } while (left_start < left_length) { arr[temp++] = left[left_start++]; } while (right_start < right_length) { arr[temp++] = right[right_start++]; } } static class pair implements Comparable<pair> { int a; int b; public pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(pair p) { if (this.b == p.b) return this.a - p.a; return this.b - p.b; } } static HashMap<Long, Integer> map_prime_factors(long n) { HashMap<Long, Integer> map = new HashMap<>(); while (n % 2 == 0) { map.put(2L, map.getOrDefault(2L, 0) + 1); n /= 2L; } for (long i = 3; i <= Math.sqrt(n); i++) { while (n % i == 0) { map.put(i, map.getOrDefault(i, 0) + 1); n /= i; } } if (n > 2) { map.put(n, map.getOrDefault(n, 0) + 1); } return map; } static long divisor(long n) { long count = 0; for (long i = 1L; i * i <= n; i++) { if (n % i == 0) { if (i == n / i) count += i; else { count += i; count += n / i; } } } return count; } // static void smallest_prime_factor(int n) { // smallest_prime_factor[1] = 1; // for (int i = 2; i <= n; i++) { // if (smallest_prime_factor[i] == 0) { // smallest_prime_factor[i] = i; // for (int j = i * i; j <= n; j += i) { // if (smallest_prime_factor[j] == 0) { // smallest_prime_factor[j] = i; // } // } // } // } // } // static int[] smallest_prime_factor; // static int count = 1; // static int[] p = new int[100002]; // static long[] flat_tree = new long[300002]; // static int[] in_time = new int[1000002]; // static int[] out_time = new int[1000002]; // static long[] subtree_gcd = new long[100002]; // static int w = 0; // static boolean poss = true; static long mod = 1000000007L; /* * (a^b^c)%mod * Using fermats Little theorem * x^(mod-1)=1(mod) * so b^c can be written as b^c=x*(mod-1)+y * then (a^(x*(mod-1)+y))%mod=(a^(x*(mod-1))*a^(y))mod * the term (a^(x*(mod-1)))%mod=a^(mod-1)*a^(mod-1) * */ static class node { int v, w; public node(int v, int w) { this.v = v; this.w = w; } } static class comparator implements Comparator<node> { public int compare(node a, node b) { return a.w - b.w; } } static int ans = 0; public static void main(String[] args) throws Exception { in = new FastReader(); out = new PrintWriter(System.out); int tc =ni(); while (tc-- > 0) { int n=ni(); int[] a=new int[n]; for(int i=0;i<n;i++)a[i]=ni(); int ans=0; int count=0; for(int i=1;i<n-1;i++){ if(a[i]>a[i-1] && a[i]>a[i+1]){ if(i+2<n)a[i+1]=Math.max(a[i+2],a[i]); else a[i+1]=a[i]; count++; } } pn(count); for(int i=0;i<n;i++)p(a[i]+" "); pn(""); } out.flush(); out.close(); } static void factorial(long[] fact, long[] fact_inv, int n, long mod) { fact[0] = 1; for (int i = 1; i < n; i++) { fact[i] = (i * fact[i - 1]) % mod; } for (int i = 0; i < n; i++) { fact_inv[i] = power(fact[i], mod - 2, mod);// (1/x)%m can be calculated by fermat's little theoram which is // (x**(m-2))%m when m is prime } } static void find(int i, int n, int[] row, int[] col, int[] d1, int[] d2) { if (i >= n) { ans++; return; } for (int j = 0; j < n; j++) { if (col[j] == 0 && d1[i - j + n - 1] == 0 && d2[i + j] == 0) { col[j] = 1; d1[i - j + n - 1] = 1; d2[i + j] = 1; find(i + 1, n, row, col, d1, d2); col[j] = 0; d1[i - j + n - 1] = 0; d2[i + j] = 0; } } } static int answer(int l, int r, int[][] dp) { if (l > r) return 0; if (l == r) { dp[l][r] = 1; return 1; } if (dp[l][r] != -1) return dp[l][r]; int val = Integer.MIN_VALUE; int mid = l + (r - l) / 2; val = 1 + Math.max(answer(l, mid - 1, dp), answer(mid + 1, r, dp)); return dp[l][r] = val; } static TreeSet<Integer> ans(int n) { TreeSet<Integer> set = new TreeSet<>(); for (int i = 1; i * i <= n; i++) { if (n % i == 0) { set.add(i); set.add(n / i); } } set.remove(1); return set; } // static long find(String s, int i, int n, long[] dp) { // // pn(i); // if (i >= n) // return 1L; // if (s.charAt(i) == '0') // return 0; // if (i == n - 1) // return 1L; // if (dp[i] != -1) // return dp[i]; // if (s.substring(i, i + 2).equals("10") || s.substring(i, i + 2).equals("20")) // { // return dp[i] = (find(s, i + 2, n, dp)) % mod; // } // if ((s.charAt(i) == '1' || (s.charAt(i) == '2' && s.charAt(i + 1) - '0' <= // 6)) // && ((i + 2 < n ? (s.charAt(i + 2) != '0' ? true : false) : (i + 2 == n ? true // : false)))) { // return dp[i] = (find(s, i + 1, n, dp) + find(s, i + 2, n, dp)) % mod; // } else // return dp[i] = (find(s, i + 1, n, dp)) % mod; static void print(int[] a) { for (int i = 0; i < a.length; i++) p(a[i] + " "); pn(""); } static long count(long n) { long count = 0; while (n != 0) { n /= 10; count++; } return count; } static void swap(long a, long b) { long temp = a; a = b; b = temp; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static int LcsOfPrefix(String a, String b) { int i = 0; int j = 0; int count = 0; while (i < a.length() && j < b.length()) { if (a.charAt(i) == b.charAt(j)) { j++; count++; } i++; } return a.length() + b.length() - 2 * count; } static void reverse(int[] a, int n) { for (int i = 0; i < n / 2; i++) { int temp = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = temp; } } static char get_char(int a) { return (char) (a + 'a'); } static void dfs(int i, List<List<Integer>> arr, boolean[] visited, int parent) { visited[i] = true; for (int v : arr.get(i)) { if (!visited[v]) { dfs(v, arr, visited, i); } } } static int find1(int[] a, int val) { int ans = -1; int l = 0; int r = a.length - 1; while (l <= r) { int mid = l + (r - l) / 2; if (a[mid] <= val) { l = mid + 1; ans = mid; } else r = mid - 1; } return ans; } static int find2(int[] a, int val) { int l = 0; int r = a.length - 1; int ans = -1; while (l <= r) { int mid = l + (r - l) / 2; if (a[mid] <= val) { ans = mid; l = mid + 1; } else r = mid - 1; } return ans; } // static void dfs(List<List<Integer>> arr, int node, int parent, long[] val) { // p[node] = parent; // in_time[node] = count; // flat_tree[count] = val[node]; // subtree_gcd[node] = val[node]; // count++; // for (int adj : arr.get(node)) { // if (adj == parent) // continue; // dfs(arr, adj, node, val); // subtree_gcd[node] = gcd(subtree_gcd[adj], subtree_gcd[node]); // } // out_time[node] = count; // flat_tree[count] = val[node]; // count++; // } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
ef99e14e509db8d2347ae718678f81ee
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
//package Codeforces; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class B { 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()); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(stz.nextToken()); } if(n == 2) { op.append("0\n"); for(int e : arr) { op.append(e); op.append(' '); } op.append("\n"); } else if(n == 3) { if(arr[1] > arr[0] && arr[1] > arr[2]) { op.append("1\n"); arr[1] = 1; } else { op.append("0\n"); } for(int e : arr) { op.append(e); op.append(' '); } op.append("\n"); } else { int count = 0; ArrayList<Integer> list = new ArrayList<>(); for (int i = 1 ; i < (n-1); i++) { if(arr[i] > arr[i-1] && arr[i] > arr[i+1]) { list.add(i); } } count = list.size(); if(count == 0) { op.append("0\n"); for(int e : arr) { op.append(e); op.append(' '); } op.append("\n"); continue; } int ans = 0; boolean lastRemaining = true; // System.out.println(list); for (int i = 0 ; i < (count - 1) ; ) { int curr = list.get(i); int next = list.get(i+1); if((i < count - 1) && curr + 2 == next) { arr[curr + 1] = Math.max(arr[curr], arr[next]); if(i == count - 2) { lastRemaining = false; } i += 2; ++ans; } else { arr[curr] = Math.max(arr[curr-1], arr[curr + 1]); ++i; ++ans; } } if(lastRemaining) { int last = list.get(list.size()-1); arr[last] = Math.max(arr[last-1], arr[last + 1]); ++ans; } op.append(ans + "\n"); for(int e : arr) { op.append(e); op.append(' '); } op.append("\n"); } } System.out.println(op); // END OF CODE } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
804efb3e38519db1557b1ad55937f847
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { static boolean isPowerOfTwo(int n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } public static Set<Integer> primeupton(int n) { Set<Integer> ans= new HashSet<>(); 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; } } // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) ans.add(i); } return ans; } public static Map<Integer,Integer> primeFactors(int n) { HashMap<Integer,Integer> map= new HashMap<>(); int count=0; while (n%2==0) { count++; n /= 2; } map.put(2,count); for (int i = 3; i <= Math.sqrt(n); i+= 2) { count=0; while (n%i == 0) { count++; n /= i; } map.put(i,count); } if (n > 2) map.put(n,1); return map; } public static String toBinary(long decimal){ StringBuilder ans= new StringBuilder(); while(decimal > 0){ ans.append(decimal%2) ; decimal = decimal/2; } return ans.reverse().toString(); } public static void initalize(int n,ArrayList<ArrayList<Integer>> adj){ for(int i=0;i<=n;i++){ adj.add(new ArrayList<Integer>()); } } public static void makegraph(int s,int d,ArrayList<ArrayList<Integer>> adj){ adj.get(s).add(d); adj.get(d).add(s); } static class Pair { int a; int b; Pair(int a, int b) { this.a = a; this.b = b; } } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } 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; } } static int sum (int n){ return n*(n+1)/2; } public static int log2(int N) { // calculate log2 N indirectly // using log() method int result = (int)(Math.log(N) / Math.log(2)); return result; } private static ArrayList<ArrayList<Integer>> adj; public static void main(String[] args) throws Exception { // your code goes here FastReader sc = new FastReader(); int t= sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int a[]= new int[n]; int ans=0; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } for(int i=1;i<n-2;i++){ int x=a[i-1]; int z=a[i+1]; int y=a[i]; int f=a[i+2]; if(y> z && y>x){ ans++; a[i+1]=Math.max(y,f); } } if(n>=3){ if(a[n-2]>a[n-1 ] && a[n-2]> a[n-3]){ ans++; a[n-2]=Math.max(a[n-1],a[n-3]); }} System.out.println(ans); for(int i=0;i<n;i++){ System.out.print(a[i]+" "); } System.out.println(); } } static boolean dfs(int cur,int par,boolean[] vis,ArrayList<ArrayList<Integer>> adj){ vis[cur]=true; ArrayList<Integer> list= adj.get(cur); for(int child:list){ if(!vis[child])dfs(child,cur,vis,adj); else { if(child!=par)return true; } } return false; } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
137251e75eb3527484fde03b07cb2a72
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.*; public class AvoidLocalMaximums { static PrintWriter pw; static Scanner sc; public static void main(String[] args) throws IOException{ sc = new Scanner(System.in); pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int[] a = sc.nextIntArray(n); int cnt = 0; Queue<Integer> q = new LinkedList<>(); for(int i=1; i<n-1 ;i++) if(a[i]>a[i-1] && a[i]>a[i+1]) q.add(i); while(!q.isEmpty()){ int x = q.poll(); if(!q.isEmpty() && (q.peek() == x+2)) { a[x + 1] = Math.max(a[q.peek()], a[x]); q.poll(); } else{ a[x+1] = a[x]; } cnt++; } pw.println(cnt); for(int i=0; i<n ;i++) pw.print(a[i]+" "); pw.println(); } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws IOException { br = new BufferedReader(new FileReader(file)); } 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 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 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
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
bc420508cafbc6911f46d679a06bf7e2
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class R772B { 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()); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(st.nextToken()); } solve(arr); } } private static void solve(int[] arr) { boolean[] isMax = new boolean[arr.length]; for (int i = 1; i < arr.length - 1; i++) { isMax[i] = arr[i] > arr[i-1] && arr[i] > arr[i+1]; } int count = 0; for (int i = 0; i < isMax.length; i++) { if (isMax[i]) { if (i + 2 < arr.length && isMax[i + 2]) { isMax[i + 2] = false; arr[i + 1] = Math.max(arr[i], arr[i+2]); count++; } else { arr[i + 1] = Math.max(arr[i], arr[i+1]); count++; } } } System.out.println(count); StringBuilder builder = new StringBuilder(); for (int i = 0; i < arr.length; i++) { builder.append(arr[i]); builder.append(" "); } System.out.println(builder.toString()); } } /* 1 1 0 1 0 1 1 0 0 0 1 */
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
269224aba006ea73240a6a881c5387e5
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.util.*; public class Test { public static void main(String[] args) throws IOException { Reader rd = new Reader(); int t = rd.nextInt(); while (t-- > 0) { int n = rd.nextInt(); List<Integer> indices = new ArrayList<>(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = rd.nextLong(); } for (int i = 1; i < n - 1; i++) { if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) indices.add(i); } int counter = 0, i = 0; while (i < indices.size()) { int currentIndex = indices.get(i); int nextIndex = -1; if (i < indices.size() - 1) nextIndex = indices.get(i + 1); if (nextIndex != -1 && nextIndex - currentIndex == 2) { arr[currentIndex + 1] = Math.max(arr[currentIndex], arr[nextIndex]); i += 2; } else { arr[currentIndex] = Math.max(arr[currentIndex - 1], arr[currentIndex + 1]); i += 1; } counter += 1; } System.out.println(counter); for (long L : arr) System.out.print(L + " "); System.out.println(); } } /** * method to solve current cp problem */ private static void solve() { } /** * you can ignore the below snippet code as it's just for taking faster input in java */ 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(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
e8fe8c55f8f4ce47593812e8581ab8d0
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public static PrintWriter out; private static void solve(int[] arr){ int count = 0; int i = 1; while(i < arr.length - 1){ if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){ if(i + 2 < arr.length - 1 && arr[i+2] > arr[i+1] && arr[i+2] > arr[i+3]){ arr[i+1] = Math.max(arr[i], arr[i+2]); count++; } else{ count++; arr[i-1] = arr[i]; } } i++; } out.println(count); StringBuilder res = new StringBuilder(); for(int x: arr){ res.append(x); res.append(" "); } res.deleteCharAt(res.length() - 1); out.println(res); } public static void main(String[] args){ MyScanner scanner = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int num = scanner.nextInt(); for(int i = 0; i < num; i++){ int n = scanner.nextInt(); int[] arr = new int[n]; for(int j = 0; j < n; j++){ arr[j] = scanner.nextInt(); } solve(arr); } out.close(); } 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
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
edecdf813da84d4bfcd59e20bcf3ac8d
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class B1635 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] temp = br.readLine().split(" "); int test = Integer.parseInt(temp[0]); while(test-- > 0){ temp = br.readLine().split(" "); int n = Integer.parseInt(temp[0]); int[] arr = new int[n]; temp = br.readLine().split(" "); for(int i = 0; i < n; i++){ arr[i] = Integer.parseInt(temp[i]); } int count = 0; for(int i = 1; i < n-1; i++){ if(arr[i] > arr[i-1] && arr[i] > arr[i+1] ){ if(i+1 < n-1){ arr[i+1] = Math.max(arr[i], arr[i+2]); }else{ arr[i+1] = arr[i]; } count++; } } System.out.println(count); for(int i = 0; i < n; i++){ System.out.print(arr[i] + " "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
864bc2bece922d9cc8e2e2951f6c6e57
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuffer out = new StringBuffer(); int T = in.nextInt(); OUTER: while (T-->0) { int n = in.nextInt(); int[] a = new int[n]; for(int i=0; i<n; i++) { a[i] = in.nextInt(); } int op = 0; for(int i=1; i<n-1; i++) { if(a[i-1]<a[i] && a[i]>a[i+1]) { op += 1; a[i+1] = Math.max(a[i], (i+2)==n?a[i]:a[i+2]); } } out.append(op+"\n"); for(int item: a) { out.append(item+" "); } out.append("\n"); } System.out.print(out); } private static int countBit(int n) { int cnt = 0; while(n!=0) { n/=2; cnt+=1; } return cnt; } private static long gcd(long a, long b) { if (a==0) return b; return gcd(b%a, a); } // private static <T> void printArr(T arr) { // println(Arrays.toString(arr)); // } private static int toInt(String s) { return Integer.parseInt(s); } private static long toLong(String s) { return Long.parseLong(s); } private static void print(String s) { System.out.print(s); } private static void println(String s) { System.out.println(s); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
78308ae30ec26a4a46f131c938656c27
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.*; import java.io.*; import java.util.*; import java.math.*; import static java.lang.Math.sqrt; import static java.lang.Math.floor; public class topcoder { public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; } static int x = -1; static int y = -1; public static int first_search(TreeNode root, TreeNode main_root) { if(root == null) return 0; int a = first_search(root.left,main_root); int b = first_search(root.right,main_root); if(a > main_root.val) x = a; if(b < main_root.val) y = b; return root.val; } public static void fix(TreeNode root, TreeNode main_root) { if(root == null) return; fix(root.left,main_root); if(root.val > main_root.val) { root.val = y; } fix(root.right,main_root); if(root.val < main_root.val); root.val = x; } public static int max(int []nums, int s, int e) { int max = Integer.MIN_VALUE; for(int i = s; i <= e; i++) { max = Math.max(max, nums[i]); } return max; } public static TreeNode new_node(int []nums, int s, int e) { int max = max(nums,s,e); TreeNode node = new TreeNode(max); return node; } public static TreeNode root; public static void res(int []nums, int s, int e) { if(s > e)return; if(root == null) { root = new_node(nums,s,e); } root.left = new_node(nums,s,e); root.right = new_node(nums,s,e); return; } static int depth(TreeNode root) { if(root == null)return 0; int a = 1+ depth(root.left); int b = 1+ depth(root.right); return Math.max(a,b); } static HashSet<Integer>set = new HashSet<>(); static void deepestLeaves(TreeNode root, int cur_depth, int depth) { if(root == null)return; if(cur_depth == depth)set.add(root.val); deepestLeaves(root.left,cur_depth+1,depth); deepestLeaves(root.right,cur_depth+1,depth); } public static void print(TreeNode root) { if(root == null)return; System.out.print(root.val+" "); System.out.println("er"); print(root.left); print(root.right); } public static HashSet<Integer>original(TreeNode root){ int d = depth(root); deepestLeaves(root,0,d); return set; } static HashSet<Integer>set1 = new HashSet<>(); static void leaves(TreeNode root) { if(root == null)return; if(root.left == null && root.right == null)set1.add(root.val); leaves(root.left); leaves(root.right); } public static boolean check(HashSet<Integer>s, HashSet<Integer>s1) { if(s.size() != s1.size())return false; for(int a : s) { if(!s1.contains(a))return false; } return true; } static TreeNode subTree; public static void smallest_subTree(TreeNode root) { if(root == null)return; smallest_subTree(root.left); smallest_subTree(root.right); set1 = new HashSet<>(); leaves(root); boolean smallest = check(set,set1); if(smallest) { subTree = root; return; } } public static TreeNode answer(TreeNode root) { smallest_subTree(root); return subTree; } } static class pair{ long first; long second; public pair(long first, long second) { this.first = first; this.second = second; } public long compareTo(pair p) { if(first == p.first)return second-p.second; return first-p.first; } } static class Compare{ static void compare(ArrayList<pair>arr) { Collections.sort(arr,new Comparator<pair>() { public int compare(pair p1, pair p2) { return (int) (p1.first-p2.first); } }); } } public static HashMap<Integer,Integer>sortByValue(HashMap<Integer,Integer>hm){ List<Map.Entry<Integer,Integer>>list = new LinkedList<Map.Entry<Integer,Integer>>(hm.entrySet()); Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>(){ public int compare(Map.Entry<Integer,Integer>o1, Map.Entry<Integer,Integer>o2) { return (o1.getValue()).compareTo(o2.getValue()); }}); HashMap<Integer,Integer>temp = new LinkedHashMap<Integer,Integer>(); for(Map.Entry<Integer,Integer>aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static class pairr implements Comparable<pairr>{ static Long value; Long index; public pairr(Long value, Long index) { this.value = value; this.index = index; } public int compareTo(pairr o) { return (int)(value-o.value); } } static class Key<K1, K2> { public K1 key1; public K2 key2; public Key(K1 key1, K2 key2) { this.key1 = key1; this.key2 = key2; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Key key = (Key) o; if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) { return false; } if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) { return false; } return true; } @Override public int hashCode() { int result = key1 != null ? key1.hashCode() : 0; result = 31 * result + (key2 != null ? key2.hashCode() : 0); return result; } @Override public String toString() { return "[" + key1 + ", " + key2 + "]"; } } public static int sumOfDigits (long n) { int sum = 0; while(n > 0) { sum += n%10; n /= 10; } return sum; } public static long binary_search(int s, int e, long num, long []ar) { if(s > e) { return -1; } int mid = (s+e)/2; if(s == e && ar[s] >= num) { return ar[s]; }else if(s == e && ar[s] < num) { return -1; }else if(ar[mid] < num) { return binary_search(mid+1,e,num,ar); }else if(ar[mid] >= num) { return binary_search(s,mid,num,ar); } return -1; } public static int index_search(int s, int e, long num, long []ar) { if(s > e) { return -1; } int mid = (s+e)/2; if(s == e && ar[s] >= num) { return s; }else if(s == e && ar[s] < num) { return -1; }else if(ar[mid] < num) { return index_search(mid+1,e,num,ar); }else if(ar[mid] >= num) { return index_search(s,mid,num,ar); } return -1; } public static void swap(int []ar, int i, int j) { for(int k= j; k >= i; k--) { int temp = ar[k]; ar[k] = ar[k+1]; ar[k+1] = temp; } } public static boolean digit_exists(long n) { while(n > 0) { if(n%10 == 9) return true; n = n/10; } return false; } public static int log(int n) { int c = 0; while(n > 0) { c++; n /=2; } return c; } public static int findOr(int[]bits){ int or=0; for(int i=0;i<32;i++){ or=or<<1; if(bits[i]>0) or=or+1; } return or; } static void simpleSieve(int limit, Vector<Integer> prime) { // Create a boolean array "mark[0..n-1]" and initialize // all entries of it as true. A value in mark[p] will // finally be false if 'p' is Not a prime, else true. boolean mark[] = new boolean[limit+1]; for (int i = 0; i < mark.length; i++) mark[i] = true; for (int p=2; p*p<limit; p++) { // If p is not changed, then it is a prime if (mark[p] == true) { // Update all multiples of p for (int i=p*p; i<limit; i+=p) mark[i] = false; } } // Print all prime numbers and store them in prime for (int p=2; p<limit; p++) { if (mark[p] == true) { prime.add(p); } } } // Prints all prime numbers smaller than 'n' public static void segmentedSieve(int n, ArrayList<Integer>l) { // Compute all primes smaller than or equal // to square root of n using simple sieve int limit = (int) (floor(sqrt(n))+1); Vector<Integer> prime = new Vector<>(); simpleSieve(limit, prime); // Divide the range [0..n-1] in different segments // We have chosen segment size as sqrt(n). int low = limit; int high = 2*limit; // While all segments of range [0..n-1] are not processed, // process one segment at a time while (low < n) { if (high >= n) high = n; // To mark primes in current range. A value in mark[i] // will finally be false if 'i-low' is Not a prime, // else true. boolean mark[] = new boolean[limit+1]; for (int i = 0; i < mark.length; i++) mark[i] = true; // Use the found primes by simpleSieve() to find // primes in current range for (int i = 0; i < prime.size(); i++) { // Find the minimum number in [low..high] that is // a multiple of prime.get(i) (divisible by prime.get(i)) // For example, if low is 31 and prime.get(i) is 3, // we start with 33. int loLim = (int) (floor(low/prime.get(i)) * prime.get(i)); if (loLim < low) loLim += prime.get(i); /* Mark multiples of prime.get(i) in [low..high]: We are marking j - low for j, i.e. each number in range [low, high] is mapped to [0, high-low] so if range is [50, 100] marking 50 corresponds to marking 0, marking 51 corresponds to 1 and so on. In this way we need to allocate space only for range */ for (int j=loLim; j<high; j+=prime.get(i)) mark[j-low] = false; } // Numbers which are not marked as false are prime for (int i = low; i<high; i++) if (mark[i - low] == true) l.add(i); // Update low and high for next segment low = low + limit; high = high + limit; } } public static int find_indexNum(long k) { long k1 = k; int power = 0; while(k > 0) { power++; k /=2 ; } long check = (long)Math.pow(2, power-1); if(k1 == check) { return power; } // System.out.println(power); long f = (long)Math.pow(2, power-1); long rem = k1-f; return find_indexNum(rem); } public static void shuffle(int []array, int num,int t_index, boolean []vis, int m ) { for(int i = 0; i < m; i++) { if(vis[i] == false) { int temp = array[i]; if(i < t_index) { vis[i] = true; } array[i] = num; array[t_index] = temp; // System.out.println(array[t_index]+" "+array[i]); break; } } } public static void rotate(int []arr,int j, int times, int m) { if(j == 0) { int temp1 = arr[0]; arr[0] = arr[times]; arr[times] = temp1; }else { int temp = arr[j]; int z = arr[0]; arr[0] = arr[times]; arr[j] = z; arr[times] = temp; } } public static void recur(int i,int A, int B,int []dp,int []metal, int n, boolean took,int ind) { if(i-A <= 0 && i-B <= 0)return; int count = 0; for(int j = 1; j <= n; j++) { if(dp[j] >= metal[j]) { count++; } } if(count == n)return; if(i-A >= 0 && i-B >= 0 && dp[i] > 0 && dp[i] > metal[i]) { dp[i]--; dp[i-A]++; dp[i-B]++; } if(ind == 6) { // System.out.println(Arrays.toString(dp)); } recur(i-A,A,B,dp,metal,n,took,ind); recur(i-B,A,B,dp,metal,n,took,ind); } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static boolean[] getSieve(int n) { boolean[] isPrime = new boolean[n+1]; for (int i = 2; i <= n; i++) isPrime[i] = true; for (int i = 2; i*i <= n; i++) if (isPrime[i]) for (int j = i; i*j <= n; j++) isPrime[i*j] = false; return isPrime; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } public static void dfs(LinkedList<Integer>[]list, HashMap<Integer,Integer>map, int parent, int n) { Stack<Integer>st = new Stack<>(); } public static boolean pos(int n) { int i = 1; boolean pos = false; while(i*i <= n) { if(i*i*2 == n || i*i*4 == n) { pos = true; break; } i++; } if(pos)return true; return false; } static long count = 0; public static void pairs (int []ar, int s, int e) { if(e <= s)return; // System.out.println(ar[s]+" "+ar[e]+" "+s+" "+e); if(ar[e]-ar[s] == e-s) { count++; //System.out.println("sdf"); } pairs(ar,s+1,e); pairs(ar,s,e-1); } public static class Pair1 implements Comparable <Pair1>{ int value; int index; public Pair1(int value, int index) { this.value = value; this.index = index; } public int compareTo(Pair1 o) { return o.value-value; } } public static long ways(long n) { return (n*(n-1))/2; } 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+= 6) { if(n%i == 0 || n%(i+2) == 0) return false; } return true; } static long nextPrime(long n) { boolean found = false; long prime = n; while(!found) { prime++; if(isPrime(prime)) found = true; } return prime; } public static boolean isValid(int h, int m, int hour, int minute) { int a = flip(hour / 10); if (a == -1) return false; int b = flip(hour % 10); if (b == -1) return false; int c = flip(minute / 10); if (c == -1) return false; int d = flip(minute % 10); if (d == -1) return false; if (10 * d + c >= h) return false; if (10 * b + a >= m) return false; return true; } public static int flip(int x) { if (x == 0) return 0; if (x == 1) return 1; if (x == 2) return 5; if (x == 5) return 2; if (x == 8) return 8; return -1; } static long maximum(long a, long b, long c, long d) { long m = Math.max(a, b); long m1 = Math.max(c, d); return Math.max(m1, m1); } static long minimum(long a, long b, long c,long d) { long m = Math.min(a, b); long m1 = Math.min(c, d); return Math.min(m, m1); } static long ans = 0; public static void solve1(boolean [][]vis,long [][]mat, int r, int c, int r2, int c2, int r1, int c1, int r3, int c3) { if(r > r1 || c > c1 || r > r2 || c > c2 || r1 > r3 || c1 > c3 || r3 < r2 || c3 < c2 || vis[r][c] || vis[r1][c1]|| vis[r2][c2] || vis[r3][c3]) return; vis[r][c] = true; vis[r1][c1] = true; vis[r2][c2] = true; vis[r3][c3] = true; long max = maximum(mat[r][c],mat[r1][c1],mat[r2][c2],mat[r3][c3]); long min = minimum(mat[r][c],mat[r1][c1],mat[r2][c2],mat[r3][c3]); long a = mat[r][c]; long b = mat[r1][c1]; long c4 = mat[r2][c2]; long d =mat[r3][c3]; long []p = {a,b,c4,d}; Arrays.sort(p); long temp = (p[2]+p[3]-p[0]-p[1]); if(r == r1 && r == r2 && r2 == r3 && r1 == r3) temp /= 2; System.out.println(Arrays.toString(p)); ans += temp; solve1(vis,mat,r+1,c,r2+1,c2,r1-1,c1,r3-1,c3); solve1(vis,mat,r,c+1,r2,c2-1,r1,c1+1,r3,c3-1); solve1 (vis,mat,r+1,c+1,r2+1,c2-1,r1-1,c1+1,r3-1,c3-1); } private static int solve(int[][] mat, int i, int c, int j, int c2, int k, int c1, int l, int c3) { // TODO Auto-generated method stub return 0; } public static int dfs(int parent, LinkedList<Integer>[]list) { for(int i : list[parent]) { if(list[parent].size() == 0) { return 0; }else { return 1 + dfs(i,list); } } return 0; } public static long answer = Integer.MAX_VALUE; public static void min_Time(int [][]dp, int i, HashSet<Integer>set, int min, int r, int c) { if(i > r) { answer = Math.min(answer, min); return; } if(min > answer)return; for(int j = i; j <= c; j++) { if(!set.contains(j)) { set.add(j); min += dp[i][j]; min_Time(dp,i+1,set,min,r,c); min -= dp[i][j]; set.remove(j); } } } public static void dp(int [][]dp, int r, int c, int o, int z, long sum) { if(r > o) { answer = Math.min(answer, sum); } if(r > o || c > z) { return; } if(sum > answer)return; sum += dp[r][c]; dp(dp,r+1,c+1,o,z,sum); sum -= dp[r][c]; dp(dp,r,c+1,o,z,sum); } static HashSet<ArrayList<Integer>>l = new HashSet<>(); public static void fourSum(Deque<Integer>ll, int i, int target, int []ar, int n) { if(ll.size() == 4) { int sum = 0; ArrayList<Integer>list = new ArrayList<>(); for(int a : ll) { sum += a; list.add(a); } if(sum == target) { Collections.sort(list); l.add(list); // System.out.println(ll); } return; } for(int j = i; j < n; j++) { ll.add(ar[j]); fourSum(ll,j+1,target,ar,n); ll.removeLast(); } } static int max_bottles(int cur, int exchange, int n){ if(cur == exchange){ cur = 0; n++; } if(n == 0)return 0; return 1+ max_bottles(cur+1,exchange,n-1); } public static void fill(int [][]mat, List<Integer>ans, int row_start, int row_end, int col_start, int col_end) { for(int i = col_start; i <= col_end; i++) { ans.add(mat[row_start][i]); } for(int i = row_start+1; i <= row_end; i++) { ans.add(mat[i][col_end]); } if(col_start == col_end)return; if(row_start == row_end)return; for(int i = col_end-1; i >= col_start; i--) { ans.add(mat[row_end][i]); } for(int i = row_end-1; i >= row_start+1; i--) { ans.add(mat[i][col_start]); } } public static void create(int [][]mat, int j, int i, int k) { if(i < 1 || j >= mat.length)return; mat[j][i] = k; create(mat,j+1,i-1,k+1); } public static long sum(int [][]mat, int x1, int y1, int x2, int y2) { long sum = 0; while(x1 <= x2) { sum += mat[x1][y1]; // System.out.println(mat[x1][y1]); x1++; } y1++; while(y1 <= y2) { sum += mat[x2][y1]; y1++; } return sum; } public static boolean allneg(int []ar, int n) { for(int i = 0; i < n; i++) { if(ar[i] >= 0)return false; } return true; } public static boolean allpos(int []ar, int n) { for(int i = 0; i < n; i++) { if(ar[i] <= 0)return false; } return true; } public static int max_pos(int []ar, int n) { int min = Integer.MAX_VALUE; for(int i = 1; i < n; i++) { if(ar[i] > 0) { break; } int a = Math.abs(ar[i]-ar[i-1]); min = Math.min(min, a); } int c = 0; boolean zero = false; TreeSet<Integer>set = new TreeSet<>(); int neg = 0; for(int i = 0; i < n; i++) { if(ar[i] <= 0) {neg++; if(ar[i] == 0) zero = true; continue;} if(ar[i] <= min) { c = 1; } } neg += c; return neg; } static final int MAX = 10000000; // prefix[i] is going to store count // of primes till i (including i). static int prefix[] = new int[MAX + 1]; static void buildPrefix() { // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. boolean prime[] = new boolean[MAX + 1]; Arrays.fill(prime, true); for (int p = 2; p * p <= MAX; 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 * 2; i <= MAX; i += p) prime[i] = false; } } // Build prefix array prefix[0] = prefix[1] = 0; for (int p = 2; p <= MAX; p++) { prefix[p] = prefix[p - 1]; if (prime[p]) prefix[p]++; } } static int query(int L, int R) { return prefix[R] - prefix[L - 1]; } static void alter(int n) { int ans = 0; boolean []vis = new boolean[n+1]; for(int i = 2; i <= n; i++){ boolean p = false; if(vis[i] == false) { for(int j = i; j <= n; j+=i) { if(vis[j] == true) { p = true; }else { vis[j] = true; } } if(!p)ans++; } } System.out.println(ans); } public static void solveDK(int []dp, int i, int D, int K) { int d = D/K; int ans = -1; int ind = d+1; while(ind < i) { int temp = i/ind; temp--; if(dp[temp*ind] == temp) { ans = dp[temp*ind]+1; dp[i] = ans; break; } ind = ind*2; } if(ans == -1) dp[i] = 1; } public static void solveKD(int []dp, int i, int D, int K) { int d = K/D; int ans = -1; int ind = d+1; while(ind < i) { int temp = i/ind; temp--; if(dp[temp*ind] == temp) { ans = dp[temp*ind]+1; dp[i] = ans; break; } ind = ind*2; } if(ans == -1) dp[i] = 1; } static int countGreater(int arr[], int n, int k) { int l = 0; int r = n - 1; // Stores the index of the left most element // from the array which is greater than k int leftGreater = n; // Finds number of elements greater than k while (l <= r) { int m = l + (r - l) / 2; // If mid element is greater than // k update leftGreater and r if (arr[m] > k) { leftGreater = m; r = m - 1; } // If mid element is less than // or equal to k update l else l = m + 1; } // Return the count of elements greater than k return (n - leftGreater); } static ArrayList<Integer>printDivisors(int n) { // Note that this loop runs till square root ArrayList<Integer>list = 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) list.add(i); else // Otherwise print both list.add(i); list.add(n/i); } } return list; } static boolean isPossible(String s, String str, int i, int j) { // System.out.println(i+" "+j); int x = i; int y = j; while(i >= 0 && j < str.length()) { if(s.charAt(i) != str.charAt(j)) { break; } i--; j++; } if(j == str.length()) { System.out.println(x+" "+y); return true; } return false; } static void leftRotate(int l, int r,int arr[], int d) { for (int i = 0; i < d; i++) leftRotatebyOne(l,r,arr); } static void leftRotatebyOne(int l, int r,int arr[]) { int i, temp; temp = arr[l]; for (i = l; i < r; i++) arr[i] = arr[i + 1]; arr[r] = temp; } static long modInverse(long a, long m) { long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient long q = a / m; long t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long solvetheproblem(long n, long k) { long mod = 1000000007; long ansss = 0; while(n > 0) { // Nearest power of 2<=N long p = (long)(Math.log(n) / Math.log(2));; // Now insert k^p in the answer long temp = (long)power(k,p,mod); ansss += temp; ansss %= mod; // update n n %= (long)Math.pow(2, p); } // Print the ans in sorted order return ansss%mod ; } static boolean pos (int [][]mat, int r, int c, int n, boolean [][]vis) { if(r <= 0 || c <= 0 || r > 2 || c > n )return false; if(r == 2 && c == n)return true; if(vis[r][c])return false; vis[r][c] = true; if(mat[r][c] == 1)return false; boolean a = pos(mat,r+1,c,n,vis); boolean b = pos(mat,r,c+1,n,vis); boolean d = pos(mat,r+1,c+1,n,vis); boolean e = pos(mat,r-1,c+1,n,vis); return a || b || d || e; } static long sameremdiv(long x, long y) { if(x <= y) { y = x-1; } long sq = (long)Math.sqrt(x); if(y <= sq) { y--; long ans = (y*(y+1))/2; return ans; }else { long ans = 0; long dif = y-sq; sq -= 2; if(sq > 1) ans += (sq*(sq+1))/2; long d = x/y; sq+=2; for(int i = (int)sq; i <= y; i++) { if(i > 1 ) { long temp = x/i; if(x%i < temp)temp--; ans += temp; } } return ans; } } static int binary(long []ar, long element, int s, int e) { int mid = (s+e)/2; // System.out.println(mid); if(s > e)return mid; if(ar[mid] == element) { return mid; }else if(ar[mid] > element) { return binary(ar,element,s,mid-1); }else { return binary(ar,element,mid+1,e); } } static boolean isGibbrish(HashSet<String>set, String str, int j) { StringBuilder sb = new StringBuilder(); if(j >= str.length()) { return true; } for(int i = j; i < str.length(); i++) { sb.append(str.charAt(i)); String temp = sb.toString(); if(set.contains(temp)) { boolean test = isGibbrish(set,str,i+1); if(test)return true; } } return false; } static TreeNode buildTree(TreeNode root,int []ar, int l, int r){ if(l > r)return null; int len = l+r; if(len%2 != 0)len++; int mid = (len)/2; int v = ar[mid]; TreeNode temp = new TreeNode(v); root = temp; root.left = buildTree(root.left,ar,l,mid-1); root.right = buildTree(root.right,ar,mid+1,r); return root; } public static int getClosest(int val1, int val2, int target) { if (target - val1 >= val2 - target) return val2; else return val1; } public static int findClosest(int start, int end, int arr[], int target) { int n = arr.length; // Corner cases if (target <= arr[start]) return arr[start]; if (target >= arr[end]) return arr[end]; // Doing binary search int i = start, j = end+1, mid = 0; while (i < j) { mid = (i + j) / 2; if (arr[mid] == target) return arr[mid]; /* If target is less than array element, then search in left */ if (target < arr[mid]) { // If target is greater than previous // to mid, return closest of two if (mid > 0 && target > arr[mid - 1]) return getClosest(arr[mid - 1], arr[mid], target); /* Repeat for left half */ j = mid; } // If target is greater than mid else { if (mid < n-1 && target < arr[mid + 1]) return getClosest(arr[mid], arr[mid + 1], target); i = mid + 1; // update i } } // Only single element left after search return arr[mid]; } static int lis(int arr[], int n) { int lis[] = new int[n]; int i, j, max = 0; /* Initialize LIS values for all indexes */ for (i = 0; i < n; i++) lis[i] = 1; /* Compute optimized LIS values in bottom up manner */ for (i = 1; i < n; i++) for (j = 0; j < i; j++) if (arr[i] >= arr[j] && lis[i] < lis[j] + 1) lis[i] = lis[j] + 1; /* Pick maximum of all LIS values */ for (i = 0; i < n; i++) if (max < lis[i]) max = lis[i]; return max; } static List<List<Integer>>res = new ArrayList<>(); public static void lists(List<Integer>list, List<Integer>temp, int target, int j) { int sum = 0; for(int i = 0; i < temp.size(); i++) { sum += temp.get(i); } if(sum > target) { return; }else if(sum == target) { Collections.sort(temp); boolean exist = false; for(List<Integer>l : res) { if(l.size() == temp.size()) { int c = 0; for(int i = 0; i < l.size(); i++) { if(l.get(i) == temp.get(i))c++; } if(c == l.size()) {exist = true; break;} } } if(!exist) { res.add(new ArrayList<>(temp)); } }else if(j == list.size())return; for(int i = j; i < list.size(); i++){ temp.add(list.get(i)); lists(list,temp,target,i+1); temp.remove(temp.size()-1); } return; } static ArrayList<Integer>dfs(int p, int c, ArrayList<Integer>l,HashMap<Integer,Integer>map, LinkedList<Integer>[]list, int v) { l.add(map.get(c)); System.out.println(c); if(c == v) { //System.out.println(c); return l; } for(int j : list[c]) { if(c == 1) { // System.out.println("Yes"+" "+j); // System.out.println("yes"+" "+list[c]); } if(j != p) { dfs(c,j,l,map,list,v); } } return new ArrayList<>(); } static boolean pos(char []a, int j) { char []ar = new char[a.length]; for(int i = 0; i < a.length; i++) { ar[i] = a[i]; } ar[j] = 'c'; ar[j-1] = 'a'; ar[j-2] = 'b'; ar[j-3] = 'a'; ar[j+1] = 'a'; ar[j+2] = 'b'; ar[j+3] = 'a'; int count = 0; for(int i = 3; i < ar.length-3; i++) { if(ar[i] == 'c' && ar[i-1] == 'a' && ar[i-2] == 'b' && ar[i-3] == 'a' && ar[i+1] == 'a' && ar[i+2] == 'b' && ar[i+3] == 'a') { count++; } } if(count == 1)return true; return false; } static void bruteforce(String s) { String ans = s.charAt(0)+""; ans += ans; StringBuilder sb = new StringBuilder(); sb.append(s.charAt(0)); for(int i = 1;i < s.length(); i++){ String d = sb.toString(); sb.reverse(); d += sb.toString(); boolean con = true; System.out.println(d+" "+s); for(int j = 0; j < Math.min(d.length(), s.length()); j++) { if(s.charAt(j) > d.charAt(j)) { con = false; break; } } sb.reverse(); sb.append(s.charAt(i)); if(con) { ans = d; break; } } System.out.println(ans+" "+"yes"); } static void permute(String s , String answer) { if (s.length() == 0) { System.out.print(answer + " "); return; } for(int i = 0 ;i < s.length(); i++) { char ch = s.charAt(i); String left_substr = s.substring(0, i); String right_substr = s.substring(i + 1); String rest = left_substr + right_substr; permute(rest, answer + ch); } } static List<List<Integer>>result = new ArrayList<>(); public static void comb(List<Integer>list, int n,int []ar, HashSet<Integer>set) { if(list.size() == n) { boolean exist = false; for(List<Integer>l : result) { int c = 0; for(int i = 0; i < l.size(); i++) { if(l.get(i) == list.get(i))c++; } if(c == n) { exist = true; break;} } if(!exist)result.add(new ArrayList<>(list)); } for(int j = 0; j < n; j++) { if(!set.contains(j)) { list.add(ar[j]); set.add(j); comb(list,n,ar,set); set.remove(j); list.remove(list.size()-1); } } return; } static void pinkSeat(int [][]mat, int i, int j, int n, int m, boolean vis[][], int [][]res) { if(i <= 0 || j <= 0 || i > n || j > m || vis[i][j])return; int a = Math.abs(i-1); int b = Math.abs(j-1); int c = Math.abs(i-1); int d = Math.abs(j-m); int e = Math.abs(i-n); int f = Math.abs(j-m); int x = Math.abs(i-n); int y = Math.abs(j-1); vis[i][j] = true; int max = Math.max(a+b,c+d); max = Math.max(max, e+f); max = Math.max(max, x+y); res[i][j] = max; pinkSeat(mat,i-1,j-1,n,m,vis,res); pinkSeat(mat,i+1,j-1,n,m,vis,res); pinkSeat(mat,i-1,j+1,n,m,vis,res); pinkSeat(mat,i+1,j+1,n,m,vis,res); pinkSeat(mat,i,j-1,n,m,vis,res); pinkSeat(mat,i,j+1,n,m,vis,res); pinkSeat(mat,i-1,j,n,m,vis,res); pinkSeat(mat,i+1,j,n,m,vis,res); } static boolean isPowerOfTwo(long n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static long ksearch(long s,long e, long []ar, long ans, long h, int n, long k) { if(s > e)return k; long mid = (s+e)/2; long count = 0; for(int i = 0; i < n; i++) { count += (ar[i])/mid; if(ar[i]%mid > 0)count++; } if(count <= h) { if(mid <= k) { ans = count; k = mid; } return ksearch(s, mid-1,ar,ans,h,n,k); }else { return ksearch(mid+1,e,ar,ans,h,n,k); } } static long mod = 1000000000; static long ans2 = 0; static void shirtCombo(long x, HashSet<Integer>set,int j, ArrayList<List<Integer>>list) { if(set.size() == list.size()) { ans2 = (ans2+x)%mod; } if(j == list.size()) { return; } int c = 0; List<Integer>temp = list.get(j); for(int a : temp ) { if(!set.contains(a)) { c++; set.add(a); shirtCombo(1,set,j+1,list); set.remove(a); } } x = (x*c)%mod; } public static class Pair { int x; int y; // Constructor public Pair(int x, int y) { this.x = x; this.y = y; } } static class Compare1{ static void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return p1.x - p2.x; } }); } } static long gcd1(long a, long b) { if (a == 0) return b; return gcd1(b % a, a); } // method to return LCM of two numbers static long lcm(long a, long b) { return (a / gcd1(a, b)) * b; } static ArrayList<List<Integer>>res1 = new ArrayList<>(); static void permuteNumbers(LinkedHashSet<Integer>set, int n) { if(res1.size() == n)return; if(set.size() == n) { List<Integer>l = new ArrayList<>(); for(int a : set) { l.add(a); } boolean pos = true; for(int i = 2; i < l.size(); i++) { if(l.get(i-1) + l.get(i-2) == l.get(i)) { pos = false; break; } } if(pos) { res1.add(new ArrayList(l)); } return; } for(int i = 1; i <= n; i++) { if(!set.contains(i)) { set.add(i); permuteNumbers(set,n); set.remove(i); } } } public static void main(String args[])throws IOException { BufferedReader ob = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(ob.readLine()); while(t --> 0) { /* StringTokenizer st = new StringTokenizer(ob.readLine()); int n = Integer.parseInt(st.nextToken()); int n = Integer.parseInt(ob.readLine()); int []ar = new int[n]; */ int n = Integer.parseInt(ob.readLine()); StringTokenizer st = new StringTokenizer(ob.readLine()); long []ar = new long[n]; for(int i = 0; i < n; i++) { ar[i] = Long.parseLong(st.nextToken()); } int ans = 0; int i = 1; for( i = 1; i < n-3; i++) { if(ar[i] > ar[i-1] && ar[i] > ar[i+1]) { if(ar[i+2] > ar[i+1] && ar[i+2] > ar[i+3]) { ar[i+1] = Math.max(ar[i], ar[i+2]); ans++; }else { ar[i] = Math.max(ar[i-1],ar[i+1]); ans++; } } } if(n == 2) { System.out.println(0); System.out.println(ar[0]+" "+ar[1]); }else { for(int j = i; j < n-1; j++) { if(ar[j] > ar[j-1] && ar[j] > ar[j+1]) { ar[j] = Math.max(ar[j-1], ar[j+1]); ans++; } } System.out.println(ans); for(long a : ar) { System.out.print(a+" "); } System.out.println(); } } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
69f2be044807422f7843fad13bb6882c
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.lang.*; import java.io.*; import java.util.*; public class AvoidLocalMaximums { 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; } } static class DisjointSet { int parent[] = null; int ranks[] = null; DisjointSet(int len) { parent = new int[len]; ranks = new int[len]; for (int indx = 0; indx < parent.length; indx++) { parent[indx] = indx; ranks[indx] = 0; } } // Time complexity: O(Alpha(n)), Alpha(n) <= 4, therefore O(1) and auxiliary // space: O(1) for recursion stack public void union(int x, int y) { int xRep = find(x); int yRep = find(y); if (xRep == yRep) return; if (ranks[xRep] < ranks[yRep]) parent[xRep] = yRep; else if (ranks[yRep] < ranks[xRep]) parent[yRep] = xRep; else { parent[yRep] = xRep; ranks[xRep]++; } } // Time complexity: O(LogN) and auxiliary space: O(1) for recursion stack public int find(int x) { if (parent[x] == x) { return x; } return parent[x] = find(parent[x]); } } private static int findGCDArr(int arr[], int n) { int result = arr[0]; for (int element: arr){ result = gcd(result, element); if(result == 1) { return 1; } } return result; } private static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static List<Integer> findFactors(int n) { List<Integer> res = new ArrayList<>(); // Note that this loop runs till square root for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { // If divisors are equal, add only one if (n/i == i) res.add(i); else // Otherwise add both res.add(i); res.add(n/i); } } return res; } private static long binPow(int a, int b, int m){ if(b==0){ return a%m; } if(b % 2 == 0){ long pw = binPow(a, b/2, m); return (1l * pw * pw % m); }else{ long pw = binPow(a, (b-1)/2, m); return 1l * pw * pw * a % m; } } private static long mulInvMod(int x, int m){ return binPow(x, m-2, m); } // end of fast i/o code public static void main(String[] args) { FastReader reader = new FastReader(); int t = reader.nextInt(); while(t-->0){ int n = reader.nextInt(), arr[] = new int[n]; // HashSet<Integer> maxi = new HashSet<>(); for(int ind = 0; ind<n; ind++){ arr[ind] = reader.nextInt(); } int chng = 0; for(int ind = 0; ind<n; ind++){ if(ind != 0 && ind != n-1){ if(arr[ind-1] < arr[ind] && arr[ind+1] < arr[ind]){ if(ind+2 < n){ arr[ind+1] = Math.max(arr[ind], arr[ind+2]); }else{ arr[ind+1] = arr[ind]; } chng++; } } } // size = maxi.size(); // for(int indx = 0; indx<n; indx++){ // if(maxi.contains(indx)){ // if(maxi.contains(indx+2)){ // maxi.remove(indx+2); // } // maxi.remove(indx); // arr[indx+1] = arr[indx]; // chng++; // } // } System.out.println(chng); for(int indx = 0; indx<n; indx++){ int ele = arr[indx]; System.out.print(ele); if(indx != n-1){ System.out.print(" "); } } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
758a6f1e013e5a962134444904ce72f9
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class AvoidLocalMaximus { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while (t-->0){ int size=Integer.parseInt(br.readLine()); String[] inp=br.readLine().split(" "); int[] arr=new int[size]; for(int i=0;i<size;i++) arr[i]=Integer.parseInt(inp[i]); int count=0; for(int i=1;i<size-1;i++){ if(arr[i-1]<arr[i] && arr[i+1]<arr[i]){ if(i==size-2){ arr[i+1]=arr[i]; count++; break; } else{ arr[i+1]=Math.max(arr[i],arr[i+2]); count++; } } } System.out.println(count); for(int i:arr) System.out.print(i+" "); System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
24cf8675b8ee9179582173386bb095f8
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
/* * Created by cravuri on 2/21/22 */ import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int t = 1; t <= T; t++) { int N = sc.nextInt(); int[] a = new int[N]; for (int i = 0; i < N; i++) { a[i] = sc.nextInt(); } List<Integer> maxes = new ArrayList<>(); for (int i = 1; i < N - 1; i++) { if (a[i] > a[i-1] && a[i] > a[i+1]) { maxes.add(i); } } int count = 0; for (int i = 0; i < maxes.size() - 1; i++) { if (maxes.get(i) == maxes.get(i + 1) - 2) { if (a[maxes.get(i)] > a[maxes.get(i+1)]) { a[maxes.get(i) + 1] = a[maxes.get(i)]; } else { a[maxes.get(i) + 1] = a[maxes.get(i+1)]; } count++; i++; } } for (int i = 1; i < N - 1; i++) { if (a[i] > a[i-1] && a[i] > a[i+1]) { if (a[i-1] > a[i+1]) { a[i] = a[i-1]; } else { a[i] = a[i+1]; } count++; } } System.out.println(count); StringBuilder ans = new StringBuilder(); for (int j : a) { ans.append(j).append(" "); } System.out.println(ans.substring(0, ans.length() - 1)); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
8e641a5933c8130572f14c3d58f12c18
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.util.*; public class B { public static void main(String[] args) { new B().run(); } BufferedReader br; PrintWriter out; long mod = (long) (1e9 + 7), inf = (long) (3e18); class pair { int F, S; pair(int f, int s) { F = f; S = s; } } void solve() { int t = ni(); while(t-- > 0) { //TODO: int n= ni(); int[] a= na(n); int c=0; for(int i=1; i<n-1; i++){ if(a[i]>a[i-1] && a[i]>a[i+1]){ c++; if(i!=n-2){ if(a[i+2]>a[i]){ a[i+1]=a[i+2]; } else{ a[i+1]=a[i]; } } else{ a[i+1]=a[i]; } } } out.println(c); for(int e: a){ out.print(e+" "); } out.println(); } } // -------- I/O Template ------------- char nc() { return ns().charAt(0); } String nLine() { try { return br.readLine(); } catch(IOException e) { return "-1"; } } double nd() { return Double.parseDouble(ns()); } long nl() { return Long.parseLong(ns()); } int ni() { return Integer.parseInt(ns()); } int[] na(int n) { int a[] = new int[n]; for(int i = 0; i < n; i++) a[i] = ni(); return a; } StringTokenizer ip; String ns() { if(ip == null || !ip.hasMoreTokens()) { try { ip = new StringTokenizer(br.readLine()); if(ip == null || !ip.hasMoreTokens()) ip = new StringTokenizer(br.readLine()); } catch(IOException e) { throw new InputMismatchException(); } } return ip.nextToken(); } void run() { try { if (System.getProperty("ONLINE_JUDGE") == null) { br = new BufferedReader(new FileReader("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/input.txt")); out = new PrintWriter("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/output.txt"); } else { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } catch (FileNotFoundException e) { System.out.println(e); } solve(); out.flush(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
5de394e517571b19277345be5a1f73a1
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.util.*; public class B { public static void main(String[] args) { new B().run(); } BufferedReader br; PrintWriter out; long mod = (long) (1e9 + 7), inf = (long) (3e18); class pair { int F, S; pair(int f, int s) { F = f; S = s; } } void solve() { int t = ni(); while(t-- > 0) { //TODO: int n= ni(); int[] a= na(n); int[] b= Arrays.copyOf(a,n); int c=0; for(int i=1; i<n-1; i++){ if(a[i]>a[i-1] && a[i]>a[i+1]){ c++; if(i!=n-2){ if(a[i+2]>a[i]){ a[i+1]=a[i+2]; } else{ a[i+1]=a[i]; } } else{ a[i+1]=a[i]; } } } out.println(c); for(int e: a){ out.print(e+" "); } out.println(); } } // -------- I/O Template ------------- char nc() { return ns().charAt(0); } String nLine() { try { return br.readLine(); } catch(IOException e) { return "-1"; } } double nd() { return Double.parseDouble(ns()); } long nl() { return Long.parseLong(ns()); } int ni() { return Integer.parseInt(ns()); } int[] na(int n) { int a[] = new int[n]; for(int i = 0; i < n; i++) a[i] = ni(); return a; } StringTokenizer ip; String ns() { if(ip == null || !ip.hasMoreTokens()) { try { ip = new StringTokenizer(br.readLine()); if(ip == null || !ip.hasMoreTokens()) ip = new StringTokenizer(br.readLine()); } catch(IOException e) { throw new InputMismatchException(); } } return ip.nextToken(); } void run() { try { if (System.getProperty("ONLINE_JUDGE") == null) { br = new BufferedReader(new FileReader("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/input.txt")); out = new PrintWriter("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/output.txt"); } else { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } catch (FileNotFoundException e) { System.out.println(e); } solve(); out.flush(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
c3c21e306e8ff24dbb8dbfe5e20d413b
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
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]; boolean[] vis = new boolean[n]; for(int i = 0; i < n; i++) A[i] = nextInt(); for(int i = 1; i < n - 1; i++) { if(A[i] > A[i-1] && A[i] > A[i+1]) { vis[i] = true; } } int total = 0; for(int i = 1; i < n - 1; i++) { if(i + 2 < n && vis[i] && vis[i+2]) { total++; A[i + 1] = Math.max(A[i], A[i + 2]); vis[i] = false; vis[i + 2] = false; continue; } if(vis[i]){ total++; A[i-1] = A[i]; vis[i] = false; } } out.println(total); for(int i = 0; i < n; i++) out.print(A[i] + " "); out.println(); } 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
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
9eda54106daa9b53c0e3d0bf924cefbe
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
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]; boolean[] vis = new boolean[n]; for(int i = 0; i < n; i++) A[i] = nextInt(); for(int i = 1; i < n - 1; i++) { if(A[i] > A[i-1] && A[i] > A[i+1]) { vis[i] = true; } } int total = 0; for(int i = 1; i < n - 1; i++) { if(i + 2 < n && vis[i] && vis[i+2]) { total++; A[i + 1] = Math.max(A[i], A[i + 2]); vis[i] = false; vis[i + 2] = false; continue; } if(vis[i]){ total++; A[i-1] = A[i]; vis[i] = false; } } out.println(total); for(int i = 0; i < n; i++) out.print(A[i] + " "); out.println(); } 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
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
d4313b072468327c725ac196f26d098c
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static MyScanner in = new MyScanner();; public static void main(String[] args) { int t = in.nextInt(); while (t > 0) { t--; solve(); } } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static int max(int a, int b) { return a > b ? a : b; } public static int min(int a, int b) { return a < b ? a : b; } public static void solve() { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } int ans = 0; for (int i = 2; i < n - 2; ++i) { if (a[i - 1] > a[i - 2] && a[i - 1] > a[i] && a[i + 1] > a[i] && a[i + 1] > a[i + 2]) { ans++; a[i] = max(a[i - 1], a[i + 1]); } } for (int i = 1; i < n - 1; ++i) { if (a[i - 1] < a[i] && a[i] > a[i + 1]) { ans++; a[i] = max(a[i - 1], a[i + 1]); } } System.out.println(ans); for (int i = 0; i < n; i++) { System.out.print(a[i] + (i == n - 1 ? "\n" : " ")); } } 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
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
65044b8f012bc6f9c046fc2ba98a13cc
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; import java.util.*; public class cfa { 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));} //SUFFICIENT DRY RUN????LOGIC VERIFIED FOR ALL TEST CASES??? void solve(){ int t=scn.nextInt(); while(t-->0) { int n=scn.nextInt(); int[] ar=scn.nextIntArray(n); if(n<=2){ w.println(0); for(int ele:ar){ w.print(ele+" "); } w.println(); continue; } int op =0; for(int i=1;i<n-1;i++){ if(ar[i]>ar[i-1]&&ar[i]>ar[i+1]){ if(i+2<n){ ar[i+1] = Math.max(ar[i],ar[i+2]); }else{ ar[i+1] = ar[i]; } op++; } } w.println(op); for(int ele:ar){ w.print(ele+" "); } w.println(); } } 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 cfa().runIO(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
f48d4676e24c1e409df72cc1d4c61ea1
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.*; public class sample { 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 { Reader in = new Reader(); int t = in.nextInt(); while(t-- > 0) { int n = in.nextInt(); int a[] = new int[n]; for(int i = 0; i < n; i++) a[i] = in.nextInt(); int c = 0; for(int i = 1; i < n - 1; i++) { if(a[i] > a[i-1] && a[i] > a[i+1]) { c++; a[i+1] = a[i]; if(i+2 < n && a[i+2] > a[i+1]) a[i+1] = a[i+2]; } } System.out.println(c); for(int i : a) System.out.print(i+" "); System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
64beef9a07534d52b8b585181f55ac89
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner input = new Scanner(System.in); StringBuilder output = new StringBuilder(); int t = input.nextInt(); while(t > 0) { int n = input.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; ++i) { a[i] = input.nextInt(); } int m = 0; for(int i = 1; i < n-1; ++i) { if(a[i] > a[i-1] && a[i] > a[i+1]) { if(i+2 < n) { a[i+1] = Math.max(a[i], a[i+2]); } else { a[i+1] = a[i]; } ++m; } } output.append(m); output.append("\n"); for(int i = 0; i < n; ++i) { output.append(a[i]); output.append(" "); } output.append("\n"); --t; } System.out.print(output); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
e65b45802ee57e8b110886f0bcb8af63
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; public class solve { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); for (int i = 0; i < t; i++) { int n = Integer.parseInt(br.readLine()); String[] input = br.readLine().split(" "); long a[] = new long[n]; for (int j = 0; j < n; j++) { a[j] = Long.parseLong(input[j]); } ArrayList<Integer> lm = new ArrayList<>(); int count = 0; int previousMaxima = -1; for (int j = 1; j < n - 1; j++) { if (a[j] > a[j - 1] && a[j] > a[j + 1]) { lm.add(j); } } for (int j = 0; j < lm.size(); j++) { int lmIndex = lm.get(j); if (previousMaxima != -1 && previousMaxima + 2 == lmIndex) { count++; a[previousMaxima + 1] = Math.max(a[lmIndex], a[previousMaxima]); previousMaxima = -1; } else if (previousMaxima != -1) { count++; a[previousMaxima + 1] = a[previousMaxima]; previousMaxima = lmIndex; } else { previousMaxima = lmIndex; } } if (previousMaxima != -1) { count++; a[previousMaxima + 1] = a[previousMaxima]; } pw.println(count); for (int j = 0; j < n; j++) { if (j > 0) pw.print(" "); pw.print(a[j]); } pw.println(); } pw.flush(); pw.close(); br.close(); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
7ab4c52ad0f3644e9d5c087a296455f5
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.*; import javax.management.Query; import java.io.*; import java.math.BigInteger; public class Contest1 { public static void main(String[] args) throws Exception { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int[] a=sc.nextIntArray(n); boolean[] b=new boolean[n]; int c=0; // b[0]=a[0]>a[1]; for (int i = 1; i < a.length-1; i++) { b[i]=a[i]>a[i-1]&&a[i]>a[i+1]; } for (int i = 1; i < b.length-2; i++) { if(b[i]&&b[i+2]&&!b[i+1]) { b[i]=false; b[i+2]=false; c++; a[i+1]=Math.max(a[i], a[i+2]); } } for (int i = 0; i < b.length; i++) { if(b[i]) { c++; a[i-1]=a[i]; } } // boolean f=false; // // f=a[0]<a[1]; // // for (int i = 0; i < a.length-1; i++) { // if(f&&a[i]>a[i+1]) { // a[i+1]=a[i]; // c++; // f=false; // if(i<n-2) // a[i+1]=Math.max(a[i], a[i+2]); // } // else if(a[i]<a[i+1]) // f=true; // } pw.println(c); for (int i = 0; i < a.length; i++) { pw.print(a[i]+" "); } pw.println(); } pw.close(); } //static int n; //static int w; //static int[] weight; //static int[] val; //static long[][] memo; // public static long dp(int idx,int rem) { // if(idx==n) // return 0; // if(memo[idx][rem]!=-1) // return memo[idx][rem]; // long res=dp(idx+1,rem); // if(weight[idx]<=rem) // res=Math.max(res, val[idx]+dp(idx+1,rem-weight[idx])); // return memo[idx][rem]= res; // } public static double ceil(Long n, Long m) { if(n%m==0) return n/m; else return n/m +1; } public static int gcd(int a, int b) { if(b==0) return a; else return gcd(b,a%b); } 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; } } } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
9d0f78dbb83fe7024216043a17af7cac
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
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()); } int ans = 0; if(n <= 2) { out.append(0).append('\n'); for (int x: arr){ out.append(x).append(' '); } out.append('\n'); continue; } for (int i = 1; i < n-1; i++) { if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){ arr[i+1] = Math.max(arr[i],(i+2 < n)? arr[i+2] : Integer.MIN_VALUE); ans++; } } out.append(ans).append('\n'); for (int x: arr) { out.append(x).append(' '); } out.append('\n'); } System.out.print(out); } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output
PASSED
7b6fcb44d725c255ec028547ddb8bfa9
train_108.jsonl
1645367700
You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i &gt; a_{i - 1}$$$ and $$$a_i &gt; a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.
256 megabytes
import java.util.Scanner; public class Main { static int N=1000000; static int t; public static void main(String[] args) { Scanner sc=new Scanner(System.in); t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int num[]=new int[n]; int st[]=new int[n]; int idx=1,res=0; for(int i=0;i<n;i++)num[i]=sc.nextInt(); for(int i=1;i<n-1;i++,idx++) { if(num[i]>num[i-1]&&num[i]>num[i+1])st[idx]=1; } for(int i=0;i<n;i++) { if(st[i]==1) { if(i+2<n&&st[i+2]==1) { num[i+1]=Math.max(num[i], num[i+2]); res++; i+=3; }else { if(num[i]>num[i-1])num[i-1]=num[i]; else if(num[i]<num[i-1])num[i]=num[i-1]; res++; } } } System.out.println(res); for(int i=0;i<n;i++) { System.out.print(num[i]+" "); } System.out.println(); } } }
Java
["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"]
2 seconds
["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"]
NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.
Java 8
standard input
[ "greedy" ]
255abf8750541f54d8ff0f1f80e1f8e7
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$$$ $$$(2 \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$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
800
For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.
standard output