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
dd9de56355c454c5d149537ee5ca3356
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*;import java.io.*;import java.math.*; public class Codechef {static long mod=(long)1e9+7; static class pair{ int first;int second;pair(int first,int second){this.first=first;this.second=second;}} static class Reader { BufferedReader in;StringTokenizer st; public Reader() {in = new BufferedReader(new InputStreamReader(System.in));st = new StringTokenizer("");} public String nextLine() throws IOException {st = new StringTokenizer("");return in.readLine();} public String next() throws IOException {while (!st.hasMoreTokens()) {st = new StringTokenizer(in.readLine());}return st.nextToken();} public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());}} static Reader in = new Reader();static PrintWriter out = new PrintWriter(System.out); static class TreeMultiset<T> {TreeMap<T,Integer> h; public TreeMultiset() {h = new TreeMap<>();}private boolean has(T key) {return h.containsKey(key);} private void add(T key) {h.put(key,h.getOrDefault(key,0)+1);}private int get(T key) {return h.get(key);} public void del(T key) {if(h.containsKey(key)) {if(h.get(key)==1) h.remove(key);else h.put(key,h.getOrDefault(key,0)-1);}} private T down(T key) { Map.Entry<T, Integer> val;val=h.floorEntry(key);if(val!=null) {return val.getKey();}return null;} public T up(T key) { Map.Entry<T, Integer> val;val=h.ceilingEntry(key);if(val!=null) {return val.getKey();}return null;} public int size(){int s=0;for(int k:h.values()) s+=k;return s;}} //-----------------------------INPUT-------------------------------------------------// public static int ni() throws IOException {return in.nextInt();} public static long nl() throws IOException {return in.nextLong();} public static String rl() throws IOException {return in.next();} public static char nc() throws IOException {return in.next().charAt(0);} //----------------------------ARRAYS INPUT--------------------------------------------// public static int[] ai(long n) throws IOException {int[] arr = new int[(int)n];for (int i = 0; i < n; i++) {arr[i] = in.nextInt();}return arr;} public static long[] al(long n) throws IOException {long[] arr = new long[(int)n];for (int i = 0; i < n; i++) {arr[i] = in.nextLong();}return arr;} public static char[] ac() throws IOException {String s = in.next();return s.toCharArray();} public static int[] ac(int n) throws IOException {String s = in.next();int[] arr =new int[n];for(int i=0;i<n;i++) arr[i]=s.charAt(i)-'0';return arr;} //----------------------------Print---------------------------------------------------// public static void pt(int[] arr) {for (int j : arr) out.print(j + " ");out.println();} public static void pt(long[] arr) {for (long l : arr) out.print(l + " ");out.println();} public static void pt(char[] arr) {for (char l : arr) out.print(l );out.println();} //--------------------------------------------Other Functions----------------------------------------------------// public static double nRoot(double x,double y){double a=1,b=x;double mid=b;while(b-a>0.000001) {mid=(a+b)/2;float p=1;for(int i=0;i<y;i++) {p*=mid;}if(p<x) a=mid;else b=mid;}return mid;} public static long sum(int[] arr) {long s=0;for(long i:arr) s+=i;return s;} public static long lower(long a) {long l=(long)Math.floor(Math.sqrt(a));return l*l;} public static long upper(long a) {long l=(long)Math.ceil(Math.sqrt(a));return l*l;} public static long gcd(long a, long b) {BigInteger x = BigInteger.valueOf(a).gcd(BigInteger.valueOf(b));return Long.parseLong(String.valueOf(x));} public static long expo(long a, long b) {long res = 1;while (b > 0) {if ((b & 1) != 0) res = res * a;a = a * a;b >>= 1;}return res;} public static long modexp(long a, long b) {long res = 1;while (b > 0) {if ((b & 1) != 0) res = (res * a % mod) % mod;a = (a % mod * a % mod) % mod;b >>= 1;}return res % mod;} public static int[] permute(int n) {int[] arr = new int[n];for (int i = 0; i < n; i++) {arr[i] = i+1;}return arr;} public static long min(long[] arr) {long min = Long.MAX_VALUE;for (long l : arr) {if (l < min) min = l;}return min;} public static long max(long[] arr) {long max = Long.MIN_VALUE;for (long l : arr) {if (l > max) max = l;}return max;} public static int max(int[] arr) {int max = Integer.MIN_VALUE;for (int l : arr) {if (l > max) max = l;}return max;} public static void sort(long[] arr) {List<Long> list = new ArrayList<>();for (long i : arr) {list.add(i);}Collections.sort(list);for (int i = 0; i < arr.length; i++) {arr[i] = list.get(i);}} public static void sort(int[] arr) {List<Integer> list = new ArrayList<>();for (int i : arr) {list.add(i);}Collections.sort(list);for (int i = 0; i < arr.length; i++) {arr[i] = list.get(i);}} public static long countfactors(long n) {long ans = 0;for (long i = 1; i * i <= n; i++) {if (n % i == 0) {ans += 2;if (n / i == i) ans--;}}return ans;} public static boolean isprime(long n) {for (long i = 2; i * i <= n; i++) {if (n % i == 0) return false;}return true;} public static int [] copy(int[] arr) {int []brr=new int[arr.length];System.arraycopy(arr, 0, brr, 0, arr.length);return brr;} public static long [] copy(long[] arr) {long []brr=new long[arr.length];System.arraycopy(arr, 0, brr, 0, arr.length);return brr;} public static long countDigit(long n) {long count = 0;while (n != 0) {n = n / 10;++count;}return count;} public static List<pair> sortpair(int[] n, int[] h) {List<pair> A=new ArrayList<pair>();for(int i=0;i<n.length;i++){A.add(new pair(h[i],n[i]));}A.sort(Comparator.comparingInt(a -> a.first));return A;} public static long kadane(long a[]) {long max_so_far=Long.MIN_VALUE, max_ending_here=0;for (long l : a) {max_ending_here = max_ending_here + l;if (max_so_far < max_ending_here) max_so_far = max_ending_here;if (max_ending_here < 0) max_ending_here = 0;}return max_so_far;} public static long gcd(long arr[]){long gc=arr[0];for(int i=1;i<arr.length;i++)gc=gcd(arr[i],gc); return gc;} public static boolean isSorted(int []arr){int n=arr.length;for(int i=0;i<n-1;i++){if(arr[i]>arr[i+1])return false;} return true;} public static boolean isSorted(long []arr){int n=arr.length;for(int i=0;i<n-1;i++){if(arr[i]>arr[i+1])return false;} return true;} static int msbNumber(int n) {return (int)(Math.log(n) / Math.log(2));} static int msbNumber(long n) {return (int)(Math.log(n) / Math.log(2));} public static void reverse(int[]a){int n = a.length;for (int i = 0; i < n / 2; i++){int temp = a[i];a[i] = a[n - i - 1];a[n - i - 1] = temp;}} public static int justLower(long[]a,long t) {int l = 0,r=a.length-1;if(t<a[0]){return -1;}if(t>=a[r]){return r;}while(l<r-1){int m=l+(r-l)/2;if(a[m]<=t){l=m;}else{r=m-1;}}return a[r]<=t?r:l;} public static int justUpper(long[]a,long t) {int l = 0,r=a.length-1;if(t<=a[0]){return 0;}if(t>a[r]){return -1;}while (l<r){int m=l+(r-l)/2;if(a[m]>=t){r=m;}else{l=m+1;}}return r;} public static void reverse(long[]a){int n = a.length;for (int i = 0; i < n / 2; i++){long temp = a[i];a[i] = a[n - i - 1];a[n - i - 1] = temp;}} //---------------------------------------------------------------------------------------------------------------------------------------// public static long ans(TreeMultiset<Long>t,long p,long t2,long t3) { long ki=0; while(true) { long k = t.down(p - 1) != null ? t.down(p - 1) : -1; if (k != -1) { t.del(k); p += (k / 2); ki++; } else break; } long a1=0,a2=0; if(t3>0) { a1=ans(t,p*3,t2,t3-1); } if(t2>0) { a2=ans(t,p*2,t3,t2-1); } return Math.max(a1,a2)+ki; } public static void Khud_Bhi_Krle_Kuch() throws IOException { int n=ni(); long []arr=al(n); int i=0; if(n%2==1) i=1; for(;i<n-1;i+=2) { if(arr[i]>arr[i+1]) { long temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } if(isSorted(arr)) out.println("YES"); else out.println("NO"); }; //--------------------------------------MAIN--------------------------------------------// public static void main(String[] args) throws Exception {int t=ni();while(t--!=0) Khud_Bhi_Krle_Kuch();out.close();}} // public static void main(String[] args) throws Exception {Khud_Bhi_Krle_Kuch();out.close();}} //ela-sorting // int n=ni();long[] arr =al(n); // HashMap<Integer,int[]>h=new HashMap<>(); // int[] bit =new int[33]; // if(arr[0]!=0) { // int c = setBitNumber(arr[0])+1; // bit[c]++; // } // else // bit[0]++; // h.put(0,bit); // for(int i=1;i<n;i++) { // int []bits; // bit=h.get(i-1); // bits=copy(bit); // long a=arr[i]; // if(arr[i]!=0) { // int c = setBitNumber(a)+1; // bits[c]++; // } // else // bits[0]++; // h.put(i,bits); // } // int q=ni(); // while(q--!=0) { // int l=ni()-1,r=ni()-1; // long x=ni(); // if(x==0) // { // out.println(r-l+1); // continue; // } // int c=setBitNumber(x); // int[] ra =h.get(r),la =h.get(l); // for(int i=0;i<32;i++) // ra[i]-=la[i]; // long a=arr[l]; // int ct=0; // ct=setBitNumber(a); // ra[ct]++; // int ans=0; // for(int i=0;i<32;i++) { // if(i!=c) { // ans+=ra[i]; // } // } // out.println(ans); // } //// out.println(setBitNumber(0)); // } //---
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 17
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
11d558bd062a9084343db3b0d4d642a1
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; public class codeforces { private static void swap(int[] x, int i, int j) { final int t = x[i]; x[i] = x[j]; x[j] = t; } public static int upperbound(int left, int right, int value, long[] a) { // upperbound while (left < right) { int mid = (right + left) / 2; if (a[mid] > value) { right = mid; } else { left = mid + 1; } } return left; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); long t = sc.nextLong(); while (t-- > 0) { int n = sc.nextInt(); int a[] = new int[n]; int b[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); b[i] = a[i]; } Arrays.sort(b); boolean flag = true; int i=0; if(n%2==1){ if(a[0]!=b[0]){ flag=false; }else{ i+=1; } } while(i<n-1){ if((a[i]==b[i] || a[i]==b[i+1]) && (a[i+1]==b[i+1] || a[i+1]==b[i])){ i+=2; }else{ flag=false; break; } } if(flag) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 17
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
977f974cca2b5faa3903ccd22a329953
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; public class Testing { public static void main(String[] args) { Scanner s = new Scanner (System.in); int t=s.nextInt(); for(int i=0;i<t;i++){ int n=s.nextInt(); boolean flag=true; ArrayList<Integer> input=new ArrayList<Integer>(n); ArrayList<Integer> temp =new ArrayList<Integer>(n);; for(int j=0;j<n;j++){ input.add(s.nextInt()); temp.add(input.get(j));} Collections.sort(temp); if (n%2!=0&&input.get(0)!=temp.get(0)) { System.out.println("NO"); flag=false; } else { for(int j=n%2==0?0:1;j<n-1;j+=2){ if((temp.get(j)==input.get(j)&&temp.get(j+1)==input.get(j+1))||(temp.get(j)==input.get(j+1)&&temp.get(j+1)==input.get(j))) continue; else { System.out.println("NO"); flag=false; break; } } } if(flag) System.out.println("YES"); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 17
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
6e2add6d919e3ecee57e1a9dd67b8c8e
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } static int gcd(int a, int b) { if (a == 0) return b; if (b == 0) return a; if (a == b) return a; if (a > b) return gcd(a-b, b); return gcd(a, b-a); } static void print(long []arr) { for(long a : arr) System.out.print(a + " "); System.out.println(); } static void print(int []arr) { for(int a : arr) System.out.print(a + " "); System.out.println(); } static void print(long a) { System.out.print(a); } static void printl(long a) { System.out.println(a); } static void printl(char ch) { System.out.println(ch); } static void print(char ch) { System.out.print(ch); } static void printl(String s) { System.out.println(s); } static void print(String s) { System.out.print(s); } static int[] intArray(int n) { int arr[] = new int[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextInt(); return arr; } static long[] longArray(int n) { long arr[] = new long[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextLong(); return arr; } static double[] doubleArray(int n) { double arr[] = new double[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextDouble(); return arr; } static int[][] intMatrix(int n, int m) { int mat[][] = new int[n][m]; for(int i = 0; i < mat.length; i++) for(int j = 0; j < mat[0].length; j++) mat[i][j] = sc.nextInt(); return mat; } static long[][] longMatrix(int n, int m) { long mat[][] = new long[n][m]; for(int i = 0; i < mat.length; i++) for(int j = 0; j < mat[0].length; j++) mat[i][j] = sc.nextLong(); return mat; } static class IntPair { int v1, v2; IntPair(int v1, int v2) { this.v1 = v1; this.v2 = v2; } } static class LongPair { long v1, v2; LongPair(long v1, long v2) { this.v1 = v1; this.v2 = v2; } } static FastReader sc = new FastReader(); public static void main(String[] args) { int tc = sc.nextInt(); while(tc-- > 0) { int n = sc.nextInt(); int a[] = intArray(n); for(int i = n - 1; i >= 0; i-=2) { if(i - 1 >= 0 && a[i] < a[i - 1]) { int temp = a[i]; a[i] = a[i - 1]; a[i - 1] = temp; } } boolean flag = true; for(int i = 0; i < n; i++) if(i + 1 < n && a[i] > a[i + 1]) {flag = false; break;} if(flag) printl("YES"); else printl("NO"); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 17
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
369f92a6cf6e589102a32d9c9cfbcb65
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class ABCSort { public static void main(String[] args) { Kattio in=new Kattio(); 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(); for(int i=n%2;i<n-1;i+=2){ if(a[i]>a[i+1]){ a[i]^=a[i+1]; a[i+1]^=a[i]; a[i]^=a[i+1]; } } boolean flag=true; for(int i=0;i<n-1;i++){ if (a[i]>a[i+1]) flag=false; } System.out.println(flag?"YES":"NO"); } } } class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // 标准 IO public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // 文件 IO public Kattio(String intput, String output) throws IOException { super(output); r = new BufferedReader(new FileReader(intput)); } // 在没有其他输入时返回 null public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 17
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
37f75cd4ad746de16d94b7aebe7ff96d
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
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; public static void Solve() throws IOException{ st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()); int[] ar=getArrIn(n); int[] arr=ar.clone(); Arrays.sort(arr); int i=0; if(n%2!=0){ if(arr[0]!=ar[0]){ bw.write("NO\n");return ; } else{ i=1; } } for(;i<n;i+=2){ if(!((ar[i]==arr[i+1] && arr[i]==ar[i+1]) || (ar[i]==arr[i] && arr[i+1]==ar[i+1]))){ bw.write("NO\n"); return ; } } bw.write("YES\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\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 17
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
043f10b3397490876146faaa5729fd23
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; public class Test1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i] = sc.nextInt(); if(n == 1) { System.out.println("yes"); continue; } int min = Integer.MAX_VALUE; String ans = "yes"; for(int i=n-1;i>=0;i -= 2) { int first = (i-1>=0)? arr[i-1] : Integer.MIN_VALUE; int sec = arr[i]; int tempMin = Math.min(first, sec); int tempMax = Math.max(first, sec); if(tempMax > min) { ans = "no"; break; } min = tempMin; } System.out.println(ans); } // #################### } // #################### }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 17
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
d9d45e664a1579d12c8559fc2de97fc5
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; public class text1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int cs = sc.nextInt(); for(int dz = 0;dz < cs;dz ++){ int length = sc.nextInt(); int[] number = new int[length]; for(int dz1 = 0;dz1 < length;dz1 ++) number[dz1] = sc.nextInt(); boolean pd = true; int min = 0; if(length >= 2) min = Math.min(number[length - 1] , number[length - 2]); else min = number[0]; for(int dj = length - 3;dj >= 0;dj -= 2){ if(number[dj] > min){ pd = false; break; } if(dj - 1 >= 0 && number[dj - 1] > min){ pd = false; break; } if(dj - 1 >= 0) min = Math.min(number[dj],number[dj - 1]); } if(pd) System.out.println("YES"); else System.out.println("NO"); } return ; } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 17
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
f127b253e43855c0f6cac66ea829d6d4
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; public class ABC_sort { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //System.out.println("Enter the number of test cases"); int t = scanner.nextInt(); for (int i = 0; i < t; i++) { //System.out.println("Enter the size of array"); int n = scanner.nextInt(); int[] a = new int[n]; for (int j = 0; j < n; j++) { //System.out.println("Enter the integers"); a[j] = scanner.nextInt(); } System.out.println(solve(n, a)); } } private static String solve(int n, int[] a) { int[] sortedA = a.clone(); Arrays.sort(sortedA); for (int i = n - 1; i > 0; i -= 2) { if (a[i] < a[i - 1]) { int tmp = a[i]; a[i] = a[i - 1]; a[i - 1] = tmp; } } return Arrays.equals(a, sortedA) ? "YES" : "NO"; } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 17
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
8be0cb3b2b940e4a99700c3bdd44e266
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Scanner; public class CF1674D { public static void main(String[] args) { Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8); int t = scanner.nextInt(); for (int i = 0; i < t; i++) { int n = scanner.nextInt(); int[] a = new int[n]; for (int j = 0; j < n; j++) { a[j] = scanner.nextInt(); } System.out.println(solve(n, a)); } } private static String solve(int n, int[] a) { int[] sortedA = a.clone(); Arrays.sort(sortedA); for (int i = n - 1; i > 0; i -= 2) { if (a[i] < a[i - 1]) { int tmp = a[i]; a[i] = a[i - 1]; a[i - 1] = tmp; } } return Arrays.equals(a, sortedA) ? "YES" : "NO"; } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 17
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
38f855903565b16eb40d38ee2584d6cc
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Solution { private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static PrintWriter pw = new PrintWriter(System.out); static void getRes() throws Exception{ int n = Integer.parseInt(br.readLine()); int[] arr = inputIntArray(n), cp = Arrays.copyOf(arr, n); Arrays.sort(cp); if (n % 2 == 1 && arr[0] != cp[0]) { pw.println("NO"); return; } int temp; for (int x = n % 2; x < n; x+=2) { if (arr[x + 1] < arr[x]) { temp = arr[x]; arr[x] = arr[x+1]; arr[x+1] = temp; } if (arr[x] != cp[x] || arr[x+1] != cp[x+1]) { pw.println("NO"); return; } } pw.println("YES"); } public static void main(String[] args) throws Exception { int tests = Integer.parseInt(br.readLine()); while (tests-- > 0) { getRes(); } pw.flush(); pw.close(); } static int[] inputIntArray(int n) throws Exception{ int[] arr = new int[n]; String[] inp = br.readLine().split(" "); for (int x = 0; x < n; x++) arr[x] = Integer.parseInt(inp[x]); return arr; } static long[] inputLongArray(int n) throws Exception{ long[] arr = new long[n]; String[] inp = br.readLine().split(" "); for (int x = 0; x < n; x++) arr[x] = Integer.parseInt(inp[x]); return arr; } static void printArray(int[] arr) { for (int x = 0; x < arr.length; x++) pw.print(arr[x] + " "); pw.println(); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 17
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
d89f7c6c62d51c7abce7deb2158307e2
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; public class abcsort { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int t=in.nextInt(); while(t-->0) { int n=in.nextInt(); int ar[]=new int [n]; for(int i=0;i<n;i++) ar[i]=in.nextInt(); int j=0,count=0; if((n&1)!=0) j=1; for(int i=j;i<(n-1);i+=2) { if(ar[i]>ar[i+1]) { ar[i]=ar[i]^ar[i+1]; ar[i+1]=ar[i]^ar[i+1]; ar[i]=ar[i]^ar[i+1]; } } for(int i=0;i<n-1;i++) { if(ar[i]>ar[i+1]) { count=1; System.out.println("NO"); break; } } if(count==0) System.out.println("YES"); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
061f028f1018883141adf7e5281deab1
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.Scanner; public class Main2 { static void solve() { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); for (int i = 0; i < t; i++) { int n = scan.nextInt(); int[] arr = new int[n]; // List<Integer> l = new ArrayList<>(); for (int j = 0; j < n; j++) { arr[j] = scan.nextInt(); // l.add(scan.nextInt()); } solveCase(arr); } } static void solveCase(int[] arr) { for(int i = arr.length - 1; i > 0; i-=2) { if(arr[i] < arr[i-1]) { int tmp = arr[i]; arr[i] = arr[i - 1]; arr[i - 1] = tmp; } } for(int i = 1; i < arr.length; i++) { if(arr[i] < arr[i - 1]) { System.out.println("NO"); return; } } System.out.println("YES"); } public static void main(String[] args) { solve(); } static class Node { int val; Node next; public Node(int val) { this.val = val; } // @Override // public int compareTo(Node node) { // if(this.max < node.max) { // return -1; // } else if(this.max > node.max) { // return 1; // } // return 0; // } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
9768adc9dde76b16570516f81ddcbbc2
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { static long INF = 2000000000000000010l; static int fx[][] = {{-1,0},{0,1},{1,0},{0,-1},{-1,-1},{1,1},{1,-1},{-1,1}}; public static void main(String[] args) throws IOException { int t = cin.nextInt(); while (t-- > 0){ // csh(); solve(); out.flush(); } out.close(); } public static void solve() { int n=cin.nextInt(); int m=-1; if(n%2!=0){ m=cin.nextInt(); } boolean f=true; for(int i=0;i<n-(n%2==1?1:0);i+=2){ int a=cin.nextInt(); int b=cin.nextInt(); if(a>b){ a=a^b; b=a^b; a=a^b; } if(a>=m){ m=b; }else{ f=false; } } if(f){ out.println("YES"); }else{ out.println("NO"); } } static FastScanner cin = new FastScanner(System.in); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in), 16384); eat(""); } public void eat(String s) { st = new StringTokenizer(s); } public String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } public boolean hasNext() { while (!st.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public Double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } public BigDecimal nextBigDecimal() { return new BigDecimal(next()); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
ac25b9705b09cb827a19c5e9d7fb67da
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; public class Test { 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+1]; for(int i=1; i<=n; i++){ a[i] = sc.nextInt(); } for(int i=n-1; i>=1; i-=2){ if(a[i]>a[i+1]) { int tmp = a[i]; a[i] = a[i+1]; a[i+1] = tmp; } } boolean s = true; for(int i=0; i<n-1; i++){ if(a[i] > a[i+1]){ s = false; break; } } if(s) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
39448df0772e1844d7ae71e07f60b943
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class D { private static FastReader fr; private static OutputStream out; private static int mod = (int)(1e9+7); private void solve() { int t = fr.nextInt(); while(t-- > 0) { int n = fr.nextInt(); int arr[] = fr.nextIntArray(n); boolean flag = false; int temp[] = new int[n]; for(int i=n-1;i>0;i-=2){ temp[i] = Math.max(arr[i], arr[i-1]); temp[i-1] = Math.min(arr[i], arr[i-1]); } if(n % 2 == 1) temp[0] = arr[0]; // for(int val: temp){ // print(val + " "); // } // println(); for(int i=0;i<n-1;i++){ if(temp[i] > temp[i+1]) flag = true; } if(flag) println("NO"); else println("YES"); } } public static void main(String args[]) throws IOException{ new D().run(); } private static int modInverse(int a) { int m0 = mod; int y = 0, x = 1; if (mod == 1) return 0; while (a > 1) { int q = (int)(a / mod); int t = mod; mod = a % mod; a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } private ArrayList<Integer> factors(int n, boolean include){ ArrayList<Integer> factors = new ArrayList<>(); if(n < 0) return factors; if(include) { factors.add(1); if(n > 1) factors.add(n); } int i = 2; for(;i*i<n;i++) { if(n % i == 0) { factors.add(i); factors.add(n / i); } } if(i * i == n) { factors.add(i); } return factors; } private ArrayList<Integer> PrimeFactors(int n) { ArrayList<Integer> primes = new ArrayList<>(); int i = 2; while (i * i <= n) { if (n % i == 0) { primes.add(i); n /= i; while (n % i == 0) { primes.add(i); n /= i; } } i++; } if(n > 1) primes.add(i); return primes; } private boolean isPrime(int n) { if(n == 0 || n == 1) { return false; } if(n % 2 == 0) { return false; } for(int i=3;i*i<=n;i+=2) { if(n % i == 0) { return false; } } return true; } private ArrayList<Integer> Sieve(int n){ boolean bool[] = new boolean[n+1]; Arrays.fill(bool, true); bool[0] = bool[1] = false; for(int i=2;i*i<=n;i++) { if(bool[i]) { int j = 2; while(i*j <= n) { bool[i*j] = false; j++; } } } ArrayList<Integer> primes = new ArrayList<>(); for(int i=2;i<=n;i++) { if(bool[i]) primes.add(i); } return primes; } private HashMap<Integer, Integer> addToHashMap(HashMap<Integer, Integer> map, int arr[]){ for(int val: arr) { if(!map.containsKey(val)) { map.put(val, 1); }else { map.put(val, map.get(val) + 1); } } return map; } private int factorial(int n) { long fac = 1; for(int i=2;i<=n;i++) { fac *= i; fac %= mod; } return (int)(fac % mod); } // private static int pow(int base,int exp){ // if(exp == 0){ // return 1; // }else if(exp == 1){ // return base; // } // int a = pow(base,exp/2); // a = ((a % mod) * (a % mod)) % mod; // if(exp % 2 == 1) { // a = ((a % mod) * (base % mod)); // } // return a; // } private static int gcd(int a,int b){ if(a == 0){ return b; } return gcd(b%a,a); } private static int lcm(int a,int b){ return (a * b)/gcd(a,b); } private void run() throws IOException{ fr = new FastReader(); out = new BufferedOutputStream(System.out); solve(); out.flush(); out.close(); } private static class FastReader{ private static BufferedReader br; private static StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedOutputStream(System.out); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public char[] nextCharArray() { return next().toCharArray(); } public int[] nextIntArray(int n) { int arr[] = new int[n]; for(int i=0;i<n;i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long arr[] = new long[n]; for(int i=0;i<n;i++) { arr[i] = nextLong(); } return arr; } public String[] nextStringArray(int n) { String arr[] = new String[n]; for(int i=0;i<n;i++) { arr[i] = next(); } return arr; } } public static void print(Object str) { try { out.write(str.toString().getBytes()); } catch (IOException e) { e.printStackTrace(); } } public static void println(Object str) { try { out.write((str.toString() + "\n").getBytes()); } catch (IOException e) { e.printStackTrace(); } } public static void println() { println(""); } public static void printArray(Object str[]){ for(Object s : str) { try { out.write(str.toString().getBytes()); } catch (IOException e) { e.printStackTrace(); } } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
1e488c702b36af124a9874540a1139c0
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class D{ public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[]data; int t = Integer.valueOf(br.readLine()); String ANSWER = ""; while((t--)!=0){ int n = Integer.parseInt(br.readLine()); int[]a = new int[n]; data = br.readLine().split(" "); for(int i = 0 ; i < n ; ++i){ a[i] = Integer.valueOf(data[i]); } for(int i = n-1 ; i > 0 ; i-=2){ if(a[i] < a[i-1]){ int aux = a[i]; a[i] = a[i - 1]; a[i-1] = aux; } } boolean YES = true; for(int i = 0 ; i < n -1 ; i++){ if(a[i+1] < a[i]){ YES = false; break; } } ANSWER+= YES ? "YES\n" : "NO\n"; } System.out.println(ANSWER); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
b6ba4819b87ed11353f924f088b8e60a
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class D{ public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[]data; int t = Integer.valueOf(br.readLine()); while((t--)!=0){ int n = Integer.parseInt(br.readLine()); int[]a = new int[n]; data = br.readLine().split(" "); for(int i = 0 ; i < n ; ++i){ a[i] = Integer.valueOf(data[i]); } for(int i = n-1 ; i > 0 ; i-=2){ if(a[i] < a[i-1]){ int aux = a[i]; a[i] = a[i - 1]; a[i-1] = aux; } } boolean YES = true; for(int i = 0 ; i < n -1 ; i++){ if(a[i+1] < a[i]){ YES = false; break; } } System.out.println(YES ? "YES" : "NO"); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
e87225372eb94c4bcbb37d611d200381
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.LinkedBlockingDeque; import javax.sql.rowset.spi.SyncResolver; import java.io.*; import java.nio.channels.NonReadableChannelException; import java.text.DateFormatSymbols; import static java.util.Arrays.sort; public class CpTemp { static int a[]; static long count=0l; static long row = 1l; static long col = 1l; static int d; static int n; static int m = 1_000_000_007; static int [] opr = new int[1001]; static int dp[] = new int[40001]; static ArrayList<Integer> h = new ArrayList<>(); static FastScanner fs = null; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); outer: while (t-- > 0) { int n = fs.nextInt(); int a[] = fs.readArray(n); int b[] = new int[n]; for(int i=0;i<n;i++){ b[i] = a[i]; } Arrays.sort(b); for(int i = (n%2);i<n;i++){ if(a[i]>a[i+1]){ int m = a[i]; a[i] = a[i+1]; a[i+1] = m; } i++; } boolean ans = true; for(int i=1;i<n;i++){ if(a[i]<a[i-1]){ ans = false; break; } } out.println(ans==true?"YES":"NO"); } out.close(); } public static void combinations(){ Arrays.fill(dp,1); for(int i=1;i<h.size();i++){ int j=0; for( j=h.get(i);j<40001 ;j++){ dp[j] = (dp[j] + dp[j-(h.get(i))])%m; } } } public static void palindromic(){ h.add(1); h.add(2); h.add(3); h.add(4); h.add(5); h.add(6); h.add(7); h.add(8); h.add(9); for(int i=11;i<40001;i++){ if(pal(i)){ h.add(i); } } } public static boolean pal(int i){ int v=i; int r=0; while(v>0){ r *= 10; r += v%10; v = v/10; } if(r==i){ return true; }else{ return false; } } static class Pair implements Comparable<Pair> { int x; String y; Pair(int x, String y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return this.x-o.x; } } static long power(long x, long y, long p) { if (y == 0) return 1; if (x == 0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if (y == 0) return 1; if (x == 0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readlongArray(int n){ long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } static boolean prime[]; static void sieveOfEratosthenes(int n) { prime = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } } public static int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static long nCk(int n, int k) { long res = 1; for (int i = n - k + 1; i <= n; ++i) res *= i; for (int i = 2; i <= k; ++i) res /= i; return res; } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
53ce342066cfdad94ef8a7557059aaf5
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.Scanner; public class Ques4 { public static void main(String[] args) { Scanner s= new Scanner(System.in); int t = s.nextInt(); while(t-->0) { int n = s.nextInt(); long arr[] = new long[n]; for(int i=0;i<n;i++) { arr[i] = s.nextLong(); } for(int i=n-1;i>=1;i-=2){ if(arr[i-1]>arr[i]) { long temp = arr[i-1]; arr[i-1] = arr[i]; arr[i]=temp; } } if(isSorted(arr,n)) { System.out.println("YES"); }else { System.out.println("NO"); } } } private static boolean isSorted(long[] arr,int n) { for(int i=0;i<n-1;i++) { if(arr[i]>arr[i+1]) return false; } return true; } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
44e09d03ab1fb6ee5ef572bb1e3d2022
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.math.*; import java.util.*; /* * Author: Atuer */ public class D_WA { // ==== Solve Code ====// static int INF = 2000000010; public static void csh() { } public static void main(String[] args) throws IOException { // csh(); int t = in.nextInt(); while (t-- > 0) { solve(); out.flush(); } out.close(); } public static void solve() { int n = in.nextInt(); boolean flag = true; int pre = -1; if (n % 2 == 1) { pre = in.nextInt(); } for (int i = 0; i < n - n % 2; i += 2) { int a = in.nextInt(); int b = in.nextInt(); if (a > b) { int t = a; a = b; b = t; } if (a >= pre) { pre = b; } else { flag = false; } } out.println(flag ? "YES" : "NO"); } public static class Node { int x, y, k; public Node(int x, int y, int k) { this.x = x; this.y = y; this.k = k; } } // ==== Solve Code ====// // ==== Template ==== // public static long cnm(int a, int b) { long sum = 1; int i = a, j = 1; while (j <= b) { sum = sum * i / j; i--; j++; } return sum; } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static void gbSort(int[] a, int l, int r) { if (l < r) { int m = (l + r) >> 1; gbSort(a, l, m); gbSort(a, m + 1, r); int[] t = new int[r - l + 1]; int idx = 0, i = l, j = m + 1; while (i <= m && j <= r) if (a[i] <= a[j]) t[idx++] = a[i++]; else t[idx++] = a[j++]; while (i <= m) t[idx++] = a[i++]; while (j <= r) t[idx++] = a[j++]; for (int z = 0; z < t.length; z++) a[l + z] = t[z]; } } // ==== Template ==== // // ==== IO ==== // static InputStream inputStream = System.in; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(System.out); 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(); } boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { return false; // TODO: handle exception } } return true; } public String nextLine() { String str = null; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public Double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } public BigDecimal nextBigDecimal() { return new BigDecimal(next()); } } // ==== IO ==== // }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
c27d15ebe2e5c94e7165f46d9074c24d
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.math.*; import java.util.*; /* * Author: Atuer */ public class D_WA { // ==== Solve Code ====// static int INF = 2000000010; public static void csh() { } public static void main(String[] args) throws IOException { // csh(); int t = in.nextInt(); while (t-- > 0) { solve(); out.flush(); } out.close(); } public static void solve() { int n = in.nextInt(); boolean flag = true; int pre = -1; if (n % 2 == 1) { pre = in.nextInt(); } for (int i = 0; i < n - n % 2; i += 2) { int a = in.nextInt(); int b = in.nextInt(); if (a > b) { int t = a; a = b; b = t; } if (a >= pre) { pre = b; } else { flag = false; } } out.println(flag ? "YES" : "NO"); } public static class Node { int x, y, k; public Node(int x, int y, int k) { this.x = x; this.y = y; this.k = k; } } // ==== Solve Code ====// // ==== Template ==== // public static long cnm(int a, int b) { long sum = 1; int i = a, j = 1; while (j <= b) { sum = sum * i / j; i--; j++; } return sum; } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static void gbSort(int[] a, int l, int r) { if (l < r) { int m = (l + r) >> 1; gbSort(a, l, m); gbSort(a, m + 1, r); int[] t = new int[r - l + 1]; int idx = 0, i = l, j = m + 1; while (i <= m && j <= r) if (a[i] <= a[j]) t[idx++] = a[i++]; else t[idx++] = a[j++]; while (i <= m) t[idx++] = a[i++]; while (j <= r) t[idx++] = a[j++]; for (int z = 0; z < t.length; z++) a[l + z] = t[z]; } } // ==== Template ==== // // ==== IO ==== // static InputStream inputStream = System.in; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(System.out); 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(); } boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { return false; // TODO: handle exception } } return true; } public String nextLine() { String str = null; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public Double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } public BigDecimal nextBigDecimal() { return new BigDecimal(next()); } } // ==== IO ==== // }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
67d7488147a8b74761d14f6e5eeb4253
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static boolean check(int a[]) { for (int i = 0; i <a.length-1; i++) { if(a[i]>a[i+1]) { return false; } } return true; } public static void main(String args[]) { 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(); } for (int i = n-1; i>=0; i-=2) { if(i-1>=0&&a[i]<a[i-1]) { int temp = a[i]; a[i] = a[i-1]; a[i-1] = temp; } } if(check(a)) { System.out.println("YES"); } else { System.out.println("NO"); } } } 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\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
6f1f06aebca31d74ebe18bcc6433f59f
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; public class MyClass{ public static void main(String[] args){ Scanner sc= new Scanner(System.in); int t = sc.nextInt(); while(t > 0){ t--; int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = sc.nextInt(); } if(n == 1 || n == 2){ System.out.println("YES"); continue; } boolean flag = true; int max = Math.max(arr[n - 1], arr[n - 2]); int min = Math.min(arr[n - 1], arr[n - 2]); int count = 0; for(int i = n - 3; i >= 0; i--){ count++; if( arr[i] > max || arr[i] > min){ flag = false; break; } if( count != 0 && count % 2 == 0){ max = Math.max(arr[i], arr[i + 1]); min = Math.min(arr[i], arr[i + 1]); } } if(flag){ System.out.println("YES"); }else{ System.out.println("NO"); } } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
13cea6a37bf6e6a5b23f34baa79859c0
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.Scanner; /** * <a href = "https://codeforces.com/contest/1674/problem/D" > Link </a> * @author Bris * @version 1.0 * @since 10:25:53 AM - May 3, 2022 */ public class D1674 { public static boolean isSorted(int[] arr) { for (int i = 1; i < arr.length; i++) { if (arr[i-1] > arr[i]) { return false; } } return true; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-->0) { int n = scanner.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = scanner.nextInt(); } for (int i = n - 1; i >= 1; i -= 2) { if (a[i] < a[i-1]) { int temp = a[i]; a[i] = a[i-1]; a[i-1] = temp; } } System.out.println(isSorted(a)? "YES" : "NO"); } scanner.close(); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
a6e847584bcca522db1fb7f9f1d0b1fb
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
/* * @Jai_Bajrang_Bali * @Har_Har_Mahadev */ /* Created by IntelliJ IDEA. * Author: Sanat Kumar Dubey (sanat04) * Date: 03-05-2022 * Time: 00:52 * File: D.java */ import java.util.*; public class D { static boolean helper(int[] arr){ boolean check=true; for (int i =0;i<arr.length-1;i++) { if(arr[i]>arr[i+1]){ check= false; break; } } if(!check) return false; return true; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t= sc.nextInt(); while (t-->0){ int n=sc.nextInt(); int[] arr=new int[n]; for (int i = 0; i < n; i++) { arr[i]= sc.nextInt(); } for (int i = n-1; i >0 ; i-=2) { if(arr[i-1]>arr[i]){ int temp=arr[i-1]; arr[i-1]=arr[i]; arr[i]=temp; } } if(helper(arr)) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
65817399bb8eb28f7e4788cdf5a4fdc2
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; import static java.lang.System.out; public class app { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int j=sc.nextInt(); int ii[]=new int[j]; int ii1[]=new int[j]; for(int i=0;i<j;i++){ ii[i]=sc.nextInt(); ii1[i]=ii[i]; } Arrays.sort(ii1); if(j==1){ out.println("yes"); }else if(j%2==0){ int iiio=0; for(int i=0;i<ii.length;i+=2){ if((ii[i]==ii1[i]||ii[i]==ii1[i+1])&&(ii[i+1]==ii1[i]||ii[i+1]==ii1[i+1])){ } else{ out.println("no"); iiio++; break; } } if(iiio==0){ out.println("yes"); } } else { if(ii[0]==ii1[0]){ int iiio=0; for(int i=1;i<ii.length;i+=2){ if((ii[i]==ii1[i]||ii[i]==ii1[i+1])&&(ii[i+1]==ii1[i]||ii[i+1]==ii1[i+1])){ } else{ out.println("no"); iiio++; break; } } if(iiio==0){ out.println("yes"); } } else{ out.println("no"); } } } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
9d5e19772a0bcb0a3d7b81558af6569e
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main15 { public boolean canSort(int[] a){ int n=a.length; int[] b=new int[n]; for(int i=0;i<n;i++){ b[i]=a[i]; } Arrays.sort(a); if(n%2==1&&a[0]!=b[0]){ return false; } if(n%2==0) { for (int i = 0; i < n / 2; i++) { if ((a[i * 2] == b[i * 2] &&a[i*2+1]==b[i*2+1])||(a[i * 2+1] == b[i * 2] &&a[i*2]==b[i*2+1])) { continue; } return false; } }else { for(int i=n-1;i>0;i-=2){ if ((a[i] == b[i] &&a[i-1]==b[i-1])||(a[i-1] == b[i] &&a[i]==b[i-1])) { continue; } return false; } } return true; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); boolean[] b=new boolean[n]; Main15 m=new Main15(); for(int i=0;i<n;i++){ int k=sc.nextInt(); int[] a=new int[k]; for(int j=0;j<k;j++){ a[j]=sc.nextInt(); } b[i]=m.canSort(a); } for(int i=0;i<n;i++){ if(b[i]){ System.out.println("YES"); }else { System.out.println("NO"); } } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
119d97e0d692474c2a93bfb659a33f37
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.math.BigInteger; import java.util.*; public class gcd_problem{ static int gcd(int a, int b) { int result = Math.min(a, b); // Find Minimum of a nd b while (result > 0) { if (a % result == 0 && b % result == 0) { break; } result--; } return result; // return gcd of a nd b } public static int[] arrInput(int n){ Scanner sc = new Scanner(System.in); int[] arr = new int[n]; for (int i = 0; i <n ; i++) { arr[i] = sc.nextInt(); } return arr; } public static int[][] twoDArrInput(int n,int m){ Scanner sc = new Scanner(System.in); int[][] arr = new int[n][m]; for (int i = 0; i <n ; i++) { for (int j = 0; j <m ; j++) { arr[i][j] = sc.nextInt(); } } return arr; } public static HashMap<Integer,Integer> feqHashMap(int[] arr,int n){ HashMap<Integer,Integer> ht= new HashMap<>(); for (int i = 0; i <n ; i++) { ht.put(arr[i],ht.getOrDefault(arr[i],0)+1); } return ht; } public static HashMap<Integer,Integer> indHashMap(int[] arr,int n){ HashMap<Integer,Integer> ht= new HashMap<>(); for (int i = 0; i <n ; i++) { ht.put(arr[i],i); } return ht; } public static void rotatingMatrixEquals(){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[][] = new int[n][n]; for (int i = 0; i <n ; i++) { String s = sc.next(); for (int j = 0; j <n ; j++) { a[i][j] = Integer.parseInt(String.valueOf(s.charAt(j))); } } int count=0; for (int i = 0; i <n ; i++) { for (int j = 0; j <n ; j++) { int sum = a[i][j] + a[j][n-1-i] + a[n-1-i][n-1-j] + a[n-1-j][i]; if(sum == 4 || sum ==0){ continue; }else { if(sum ==3 || sum==1){ count+=1; } else if(sum ==2){ count+=2; } } } } System.out.println(count/4); } public void greedyChangesInArrayBasedOnMaxAndMin(){ Scanner sc = new Scanner(System.in); int n= sc.nextInt(); int k = sc.nextInt();; int a[] = new int[n]; for (int i = 0; i <n ; i++) { a[i] =sc.nextInt(); } int l = a[0]; int r = a[0]; int ans = 0; for (int i = 1; i < n; i++) { if (a[i] > r) { r = a[i]; } if (a[i] < l) { l = a[i]; } if (Math.abs(r-l) > 2 * k) { ans++; l = r = a[i]; } } System.out.println(ans); } public static void solve(int n,int[] a){ if(n<0){ return; } int s = (int) Math.sqrt(2*n); s =s*s; int l =s-n; solve(l-1,a); while(l<=n){ a[l] =n; a[n] =l; l++; n--; } } public static void sortbyColumn(long arr[][], int col) { Arrays.sort(arr, new Comparator<long[]>(){ @Override public int compare(final long[] entry1, final long[] entry2) { if (entry1[col] > entry2[col]) return 1; else return -1; } }); } public static void dpPermutation(){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = new int[n]; solve(n-1,a); for (int i = 0; i <n ; i++) { System.out.print(a[i]+" "); } System.out.println(); } public static void ApByMultiplication(){ Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); String ans ="NO"; if((c-b) == b-a){ ans = "YES"; System.out.println(ans); } else if((c+a)%(2*b) ==0){ ans ="YES"; System.out.println(ans); } else if(2*b-c>0 && (2*b -c)%a==0){ ans ="YES"; System.out.println(ans); }else if(2*b-a>0 && (2*b -a)%c==0){ ans ="YES"; System.out.println(ans); }else{ System.out.println(ans); } } public static void makingArrayEqual(){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = new int[n]; for (int i = 0; i <n ; i++) { a[n-1-i]=sc.nextInt(); } int ans= 0; int pointer=1; while(pointer<= n-1) { if( a[pointer] == a[0] ){ pointer ++; continue; } ans ++; pointer *= 2; } System.out.println(ans); } public static int go(int i, int sum, int n, int arr[]) { if (i == n) return 0; for (int j = i + 1, cur = 0; j <= n; ++j) { cur += arr[j - 1]; if (cur > sum) return n; if (cur == sum) return Math.max(j - i, go(j, sum,n,arr)); } return n; } public static int solver(int n, int arr[]) { int ans = n; for (int len = 1, sum = 0; len < n; ++len) { sum += arr[len - 1]; ans = Math.min(ans, go(0, sum,n,arr)); } return ans; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t>0) { t--; int n = sc.nextInt(); int a[] = new int[n]; for (int i = 0; i <n ; i++) { a[i] = sc.nextInt(); } int c[] = new int[n]; if(n%2==0){ for (int i = 0; i <n-1 ; i+=2) { int x = a[n-1-i]; int y = a[n-2-i]; if(x>y){ c[n-1-i] = x; c[n-2-i] = y; }else{ c[n-1-i] = y; c[n-2-i] = x; } } } else{ c[0] = a[0]; for (int i = 0; i <n-2 ; i+=2) { int x = a[n-1-i]; int y = a[n-2-i]; if(x>y){ c[n-1-i] = x; c[n-2-i] = y; }else{ c[n-1-i] = y; c[n-2-i] = x; } } } String ans = "YES"; for (int i = 0; i <n-1 ; i++) { if(c[i]>c[i+1]){ ans = "NO"; } } //System.out.println(Arrays.toString(c)); System.out.println(ans); //int prefixSum[] = new int[n]; //prefixSum[0] = a[0]; //int max = 0; //ArrayList<Integer> a = new ArrayList<>(); /*for(int i=0;i<n;i++){ int x = sc.nextInt(); a[i] =x; //a.add(x); // index hashmap //ht.put(a[i],i); //frequency hashmap //ht.put(a[i] ,ht.getOrDefault(a[i],0)+1); //max // max = Math.max(max,a[i]); //prefixSum[i] = prefixSu[i-1] + a[i]; }*/ /*int ans=0; int start=0; for (int i = 0; i <n ; i++) { if(i-start>0 && a[i] > i-start){ ans+=i-start+1; }else if(a[i] == i-start){ ans+=1; }else{ ans+=1; start=i; } } System.out.println(ans);*/ /*for(int i=0;i<n;i++){ int y = sc.nextInt(); b[i] =y; //a.add(x); // index hashmap //ht.put(a[i],i); //frequency hashmap //ht.put(a[i] ,ht.getOrDefault(a[i],0)+1); //max // max = Math.max(max,a[i]); //prefixSum[i] = prefixSu[i-1] + a[i]; } int cou1=0; for (int i = 0; i <n ; i++) { if(a[i] !=b[i]){ cou1++; } } Arrays.sort(a); Arrays.sort(b); int cou2=1; for (int i = 0; i <n ; i++) { if(a[i] != b[i]){ cou2++; } } System.out.println(Math.min(cou1,cou2));*/ } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
3096e3d7771ec2c16e7909cd494f6a00
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
//Does not work, WIP import java.io.*; import java.util.*; import java.util.StringTokenizer; public class ABCSort implements Runnable { public static void main(String [] args) { new Thread(null, new ABCSort(), "whatever", 1<<26).start(); } public void run() { FastScanner scanner = new FastScanner(System.in); StringBuilder answers = new StringBuilder(); int tests = scanner.nextInt(); for (int t = 0; t < tests; t++) { int n = scanner.nextInt(); int[] input = new int[n]; int min = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { int num = scanner.nextInt(); input[i] = num; } if (n < 3) { answers.append("YES\n"); } else { ArrayList<Pair> pairs = new ArrayList<>(); for (int i = n - 1; i >= 0; i -= 2) { if (i == 0) { pairs.add(new Pair(input[i], input[i])); } else { pairs.add(new Pair(input[i], input[i-1])); } } boolean success = true; Pair lastPair = pairs.get(0); for (int i = 1; i < pairs.size(); i++) { if (!lastPair.fitsInside(pairs.get(i))) { success = false; break; } lastPair = pairs.get(i); } answers.append(success ? "YES\n" : "NO\n"); } } System.out.println(answers); } class Pair { int num1; int num2; public Pair(int num1, int num2) { this.num1 = num1; this.num2 = num2; } public int getMax() { return Math.max(num1, num2); } public int getMin() { return Math.min(num1, num2); } public boolean fitsInside(Pair that) { return that.getMax() <= this.getMin(); } public String toString() { return "("+ num1 + ", " + num2 + ")"; } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner(InputStream in) { this(new InputStreamReader(in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next());} } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
1fe4a88b8358ee09971cbbfe99252856
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class practice { 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(); } } public static boolean issort(List<Integer> l) { int a=l.get(0); int b=0; for(int i=1;i<l.size();i++) { b=l.get(i); if(a>b) { return false; } a=b; } return true; } public static void main(String[] args) throws Exception { Scanner s= new Scanner(System.in); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int arr[]= new int[n]; for(int i=0; i<n;i++) { arr[i]=s.nextInt(); } if(n==1) { System.out.println("YES"); continue; } List<Integer> l= new ArrayList<Integer>(); if(n%2!=0) { l.add(arr[0]); for(int i=1; i<n;i+=2) { int a=arr[i]; int b=arr[i+1]; if(a>b) { l.add(b); l.add(a); }else { l.add(a); l.add(b); } } }else { for(int i=0; i<n;i+=2) { int a=arr[i]; int b=arr[i+1]; if(a>b) { l.add(b); l.add(a); }else { l.add(a); l.add(b); } } } boolean p=issort(l); if(p) { System.out.println("YES"); }else { System.out.println("NO"); } } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
5a48541e989e5be9acdf902dc232cfdb
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
//BA9A487E52394FFB8C67EA0BDF0F26C8 import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int T=sc.nextInt(); while(T-->0) { int n=sc.nextInt(); int[] a=new int[n+1]; for(int i=1;i<=n;i++) a[i]=sc.nextInt(); for(int i=n;i>1;i-=2) { if(a[i-1]>a[i]) { int t=a[i-1]; a[i-1]=a[i]; a[i]=t; } } boolean flag=true; for(int i=2;i<=n;i++) { if(a[i]>=a[i-1]) continue; flag=false; } if(flag) System.out.println("YES"); else System.out.println("NO"); } sc.close(); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
13df151257348e802ca18b2dae4051d6
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int T=sc.nextInt(); while(T-->0) { int n=sc.nextInt(); int[] a=new int[n+1]; for(int i=1;i<=n;i++) a[i]=sc.nextInt(); for(int i=n;i>1;i-=2) { if(a[i-1]>a[i]) { int t=a[i-1]; a[i-1]=a[i]; a[i]=t; } } boolean flag=true; for(int i=2;i<=n;i++) { if(a[i]>=a[i-1]) continue; flag=false; } if(flag) System.out.println("YES"); else System.out.println("NO"); } sc.close(); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
c91046a84d06e6d38be90d1800761ceb
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int T=sc.nextInt(); while(T-->0) { int n=sc.nextInt(); int[] a=new int[n+1]; for(int i=1;i<=n;i++) a[i]=sc.nextInt(); for(int i=n;i>1;i-=2) { if(a[i-1]>a[i]) { int t=a[i-1]; a[i-1]=a[i]; a[i]=t; } } boolean flag=true; for(int i=2;i<=n;i++) { if(a[i]>=a[i-1]) continue; flag=false; } if(flag) System.out.println("YES"); else System.out.println("NO"); } sc.close(); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
8127445876af2669eba9983de4cc6f4b
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
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 D_Round_786_Div3 { public static long MOD = 998244353; static int[] dp; 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(); for (int z = 0; z < T; z++) { int n = in.nextInt(); int[] data = new int[n]; for (int i = 0; i < n; i++) { data[i] = in.nextInt(); } // if(z == 264){ // System.out.println(Arrays.toString(data)); // } Node root = null; boolean ok = true; for (int i = n - 1; i >= 0; i--) { if (root == null) { root = new Node(data[i]); continue; } int count = n - i - 1; Node node = new Node(data[i]); if (count % 2 == 0) { Node nxt = root.nxt; root.nxt = node; node.back = root; node.nxt = nxt; nxt.back = node; if (nxt.value < data[i] || root.value < data[i]) { ok = false; break; } root = node; continue; } Node nxt = root.nxt; Node back = root.back; if (nxt == null) { root.nxt = node; node.back = root; continue; } if (nxt.value < back.value) { if (back.value < data[i] || (root.value < data[i] && nxt.value < data[i])) { ok = false; break; } root.nxt = node; node.back = root; node.nxt = nxt; nxt.back = node; continue; } if (nxt.value < data[i] || (root.value < data[i] && back.value < data[i])) { ok = false; break; } root.back = node; node.nxt = root; back.nxt = node; node.back = back; root = node; } out.println(ok ? "YES" : "NO"); } out.close(); } static class Node { Node back, nxt; int value; Node(int value) { this.value = value; } } 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) % MOD; } else { return (val * ((val * a) % MOD)) % MOD; } } 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\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
cd1912542e61e50a7c5a02817843dbb1
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.BufferedReader; import java.io.Closeable; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; import static java.lang.Math.*; public class ABCSort implements Closeable { private final InputReader in; private final PrintWriter out; public ABCSort() { in = new InputReader(System.in); out = new PrintWriter(System.out); } public void solve() { int t = in.ni(); while (t-- > 0) { int n = in.ni(); int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = in.ni(); } boolean ok = true; int max = Integer.MAX_VALUE; for (int i = n - 1; i > 0; i -= 2) { int mn = Math.min(x[i], x[i - 1]); int mx = Math.max(x[i], x[i - 1]); if (mx <= max) { max = mn; } else { ok = false; break; } } if (n % 2 == 1) { ok &= x[0] <= max; } out.println(ok ? "YES" : "NO"); } } @Override public void close() throws IOException { in.close(); out.close(); } 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 ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (ABCSort instance = new ABCSort()) { instance.solve(); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
15f8de7cd08a5f851a40780822cc64d0
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.awt.*; import java.util.*; import java.io.*; // You have that in you, you can do it........ public class Codeforces { static FScanner sc = new FScanner(); static PrintWriter out = new PrintWriter(System.out); static final Random random = new Random(); static long mod = 1000000007L; static HashMap<String, Integer> map = new HashMap<>(); static boolean[] sieve = new boolean[1000000]; static double[] fib = new double[1000000]; public static boolean yes(int[] a) { int[] b=a.clone(); Arrays.sort(b); boolean bb=true; for(int i=0;i<a.length;i++) { if(a[i]!=b[i]) { bb=false; break; } } return bb; } public static void main(String args[]) throws IOException { int T = sc.nextInt(); while (T-- > 0) { int n = sc.nextInt(); int[] a = sc.readintarray(n); for(int i=n-1;i>0;i-=2) { if(a[i]<a[i-1]) { int t=a[i]; a[i]=a[i-1]; a[i-1]=t; } } if(yes(a)) out.println("YES"); else out.println("NO"); } out.close(); } // TemplateCode static int maxi(int[] a) { int maxi = 0; int max = Integer.MIN_VALUE; for (int i = 0; i < a.length; i++) { if (a[i] > max) { max = a[i]; maxi = i; } } return maxi; } static void fib() { fib[0] = 0; fib[1] = 1; for (int i = 2; i < fib.length; i++) fib[i] = fib[i - 1] + fib[i - 2]; } static void primeSieve() { for (int i = 0; i < sieve.length; i++) sieve[i] = true; for (int i = 2; i * i <= sieve.length; i++) { if (sieve[i]) { for (int j = i * i; j < sieve.length; j += i) { sieve[j] = false; } } } } static int max(int a, int b) { if (a < b) return b; return a; } static int min(int a, int b) { if (a < b) return a; return b; } static void ruffleSort(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 <E> void print(E res) { System.out.println(res); } static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static int abs(int a) { if (a < 0) return -1 * a; return a; } static class Pair { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } } static class FScanner { BufferedReader br; StringTokenizer st; public FScanner() { 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[] readintarray(int n) { int res[] = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] readlongarray(int n) { long res[] = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
0f52c4318c46546011a646f933cb46f0
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = 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 < arr.length; i++) { arr[i] = sc.nextInt(); } int i = 0; if(n%2==1) i++; while(i<arr.length-1) { if(arr[i+1]<arr[i]) { int x = arr[i]; arr[i] = arr[i+1]; arr[i+1] = x; } i+=2; } boolean f = true; for (int j = 0; j < arr.length-1; j++) { if(arr[j+1]<arr[j]) { f = false; break; } } pw.println(f?"YES":"NO"); } pw.flush(); } public static int solve(int b,int x,int y) { if(b==1) { if(x==y) { return 1; } return -1; } int c = 0; while(x<y) { x = b*x; c++; } if(x==y) return c; return -1; } 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\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
6f37bbee38d067f3aa6e0501272e0b5e
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
//package solution; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; public class Solution implements Runnable { public void solve() throws Exception { int cse = sc.nextInt(); while (cse-- > 0) { int n = sc.nextInt(); boolean possible = true; int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } for (int i = n % 2; i < n; i += 2) { if (arr[i] > arr[i + 1]) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } for (int i = 1; i < n; i++) { if (arr[i - 1] > arr[i]) { possible = false; break; } } out.println(possible ? "YES" : "NO"); } } static Throwable uncaught; BufferedReader in; FastScanner sc; PrintWriter out; @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new FastScanner(in); solve(); } catch (Throwable uncaught) { Solution.uncaught = uncaught; } finally { out.close(); } } public static void main(String[] args) throws Throwable { Thread thread = new Thread(null, new Solution(), "", (1 << 26)); thread.start(); thread.join(); if (Solution.uncaught != null) { throw Solution.uncaught; } } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
738071311bd194c4bcdf7478d6f097d3
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
// package CodeforcesRound786Div3; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class D { public static void main(String[] args) { FastReader fr = new FastReader(); int t = fr.nextInt(); while (t-- > 0) { int n = fr.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = fr.nextLong(); } if (n % 2 == 0) { long max = Math.max(a[0], a[1]); long min = Long.MAX_VALUE; boolean flag = true; for (int i = 2; i < n - 1; i += 2) { if (Math.min(a[i], a[i + 1]) >= max) { max = Math.max(a[i], a[i + 1]); } else { flag = false; break; } } if (flag) { System.out.println("YES"); } else { System.out.println("NO"); } } else { long min = Long.MAX_VALUE; boolean flag = true; if(n == 1) { System.out.println("YES"); } else { if (a[0] > Math.min(a[1], a[2])) { flag = false; } if (flag) { long max = Math.max(a[1], a[2]); long min1 = Long.MAX_VALUE; boolean check = true; for (int i = 3; i < n - 1; i += 2) { if (Math.min(a[i], a[i + 1]) >= max) { max = Math.max(a[i], a[i + 1]); } else { check = false; break; } } if (check) { System.out.println("YES"); } else { System.out.println("NO"); } } else { System.out.println("NO"); } } } } } public int gcd(int a, int b) { if (b==0) return a; return gcd(b,a%b); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
9e01a29a2341f721579fa4968d4d91a0
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.Scanner; public class D { public static void main(String[] args) { new D().solve(); } public void solve() { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- > 0) { int n = scanner.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = scanner.nextInt(); } int i = n - 1; for (; i > 2; i -= 2) { if (Math.min(a[i], a[i - 1]) < Math.max(a[i - 2], a[i - 3])) break; } if (i > 2 || (n % 2 == 1 && n >= 3 && Math.min(a[1], a[2]) < a[0])) System.out.println("NO"); else System.out.println("YES"); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
231aabfe743d57bf27b789f63c4e6985
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class D_ABC_Sort { public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(), arr[] = sc.readArray(n); for (int i = n % 2; i < n - 1; i += 2) { if (arr[i] > arr[i + 1]) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } boolean flag = false; for (int i = 1; i < n; i++) { if (arr[i] < arr[i - 1]) { out.println("NO"); flag = true; break; } } if(!flag) out.println("YES"); } out.close(); } public static PrintWriter out; public static long mod = (long) 1e9 + 7; 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()); } 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; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void SieveOfEratosthenes(int n, boolean prime[]) { prime[0] = false; 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 void dfs(int root, boolean[] vis, int[] value, ArrayList[] gr, int prev) { vis[root] = true; value[root] = 3 - prev; prev = 3 - prev; for (int i = 0; i < gr[root].size(); i++) { int next = (int) gr[root].get(i); if (!vis[next]) dfs(next, vis, value, gr, prev); } } static boolean isPrime(int n) { for (int i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } static boolean isPrime(long n) { for (long i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } static int abs(int a) { return a > 0 ? a : -a; } static int max(int a, int b) { return a > b ? a : b; } static int min(int a, int b) { return a < b ? a : b; } static long pow(long n, long m) { if (m == 0) return 1; long temp = pow(n, m / 2); long res = ((temp * temp) % mod); if (m % 2 == 0) return res; return (res * n) % mod; } static long modular_add(long a, long b) { return ((a % mod) + (b % mod)) % mod; } static long modular_sub(long a, long b) { return ((a % mod) - (b % mod) + mod) % mod; } static long modular_mult(long a, long b) { return ((a % mod) * (b % mod)) % mod; } static long lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static int gcd(int n1, int n2) { if (n2 != 0) return gcd(n2, n1 % n2); else return n1; } static class Pair { int u, v; Pair(int u, int v) { this.u = u; this.v = v; } static void sort(Pair[] coll) { List<Pair> al = new ArrayList<>(Arrays.asList(coll)); Collections.sort(al, new Comparator<Pair>() { public int compare(Pair p1, Pair p2) { return p1.u - p2.u; } }); for (int i = 0; i < al.size(); i++) { coll[i] = al.get(i); } } } static void sort(int[] a) { ArrayList<Integer> list = new ArrayList<>(); for (int i : a) list.add(i); Collections.sort(list); for (int i = 0; i < a.length; i++) a[i] = list.get(i); } static void sort(long a[]) { ArrayList<Long> list = new ArrayList<>(); for (long i : a) list.add(i); Collections.sort(list); for (int i = 0; i < a.length; i++) a[i] = list.get(i); } static int[] array(int n, int value) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = value; return a; } static long sum(long a[]) { long sum = 0; for (long i : a) sum += i; return (sum); } static long count(long a[], long x) { long c = 0; for (long i : a) if (i == x) c++; return (c); } static int sum(int a[]) { int sum = 0; for (int i : a) sum += i; return (sum); } static int count(int a[], int x) { int c = 0; for (int i : a) if (i == x) c++; return (c); } static int count(String s, char ch) { int c = 0; char x[] = s.toCharArray(); for (char i : x) if (ch == i) c++; return (c); } static int[] freq(int a[], int n) { int f[] = new int[n + 1]; for (int i : a) f[i]++; return f; } static int[] pos(int a[], int n) { int f[] = new int[n + 1]; for (int i = 0; i < n; i++) f[a[i]] = i; return f; } static boolean isPalindrome(String s) { StringBuilder sb = new StringBuilder(); sb.append(s); String str = String.valueOf(sb.reverse()); if (s.equals(str)) return true; else return false; } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
bc203b562b3225f737e8f9be9ee98035
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; public class D { public static void main(String[] args) { int[] nums = new int[200000]; Stack<Integer> stack = new Stack<>(); Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); for (int i = 0; i < n; i++) nums[i] = sc.nextInt(); for (int i = n-1; i >= 0; i--) stack.push(nums[i]); Arrays.sort(nums, 0, n); int idx = 0; while (!stack.isEmpty()) { if (stack.peek() == nums[idx]) { stack.pop(); idx++; continue; } if (stack.size() % 2 == 0) { int a = stack.pop(); if (stack.peek() == nums[idx]) { stack.pop(); stack.push(a); idx++; continue; } stack.push(a); } break; } if (stack.isEmpty()) { System.out.println("YES"); } else System.out.println("NO"); stack.clear(); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
008aa3c4952b04219d410ac6811b8c9a
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
//package codeforce.div3.r786; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; import java.util.StringTokenizer; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.ArrayList; import java.util.List; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.HashSet; import static java.lang.System.out; import static java.util.stream.Collectors.joining; /** * @author pribic (Priyank Doshi) * @see <a href="https://codeforces.com/contest/1674/problem/D" target="_top">https://codeforces.com/contest/1674/problem/D</a> * @since 02/05/22 8:52 PM */ public class D { static FastScanner sc = new FastScanner(System.in); public static void main(String[] args) { try (PrintWriter out = new PrintWriter(System.out)) { int T = sc.nextInt(); for (int tt = 1; tt <= T; tt++) { int n = sc.nextInt(); int[] arr = new int[n]; int min = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); min = Math.min(min, arr[i]); } boolean sort = true; for (int i = arr.length % 2; i + 1 < arr.length; i += 2) { if (arr[i] > arr[i + 1]) { int t = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = t; } } for (int i = 0; i < arr.length - 1; i++) { if (arr[i] > arr[i + 1]) sort = false; } System.out.println(sort ? "YES" : "NO"); } } } static 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), 32768); } 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()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
d61a63d030f68231a27d150e236c6528
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class codeMaster { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tc = sc.nextInt(); while(tc-->0){ int n = sc.nextInt(); Integer[] arr = sc.nextIntegerArray(n); Integer[] copy = arr.clone(); Arrays.sort(copy); for(int i = n - 1; i>0; i-=2){ if(arr[i - 1] > arr[i]){ int temp = arr[i]; arr[i] = arr[i - 1]; arr[i - 1] = temp; } } boolean ans = true; for(int i = 0; i<n; i++)if(!Objects.equals(arr[i], copy[i])){ans = false; break;} if(ans)pw.println("YES"); else pw.println("NO"); } pw.flush(); } static class SegmentTree{ long[] tree; int N; public SegmentTree(long[] arr){ N = arr.length; tree = new long[2*N - 1]; build(tree, arr); } public void build(long[] tree, long[] arr){ for(int i = N-1, j = 0; i<tree.length; i++, j++)tree[i] = arr[j]; for(int i = tree.length - 1, j = i - 1, k = N-2; k>=0; i -= 2, j-= 2, k--){ tree[k] = tree[i] + tree[j]; } } public void update(int idx, int val){ tree[idx + N - 2] = val; boolean f = true; int i = idx + N - 2; int j = i - 1; if(i % 2 != 0){ i++; j++; } for(int k = (tree.length - N - 1) - ((tree.length - 1 - i)/2); k>=0; ){ tree[k] = tree[i] + tree[j]; f = !f; i = k; j = k - 1; if(k % 2 != 0){ i++; j++; } k = (tree.length - N - 1) - ((tree.length - 1 - i)/2); } } } public static boolean isSorted(Long[] arr){ boolean f = true; for(int i = 1; i<arr.length; i++){ if(arr[i] < arr[i - 1]){ f = false; break; } } return f; } public static int binary_Search(long key, Long[] arr, int start){ int low = start; int high = arr.length; int mid = (low + high) / 2; while(low < high){ mid = (low + high) / 2; if(arr[mid] == key)break; else if(arr[mid] > key){ high = mid; }else{ low = mid; } } return mid; } public static int differences(int n, int test){ int changes = 0; while(test > 0){ if(test % 10 != n % 10)changes++; test/=10; n/=10; } return changes; } static int maxSubArraySum(int a[], int size) { int max_so_far = Integer.MIN_VALUE, max_ending_here = 0,start = 0, end = 0, s = 0; for (int i = 0; i < size; i++) { max_ending_here += a[i]; if (max_so_far < max_ending_here) { max_so_far = max_ending_here; start = s; end = i; } if (max_ending_here < 0) { max_ending_here = 0; s = i + 1; } } return start; } static int maxSubArraySum2(int a[], int size) { int max_so_far = Integer.MIN_VALUE, max_ending_here = 0,start = 0, end = 0, s = 0; for (int i = 0; i < size; i++) { max_ending_here += a[i]; if (max_so_far < max_ending_here) { max_so_far = max_ending_here; start = s; end = i; } if (max_ending_here < 0) { max_ending_here = 0; s = i + 1; } } return end; } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static class Pair implements Comparable<Pair>{ int x; int y; public Pair(int x, int y){ this.x = x; this.y = y; } public int compareTo(Pair x){ if(this.y - x.y != 0) return this.y - x.y; else return this.x - x.x; } public String toString(){ return "("+this.x + ", " + this.y + ")"; } } public static boolean isSubsequence(char[] arr, String s){ boolean ans = false; for(int i = 0, j = 0; i<arr.length; i++){ if(arr[i] == s.charAt(j)){ j++; } if(j == s.length()){ ans = true; break; } } return ans; } public static void sortIdx(long[]a,long[]idx) { mergesortidx(a, idx, 0, a.length-1); } static void mergesortidx(long[] arr,long[]idx,int b,int e) { if(b<e) { int m=b+(e-b)/2; mergesortidx(arr,idx,b,m); mergesortidx(arr,idx,m+1,e); mergeidx(arr,idx,b,m,e); } return; } static void mergeidx(long[] arr,long[]idx,int b,int m,int e) { int len1=m-b+1,len2=e-m; long[] l=new long[len1]; long[] lidx=new long[len1]; long[] r=new long[len2]; long[] ridx=new long[len2]; for(int i=0;i<len1;i++) { l[i]=arr[b+i]; lidx[i]=idx[b+i]; } for(int i=0;i<len2;i++) { r[i]=arr[m+1+i]; ridx[i]=idx[m+1+i]; } int i=0,j=0,k=b; while(i<len1 && j<len2) { if(l[i]<=r[j]) { arr[k++]=l[i++]; idx[k-1]=lidx[i-1]; } else { arr[k++]=r[j++]; idx[k-1]=ridx[j-1]; } } while(i<len1) { idx[k]=lidx[i]; arr[k++]=l[i++]; } while(j<len2) { idx[k]=ridx[j]; arr[k++]=r[j++]; } return; } 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(); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
bf567d0f58c8da48aeb50ce4937d76e5
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; public class Main { 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; } int[] readArray(int n) { int[] ar = new int[n]; for (int i = 0; i < n; i++) ar[i] = nextInt(); return ar; } long[] readArray2(int n) { long[] ar = new long[n]; for (int i = 0; i < n; i++) ar[i] = nextLong(); return ar; } } static class DSU { int[] parent; int[] rank; int[] sz; public DSU(int N) { parent = new int[N]; for (int i = 0; i < N; ++i) parent[i] = i; rank = new int[N]; sz = new int[N]; Arrays.fill(sz, 1); } public int find(int x) { if (parent[x] != x) parent[x] = find(parent[x]); return parent[x]; } public void union(int x, int y) { int xr = find(x), yr = find(y); if (xr == yr) return; if (rank[xr] < rank[yr]) { int tmp = yr; yr = xr; xr = tmp; } if (rank[xr] == rank[yr]) rank[xr]++; parent[yr] = xr; sz[xr] += sz[yr]; } public int size(int x) { return sz[find(x)]; } public int top() { return size(sz.length - 1) - 1; } } static class Pair{ int x; int y; char d,pd; public Pair(int x,int y,char d,char pd){ this.x=x;this.y=y;this.d=d;this.pd=pd; } public String toString(){ return x+" "+y; } } static class Query{ int a,b,c,d; public Query(int a,int b,int c,int d){ this.a=a+1;this.b=b+1;this.c=c+1;this.d=d+1; } } static class Recruit{ long cost; long power; public Recruit(long cost,long power){ this.cost=cost;this.power=power; } } static class SegmentTree { int[] segmentTree; public SegmentTree(int n) { segmentTree=new int[4*n]; segmentTree[0]=0; buildSegmentTree(0,n-1,1); } private void buildSegmentTree(int start,int end,int treeIndex) { if(start==end) { segmentTree[treeIndex]=0; return; } int mid=(start+end)/2; buildSegmentTree(start,mid,2*treeIndex); buildSegmentTree(mid+1,end,2*treeIndex+1); segmentTree[treeIndex]=segmentTree[2*treeIndex]+segmentTree[2*treeIndex+1]; } private void updateSegmentTree(int start,int end,int treeIndex, int index,int val) { if(start==end) { segmentTree[treeIndex]=val; return; } int mid=(start+end)/2; if(index<=mid) updateSegmentTree(start,mid,2*treeIndex,index,val); else updateSegmentTree(mid+1,end,2*treeIndex+1,index,val); segmentTree[treeIndex]=segmentTree[2*treeIndex]+segmentTree[2*treeIndex+1]; } private int findSumRangeSegmentTree(int start,int end,int treeIndex, int left,int right) { if(start>=left&&end<=right) return segmentTree[treeIndex]; if(start>right||end<left) return 0; if(start==end) return 0; int mid=(start+end)/2; return findSumRangeSegmentTree(start,mid,2*treeIndex,left,right)+ findSumRangeSegmentTree(mid+1,end,2*treeIndex+1,left,right); } } static class Next{ int even,odd; public Next(){ even=odd=-1; } } private static long addArray(long[] a){ long sum=0; for(long b:a) sum+=b; return sum; } private static long addArray(int[] a){ long sum=0; for(long b:a) sum+=b; return sum; } private static void printAllPerm(int n){ List<Integer> val=new ArrayList<>(); for(int i=1;i<=n;i++) val.add(i); printAllPerm(val,n,new ArrayList<>()); } private static void printAllPerm(List<Integer> val,int n,List<Integer> current){ if(current.size()==n){ //System.out.print(current); return; } for(int j=0;j<val.size();j++){ current.add(val.remove(j)); printAllPerm(val,n,current); val.add(j,current.remove(current.size()-1)); } } private static MyScanner sc; private static SegmentTree st; public static void main(String[] args) { sc=new MyScanner(); int T=sc.nextInt(); for(int i=0;i<T;i++){ solve(i+1); } // solve(0); } private static void solve(int test) { int n=sc.nextInt(); int[] a=sc.readArray(n); for(int i=n-2;i>=0;i-=2){ if(a[i]>a[i+1]) swap(a,i,i+1); } for(int i=1;i<n;i++){ if(a[i]<a[i-1]){ System.out.println("NO"); return; } } System.out.println("YES"); } private static void swap(int[] a,int i,int j){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
527496e80c4900aacfbb6c0eac3cdcce
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) throws FileNotFoundException { FastScanner fs = new FastScanner(); int tt = fs.nextInt(); while(tt-- > 0) { int n = fs.nextInt(); int[] a = fs.readArray(n); boolean flag = true; for(int i = 0; i < a.length; i++) { if(n%2 == 0) { if(i+1 < a.length && a[i+1] < a[i]) { // System.out.println(a[i] + " " + a[i+1]); int temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } n--; }else { n--; } } // System.out.println(Arrays.toString(a)); for(int i = 0; i < a.length-1; i++) { if(a[i+1]<a[i]) { flag = false; break; } } if(flag) { System.out.println("YES"); }else { System.out.println("NO"); } } } public static int gcd(int a, int b) { if(b == 0) return a; return gcd(b, a%b); } 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\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
9129ddf78cdd731eb781c4a40104e1b2
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; //import javafx.util.*; public class Main { static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static int INF = Integer.MAX_VALUE; static int NINF = Integer.MIN_VALUE; static long mod = 1000000007l; public static void main (String[] args) throws java.lang.Exception { //check if you have to take product or the constraints are big int T = i(); while(T-- > 0){ int n = i(); int[] arr = input(n); boolean flag = true; int[] copy = arr.clone(); sort(copy); int x = n%2; for(int i = 0 + x;i < n - 1 + x;i++){ if(arr[i] == copy[i] && arr[i + 1] == copy[i + 1]){ i++; continue; } if(arr[i] == copy[i + 1] && arr[i + 1] == copy[i]){ i++; continue; } flag = false; break; } out.println(flag ? "YES" : "NO"); } out.close(); } public static void sort(int[] arr){ ArrayList<Integer> ls = new ArrayList<>(); for(int x : arr){ ls.add(x); } Collections.sort(ls); for(int i = 0;i < arr.length;i++){ arr[i] = ls.get(i); } } public static void reverse(int[] arr){ int n = arr.length; for(int i = 0;i < n/2;i++){ int temp = arr[i]; arr[i] = arr[n-1-i]; arr[n-1-i] = temp; } } public static void sort(long[] arr){ 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); } } public static void reverse(long[] arr){ int n = arr.length; for(int i = 0;i < n/2;i++){ long temp = arr[i]; arr[i] = arr[n-1-i]; arr[n-1-i] = temp; } } public static void print(int[] arr){ int n = arr.length; for(int i = 0;i < n;i++){ out.print(arr[i] + " "); } out.println(); } public static void print(ArrayList<Integer> arr){ int n = arr.size(); for(int i = 0;i < n;i++){ out.print(arr.get(i) + " "); } out.println(); } 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; } } class pair implements Comparable<pair> { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } // public int hashCode() { // return new Integer(x).hashCode() * 31 + new Integer(y).hashCode(); // } public int compareTo(pair other) { return Integer.compare(this.y,other.y); } } 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; } } // in.nextLine().toCharArray();
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
3ee010512c7ad1275a32a4bf0908085a
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
//package Codeforces; import java.io.*; import java.util.*; public class D { 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 == 1) { op.append("YES\n"); continue; } int[] choices = new int[n]; int i1 = 0, i2 = n-1; for (int i = n-1 ; i >= 1 ; i -= 2, ++i1, --i2) { choices[i1] = arr[i]; choices[i2] = arr[i-1]; } // System.out.println(Arrays.toString(choices)); ArrayList<Integer> list = new ArrayList<>(n); if(n % 2 == 0) { int right = n >> 1; int left = right - 1; while (right < n && left >= 0) { if(choices[left] > choices[right]) { list.add(choices[right]); list.add(choices[left]); } else { list.add(choices[left]); list.add(choices[right]); } --left; ++right; } } else { list.add(arr[0]); int right = (n >> 1) + 1; int left = right - 2; while (right < n && left >= 0) { if(choices[left] > choices[right]) { list.add(choices[right]); list.add(choices[left]); } else { list.add(choices[left]); list.add(choices[right]); } --left; ++right; } } boolean yes = true; for (int i = 1 ; i < n ; i++) { int num1 = list.get(i); int num0 = list.get(i-1); if(num0 > num1) { yes = false; break; } } // System.out.println(list); if(yes) { op.append("YES\n"); } else { op.append("NO\n"); } } System.out.println(op); // END OF CODE } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
cf41633ab5c12b697cf198a9efef455a
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class ABCSort { private static final int START_TEST_CASE = 1; public static void solveCase(FastIO io, int testCase) { final int N = io.nextInt(); final int[] A = io.nextIntArray(N); for (int i = N - 3; i >= 0; i -= 2) { int currMax = A[i]; if (i - 1 >= 0) { currMax = Math.max(currMax, A[i - 1]); } int prevMin = Math.min(A[i + 1], A[i + 2]); if (currMax > prevMin) { io.println("NO"); return; } } io.println("YES"); } public static void solve(FastIO io) { final int T = io.nextInt(); for (int t = 0; t < T; ++t) { solveCase(io, START_TEST_CASE + t); } } public static class FastIO { private InputStream reader; private PrintWriter writer; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastIO(InputStream r, OutputStream w) { reader = r; writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w))); } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = reader.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } // TODO: read this byte-by-byte like the other read functions. public double nextDouble() { return Double.parseDouble(nextString()); } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printlnArray(int[] arr) { printArray(arr); writer.println(); } public void printlnArray(long[] arr) { printArray(arr); writer.println(); } public void printf(String format, Object... args) { print(String.format(format, args)); } public void flush() { writer.flush(); } } public static void main(String[] args) { FastIO io = new FastIO(System.in, System.out); solve(io); io.flush(); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
4912f1e384864a84dfffd726eec84be6
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class ABCSort { private static final int START_TEST_CASE = 1; public static void solveCase(FastIO io, int testCase) { final int N = io.nextInt(); final int[] A = io.nextIntArray(N); for (int i = N - 1; i - 1 >= 0; i -= 2) { if (A[i] < A[i - 1]) { swap(A, i, i - 1); } } for (int i = 1; i < N; ++i) { if (A[i] < A[i - 1]) { io.println("NO"); return; } } io.println("YES"); } private static void swap(int[] A, int i, int j) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; } public static void solve(FastIO io) { final int T = io.nextInt(); for (int t = 0; t < T; ++t) { solveCase(io, START_TEST_CASE + t); } } public static class FastIO { private InputStream reader; private PrintWriter writer; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastIO(InputStream r, OutputStream w) { reader = r; writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w))); } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = reader.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } // TODO: read this byte-by-byte like the other read functions. public double nextDouble() { return Double.parseDouble(nextString()); } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printlnArray(int[] arr) { printArray(arr); writer.println(); } public void printlnArray(long[] arr) { printArray(arr); writer.println(); } public void printf(String format, Object... args) { print(String.format(format, args)); } public void flush() { writer.flush(); } } public static void main(String[] args) { FastIO io = new FastIO(System.in, System.out); solve(io); io.flush(); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
67663f3fa853ef45487575d3dd86311a
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class Main { private static void solve(int n, int[] arr){ if(n <= 2){ out.println("YES"); return; } for(int i = n - 2; i >= 0; i-=2){ if (arr[i] > arr[i+1]){ int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } } for(int i = 1; i < n; i++){ if(arr[i] < arr[i-1]){ out.println("NO"); return; } } out.println("YES"); } public static void main(String[] args){ MyScanner scanner = new MyScanner(); int testCount = scanner.nextInt(); for(int testIdx = 1; testIdx <= testCount; testIdx++){ int n = scanner.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = scanner.nextInt(); } solve(n, arr); } out.close(); } public static void printResult(int idx, long res){ out.println("Case #" + idx + ": " + res); } static void print1DArray(int[] arr){ for(int i = 0; i < arr.length; i++){ out.print(arr[i]); if(i < arr.length - 1){ out.print(' '); } } out.print('\n'); } static void print1DArrayList(List<Integer> arrayList){ for(int i = 0; i < arrayList.size(); i++){ out.print(arrayList.get(i)); if(i < arrayList.size() - 1){ out.print(' '); } } out.print('\n'); } public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
740e9a1421b0193dd252fceb6a194745
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.Arrays; import java.util.Scanner; import java.util.Stack; /** * https://codeforces.com/problemset/problem/1674/D * D. A-B-C Sort * */ public class Main { static int N = 2 * 100010; static int[] a = new int[N]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { solve(sc); } } static void solve(Scanner sc) { int n = sc.nextInt(); for(int i = 1; i <= n; i++) { a[i] = sc.nextInt(); } Stack<int[]> stk = new Stack(); for(int i = n; i > 0; i -= 2) { if(!stk.isEmpty() && Math.min(stk.peek()[0], stk.peek()[1]) < Math.max(a[i], a[i - 1])) { System.out.println("No"); return ; } stk.push(new int[]{a[i], a[i - 1]}); } System.out.println("Yes"); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
ce5b2748beef67e14b4c428eef4c70c3
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; public class Solution { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } for(int i=n-1;i>0;i-=2) { if(arr[i]<arr[i-1]) { int temp=arr[i]; arr[i]=arr[i-1]; arr[i-1]=temp; } } if(isSorted(arr)) System.out.println("Yes"); else System.out.println("No"); } } static boolean isSorted(int c[]) { for(int i=0;i<c.length-1;i++) { if(c[i]>c[i+1]) return false; } return true; } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
e1a93e9c25cb94bc723ecbd51471d836
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class D_A_B_C_Sort { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); // try { // out = new PrintWriter("output.txt"); // } catch (Exception e) { // e.printStackTrace(); // } int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int[] arr = in.readIntArray(n); if (n <= 2) { out.println("YES"); continue; } boolean even = true, sort = true; int mid = arr[n-1], right = -1, left = -1, sec_mid = arr[n-1]; for (int i = n-1; i >= 0; i--) { // out.println(mid + " " + arr[i] + " " + even); if (even) { if (mid < arr[i]) { sort = false; break; } if (i != n-1) { left = mid; mid = arr[i]; right = sec_mid; } even = false; } else { if (right != -1 && (arr[i] > left)) { sort = false; break; } if (arr[i] >= mid) { sec_mid = arr[i]; } else { sec_mid = mid; mid = arr[i]; } even = true; } } if (sort) out.println("YES"); else out.println("NO"); } // keep track of middle element in b array // when len of b is odd we can place the incoming elem either left or right // it has to be smaller than left of mid elem as if it was greater than while putting in c it will be // removed first and then the left elem which was smaller (c should be ascending) // when it is even we have to place it in middle of both mids and it will be removed first in c array // hence incoming elem has to be smaller than both mids out.flush(); out.close(); } } // For fast input output class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { // br = new BufferedReader(new InputStreamReader(System.in)); try { br = new BufferedReader(new FileReader("input.txt")); } 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; } int[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } String[] readStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } } // end of fast i/o code
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
3614987a8d4eec1ccb029d43687ec0e2
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.Scanner; public class Solution { static private String solve(int[] arr) { int n = arr.length; int k = n-1; while(k > 0) { if(arr[k] < arr[k-1]) { swap(arr, k, k-1); } k -= 2; } k = 0; while(k <= n-2) { if(arr[k] > arr[k+1]) { return "NO"; } k++; } return "YES"; } static private void swap(int[] arr, int i, int j) { int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } static public void main(String[] str) { 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]; for(int j = 0; j < n; j++) { arr[j] = sc.nextInt(); } System.out.println(solve(arr)); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
041925e457eb79d2453e7908c214c9bb
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; /* */ public class D{ static FastReader sc=null; public static void main(String[] args) { sc=new FastReader(); int t=sc.nextInt(); for(int tt=0;tt<t;tt++) { int n=sc.nextInt(); int a[]=sc.readArray(n); for(int i=n-2;i>=0;i-=2) { if(a[i]>a[i+1]) { int temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } } if(isSorted(a))System.out.println("YES"); else System.out.println("NO"); } } static boolean isSorted(int a[]) { for(int i=0;i+1<a.length;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\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
46102ffe5111dab71f70f42d69ce6089
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; public class ABCSort { 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(); for(int i = n-1; i >= 0; i -= 2) { if(i-1 >= 0 && a[i] < a[i-1]) { int temp = a[i]; a[i] = a[i-1]; a[i-1] = temp; } } boolean sorted = true; for(int i = 0; i < n - 1; ++i) { if(a[i] > a[i+1]) sorted = false; } System.out.println((sorted) ? "YES": "NO"); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
25384c48812d8339fffec0d655b15bbc
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; /** * ABCSort */ public class ABCSort { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-- > 0){ int n = s.nextInt(); Integer[] a = new Integer[n]; for (int i=0; i<n; i++){ a[i] = s.nextInt(); } int i=n%2; for(; i<n; i+=2){ if (a[i] > a[i+1]){ Integer x = a[i]; a[i] = a[i+1]; a[i+1] = x; } } boolean isSort = true; for(int j=1; j<n; j++){ if (a[j-1] > a[j]){ isSort = false; break; } } if (isSort){ System.out.println("YES"); } else { System.out.println("NO"); } } s.close(); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
2b7ec69d849a50d06b73779970e71e74
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
//import java.io.IOException; import java.io.*; import java.util.*; public class ABCSort { static InputReader inputReader=new InputReader(System.in); static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static void solve() throws IOException { int n=inputReader.nextInt(); int arr[]=new int[n]; int sorted[]=new int[n]; for (int i=0;i<n;i++) { arr[i]=inputReader.nextInt(); sorted[i]=arr[i]; } Arrays.sort(sorted); int start=0; int sortedind=0; if (n%2!=0) { if (arr[0]!=sorted[0]) { out.println("NO"); return; } sortedind++; start=1; } PriorityQueue<Integer>priorityQueue=new PriorityQueue<>(); for (int i=start;i<n;i+=2) { for (int j=i;j<i+2&j<n;j++) { priorityQueue.add(arr[j]); } for (int j=start;j<start+2&j<n;j++) { if (sorted[sortedind]!=priorityQueue.peek()) { out.println("NO"); return; } priorityQueue.poll(); sortedind++; } } out.println("YES"); } static PrintWriter out=new PrintWriter((System.out)); static void SortDec(long arr[]) { List<Long>list=new ArrayList<>(); for(long ele:arr) { list.add(ele); } Collections.sort(list,Collections.reverseOrder()); for (int i=0;i<list.size();i++) { arr[i]=list.get(i); } } static void Sort(long arr[]) { List<Long>list=new ArrayList<>(); for(long ele:arr) { list.add(ele); } Collections.sort(list); for (int i=0;i<list.size();i++) { arr[i]=list.get(i); } } public static void main(String args[])throws IOException { int t=inputReader.nextInt(); while (t-->0) { solve(); } long s = System.currentTimeMillis(); // out.println(System.currentTimeMillis()-s+"ms"); out.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { 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 = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
2e40f5ac873d3381c06242125951106f
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class ProblemD { static ArrayList <Integer>tree[]; static int count= 0; static char color[]; static boolean vis[]; public static void main(String[] args) throws Exception{ StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int test = Integer.parseInt(br.readLine()); while(test-->0){ int n = Integer.parseInt(br.readLine()); String line[] = br.readLine().split(" "); int arr[] = new int[n]; for(int i =0;i<n;i++)arr[i] = Integer.parseInt(line[i]); int res[] = new int[n]; int z = 0; for(int i = n-2;i>=0;i-=2){ res[z++] = arr[i]; } for(int i = (n%2 == 0? 1:0);i<n;i+=2){ res[z++] = arr[i]; } boolean ok = true; sort(arr); if(n%2==1){ if(res[n/2] != arr[0])ok = false; } int l =-1,r=-1; if(ok){ if(n%2 == 0){ l = (n/2)-1; r = n/2; }else{ l = (n/2)-1; r = (n/2)+1; } z = n%2 == 0? 0:1; while(l>=0 && r<n){ ArrayList<Integer>list = new ArrayList<>(); list.add(res[l]); list.add(res[r]); Collections.sort(list); if(list.get(0) != arr[z] || (z+1<n && list.get(1) != arr[z+1])){ ok = false; break; } z+=2; l--; r++; } } if(ok)sb.append("YES"); else sb.append("NO"); sb.append("\n"); } System.out.println(sb); } public static void sort(int arr[]){ ArrayList<Integer> list = new ArrayList<>(); for(int it:arr)list.add(it); Collections.sort(list); for(int i = 0;i<list.size();i++){ arr[i] = list.get(i); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
2f499871073c4e6d0b90eba629047faa
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc=new FastReader(); // static long dp[][]; // static boolean v[][][]; // static int mod=998244353;; // static int mod=1000000007; static long oset[]; static int oset_p; static long mod=1000000007; // static int max; static int bit[]; //static long fact[]; static HashMap<Long,Long> map; //static StringBuffer sb=new StringBuffer(""); //static HashMap<Integer,Integer> map; static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) { // StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); outer :while (ttt-- > 0) { int n=i(); long a[]=inputL(n); int r=0; if(n==1) { out.println("YES"); continue outer; } else { long max=0; if(n%2==0) { r=0; max=0;} else { r=1; max=a[0]; } while(r<n) { long mx,mn; if(r<n-1) { mx=max(a[r],a[r+1]);mn=min(a[r],a[r+1]);} else { mx=a[r];mn=a[r];} if(max>mn) { out.println("NO"); continue outer; } r+=2; max=mx; } } out.println("YES"); } out.close(); } static class Pair implements Comparable<Pair> { int x; int y; // int z; Pair(int x,int y){ this.x=x; this.y=y; // this.z=z; } @Override public int compareTo(Pair o) { if(this.x>o.x) return 1; else if(this.x<o.x) return -1; else { if(this.y>o.y) return 1; else if(this.y<o.y) return -1; else return 0; } } public int hashCode() { final int temp = 14; int ans = 1; ans =x*31+y*13; return ans; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null) { return false; } if (this.getClass() != o.getClass()) { return false; } Pair other = (Pair)o; if (this.x != other.x || this.y!=other.y) { return false; } return true; } // /* FOR TREE MAP PAIR USE */ // public int compareTo(Pair o) { // if (x > o.x) { // return 1; // } // if (x < o.x) { // return -1; // } // if (y > o.y) { // return 1; // } // if (y < o.y) { // return -1; // } // return 0; // } } static int find(int A[],int a) { if(A[a]==a) return a; return A[a]=find(A, A[a]); } //static int find(int A[],int a) { // if(A[a]==a) // return a; // return find(A, A[a]); //} //FENWICK TREE static void update(int i, int x){ for(; i < bit.length; i += (i&-i)) bit[i] += x; } static int sum(int i){ int ans = 0; for(; i > 0; i -= (i&-i)) ans += bit[i]; return ans; } //END static long summation(long A[],int si,int ei) { long ans=0; for(int i=si;i<=ei;i++) ans+=A[i]; return ans; } static void add(long v) { if(!map.containsKey(v)) { map.put(v, (long)1); } else { map.put(v, map.get(v)+(long)1); } } static void remove(long v) { if(map.containsKey(v)) { map.put(v, map.get(v)-(long)1); if(map.get(v)==0) map.remove(v); } } public static int upper(long A[],long k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { ans=mid; l=mid+1; } else { u=mid-1; } } return ans; } public static int lower(long A[],long k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { l=mid+1; } else { ans=mid; u=mid-1; } } return ans; } static int[] copy(int A[]) { int B[]=new int[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long[] copy(long A[]) { long B[]=new long[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void reverse(long A[]) { int n=A.length; long B[]=new long[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void reverse(int A[]) { int n=A.length; int B[]=new int[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static long[][] input(int n,int m){ long A[][]=new long[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=l(); } } return A; } static char[][] charinput(int n,int m){ char A[][]=new char[n][m]; for(int i=0;i<n;i++) { String s=s(); for(int j=0;j<m;j++) { A[i][j]=s.charAt(j); } } return A; } static int nextPowerOf2(int n) { if(n==0) return 1; n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } static int highestPowerof2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static long highestPowerof2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long [] prefix(long A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] prefix(int A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] suffix(long A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static long [] suffix(int A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static void fill(int dp[]) { Arrays.fill(dp, -1); } static void fill(int dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(int dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(int dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static void fill(long dp[]) { Arrays.fill(dp, -1); } static void fill(long dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(long dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(long dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long power(long x, long y, long p) { if(y==0) return 1; if(x==0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } static void print(int A[]) { for(int i : A) { out.print(i+" "); } out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static HashMap<Integer,Long> hash(int A[]){ HashMap<Integer,Long> map=new HashMap<Integer, Long>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+(long)1); } else { map.put(i, (long)1); } } return map; } static HashMap<Long,Long> hash(long A[]){ HashMap<Long,Long> map=new HashMap<Long, Long>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+(long)1); } else { map.put(i, (long)1); } } return map; } static TreeMap<Integer,Integer> tree(int A[]){ TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Long,Integer> tree(long A[]){ TreeMap<Long,Integer> map=new TreeMap<Long, Integer>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static 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\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
8071929682d1aa284f19af22a3e07e12
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
/* * way to red */ 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 CF786B { public static void main(String AC[]) 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()); // long N = Long.parseLong(st.nextToken()); int N = Integer.parseInt(st.nextToken()); int[] arr = readArr(N, infile, st); // String s = String.valueOf(st.nextToken()); if(N%2==0) { for(int i=0;i<N-1;i+=2) { if(arr[i]>arr[i+1]) { swap(arr,i,i+1); } } }else { for(int i=1;i<N-1;i+=2) { if(arr[i]>arr[i+1]) { swap(arr,i,i+1); } } } boolean flag = true; for(int i=0;i<N-1;i++) { if(arr[i]>arr[i+1]) { flag = false; break; } } if(flag) sb.append("YES").append('\n'); else sb.append("NO").append('\n'); } out.print(sb); out.flush(); //BufferedReader infile = new BufferedReader(new FileReader("input.txt")); //System.setOut(new PrintStream(new File("output.txt"))); } private static void swap(int[] arr, int i, int j) { // TODO Auto-generated method stub int temp = arr[i]; arr[i]= arr[j]; arr[j]= temp; } static final int mod = 1_000_000_007; 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 long[] readArr2(int N, BufferedReader infile, StringTokenizer st) throws Exception { long[] arr = new long[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Long.parseLong(st.nextToken()); return arr; } public static void print(int[] arr) { //for debugging only for(int x: arr) out.print(x+" "); out.println(); } //input shenanigans /* Random stuff to try when stuck: -if it's 2C then it's dp -for combo/probability problems, expand the given form we're interested in -make everything the same then build an answer (constructive, make everything 0 then do something) -something appears in parts of 2 --> model as graph -assume a greedy then try to show why it works -find way to simplify into one variable if multiple exist -treat it like fmc (note any passing thoughts/algo that could be used so you can revisit them) -find lower and upper bounds on answer -figure out what ur trying to find and isolate it -see what observations you have and come up with more continuations -work backwards (in constructive, go from the goal to the start) -turn into prefix/suffix sum argument (often works if problem revolves around adjacent array elements) -instead of solving for answer, try solving for complement (ex, find n-(min) instead of max) -draw something -simulate a process -dont implement something unless if ur fairly confident its correct -after 3 bad submissions move on to next problem if applicable -do something instead of nothing and stay organized -write stuff down Random stuff to check when wa: -if code is way too long/cancer then reassess -switched N/M -int overflow -switched variables -wrong MOD -hardcoded edge case incorrectly Random stuff to check when tle: -continue instead of break -condition in for/while loop bad Random stuff to check when rte: -switched N/M -long to int/int overflow -division by 0 -edge case for empty list/data structure/N=1 */ public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static long gcd(long a, long b) { if(a > b) a = (a+b)-(b=a); if(a == 0L) return b; return gcd(b%a, a); } public static long totient(long n) { long result = n; for (int p = 2; p*p <= n; ++p) if (n % p == 0) { while(n%p == 0) n /= p; result -= result/p; } if (n > 1) result -= result/n; return result; /* find phi(i) from 1 to N fast O(N*loglogN) long[] arr = new long[N+1]; for(int i=1; i <= N; i++) arr[i] = i; for(int v=2; v <= N; v++) if(arr[v] == v) for(int a=v; a <= N; a+=v) arr[a] -= arr[a]/v; */ } public static ArrayList<Integer> findDiv(int N) { //gens all divisors of N ArrayList<Integer> ls1 = new ArrayList<Integer>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++) if(N%i == 0) { ls1.add(i); ls2.add(N/i); } Collections.reverse(ls2); for(int b: ls2) if(b != ls1.get(ls1.size()-1)) ls1.add(b); return ls1; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } //custom multiset (replace with HashMap if needed) public static void push(TreeMap<Integer, Integer> map, int k, int v) { //map[k] += v; if(!map.containsKey(k)) map.put(k, v); else map.put(k, map.get(k)+v); } public static void pull(TreeMap<Integer, Integer> map, int k, int v) { //assumes map[k] >= v //map[k] -= v int lol = map.get(k); if(lol == v) map.remove(k); else map.put(k, lol-v); } public static int[] compress(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int boof = 1; //min value for(int x: ls) if(!map.containsKey(x)) map.put(x, boof++); int[] brr = new int[arr.length]; for(int i=0; i < arr.length; i++) brr[i] = map.get(arr[i]); return brr; } public static long[][] multiply(long[][] left, long[][] right) { long MOD = 1000000007L; int N = left.length; int M = right[0].length; long[][] res = new long[N][M]; for(int a=0; a < N; a++) for(int b=0; b < M; b++) for(int c=0; c < left[0].length; c++) { res[a][b] += (left[a][c]*right[c][b])%MOD; if(res[a][b] >= MOD) res[a][b] -= MOD; } return res; } public static long[][] power(long[][] grid, long pow) { long[][] res = new long[grid.length][grid[0].length]; for(int i=0; i < res.length; i++) res[i][i] = 1L; long[][] curr = grid.clone(); while(pow > 0) { if((pow&1L) == 1L) res = multiply(curr, res); pow >>= 1; curr = multiply(curr, curr); } return res; } /* * Data Structures */ class DSU { public int[] dsu; public int[] size; public DSU(int N) { dsu = new int[N+1]; size = new int[N+1]; for(int i=0; i <= N; i++) { dsu[i] = i; size[i] = 1; } } //with path compression, no find by rank public int find(int x) { return dsu[x] == x ? x : (dsu[x] = find(dsu[x])); } public void merge(int x, int y) { int fx = find(x); int fy = find(y); dsu[fx] = fy; } public void merge(int x, int y, boolean sized) { int fx = find(x); int fy = find(y); size[fy] += size[fx]; dsu[fx] = fy; } } class FenwickTree { //Binary Indexed Tree //1 indexed public int[] tree; public int size; public FenwickTree(int size) { this.size = size; tree = new int[size+5]; } public void add(int i, int v) { while(i <= size) { tree[i] += v; i += i&-i; } } public int find(int i) { int res = 0; while(i >= 1) { res += tree[i]; i -= i&-i; } return res; } public int find(int l, int r) { return find(r)-find(l-1); } } class SegmentTree { //Tlatoani's segment tree //iterative implementation = low constant runtime factor //range query, non lazy final int[] val; final int treeFrom; final int length; public SegmentTree(int treeFrom, int treeTo) { this.treeFrom = treeFrom; int length = treeTo - treeFrom + 1; int l; for (l = 0; (1 << l) < length; l++); val = new int[1 << (l + 1)]; this.length = 1 << l; } public void update(int index, int delta) { //replaces value int node = index - treeFrom + length; val[node] = delta; for (node >>= 1; node > 0; node >>= 1) val[node] = comb(val[node << 1], val[(node << 1) + 1]); } public int query(int from, int to) { //inclusive bounds if (to < from) return 0; //0 or 1? from += length - treeFrom; to += length - treeFrom + 1; //0 or 1? int res = 0; for (; from + (from & -from) <= to; from += from & -from) res = comb(res, val[from / (from & -from)]); for (; to - (to & -to) >= from; to -= to & -to) res = comb(res, val[(to - (to & -to)) / (to & -to)]); return res; } public int comb(int a, int b) { //change this return Math.max(a,b); } } class LazySegTree { //definitions private int NULL = -1; private int[] tree; private int[] lazy; private int length; public LazySegTree(int N) { length = N; int b; for(b=0; (1<<b) < length; b++); tree = new int[1<<(b+1)]; lazy = new int[1<<(b+1)]; } public int query(int left, int right) { //left and right are 0-indexed return get(1, 0, length-1, left, right); } private int get(int v, int currL, int currR, int L, int R) { if(L > R) return NULL; if(L <= currL && currR <= R) return tree[v]; propagate(v); int mid = (currL+currR)/2; return comb(get(v*2, currL, mid, L, Math.min(R, mid)), get(v*2+1, mid+1, currR, Math.max(L, mid+1), R)); } public void update(int left, int right, int delta) { add(1, 0, length-1, left, right, delta); } private void add(int v, int currL, int currR, int L, int R, int delta) { if(L > R) return; if(currL == L && currR == R) { //exact covering tree[v] += delta; lazy[v] += delta; return; } propagate(v); int mid = (currL+currR)/2; add(v*2, currL, mid, L, Math.min(R, mid), delta); add(v*2+1, mid+1, currR, Math.max(L, mid+1), R, delta); tree[v] = comb(tree[v*2], tree[v*2+1]); } private void propagate(int v) { //tree[v] already has lazy[v] if(lazy[v] == 0) return; tree[v*2] += lazy[v]; lazy[v*2] += lazy[v]; tree[v*2+1] += lazy[v]; lazy[v*2+1] += lazy[v]; lazy[v] = 0; } private int comb(int a, int b) { return Math.max(a,b); } } class RangeBit { //FenwickTree and RangeBit are faster than LazySegTree by constant factor final int[] value; final int[] weightedVal; public RangeBit(int treeTo) { value = new int[treeTo+2]; weightedVal = new int[treeTo+2]; } private void updateHelper(int index, int delta) { int weightedDelta = index*delta; for(int j = index; j < value.length; j += j & -j) { value[j] += delta; weightedVal[j] += weightedDelta; } } public void update(int from, int to, int delta) { updateHelper(from, delta); updateHelper(to + 1, -delta); } private int query(int to) { int res = 0; int weightedRes = 0; for (int j = to; j > 0; j -= j & -j) { res += value[j]; weightedRes += weightedVal[j]; } return ((to + 1)*res)-weightedRes; } public int query(int from, int to) { if (to < from) return 0; return query(to) - query(from - 1); } } class SparseTable { public int[] log; public int[][] table; public int N; public int K; public SparseTable(int N) { this.N = N; log = new int[N+2]; K = Integer.numberOfTrailingZeros(Integer.highestOneBit(N)); table = new int[N][K+1]; sparsywarsy(); } private void sparsywarsy() { log[1] = 0; for(int i=2; i <= N+1; i++) log[i] = log[i/2]+1; } public void lift(int[] arr) { int n = arr.length; for(int i=0; i < n; i++) table[i][0] = arr[i]; for(int j=1; j <= K; j++) for(int i=0; i + (1 << j) <= n; i++) table[i][j] = Math.min(table[i][j-1], table[i+(1 << (j - 1))][j-1]); } public int query(int L, int R) { //inclusive, 1 indexed L--; R--; int mexico = log[R-L+1]; return Math.min(table[L][mexico], table[R-(1 << mexico)+1][mexico]); } } class LCA { public int N, root; public ArrayDeque<Integer>[] edges; private int[] enter; private int[] exit; private int LOG = 17; //change this private int[][] dp; public LCA(int n, ArrayDeque<Integer>[] edges, int r) { N = n; root = r; enter = new int[N+1]; exit = new int[N+1]; dp = new int[N+1][LOG]; this.edges = edges; int[] time = new int[1]; //change to iterative dfs if N is large dfs(root, 0, time); dp[root][0] = 1; for(int b=1; b < LOG; b++) for(int v=1; v <= N; v++) dp[v][b] = dp[dp[v][b-1]][b-1]; } private void dfs(int curr, int par, int[] time) { dp[curr][0] = par; enter[curr] = ++time[0]; for(int next: edges[curr]) if(next != par) dfs(next, curr, time); exit[curr] = ++time[0]; } public int lca(int x, int y) { if(isAnc(x, y)) return x; if(isAnc(y, x)) return y; int curr = x; for(int b=LOG-1; b >= 0; b--) { int temp = dp[curr][b]; if(!isAnc(temp, y)) curr = temp; } return dp[curr][0]; } private boolean isAnc(int anc, int curr) { return enter[anc] <= enter[curr] && exit[anc] >= exit[curr]; } } class BitSet { private int CONS = 62; //safe public long[] sets; public int size; public BitSet(int N) { size = N; if(N%CONS == 0) sets = new long[N/CONS]; else sets = new long[N/CONS+1]; } public void add(int i) { int dex = i/CONS; int thing = i%CONS; sets[dex] |= (1L << thing); } public int and(BitSet oth) { int boof = Math.min(sets.length, oth.sets.length); int res = 0; for(int i=0; i < boof; i++) res += Long.bitCount(sets[i] & oth.sets[i]); return res; } public int xor(BitSet oth) { int boof = Math.min(sets.length, oth.sets.length); int res = 0; for(int i=0; i < boof; i++) res += Long.bitCount(sets[i] ^ oth.sets[i]); return res; } } class MaxFlow { //Dinic with optimizations (see magic array in dfs function) public int N, source, sink; public ArrayList<Edge>[] edges; private int[] depth; public MaxFlow(int n, int x, int y) { N = n; source = x; sink = y; edges = new ArrayList[N+1]; for(int i=0; i <= N; i++) edges[i] = new ArrayList<Edge>(); depth = new int[N+1]; } public void addEdge(int from, int to, long cap) { Edge forward = new Edge(from, to, cap); Edge backward = new Edge(to, from, 0L); forward.residual = backward; backward.residual = forward; edges[from].add(forward); edges[to].add(backward); } public long mfmc() { long res = 0L; int[] magic = new int[N+1]; while(assignDepths()) { long flow = dfs(source, Long.MAX_VALUE/2, magic); while(flow > 0) { res += flow; flow = dfs(source, Long.MAX_VALUE/2, magic); } magic = new int[N+1]; } return res; } private boolean assignDepths() { Arrays.fill(depth, -69); ArrayDeque<Integer> q = new ArrayDeque<Integer>(); q.add(source); depth[source] = 0; while(q.size() > 0) { int curr = q.poll(); for(Edge e: edges[curr]) if(e.capacityLeft() > 0 && depth[e.to] == -69) { depth[e.to] = depth[curr]+1; q.add(e.to); } } return depth[sink] != -69; } private long dfs(int curr, long bottleneck, int[] magic) { if(curr == sink) return bottleneck; for(; magic[curr] < edges[curr].size(); magic[curr]++) { Edge e = edges[curr].get(magic[curr]); if(e.capacityLeft() > 0 && depth[e.to]-depth[curr] == 1) { long val = dfs(e.to, Math.min(bottleneck, e.capacityLeft()), magic); if(val > 0) { e.augment(val); return val; } } } return 0L; //no flow } private class Edge { public int from, to; public long flow, capacity; public Edge residual; public Edge(int f, int t, long cap) { from = f; to = t; capacity = cap; } public long capacityLeft() { return capacity-flow; } public void augment(long val) { flow += val; residual.flow -= val; } } } class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
471d1620ceb986c93997200dffca1f98
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class Main { public static int INF = 0x3f3f3f3f; public static int mod = 1000000007; public static int mod9 = 998244353; public static void main(String args[]){ try { PrintWriter o = new PrintWriter(System.out); boolean multiTest = true; // init if(multiTest) { int t = fReader.nextInt(), loop = 0; while (loop < t) {loop++;solve(o);} } else solve(o); o.close(); } catch (Exception e) {e.printStackTrace();} } static void solve(PrintWriter o){ try { int n = fReader.nextInt(); int[] a = new int[n]; int min = Integer.MAX_VALUE; for(int i=0;i<n;i++) { a[i] = fReader.nextInt(); min = Math.min(min, a[i]); } boolean ok = true; if((n & 1) == 1){ for(int i=1;i<n-1;i+=2){ if(a[i] > a[i+1]){ int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } } for(int i=0;i<n-1;i++){ if(a[i] > a[i+1]){ ok = false; break; } } } else { for(int i=0;i<n-1;i+=2){ if(a[i] > a[i+1]){ int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } } for(int i=0;i<n-1;i++){ if(a[i] > a[i+1]){ ok = false; break; } } } if(ok) o.println("YES"); else o.println("NO"); } catch (Exception e){e.printStackTrace();} } public static int upper_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) <= val) l = mid + 1; else r = mid; } return l; } public static int lower_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) < val) l = mid + 1; else r = mid; } return l; } public static long gcd(long a, long b){ return b == 0 ? a : gcd(b, a%b); } public static void reverse(int[] array){ reverse(array, 0 , array.length-1); } public static void reverse(int[] array, int left, int right) { if (array != null) { int i = left; for(int j = right; j > i; ++i) { int tmp = array[j]; array[j] = array[i]; array[i] = tmp; --j; } } } public static long qpow(long a, long n){ long ret = 1l; while(n > 0){ if((n & 1) == 1) ret = ret * a; n >>= 1; a = a * a; } return ret; } public static class unionFind { int[] parent; int[] size; int n; public unionFind(int n){ this.n = n; parent = new int[n]; size = new int[n]; for(int i=1;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int p){ while(p != parent[p]){ parent[p] = parent[parent[p]]; p = parent[p]; } return p; } public void union(int p, int q){ int root_p = find(p); int root_q = find(q); if(root_p == root_q) return; if(size[root_p] >= size[root_q]){ parent[root_q] = root_p; size[root_p] += size[root_q]; size[root_q] = 0; } else{ parent[root_p] = root_q; size[root_q] += size[root_p]; size[root_p] = 0; } n--; } public int getCount(){ return n; } public int[] getSize(){ return size; } } public static class BIT { int[] tree; int n; public BIT(int n){ this.n = n; tree = new int[n+1]; } public void add(int x, int val){ while(x <= n){ tree[x] += val; x += lowBit(x); } } public int query(int x){ int ret = 0; while(x > 0){ ret += tree[x]; x -= lowBit(x); } return ret; } public int lowBit(int x) { return x&-x; } } public static class fReader { private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tokenizer = new StringTokenizer(""); private static String next() throws IOException{ while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());} return tokenizer.nextToken(); } public static int nextInt() throws IOException {return Integer.parseInt(next());} public static Long nextLong() throws IOException {return Long.parseLong(next());} public static double nextDouble() throws IOException {return Double.parseDouble(next());} public static char nextChar() throws IOException {return next().toCharArray()[0];} public static String nextString() throws IOException {return next();} public static String nextLine() throws IOException {return reader.readLine();} } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
29779d89ff7c1eb77a8b131b3e3841d5
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import static java.lang.Math.*; import java.util.*; import java.io.*; public class x1674D { public static void main(String args[]) 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); for(int i=N-2; i >= 0; i-=2){ if(arr[i] > arr[i+1]){ int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } } boolean inc = true; for(int i=1; i < N; i++) if(arr[i-1] > arr[i]) inc = false; if(inc) sb.append("YES\n"); else sb.append("NO\n"); } System.out.print(sb); } 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; } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
6ccef0b5832d9300b481fb6a11011696
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import static java.lang.Math.*; import java.util.*; import java.io.*; public class x1674D { public static void main(String args[]) 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); for(int i=N-2; i >= 0; i-=2){ if(arr[i] > arr[i+1]){ int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } } boolean inc = true; for(int i=1; i < N; i++) if(arr[i-1] > arr[i]) inc = false; if(inc) sb.append("YES\n"); else sb.append("NO\n"); } System.out.print(sb); } 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; } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
4afd6f4502db130aa148d981d4d8de4a
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
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.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.StringTokenizer; public class CodeForces { static int mod = (int)1e9+7; public static void main(String[] args) throws InterruptedException { // PrintWriter out = new PrintWriter("output.txt"); // File input = new File("input.txt"); // FastScanner fs = new FastScanner(input); FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int testNumber =fs.nextInt(); for (int T =0;T<testNumber;T++){ int n = fs.nextInt(); int []arr= fs.readArray(n); int []sorted = Arrays.copyOf(arr, n); sort(sorted); boolean res =true; if (n%2==1){ if(arr[0]!=sorted[0])res=false; for (int i=1;i<n;i+=2){ if((arr[i]==sorted[i]&&arr[i+1]==sorted[i+1])||(arr[i]==sorted[i+1]&&arr[i+1]==sorted[i])){ } else res=false; } } else { for (int i=0;i<n;i+=2){ if((arr[i]==sorted[i]&&arr[i+1]==sorted[i+1])||(arr[i]==sorted[i+1]&&arr[i+1]==sorted[i])){ } else res=false; } } out.print(res?"YES\n":"NO\n"); } out.flush(); } static long modInverse( long n, long p) { return FastPower(n, p - 2, p); } static int[] factorials(int max,int mod){ int [] ans = new int[max+1]; ans[0]=1; for (int i=1;i<=max;i++){ ans[i]=ans[i-1]*i; ans[i]%=mod; } return ans; } static String toBinary(int num,int bits){ String res =Integer.toBinaryString(num); while(res.length()<bits)res="0"+res; return res; } static String toBinary(long num,int bits){ String res =Long.toBinaryString(bits); while(res.length()<bits)res="0"+res; return res; } static long LCM(long a,long b){ return a*b/gcd(a,b); } static long FastPower(long x,long p,long mod){ if(p==0)return 1; long ans =FastPower(x, p/2,mod); ans%=mod; ans*=ans; ans%=mod; if(p%2==1)ans*=x; ans%=mod; return ans; } static double FastPower(double x,int p){ if(p==0)return 1.0; double ans =FastPower(x, p/2); ans*=ans; if(p%2==1)ans*=x; return ans; } static int FastPowerInt(int x,int p){ if(p==0)return 1; int ans =FastPowerInt(x, p/2); ans*=ans; if(p%2==1)ans*=x; return ans; } static long FastPower(long x,long p){ if(p==0)return 1; long ans =FastPower(x, p/2); ans*=ans; if(p%2==1)ans*=x; return ans; } 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()); } 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; } 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 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; } static int binarySearchSmallerOrEqual(long arr[], long 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; } public static int binarySearchStrictlySmaller(long[] arr, long 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; } 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; } int[] copy (int [] arr , int start){ int[] res = new int[arr.length-start]; for (int i=start;i<arr.length;i++)res[i-start]=arr[i]; return res; } static int[] swap(int [] A,int l,int r){ int[] B=new int[A.length]; for (int i=0;i<l;i++){ B[i]=A[i]; } int k=0; for (int i=r;i>=l;i--){ B[l+k]=A[i]; k++; } for (int i=r+1;i<A.length;i++){ B[i]=A[i]; } return B; } static int mex (int[] d){ int [] a = Arrays.copyOf(d, d.length); sort(a); if(a[0]!=0)return 0; int ans=1; for(int i=1;i<a.length;i++){ if(a[i]==a[i-1])continue; if(a[i]==a[i-1]+1)ans++; else break; } return ans; } static int[] mexes(int[] arr){ int[] freq = new int [100000+7]; for (int i:arr)freq[i]++; int maxMex =0; for (int i=0;i<=100000+7;i++){ if(freq[i]!=0)maxMex++; else break; } int []ans = new int[arr.length]; ans[arr.length-1] = maxMex; for (int i=arr.length-2;i>=0;i--){ freq[arr[i+1]]--; if(freq[arr[i+1]]<=0){ if(arr[i+1]<maxMex) maxMex=arr[i+1]; ans[i]=maxMex; } else { ans[i]=ans[i+1]; } } return ans; } static int [] freq (int[]arr,int max){ int []b = new int[max]; for (int i:arr)b[i]++; return b; } static int[] prefixSum(int[] arr){ int [] a = new int[arr.length]; a[0]=arr[0]; for (int i=1;i<arr.length;i++)a[i]=a[i-1]+arr[i]; return a; } static class Pair { int x; int y; int extra; public Pair(int x,int y){ this.x=x; this.y=y; } public Pair(int x,int y,int extra){ this.x=x; this.y=y; this.extra=extra; } // @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+(int)y*2; // } // // } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
bba465f419f14aa5596c4ff66ddc8b60
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Main implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } 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 boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main(), "Main", 1 << 26).start(); } public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = 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(); } for(int i=n-1;i>=1;i=i-2){ if(arr[i-1]>arr[i]){ long temp = arr[i-1]; arr[i-1]=arr[i]; arr[i]=temp; } } int isSor=1; for(int i=1;i<n;i++){ if(arr[i-1]>arr[i]){ isSor = 0; break; } } if(isSor == 0) w.println("NO"); else w.println("YES"); } w.close(); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
9d4dd702e400c10d6a71d43d28f20cfb
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
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(); int a[] = sc.nextIntArray(n); for (int i = n-1; i >= (n%2==0?0:1); i -= 2) { if (a[i] < a[i - 1]) { int tmp = a[i]; a[i] = a[i - 1]; a[i - 1] = tmp; } } boolean sorted = true; for (int i = 0; i < n-1; i++) { if (a[i] > a[i + 1]) sorted = false; } if (sorted) pw.println("YES"); else pw.println("NO"); } pw.flush(); } public static void sort(Pair[] in) { shuffle(in); Arrays.sort(in); } public static void shuffle(Pair[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); Pair tmp = in[i]; in[i] = in[idx]; in[idx] = tmp; } } static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return x - o.x; } public String toString() { return x + " " + y; } } static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } public static void display(char[][] a) { for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j]); } System.out.println(); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
b2676b8e7fc399612344f52778f55db7
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.text.StringCharacterIterator; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ /* Template created by Jabra Ram - @hack41 */ public class Solution { public static void main (String[] args) throws java.lang.Exception { // your code goes here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t-->0){ int n = Integer.parseInt(br.readLine()); // String [] str = br.readLine().split(" "); String [] array = br.readLine().split(" "); // int x = Integer.parseInt(str[0]); // int y = Integer.parseInt(str[1]); // int i; // int n = str.length(); int [] arr = new int[n]; int [] temp = new int[n]; for(int i=0;i<n;i++){ arr[i] = Integer.parseInt(array[i]); temp[i] = Integer.parseInt(array[i]); } boolean f = true; Arrays.sort(arr); for(int i=n;i>=0;i-=2){ if(i+1<n){ if(temp[i]==arr[i] && temp[i+1]==arr[i+1] || temp[i]==arr[i+1] && temp[i+1]==arr[i]){ continue; } else{ f = false; break; } } } if(f){ System.out.println("YES"); } else{ System.out.println("NO"); } } } public static int gcd(int a, int b){ if(b>a){ return gcd(b,a); } if(b==0) return a; return gcd(b, a%b); } public static long factMod(int n, int mod){ long ans = 1; for(long i=1;i<=n/2;i++){ ans = ans*i*i%mod; ans = ans%mod; } return ans; } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
c2bef742e249af5b0e90be4c34ae2217
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; // To show menu bar, simply press the ALT key. public class test { // static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static FastReader in ; static PrintWriter out ; static{ if (System.getProperty("ONLINE_JUDGE") == null) { // Try block to check for exceptions try { // Sets the Output Stream // to output.txt System.setOut(new PrintStream(new FileOutputStream("output.java"))); System.setIn(new FileInputStream("input.java")); } // Catch block to handle the exceptions catch (Exception e) { } } in = new FastReader(); out = new PrintWriter(System.out); } public static void main(String[] args) throws IOException { int t = in.ni(); while (t-- > 0 ){ int n = in.ni(); int[] arr = in.readIntArray(null, n); boolean result = true; for (int i = n-1; i >= 0; i -= 2){ if (i < 2) break; int big = arr[i] < arr[i-1] ? arr[i] : arr[i-1]; int small = arr[i-2]; if (i >= 3) small = arr[i-2] > arr[i-3] ? arr[i-2] : arr[i-3]; if ( big < small ){ result = false; break; } } if (result) out.println("YES"); else out.println("NO"); } out.flush(); } public static void solve(){ } public static int binarySearch(int[] arr, int x, int p, int hi){ int lo = 0, mid = (hi+lo)/2; while (hi - lo > 1){ mid = (hi+lo)/2; if ( arr[mid] + p*(mid+1) <= x ) lo = mid; else if (arr[mid] + p*(mid+1) > x ) hi = mid-1; } if (arr[hi] + p*(hi+1) <= x) return hi+1; else if (arr[lo] + p*(lo+1) <= x) return lo+1; return -1; } public boolean predicate(){ return true; } public static void printBinary(int n){ for (int i = 30; i >= 0; i--) out.print((n>>i)&1 ); out.println(); } public static boolean arrayContains(int[][] arr, int j, int element){ for (int i = 0; i < arr[j].length; i++){ if (arr[j][i] == element) return true; } return false; } public static class Trio { int a, b, c; public Trio(int a, int b, int c){ this.a = a; this.b = b; this.c = c; } } 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); } public static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } public static String reverseString(String input ) { StringBuilder str = new StringBuilder(""); for ( int i = input.length()-1; i >= 0; i-- ) str.append(input.charAt(i)); return str.toString(); } static class Compare implements Comparator<int[]> { //While primitives can not though //primitive type arrays can be passed as a generic parameter public int compare( int[] a, int[] b ){ if ( a[0] > b[0] ) return 2; else if ( a[0] < b[0] ) return -2; else return 0; } } static ArrayList<Integer> factors(int n) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(n); if ( n != 1) list.add(1); int sq = (int)Math.sqrt(n); if ( n%sq == 0 && sq != 1 ) list.add(sq); for ( int i = 2; i < sq; i++ ) if ( n%i == 0){ list.add(i); list.add(n/i); } return list; } static class Pair implements Comparable<Pair>{ int a; int b; //LinkedList<Pair> list = new LinkedList<>(); public Pair(int a, int b ){ this.a = a; this.b = b; } public int compareTo(Pair p){ if (this.a > p.a ) return 2; else if (this.a < p.a) return -2; else return 0; } public boolean equals(Object o){ if (!(o instanceof Pair)) return false; Pair curr = (Pair) o; if (curr.a == this.a) return true; // && curr.b == this.b else return false; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni(){ return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } public int[] readIntArray(int[] arr,int n){ if ( arr == null ) arr = new int[n]; for ( int i = 0; i < n; i++ ) arr[i] = ni(); return arr; } public long[] readLongArray(long[] arr, int n ){ if ( arr == null ) arr = new long[n]; for ( int i = 0; i < n; i++ ) arr[i] = nl(); return arr; } public String readLine() { // readLine method skips to next line String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
f8e9b4e86857dd09b71c94783ae07187
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class Main { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(); int[] arr = sc.na(n); int i = 0; if(n%2==1) i++; for(; i < n; i+=2) { if(arr[i] > arr[i+1]) { int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } } for(i = 1; i < n; i++) { if(arr[i] < arr[i-1]) { w.p("NO"); return; } } w.p("YES"); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
d0fee560ed49bf464237c34a768bde04
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class Main { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// static StringBuilder addInMiddle (StringBuilder sb, char ch) { int n = sb.length(); sb.insert(n/2, 1); return sb; } //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(); int[] arr = sc.na(n); ArrayDeque<Integer> q = new ArrayDeque<>(); for(int i = 0; i < n; i++) { if(i%2==0) { q.addLast(arr[i]); } else q.addFirst(arr[i]); } List<Integer> li = new ArrayList<>(q); int l = -1; int r = -1; boolean takeL = false; boolean takeR = false; if(n%2==0) { r = n/2; l = r-1; } else { l = n/2; r = l+1; takeL = true; } int ct = 0; int prev = -1; while(ct < n) { if(takeL) { if(prev > li.get(l)) { w.p("NO"); return; } prev = li.get(l); ct++; l--; takeL = false; } else if(takeR) { if(prev > li.get(r)) { w.p("NO"); return; } prev = li.get(r); ct++; r++; takeR = false; } else { if(li.get(l) > li.get(r)) { if(prev > li.get(r)) { w.p("NO"); return; } prev = li.get(r); ct++; r++; takeL = true; } else { if(prev > li.get(l)) { w.p("NO"); return; } prev = li.get(l); ct++; l--; takeR = true; } } } w.p("YES"); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
d4c4ae48c354b5b8dae6984ad5324d3b
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
//Author : -crazy_coder- import static java.lang.Math.*; import static java.lang.System.out; import java.io.*; import java.util.*; public class Main{ public static void readArray(int[] arr,int st,int length,BufferedReader br)throws Exception{String[] str=br.readLine().split(" "); for(int i=st;i<length;i++){arr[i]=Integer.parseInt(str[i]);}} public static void printArray(int[] arr){for(int i=0;i<arr.length;i++){out.print(arr[i]+" ");}out.println();} public static void primeUsingBits(BitSet bit,int n){bit.flip(0,n+1);bit.set(0,false);bit.set(1,false);for(int i=2;i*i<=n;i++){if(bit.get(i)){for(int j=i*i;j<=n;j+=i){bit.set(j,false);}}}} public static int Gcd(int a,int b){if(b==0)return a;return Gcd(b,a%b);} public static int Lcm(int a,int b){return a*b/Gcd(a,b);} //<---------------------------------------Main Function----------------------------------------> public static void main(String[] args)throws Exception{ BufferedReader br=new BufferedReader(new BufferedReader(new InputStreamReader(System.in))); int t=Integer.parseInt(br.readLine()); // int t=1; while(t-->0){ //write your code here int n=Integer.parseInt(br.readLine()); String[] str=br.readLine().split(" "); int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=Integer.parseInt(str[i]); } int i=0; if(n%2==1)i=1; for(;i<n-1;i++){ if(arr[i]>arr[i+1]){ int temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } i++; } boolean flag=true; for(i=0;i<n-1;i++){ if(arr[i]>arr[i+1]){flag=false;break;} } if(flag)out.println("YES"); else out.println("NO"); } } // <----------------------------------------XXXXXXXXXXXXXXXXXXXXX-----------------------------------------> ////////////////////////********fastModExpo********///////////////////////// public static long fastModExpo(long a,long n,long mod){ if(n==0)return 1; long ans=1; while(n>1){ if((n&1)==1){ ans=ans*a%mod; } a=a*a%mod; n>>=1; } ans=ans*a%mod; return ans; } // ****** next permutation================= public static int[] swap(int arr[], int start, int end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; return arr; } public static int[] reverse(int arr[], int start, int end) { while (start < end) { int temp = arr[start]; arr[start++] = arr[end]; arr[end--] = temp; } return arr; } public static boolean next_permutation(int arr[]) { if (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; int nextGreater = arr.length - 1; for (int i = arr.length - 1; i > last; i--) { if (arr[i] > arr[last]) { nextGreater = i; break; } } arr = swap(arr, nextGreater, last); arr = reverse(arr, last + 1, arr.length - 1); return true; } ////////////////////////////*******FastReader********////////////////////////////////// static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null||!st.hasMoreTokens()){ try{ st=new StringTokenizer(br.readLine()); }catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str=""; try{ str=br.readLine().trim(); }catch(Exception e){ e.printStackTrace(); } return str; } } ////////////////////////////*******FastWriter********////////////////////////////////// static class FastWriter { private final BufferedWriter bw; public FastWriter(){ this.bw=new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object)throws IOException{ bw.append(""+object); } public void println(Object object)throws IOException{ print(object); bw.append("\n"); } public void close()throws IOException{ bw.close(); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
e6d143f4b563202b1a390165015a0f71
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class D_A_B_C_Sort { static Scanner in = new Scanner(); static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans = new StringBuilder(); static int testCases, n, m; static long a[]; static void swap(long a[], int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void solve(int t) { long copyA[] = new long[n]; for(int i = 0; i < n; ++i) { copyA[i] = a[i]; } sort(copyA, 0, n - 1); for(int i = n - 1; i >= 1; i -= 2) { if(a[i - 1] > a[i]) { swap(a, i, i - 1); } } boolean equal = true; for(int i = 0; i < n; ++i) { if(a[i] != copyA[i]) { equal = false; break; } } if(equal) { ans.append("YES"); } else { ans.append("NO"); } if(t != testCases) { ans.append("\n"); } } public static void main(String[] priya) throws IOException { 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(t + 1); } out.print(ans.toString()); out.flush(); in.close(); } static boolean isSmaller(String str1, String str2) { int n1 = str1.length(), n2 = str2.length(); if (n1 < n2) { return true; } if (n2 < n1) { return false; } for (int i = 0; i < n1; i++) { if (str1.charAt(i) < str2.charAt(i)) { return true; } else if (str1.charAt(i) > str2.charAt(i)) { return false; } } return false; } static String sub(String str1, String str2) { if (isSmaller(str1, str2)) { String t = str1; str1 = str2; str2 = t; } String str = ""; int n1 = str1.length(), n2 = str2.length(); int diff = n1 - n2; int carry = 0; for (int i = n2 - 1; i >= 0; i--) { int sub = (((int) str1.charAt(i + diff) - (int) '0') - ((int) str2.charAt(i) - (int) '0') - carry); if (sub < 0) { sub = sub + 10; carry = 1; } else { carry = 0; } str += String.valueOf(sub); } for (int i = n1 - n2 - 1; i >= 0; i--) { if (str1.charAt(i) == '0' && carry > 0) { str += "9"; continue; } int sub = (((int) str1.charAt(i) - (int) '0') - carry); if (i > 0 || sub > 0) { str += String.valueOf(sub); } carry = 0; } return new StringBuilder(str).reverse().toString(); } static String sum(String str1, String str2) { if (str1.length() > str2.length()) { String t = str1; str1 = str2; str2 = t; } String str = ""; int n1 = str1.length(), n2 = str2.length(); int diff = n2 - n1; int carry = 0; for (int i = n1 - 1; i >= 0; i--) { int sum = ((int) (str1.charAt(i) - '0') + (int) (str2.charAt(i + diff) - '0') + carry); str += (char) (sum % 10 + '0'); carry = sum / 10; } for (int i = n2 - n1 - 1; i >= 0; i--) { int sum = ((int) (str2.charAt(i) - '0') + carry); str += (char) (sum % 10 + '0'); carry = sum / 10; } if (carry > 0) { str += (char) (carry + '0'); } return new StringBuilder(str).reverse().toString(); } static long detect_sum(int i, long a[], long sum) { if (i >= a.length) { return sum; } return detect_sum(i + 1, a, sum + a[i]); } static String mul(String num1, String num2) { int len1 = num1.length(); int len2 = num2.length(); if (len1 == 0 || len2 == 0) { return "0"; } int result[] = new int[len1 + len2]; int i_n1 = 0; int i_n2 = 0; for (int i = len1 - 1; i >= 0; i--) { int carry = 0; int n1 = num1.charAt(i) - '0'; i_n2 = 0; for (int j = len2 - 1; j >= 0; j--) { int n2 = num2.charAt(j) - '0'; int sum = n1 * n2 + result[i_n1 + i_n2] + carry; carry = sum / 10; result[i_n1 + i_n2] = sum % 10; i_n2++; } if (carry > 0) { result[i_n1 + i_n2] += carry; } i_n1++; } int i = result.length - 1; while (i >= 0 && result[i] == 0) { i--; } if (i == -1) { return "0"; } String s = ""; while (i >= 0) { s += (result[i--]); } return s; } static class Node<T> { T data; Node<T> next; public Node() { this.next = null; } public Node(T data) { this.data = data; this.next = null; } public T getData() { return data; } public void setData(T data) { this.data = data; } public Node<T> getNext() { return next; } public void setNext(Node<T> next) { this.next = next; } @Override public String toString() { return this.getData().toString() + " "; } } static class ArrayList<T> { Node<T> head, tail; int len; public ArrayList() { this.head = null; this.tail = null; this.len = 0; } int size() { return len; } boolean isEmpty() { return len == 0; } int indexOf(T data) { if (isEmpty()) { throw new ArrayIndexOutOfBoundsException(); } Node<T> temp = head; int index = -1, i = 0; while (temp != null) { if (temp.getData() == data) { index = i; } i++; temp = temp.getNext(); } return index; } void add(T data) { Node<T> newNode = new Node<>(data); if (isEmpty()) { head = newNode; tail = newNode; len++; } else { tail.setNext(newNode); tail = newNode; len++; } } void see() { if (isEmpty()) { throw new ArrayIndexOutOfBoundsException(); } Node<T> temp = head; while (temp != null) { out.print(temp.getData().toString() + " "); out.flush(); temp = temp.getNext(); } out.println(); out.flush(); } void inserFirst(T data) { Node<T> newNode = new Node<>(data); Node<T> temp = head; if (isEmpty()) { head = newNode; tail = newNode; len++; } else { newNode.setNext(temp); head = newNode; len++; } } T get(int index) { if (isEmpty() || index >= len) { throw new ArrayIndexOutOfBoundsException(); } Node<T> temp = head; int i = 0; T data = null; while (temp != null) { if (i == index) { data = temp.getData(); } i++; temp = temp.getNext(); } return data; } void addAt(T data, int index) { if (index >= len) { throw new ArrayIndexOutOfBoundsException(); } Node<T> newNode = new Node<>(data); int i = 0; Node<T> temp = head; while (temp.next != null) { if (i == index) { newNode.setNext(temp.next); temp.next = newNode; } i++; temp = temp.getNext(); } // temp.setNext(temp); len++; } void popFront() { if (isEmpty()) { throw new ArrayIndexOutOfBoundsException(); } if (head == tail) { head = null; tail = null; } else { head = head.getNext(); } len--; } void removeAt(int index) { if (index >= len) { throw new ArrayIndexOutOfBoundsException(); } if (index == 0) { this.popFront(); return; } Node<T> temp = head; int i = 0; Node<T> n = new Node<>(); while (temp != null) { if (i == index) { n.next = temp.next; temp.next = n; break; } i++; n = temp; temp = temp.getNext(); } tail = n; --len; } void clearAll() { this.head = null; this.tail = null; } } static void merge(long a[], int left, int right, int mid) { int n1 = mid - left + 1, n2 = right - mid; long L[] = new long[n1]; long R[] = new long[n2]; for (int i = 0; i < n1; i++) { L[i] = a[left + i]; } for (int i = 0; i < n2; i++) { R[i] = a[mid + 1 + i]; } int i = 0, j = 0, k1 = left; while (i < n1 && j < n2) { if (L[i] <= R[j]) { a[k1] = L[i]; i++; } else { a[k1] = R[j]; j++; } k1++; } while (i < n1) { a[k1] = L[i]; i++; k1++; } while (j < n2) { a[k1] = R[j]; j++; k1++; } } static void sort(long a[], int left, int right) { if (left >= right) { return; } int mid = (left + right) / 2; sort(a, left, mid); sort(a, mid + 1, right); merge(a, left, right, mid); } static class Scanner { BufferedReader in; StringTokenizer st; public Scanner() { in = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } String nextLine() throws IOException { return in.readLine(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } void close() throws IOException { in.close(); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
52babc7e9b609474c01297c8a440ab8a
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class Failed { static BufferedReader bf; static PrintWriter out; public static void main (String[] args)throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int t = nextInt(); while(t-->0){ solve(); } } public static void solve()throws IOException{ int n = nextInt(); int[]arr = nextIntArray(n); if(n <= 2){ println("YES"); return; } int left = arr[n-1]; int right = arr[n-2]; for(int i = n-3;i>=0;i--){ int leftCandidate = arr[i]; if(arr[i] > left || arr[i] > right){ println("NO"); return; } i--; if(i >=0 && (arr[i] > left || arr[i] > right)){ println("NO"); return; } if(i >= 0){ left = leftCandidate; right =arr[i]; } } println("YES"); } public static boolean isSorted(int[]arr){ for(int i =0;i<arr.length-1;i++){ if(arr[i]>arr[i+1])return false; } return true; } public static long gcd(long a,long b){ if(b == 0)return a; return gcd(b,a%b); } // code for input public static void print(String s ){ System.out.print(s); } public static void sort(long[]arr){ List<Long>list = new ArrayList<>(); for(int i =0;i<arr.length;i++){ list.add(arr[i]); } Collections.sort(list); for(int i =0;i<arr.length;i++){ arr[i] = list.get(i); } } public static void print(int num ){ System.out.print(num); } public static void print(long num ){ System.out.print(num); } public static void println(String s){ System.out.println(s); } public static void println(int num){ System.out.println(num); } public static void println(long num){ System.out.println(num); } public static void println(){ System.out.println(); } public static int toInt(String s){ return Integer.parseInt(s); } public static long toLong(String s){ return Long.parseLong(s); } public static String[] nextStringArray()throws IOException{ return bf.readLine().split(" "); } public static int nextInt()throws IOException{ return Integer.parseInt(bf.readLine()); } public static long nextLong()throws IOException{ return Long.parseLong(bf.readLine()); } public static String nextString()throws IOException{ return bf.readLine(); } public static int[] nextIntArray(int n)throws IOException{ String[]str = bf.readLine().split(" "); int[]arr = new int[n]; for(int i =0;i<n;i++){ arr[i] = Integer.parseInt(str[i]); } return arr; } public static long[] nextLongArray(int n)throws IOException{ String[]str = bf.readLine().split(" "); long[]arr = new long[n]; for(int i =0;i<n;i++){ arr[i] = Long.parseLong(str[i]); } return arr; } public static int[][] newIntMatrix(int r,int c)throws IOException{ int[][]arr = new int[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Integer.parseInt(str[j]); } } return arr; } public static long[][] newLongMatrix(int r,int c)throws IOException{ long[][]arr = new long[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Long.parseLong(str[j]); } } return arr; } } // if a problem is related to binary string it could also be related to parenthesis // try to use binary search in the question it might work // try sorting // try to think in opposite direction of question it might work in your way // if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+ or a,b,c,d in general // gcd(1.p1,2.p2,3.p3,4.p4....n.pn) cannot be greater than 2 it has been proveds
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
4173e73624e987e6d345bbe214d87195
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
/* Author:Farhan Shaikh */ 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 Pupil { static FastReader sc = new FastReader(); public static void main (String[] args) throws java.lang.Exception { // your code goes here int t=sc.nextInt(); while(t>0){ int n=sc.nextInt(); int arr[]=new int[n]; int brr[]=new int[n]; ai(arr,n); for(int i=0;i<n;i++) { brr[i]=arr[i]; } Arrays.sort(brr); if(n==1) { yes(); } else { boolean b=true; for(int i=n-1;i>=1;i=i-2) { if(arr[i-1]>arr[i]) { int temp=arr[i-1]; arr[i-1]=arr[i]; arr[i]=temp; } } for(int i=0;i<n;i++) { if(arr[i]!=brr[i]) { b=false; break; } } if(b) { yes(); } else { no(); } } t--; } } // FAST I/O 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 boolean two(int n)//power of two { if((n&(n-1))==0) { return true; } else{ return false; } } public static boolean isPrime(long n){ for (int i = 2; i * i <= n; ++i) { if (n % i == 0) { return false; } } return true; } public static int digit(int n) { int n1=(int)Math.floor((int)Math.log10(n)) + 1; return n1; } public static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } public static long highestPowerof2(long x) { // check for the set bits x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; // Then we remove all but the top bit by xor'ing the // string of 1's with that string of 1's shifted one to // the left, and we end up with just the one top bit // followed by 0's. return x ^ (x >> 1); } public static void yes() { System.out.println("YES"); return; } public static void no() { System.out.println("NO"); return ; } public static void al(long arr[],int n) { for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); } return ; } public static void ai(int arr[],int n) { for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } return ; } } //CAAL THE BELOW FUNCTION IF PARING PRIORITY IS NEEDED // // PriorityQueue<pair> pq = new PriorityQueue<>(); **********declare the syntax in the main function****** // pq.add(1,2)/////// // class pair implements Comparable<pair> { // int value, index; // pair(int v, int i) { index = i; value = v; } // @Override // public int compareTo(pair o) { return o.value - value; } // } // User defined Pair class // class Pair { // int x; // int y; // // Constructor // public Pair(int x, int y) // { // this.x = x; // this.y = y; // } // } // Arrays.sort(arr, new Comparator<Pair>() { // @Override public int compare(Pair p1, Pair p2) // { // return p1.x - p2.x; // } // }); // class Pair { // int height, id; // // public Pair(int i, int s) { // this.height = s; // this.id = i; // } // } //Arrays.sort(trips, (a, b) -> Integer.compare(a[1], b[1])); // ArrayList<ArrayList<Integer>> connections = new ArrayList<ArrayList<Integer>>(); // for(int i = 0; i<n;i++) // connections.add(new ArrayList<Integer>());
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
edc5906285e59508dca97538c37ec1c5
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class Main { 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"); // } Scanner in = new Scanner(System.in); int t = in.nextInt(); while(t-- > 0) { int n = in.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = in.nextInt(); } System.out.println(solve(arr)); } } static String solve(int[] nums) { int[] sort = new int[nums.length]; for(int i = 0; i < nums.length; i++) { sort[i] = nums[i]; } Arrays.sort(sort); if(nums.length % 2 == 1) { if(nums[0] != sort[0]) return "NO"; int i = 1; while(i < nums.length) { int[] n = new int[] {nums[i], nums[i+1]}; int[] s = new int[] {sort[i], sort[i+1]}; Arrays.sort(n); Arrays.sort(s); if(s[0] == n[0] && s[1] == n[1]) { i += 2; } else { return "NO"; } } return "YES"; } int i = 0; while(i < nums.length) { int[] n = new int[] {nums[i], nums[i+1]}; int[] s = new int[] {sort[i], sort[i+1]}; Arrays.sort(n); Arrays.sort(s); if(s[0] == n[0] && s[1] == n[1]) { i += 2; } else { return "NO"; } } return "YES"; } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
7e517f407330cfee3c05bc90ffcd5712
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.lang.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t= scanner.nextInt(); while(t!=0) { int n= scanner.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=scanner.nextInt(); } int arr1[]=arr.clone(); Arrays.sort(arr1); boolean ans=true; for(int i=n-1;i>0;i-=2) { if((arr[i]!=arr1[i] || arr[i-1]!=arr1[i-1])&&(arr[i]!=arr1[i-1] || arr[i-1]!=arr1[i])) { ans=false; break; } } if(n%2!=0 && arr[0]!=arr1[0]) { ans=false; } if(ans) { System.out.println("YES"); } else { System.out.println("NO"); } t--; } scanner.close(); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
3c2ec632a2020fe550ea3f4265318155
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class D { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = in.nextInt(); for (int tt = 0; tt < t; tt++) { ArrayDeque<Integer> dq = new ArrayDeque<>(); int n = in.nextInt(); for (int i = 0; i < n; i++) dq.add(in.nextInt()); if (solve(dq, n)) pw.println("YES"); else pw.println("NO"); } pw.close(); } static boolean solve(ArrayDeque<Integer> dq, int n) { if (dq.size() == 1) return true; ArrayDeque<Integer> sort = new ArrayDeque<>(); ArrayList<Integer> ls = new ArrayList<>(); for (int x: dq) ls.add(x); Collections.sort(ls); for (int x: ls) sort.add(x); // debug(sort); if (dq.size() % 2 != 0) { int v = dq.pollFirst(); int sV = sort.pollFirst(); if (v != sV) return false; } // debug(sort, dq); while (!sort.isEmpty()) { int sF = sort.pollLast(); int sS = sort.pollLast(); int f = dq.pollLast(); int s = dq.pollLast(); if (Math.min(sF, sS) != Math.min(f, s)) return false; if (Math.max(sF, sS) != Math.max(f, s)) return false; } return true; } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
37b21d402fcd0fb8495890d6eb2e6d50
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class Main implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); public static void main(String[] args) { new Thread(null, new Main(), "", 256 * (1L << 20)).start(); } public void run() { try { long t1 = System.currentTimeMillis(); if (System.getProperty("ONLINE_JUDGE") != null) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } Locale.setDefault(Locale.US); solve(); in.close(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = " + (t2 - t1)); } catch (Throwable t) { t.printStackTrace(System.err); System.exit(-1); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } int[] readIntArray(int n) throws IOException { int [] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = readInt(); } return a; } long[] readLongArray(int n) throws IOException { long [] a = new long[n]; for(int i = 0; i < n; i++) { a[i] = readLong(); } return a; } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } void solveTest() throws IOException { int n = readInt(); long[] a = readLongArray(n); long last = 0; int index = 0; if (n%2==1) { last = a[0]; index++; } for(;index<n;index+=2) { int first = index; int second = index + 1; long smaller = Math.min(a[first], a[second]); long larger = Math.max(a[first], a[second]); if(smaller < last) { out.println("NO"); return; } last = larger; } out.println("YES"); } void solve() throws IOException { int numTests = readInt(); while(numTests-->0) { solveTest(); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
d97a71535e3d8a2e2826b9f5b7b93268
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
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 Pranay */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DABCSort solver = new DABCSort(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class DABCSort { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); int[] a = in.readArray(n); int st = n % 2; for (int i = st; i < n - 1; i += 2) { if (a[i + 1] < a[i]) { int tmp = a[i]; a[i] = a[i + 1]; a[i + 1] = tmp; } } for (int i = 0; i < n - 1; ++i) { if (a[i + 1] < a[i]) { out.println("NO"); return; } } out.println("YES"); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] readArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
c8aa04209192409ae93a9bf399c4a8d5
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); Loop : for(int T=Integer.parseInt(br.readLine()); T-->0;) { int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int min = n%2==0 ? 0 : Integer.parseInt(st.nextToken()); for(int i=min == 0 ? 0 : 1; i<n; i+=2) { int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); if(Math.min(a, b) < min) { bw.write("NO\n"); continue Loop; }else min = Math.max(a, b); } bw.write("YES\n"); } bw.close(); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
e77f5dd0b61730e9b49e2d0ab462c8c6
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; import java.io.*; //import java.math.BigInteger; public class code{ public static class Pair{ int a; int b; Pair(int i,int j){ a=i; b=j; } } public static int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } public 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; } } public static void shuffle(long 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 long x = a[t]; a[t] = a[i]; a[i] = x; } } public static PrintWriter out = new PrintWriter(System.out); public static long[][] dp; //@SuppressWarnings("unchecked") public static void main(String[] arg) throws IOException{ //Reader in=new Reader(); //PrintWriter out = new PrintWriter(System.out); //Scanner in = new Scanner(System.in); FastScanner in=new FastScanner(); int t=in.nextInt(); while(t-- > 0){ int n=in.nextInt(); int[] a=new int[n]; int[] b=new int[n]; for(int i=0;i<n;i++){ a[i]=in.nextInt(); b[i]=a[i]; } if(n==1){ out.println("YES"); continue; } shuffle(b, n); Arrays.sort(b); int i=0,j=n-1; boolean flag=true; for(i=n-1;i>0;i-=2){ int a1=a[i]; int a2=a[i-1]; int b1=b[i]; int b2=b[i-1]; if(!((a1==b1 && a2==b2) || (a1==b2 && a2==b1))){ //out.println(a1+" "+a2+" "+b1+" "+b2); flag=false; break; } } if(flag) out.println("YES"); else out.println("NO"); } out.flush(); } } class Fenwick{ int[] bit; public Fenwick(int n){ bit=new int[n]; //int sum=0; } public void update(int index,int val){ index++; for(;index<bit.length;index += index&(-index)) bit[index]+=val; } public int presum(int index){ int sum=0; for(;index>0;index-=index&(-index)) sum+=bit[index]; return sum; } public int sum(int l,int r){ return presum(r+1)-presum(l); } } class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
843cc6332657fc3209762ae71d980633
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class D { public static void main(String[] args)throws IOException { FastScanner scan = new FastScanner(); PrintWriter output = new PrintWriter(System.out); int t = scan.nextInt(); for(int tt = 0;tt<t;tt++) { int n = scan.nextInt(); int arr[] = scan.readArray(n); int sorted[] = arr.clone(); sort(sorted); for(int i = n-1;i>0;i-=2) { if(arr[i] < arr[i-1]) { int temp = arr[i]; arr[i] = arr[i-1]; arr[i-1] = temp; } } if(equal(arr, sorted)) output.println("YES"); else output.println("NO"); } output.flush(); } public static boolean equal(int[] arr, int[] sorted) { int n = arr.length; for(int i = 0;i<n;i++) { if(arr[i] != sorted[i]) return false; } return true; } public static int[] sort(int arr[]) { List<Integer> list = new ArrayList<>(); for(int i:arr) list.add(i); Collections.sort(list); for(int i = 0;i<list.size();i++) arr[i] = list.get(i); return arr; } public static int gcd(int a, int b) { if(a == 0) return b; return gcd(b%a, a); } public static void printArray(int arr[]) { for(int i:arr) System.out.print(i+" "); System.out.println(); } 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\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
14e8ac27c94323ff1b085681bbdf4e4c
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
// observation import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void swap(int a[], int x, int y){ int temp = a[x]; a[x] = a[y]; a[y] = temp; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); while(t-- > 0) { Map<Integer, Integer> map = new HashMap<>(); List<Integer> al = new ArrayList<>(); int n = sc.nextInt(); int a[] = new int[n]; for(int i = 0; i < n; i++) { a[i] = sc.nextInt(); } if(n%2 != 0) { for(int i = 1; i < n; i+=2) { if(a[i+1] < a[i]) swap(a, i, i+1); } } else { for(int i = 0; i < n; i+=2) { if(a[i+1] < a[i]) swap(a, i, i+1); } } boolean sorted = true; for(int i = 1; i < n; i++) { if(a[i] < a[i-1]) { sorted = false; } } if(sorted) sb.append("YES\n"); else sb.append("NO\n"); } System.out.println(sb); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
7adda6cd7a51b99d3fc8fe49dddf3ed5
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
// "static void main" must be defined in a public class. import java.io.*; import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int[] a = new int[n]; for(int i=0; i<n; i++){ a[i] = sc.nextInt(); } if(n%2==0){ for(int i=1; i<n; i+=2){ if(a[i]<a[i-1]){ int temp = a[i]; a[i]=a[i-1]; a[i-1]=temp; } } boolean flag = true; for(int i=1; i<n; i++){ if(a[i]<a[i-1]){ flag = false; break; } } if(flag){ System.out.println("YES"); } else{ System.out.println("NO"); } } else{ for(int i=2; i<n; i+=2){ if(a[i]<a[i-1]){ int temp = a[i]; a[i]=a[i-1]; a[i-1]=temp; } } boolean flag = true; for(int i=1; i<n; i++){ if(a[i]<a[i-1]){ flag = false; break; } } if(flag){ System.out.println("YES"); } else{ System.out.println("NO"); } } } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
26e9046dd6e21634caebaf3b72b734e5
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class CodeForces { /*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/ public static void solve(int tCase) throws IOException { int n = sc.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++)arr[i] = sc.nextInt(); for(int i=n%2;i<n-1;i+=2){ if(arr[i] > arr[i+1]){ int t = arr[i]; arr[i] = arr[i+1]; arr[i+1] = t; } } for(int i=1;i<n;i++){ if(arr[i] < arr[i-1]){ out.println("NO");return; } } out.println("YES"); } public static void main(String[] args) throws IOException { openIO(); int testCase = 1; testCase = sc.nextInt(); for (int i = 1; i <= testCase; i++) solve(i); closeIO(); } /*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/ /*--------------------------------------HELPER FUNCTIONS STARTS HERE-----------------------------------------*/ public static int mod = (int) 1e9 + 7; // public static int mod = 998244353; public static int inf_int = (int) 2e9; public static long inf_long = (long) 2e18; public static void _sort(int[] arr, boolean isAscending) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } public static void _sort(long[] arr, boolean isAscending) { int n = arr.length; List<Long> list = new ArrayList<>(); for (long ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // time : O(1), space : O(1) public static int _digitCount(long num,int base){ // this will give the # of digits needed for a number num in format : base return (int)(1 + Math.log(num)/Math.log(base)); } // time : O(n), space: O(n) public static long _fact(int n){ // simple factorial calculator long ans = 1; for(int i=2;i<=n;i++) ans = ans * i % mod; return ans; } // time for pre-computation of factorial and inverse-factorial table : O(nlog(mod)) public static long[] factorial , inverseFact; public static void _ncr_precompute(int n){ factorial = new long[n+1]; inverseFact = new long[n+1]; factorial[0] = inverseFact[0] = 1; for (int i = 1; i <=n; i++) { factorial[i] = (factorial[i - 1] * i) % mod; inverseFact[i] = _modExpo(factorial[i], mod - 2); } } // time of factorial calculation after pre-computation is O(1) public static int _ncr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod); } public static int _npr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[n - r] % mod); } // euclidean algorithm time O(max (loga ,logb)) public static long _gcd(long a, long b) { while (a>0){ long x = a; a = b % a; b = x; } return b; // if (a == 0) // return b; // return _gcd(b % a, a); } // lcm(a,b) * gcd(a,b) = a * b public static long _lcm(long a, long b) { return (a / _gcd(a, b)) * b; } // binary exponentiation time O(logn) public static long _modExpo(long x, long n) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans *= x; ans %= mod; n--; } else { x *= x; x %= mod; n >>= 1; } } return ans; } // function to find a/b under modulo mod. time : O(logn) public static long _modInv(long a,long b){ return (a * _modExpo(b,mod-2)) % mod; } //sieve or first divisor time : O(mx * log ( log (mx) ) ) public static int[] _seive(int mx){ int[] firstDivisor = new int[mx+1]; for(int i=0;i<=mx;i++)firstDivisor[i] = i; for(int i=2;i*i<=mx;i++) if(firstDivisor[i] == i) for(int j = i*i;j<=mx;j+=i) if(firstDivisor[j]==j)firstDivisor[j] = i; return firstDivisor; } // check if x is a prime # of not. time : O( n ^ 1/2 ) private static boolean _isPrime(long x){ for(long i=2;i*i<=x;i++) if(x%i==0)return false; return true; } static class Pair<K, V>{ K ff; V ss; public Pair(K ff, V ss) { this.ff = ff; this.ss = ss; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || this.getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return ff.equals(pair.ff) && ss.equals(pair.ss); } @Override public int hashCode() { return Objects.hash(ff, ss); } @Override public String toString(){ return ff.toString()+" "+ss.toString(); } } /*--------------------------------------HELPER FUNCTIONS ENDS HERE-----------------------------------------*/ /*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/ static FastestReader sc; static PrintWriter out; private static void openIO() throws IOException { sc = new FastestReader(); out = new PrintWriter(System.out); } public static void closeIO() throws IOException { out.flush(); out.close(); sc.close(); } private static final class FastestReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public FastestReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastestReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() throws IOException { int b; //noinspection StatementWithEmptyBody while ((b = read()) != -1 && isSpaceChar(b)) {} return b; } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); final boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return neg?-ret:ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); final boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return neg?-ret:ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); final boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10); return neg?-ret:ret; } public String nextLine() throws IOException { final byte[] buf = new byte[(1<<10)]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } /*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/ } /** Some points to keep in mind : * 1. don't use Arrays.sort(primitive data type array) * 2. try to make the parameters of a recursive function as less as possible, * more use static variables. * **/
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
1256e57fff9111b432411cec896f8b29
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.security.KeyPair; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; /* Name of the class has to be "Main" only if the class is public. */ public class temp { abstract class sort implements Comparator<ArrayList<Integer>>{ @Override public int compare(ArrayList<Integer> a,ArrayList<Integer> b) { return a.get(0)-b.get(0); } } private static Comparator sort; public static void main (String[] args) throws Exception { FastScanner sc= new FastScanner(); int tt = sc.nextInt(); while(tt-- >0){ int n=sc.nextInt(); int nums[]=sc.nextInts(n); for(int i=n%2;i<n;i+=2) { if(nums[i]>nums[i+1]) { int temp=nums[i]; nums[i]=nums[i+1]; nums[i+1]=temp;} } boolean bob=false; for(int i=1;i<n;i++) { if(nums[i-1]>nums[i]) { System.out.println("NO"); bob=true;break; } } if(bob==false) System.out.println("YES"); } } public static int fac(int n) { if(n==0) return 1; return n*fac(n-1); } public static boolean palin(String res) { StringBuilder sb=new StringBuilder(res); String temp=sb.reverse().toString(); return(temp.equals(res)); } public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static int gcd(int a, int b) { if(a > b) a = (a+b)-(b=a); if(a == 0L) return b; return gcd(b%a, a); } public static ArrayList<Integer> findDiv(int N) { //gens all divisors of N ArrayList<Integer> ls1 = new ArrayList<Integer>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++) if(N%i == 0) { ls1.add(i); ls2.add(N/i); } Collections.reverse(ls2); for(int b: ls2) if(b != ls1.get(ls1.size()-1)) ls1.add(b); return ls1; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } static class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
4f0d80b13a3925391756ba77620df782
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class cp { static PrintWriter w = new PrintWriter(System.out); static FastScanner s = new FastScanner(); static int mod = 1000000007; static class Edge { int src; int wt; int nbr; Edge(int src, int nbr, int et) { this.src = src; this.wt = et; this.nbr = nbr; } } class EdgeComparator implements Comparator<Edge> { @Override //return samllest elemnt on polling public int compare(Edge s1, Edge s2) { if (s1.wt < s2.wt) { return -1; } else if (s1.wt > s2.wt) { return 1; } return 0; } } static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static void prime_till_n(boolean[] prime) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. for (int p = 2; p * p < prime.length; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i < prime.length; i += p) { prime[i] = false; } } } // int l = 1; // for (int i = 2; i <= n; i++) { // if (prime[i]) { // w.print(i+","); // arr[i] = l; // l++; // } // } //Time complexit Nlog(log(N)) } static int noofdivisors(int n) { //it counts no of divisors of every number upto number n int arr[] = new int[n + 1]; for (int i = 1; i <= (n); i++) { for (int j = i; j <= (n); j = j + i) { arr[j]++; } } return arr[0]; } static char[] reverse(char arr[]) { char[] b = new char[arr.length]; int j = arr.length; for (int i = 0; i < arr.length; i++) { b[j - 1] = arr[i]; j = j - 1; } return b; } static long factorial(int n) { if (n == 0) { return 1; } long su = 1; for (int i = 1; i <= n; i++) { su *= (long) i; } return su; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } long[] readArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } static class Vertex { int x; int y; int wt; public Vertex(int x, int y) { this.x = x; this.y = y; } public Vertex(int x, int y, int wt) { this.x = x; this.y = y; this.wt = wt; } } public static long power(long x, int n) { if (n == 0) { return 1l; } long pow = power(x, n / 2) % mod; if ((n & 1) == 1) // if `y` is odd { return ((((x % mod) * (pow % mod)) % mod) * (pow % mod)) % mod; } // otherwise, `y` is even return ((pow % mod) * (pow % mod)) % mod; } public static void main(String[] args) { { int t = s.nextInt(); // int t = 1; while (t-- > 0) { solve(); } w.close(); } } public static void solve() { int n = s.nextInt(); int arr[]= new int[n]; for(int i=0;i<n;i++) { arr[i]=s.nextInt(); } if(n%2==0) { for(int i=0;i<n-1;i+=2) { if(arr[i]>arr[i+1]) { int temp = arr[i]; arr[i]=arr[i+1];arr[i+1] = temp; } } } else { for(int i=1;i<n-1;i+=2) { if(arr[i]>arr[i+1]) { int temp = arr[i]; arr[i]=arr[i+1];arr[i+1] = temp; } } } for(int i=0;i<n-1;i++) { if(arr[i]>arr[i+1]) { w.println("NO"); return; } } w.println("YES"); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
1afd709ec3b85f002ea72b1e227814d9
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
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(); List<Integer> list = new ArrayList<>(); for(int i = 0;i < n;i++) list.add(sc.nextInt()); for(int i = n - 1;i > 0;i -= 2) { if(list.get(i) < list.get(i - 1)) Collections.swap(list, i,i - 1); } boolean ok = true; for(int i = 0;i < n - 1;i++) { if(list.get(i) > list.get(i + 1)) { ok = false; break; } } out.println(ok ? "YES" : "NO"); } out.flush(); } public static void main(String[] args) throws IOException { solve(); } 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; } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
0cd6606c971609c29ede4cdf6d5b20d3
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.Scanner; public class Solution { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-->0) { int n = scan.nextInt(); int arr[] = new int[n]; for(int i=0; i<n; i++) arr[i] = scan.nextInt(); for(int i=n-1; i>0; i-=2) { if(arr[i] < arr[i-1]) { int temp = arr[i]; arr[i] = arr[i-1]; arr[i-1] = temp; } } int flag = 1; for(int i=1; i<n; i++) { if(arr[i] < arr[i-1]) { flag = 0; break; } } System.out.println(flag == 1 ? "Yes" : "No"); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
9ef64da782583839f206418d4c825f38
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; public class ABCSort { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int count =0; boolean flag = true; while(t-->0) { int n = sc.nextInt(); int a[]= new int[n]; for(int i=0;i<n;i++) { a[i]= sc.nextInt(); } for(int i= n-1;i>=1;i-=2) { if(a[i]<a[i-1]) { int temp = a[i]; a[i] = a[i-1]; a[i-1] = temp; } } boolean s = true; for(int i=0; i<n-1; i++){ if(a[i] > a[i+1]){ s = false; break; } } if(s) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
0ca04263973c551afd3fda18eb3aa810
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; public class Main { // static int [] arr; // static boolean prime[] = new boolean[1000]; // static int l; // static String s; static StringBuilder sb; // static HashSet<L> hs; // static HashSet<Long> hs = new HashSet<Long>(); // static int ans; // static boolean checked []; static final int mod = 1000000007; // static int[][] dp; static boolean visited[][]; // static int[] w ,v; // static int n; // static int arr[][]; // static long ans; static Scanner sc; static PrintWriter out; static int n, m, w, t; // static char [] a , b; static StringBuilder sb; // static int ans; // static int grid[][]; static int [] res,arr; //static char[] []arr; public static void main(String[] args) throws IOException, InterruptedException { sc = new Scanner(System.in); out = new PrintWriter(System.out); t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); arr = sc.nextIntArr(n); for(int i = n-1 ; i > 0 ; i-=2) { if(arr[i-1] > arr[i]) swap(i-1,i,arr); } //out.println(Arrays.toString(arr)); boolean ok = true; for(int i = 0 ; i < n-1 ; i++) if(arr[i]> arr[i+1]) ok = false; if(ok) out.println("YES"); else out.println("NO"); } out.close(); } public static void swap(int i , int j ,int [] arr) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static void solve(int l , int r ,int d) { if(r<0 || r>= arr.length) return; if(l>=arr.length || l<0) return; if(l>r) return; int m = 0; int idx = -1; for(int i = l ; i <= r ; i++) { if(arr[i] > m) { idx = i; m = arr[i]; } } res[idx] = d; solve(l,idx-1,d+1); solve(idx+1,r,d+1); } // public static int trace(int idx,int cost){ // if(cost> t || idx == m) // return 0; // int take = v[idx] + solve(idx+1,cost + 3*w*d[idx]); // //int leave = solve(idx+1,t); // if(dp[idx][cost] == take){ // sb.append(d[idx]+" "+v[idx]+"\n"); // return 1 + trace(idx+1,cost+3*w*d[idx]); // } // // return trace(idx+1,cost); // } private static void reverse(long[] arr) { // TODO Auto-generated method stub } // recursive implementation static long arrayLCM(int[] arr, int idx) { // lcm(a,b) = (a*b/gcd(a,b)) if (idx == arr.length - 1) { return arr[idx]; } long a = arr[idx]; long b = arrayLCM(arr, idx + 1); return (a * b / gcd(a, b)); // } static int longestSubarrWthSumDivByK(int arr[], int n, int k) { // unordered map 'um' implemented as // hash table HashMap<Integer, Integer> um = new HashMap<Integer, Integer>(); // 'mod_arr[i]' stores (sum[0..i] % k) int mod_arr[] = new int[n]; int max_len = 0; long curr_sum = 0; // traverse arr[] and build up the // array 'mod_arr[]' for (int i = 0; i < n; i++) { curr_sum += arr[i]; // as the sum can be negative, // taking modulo twice mod_arr[i] = (int) ((curr_sum % k) + k) % k; // if true then sum(0..i) is // divisible by k if (mod_arr[i] == 0) // update 'max' max_len = i + 1; // if value 'mod_arr[i]' not present in 'um' // then store it in 'um' with index of its // first occurrence else if (um.containsKey(mod_arr[i]) == false) um.put(mod_arr[i], i); else // if true, then update 'max' if (max_len < (i - um.get(mod_arr[i]))) max_len = i - um.get(mod_arr[i]); } // return the required length of longest subarray // with sum divisible by 'k' return max_len; } static int longestSubArrayOfSumK(int[] arr, int n, int k) { // HashMap to store (sum, index) tuples HashMap<Integer, Integer> map = new HashMap<>(); int sum = 0, maxLen = 0; // traverse the given array for (int i = 0; i < n; i++) { // accumulate sum sum += arr[i]; // when subarray starts from index '0' if (sum == k) maxLen = i + 1; // make an entry for 'sum' if it is // not present in 'map' if (!map.containsKey(sum)) { map.put(sum, i); } // check if 'sum-k' is present in 'map' // or not if (map.containsKey(sum - k)) { // update maxLength if (maxLen < (i - map.get(sum - k))) maxLen = i - map.get(sum - k); } } return maxLen; } static boolean isPrime(long n) { if (n == 2 || n == 3) return true; if (n <= 1 || n % 2 == 0 || n % 3 == 0) return false; // To check through all numbers of the form 6k ± 1 for (long i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } static long smallestDivisor(long n) { // if divisible by 2 if (n % 2 == 0) return 2; // iterate from 3 to sqrt(n) for (long i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } static long nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } static long fact(int n) { long res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static boolean isSorted(int[] arr) { for (int i = 0; i < arr.length - 1; i++) if (arr[i] > arr[i + 1]) return false; return true; } // static void findsubsequences(String s, String ans){ // if (s.length() == 0) { // if(ans!="") // if(ans.length()!=l) // al.add(Long.parseLong(ans)); // return; // } // // // We add adding 1st character in string // findsubsequences(s.substring(1), ans + s.charAt(0)); // // // Not adding first character of the string // // because the concept of subsequence either // // character will present or not // findsubsequences(s.substring(1), ans); //} static void sieve(int n, boolean[] prime, List<Integer> al) { for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p]) { al.add(p); 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) // System.out.print(i + " "); // } public static void reverse(Object[] arr) { int i = 0; int j = arr.length - 1; while (i < j) { Object temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } } class Pair implements Comparable<Pair> { int a; int b; public Pair(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return this.a - o.a; } public String toString() { return "( " + this.a + " , " + this.b + " )\n"; } } class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public int[][] nextInt2DArr(int l, int w) throws IOException { int[][] arr = new int[l][w]; for (int i = 0; i < l; i++) for (int j = 0; j < w; j++) arr[i][j] = Integer.parseInt(next()); return arr; } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public 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 { return Double.parseDouble(next()); } public int[] nextIntArr(int length) throws IOException { int[] arr = new int[length]; for (int i = 0; i < length; i++) arr[i] = Integer.parseInt(next()); return arr; } public long[] nextLongArr(int length) throws IOException { long[] arr = new long[length]; for (int i = 0; i < length; i++) arr[i] = Long.parseLong(next()); return arr; } public boolean ready() throws IOException { return br.ready(); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
dfec9a797e56edabcdd55786c2db5dc7
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main1674D { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); while (t > 0) { int n = sc.nextInt(); int[] array = new int[n]; for(int i = 0; i < n; i++){ array[i] = sc.nextInt(); } if(n == 1){ System.out.println("YES"); }else { boolean flag = true; if(n % 2 == 0){ for(int i = 0; i <n ;i +=2){ if(array[i]> array[i+1]){ int tmp = array[i]; array[i] = array[i+1]; array[i+1] = tmp; } } }else { for(int i = 1; i <n ;i +=2){ if(array[i]> array[i+1]){ int tmp = array[i]; array[i] = array[i+1]; array[i+1] = tmp; } } } for(int i = 1;i< n;i++){ if(array[i]< array[i-1]){ flag = false; break; } } if(flag){ System.out.println("YES"); }else{ System.out.println("NO"); } } t--; } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
39ade188a7f2450caaaca7c79b289795
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.*; public class problemD { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num_tc = 1; num_tc = sc.nextInt(); for(int tc=1;tc<=num_tc;tc++){ solve(sc); } } static void solve(Scanner sc){ /* Write your testcase-solving logic here */ int n = sc.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); } for(int i=n-1;i>0;i-=2){ if(arr[i]<arr[i-1]){ int temp = arr[i]; arr[i] = arr[i-1]; arr[i-1] = temp; } } Boolean isSorted = true; for(int i=1;i<n;i++){ if(arr[i]<arr[i-1]){ isSorted = false; break; } } System.out.println(isSorted ? "YES" : "NO"); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
0991a66cdf8870c6ff460e3d1dd1fd38
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public class Solution{ public static void main(String[] args) throws IOException { FastScanner f= new FastScanner(); int ttt=1; ttt=f.nextInt(); PrintWriter out=new PrintWriter(System.out); outer: for(int tt=0;tt<ttt;tt++) { int n=f.nextInt(); int[] l=f.readArray(n); for(int i=n-1;i>-1;i-=2) { if(i>0) { if(l[i]<l[i-1]) { int temp=l[i]; l[i]=l[i-1]; l[i-1]=temp; } } } for(int i=0;i<n-1;i++) { if(l[i]>l[i+1]) { System.out.println("NO"); continue outer; } } System.out.println("YES"); } out.close(); } static void sort(int[] p) { ArrayList<Integer> q = new ArrayList<>(); for (int i: p) q.add( i); Collections.sort(q); for (int i = 0; i < p.length; i++) p[i] = q.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()); } long[] readLongArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
700276de4380a8ebfa9a85b734f54d26
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.*; import java.util.*; public final class Main { //int 2e9 - long 9e18 static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(0, 1), new Pair(1, 0), new Pair(0, -1)}; static int mod = (int) (1e9 + 7); static int mod2 = 998244353; public static void main(String[] args) { int tt = i(); while (tt-- > 0) { solve(); } out.flush(); } public static void solve() { int n = i(); int[] a = input(n); if (n <= 2) { out.println("YES"); return; } int i = n - 2; int last = (int) 1e7; while (i >= 0) { int min = Math.min(a[i], a[i + 1]); int max = Math.max(a[i], a[i + 1]); if (max > last) { out.println("NO"); return; } last = min; i -= 2; } if (i == -1) { if (a[0] > last) { out.println("NO"); return; } } out.println("YES"); } // (10,5) = 2 ,(11,5) = 3 static long upperDiv(long a, long b) { return (a / b) + ((a % b == 0) ? 0 : 1); } static long sum(int[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static int[] preint(int[] a) { int[] pre = new int[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] pre(int[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] post(int[] a) { long[] post = new long[a.length + 1]; post[0] = 0; for (int i = 0; i < a.length; i++) { post[i + 1] = post[i] + a[a.length - 1 - i]; } return post; } static long[] pre(long[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static void print(char A[]) { for (char c : A) { out.print(c); } out.println(); } static void print(boolean A[]) { for (boolean c : A) { out.print(c + " "); } out.println(); } static void print(int A[]) { for (int c : A) { out.print(c + " "); } out.println(); } static void print(long A[]) { for (long i : A) { out.print(i + " "); } out.println(); } static void print(List<Integer> A) { for (int a : A) { out.print(a + " "); } } 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 String c() { return in.next(); } static int[][] inputWithIdx(int N) { int A[][] = new int[N][2]; for (int i = 0; i < N; i++) { A[i] = new int[]{i, in.nextInt()}; } return A; } 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; } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long GCD(long a, long b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long LCM(int a, int b) { return (long) a / GCD(a, b) * b; } static long LCM(long a, long b) { return a / GCD(a, b) * b; } // find highest i which satisfy a[i]<=x static int lowerbound(int[] a, int x) { int l = 0; int r = a.length - 1; while (l < r) { int m = (l + r + 1) / 2; if (a[m] <= x) { l = m; } else { r = m - 1; } } return l; } static void shuffle(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } } static void shuffleAndSort(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static void shuffleAndSort(int[][] arr, Comparator<? super int[]> comparator) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int[] temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr, comparator); } static void shuffleAndSort(long[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); long temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static boolean isPerfectSquare(double number) { double sqrt = Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static void swap(int A[], int a, int b) { int t = A[a]; A[a] = A[b]; A[b] = t; } static void swap(char A[], int a, int b) { char t = A[a]; A[a] = A[b]; A[b] = t; } static long pow(long a, long b, int mod) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow = (pow * x) % mod; } x = (x * x) % mod; b /= 2; } return pow; } static long pow(long a, long b) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow *= x; } x = x * x; b /= 2; } return pow; } static long modInverse(long x, int mod) { return pow(x, mod - 2, mod); } 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; } public static String reverse(String str) { if (str == null) { return null; } return new StringBuilder(str).reverse().toString(); } public static void reverse(int[] arr) { int l = 0; int r = arr.length - 1; while (l < r) { swap(arr, l, r); l++; r--; } } public static String repeat(char ch, int repeat) { if (repeat <= 0) { return ""; } final char[] buf = new char[repeat]; for (int i = repeat - 1; i >= 0; i--) { buf[i] = ch; } return new String(buf); } public static int[] manacher(String s) { char[] chars = s.toCharArray(); int n = s.length(); int[] d1 = new int[n]; for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1); while (0 <= i - k && i + k < n && chars[i - k] == chars[i + k]) { k++; } d1[i] = k--; if (i + k > r) { l = i - k; r = i + k; } } return d1; } public static int[] kmp(String s) { int n = s.length(); int[] res = new int[n]; for (int i = 1; i < n; ++i) { int j = res[i - 1]; while (j > 0 && s.charAt(i) != s.charAt(j)) { j = res[j - 1]; } if (s.charAt(i) == s.charAt(j)) { ++j; } res[i] = j; } return res; } } class Pair { int i; int j; Pair(int i, int j) { this.i = i; this.j = j; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Pair pair = (Pair) o; return i == pair.i && j == pair.j; } @Override public int hashCode() { return Objects.hash(i, j); } } class ThreePair { int i; int j; int k; ThreePair(int i, int j, int k) { this.i = i; this.j = j; this.k = k; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } ThreePair pair = (ThreePair) o; return i == pair.i && j == pair.j && k == pair.k; } @Override public int hashCode() { return Objects.hash(i, j); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } class Node { int val; public Node(int val) { this.val = val; } } class ST { int n; Node[] st; ST(int n) { this.n = n; st = new Node[4 * Integer.highestOneBit(n)]; } void build(Node[] nodes) { build(0, 0, n - 1, nodes); } private void build(int id, int l, int r, Node[] nodes) { if (l == r) { st[id] = nodes[l]; return; } int mid = (l + r) >> 1; build((id << 1) + 1, l, mid, nodes); build((id << 1) + 2, mid + 1, r, nodes); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } void update(int i, Node node) { update(0, 0, n - 1, i, node); } private void update(int id, int l, int r, int i, Node node) { if (i < l || r < i) { return; } if (l == r) { st[id] = node; return; } int mid = (l + r) >> 1; update((id << 1) + 1, l, mid, i, node); update((id << 1) + 2, mid + 1, r, i, node); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } Node get(int x, int y) { return get(0, 0, n - 1, x, y); } private Node get(int id, int l, int r, int x, int y) { if (x > r || y < l) { return new Node(0); } if (x <= l && r <= y) { return st[id]; } int mid = (l + r) >> 1; return comb(get((id << 1) + 1, l, mid, x, y), get((id << 1) + 2, mid + 1, r, x, y)); } Node comb(Node a, Node b) { if (a == null) { return b; } if (b == null) { return a; } return new Node(GCD(a.val, b.val)); } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
88b86f5763c45c7977480d3ad7d9766e
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import java.util.StringTokenizer; public class Main { public Main() { FastScanner input = new FastScanner(System.in); StringBuilder output = new StringBuilder(); int t = input.nextInt(); for (int i = 0; i < t; i++) { int n = input.nextInt(); int[] nums = new int[n]; for (int j = 0; j < nums.length; j++) { nums[j] = input.nextInt(); } ArrayList<Pair> pairs = new ArrayList<>(); for (int j = nums.length - 1; j >= 0; j-= 2) { if (j > 0) { pairs.add(new Pair(nums[j], nums[j - 1])); } else { pairs.add(new Pair(nums[j])); } } int prevMin = 0; boolean canDo = true; for (int j = pairs.size() - 1; j >= 0; j--) { Pair pair = pairs.get(j); if (pair.lesser < prevMin) { canDo = false; break; } else { prevMin = pair.higher; } } output.append(canDo ? "YES" : "NO"); output.append("\n"); } System.out.println(output); } class Pair { int lesser; int higher; boolean hasOne; public Pair(int num1, int num2) { lesser = Math.min(num1, num2); higher = Math.max(num1, num2); hasOne = false; } public Pair(int num) { lesser = higher = num; hasOne = true; } public String toString() { return "{" + lesser + ", " + higher + "}"; } } public static void main(String[] args) { new Main(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner(InputStream in) { this(new InputStreamReader(in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next());} } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
39cf439e1d0346c93270213b7f156325
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
//package com.example.lib; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashMap; public class Algorithm { public static void main(String[] rgs) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); // BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\USER\\AndroidStudioProjects\\MyApplication\\lib\\src\\main\\java\\com\\example\\lib\\inpu")); StringBuilder stringBuilder= new StringBuilder(); int t= Integer.parseInt(bufferedReader.readLine()); for (int i = 0; i < t; i++) { int size= Integer.parseInt(bufferedReader.readLine()); String[] arr= bufferedReader.readLine().split(" "); int[] ar= new int[size]; for (int j = 0; j < arr.length; j++) { ar[j]= Integer.parseInt(arr[j]); } int [] sorted = Arrays.copyOf(ar,size); Arrays.sort(sorted); boolean broken= false; for (int j = size-1; j >0 ; j-=2) { int max = Math.max(ar[j],ar[j-1]); int min = Math.min(ar[j],ar[j-1]); if(sorted[j]!=max || sorted[j-1] !=min){ stringBuilder.append("NO\n"); broken=true; break; } } if(broken)continue; stringBuilder.append("YES\n"); } System.out.println(stringBuilder); } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 8
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output