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
0c910a2b1665c84e0168ca504f7cd57e
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 ACM { public static void main(String[] args) { Scanner in = new Scanner(System.in); for (int t = in.nextInt(); t > 0; t--) { int n = in.nextInt(); int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = in.nextInt(); help(array); } } private static void help(int[] array) { for (int i = array.length - 1; i > 0; i -= 2) { if (array[i - 1] > array[i]) { int t = array[i - 1]; array[i - 1] = array[i]; array[i] = t; } } for (int i = array.length - 1; i > 0; i--) { if (array[i] < array[i - 1]) { System.out.println("NO"); return; } } 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
5ccdb3a863584ed5d917187d1c2080d8
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 D1674 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for (int t=0; t<T; t++) { int N = in.nextInt(); int[] A = new int[N]; for (int n=0; n<N; n++) { A[n] = in.nextInt(); } for (int n=N-1; n>0; n-=2) { if (A[n-1] > A[n]) { int temp = A[n]; A[n] = A[n-1]; A[n-1] = temp; } } boolean sorted = true; for (int n=1; n<N; n++) { if (A[n-1] > A[n]) { sorted = false; break; } } 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
e8f888d9264e14190fac2a216d8e5e6e
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 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 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
658242c5396c057f2e3af9a2ffc149cf
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 { public static PrintWriter out; public static void main(String[] args)throws IOException{ Scanner sc=new Scanner(); out=new PrintWriter(System.out); 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>=1;i-=2){ if(a[i-1]>a[i]) { int temp = a[i-1]; a[i-1] = a[i]; a[i]=temp; } } out.println(isSorted(a, n)?"YES":"NO"); } out.close(); } public static boolean isSorted(int[] arr,int n) { for(int i=0;i<n-1;i++) { if(arr[i]>arr[i+1]) return false; } return true; } public static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
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
aa3121600cb5b6c59b94de30ce5d0fee
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 { static PrintStream out = new PrintStream(System.out); static LinkedList<LinkedList<Integer>> adj; static boolean[] vis; //static ArrayList<ArrayList<Integer>> lists; public static void main(String[] args){ FastScanner sc = new FastScanner(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int[] arr = sc.nextIntArray(n); if(n == 1 || n == 2){ out.println("YES"); continue; } ArrayList<Integer> evens = new ArrayList(); ArrayList<Integer> odds = new ArrayList(); for(int i = 0; i < n; i++){ if(i % 2 == 0) evens.add(arr[i]); else odds.add(arr[i]); } boolean flag1 = true; boolean flag2 = true; for(int i = 1; i < evens.size(); i++){ if(evens.get(i - 1) > evens.get(i)){ flag1=false; break; } } for(int i = 1; i < odds.size(); i++){ if(odds.get(i-1)>odds.get(i)){ flag2=false; break; } } if(!flag1 || !flag2){ out.println("NO"); continue; } Queue<Integer> e = new LinkedList(); for(int i : evens) e.add(i); Queue<Integer> o = new LinkedList(); for(int i : odds) o.add(i); ArrayList<Integer> ans = new ArrayList(); for(int i = 0 ;i < n; i++){ if(e.size() > o.size()){ ans.add(e.poll()); } else if(e.size() < o.size()){ ans.add(o.poll()); } else { if(e.peek() <= o.peek()){ ans.add(e.poll()); } else { ans.add(o.poll()); } } } boolean ok = true; for(int i = 1; i < ans.size(); i++){ if(ans.get(i - 1) > ans.get(i)){ ok = false; break; } } if(!ok) out.println("NO"); else out.println("YES"); } } public static void dfs(int v){ if(vis[v]) return; vis[v] = true; LinkedList<Integer> neighbors = adj.get(v); Iterator<Integer> it = neighbors.iterator(); while(it.hasNext()){ int node = it.next(); if(!vis[node]){ dfs(node); } } } public static void mergeSort(int[] inputArray){ int n = inputArray.length; // if input array is empty or contains only one element (meaning already sorted) if(n < 2){ return; } // split input array int mid = n / 2; int[] leftHalf = new int[mid]; int[] rightHalf = new int[n - mid]; for(int i = 0; i < mid; i++){ leftHalf[i] = inputArray[i]; } for(int i = mid; i < n; i++){ rightHalf[i - mid] = inputArray[i]; } // merge sort both halves mergeSort(leftHalf); mergeSort(rightHalf); // merge halves back into one array merge(inputArray, leftHalf, rightHalf); } public static void merge(int[] inputArray, int[] leftArray, int[] rightArray){ int leftSize = leftArray.length; int rightSize = rightArray.length; int i = 0, j = 0, k = 0; // get smaller element between two arrays while(i < leftSize && j < rightSize){ if(leftArray[i] <= rightArray[j]){ inputArray[k] = leftArray[i++]; } else{ for(int x = i ; x < leftSize; x++){ //out.print(leftArray[x] + " " + rightArray[j]); int u = leftArray[x]; int v = rightArray[j]; adj.get(u).add(v); adj.get(v).add(u); } inputArray[k] = rightArray[j++]; } k++; } // check left for leftovers while(i < leftSize){ inputArray[k++] = leftArray[i++]; } // check right for leftovers while(j < rightSize){ inputArray[k++] = rightArray[j++]; } } public static void addUndirectedEdge(int u, int v){ adj.get(u).add(v); adj.get(v).add(u); } } // custom I/O class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextIntArray(int length) { int[] arr = new int[length]; for (int i = 0; i < length; i++) arr[i] = nextInt(); return arr; } } 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 p) { return this.a - p.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
4fe07272c36ac2384d075d7485774bfd
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) { Scanner obj = new Scanner(System.in); int t = obj.nextInt(); while(t-->0) { int n = obj.nextInt(); int [] a = new int [n]; for(int i =0;i<n;i++) a[i] = obj.nextInt(); int i=0; if(n%2!=0) i++; while(!(i>n-1||i+1>n-1)) { if(a[i]>a[i+1]) { // Swapping using xor a[i] = a[i]^a[i+1]; a[i+1] = a[i]^a[i+1]; a[i] = a[i]^a[i+1]; } i=i+2; } boolean flag = true; for(int k=0;k<n-1;k++) { if(a[k]>a[k+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
f561d2b00719783864984e906c62adf5
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 Round786DIV3; import java.io.*; import java.util.StringTokenizer; public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int [] a = sc.nextIntArray(n); // we want to make array c sorted in non decreasing order int i =0; int [] c = new int[n]; if(n%2!=0) { c[0]=a[0]; i++; } while(n-i>1) { c[i]=Math.min(a[i], a[i+1]); c[i+1]=Math.max(a[i], a[i+1]); i+=2; } boolean sorted=true; for(int j =0;j<n-1;j++) { if(c[j]>c[j+1])sorted=false; } pw.println(sorted?"YES":"NO"); } pw.close(); } // -------------------------------------------------------Scanner--------------------------------------------------- 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
94b8bcc8d82cfa5285744e7537db5c30
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) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for(int ttt = 1; ttt <= T; ttt++) { 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]; } Arrays.sort(b); int cur = 0; int e = a.length%2; boolean p = true; while(cur < n) { if(e==0) { if(a[cur]==b[cur]) { } else if(a[cur+1]==b[cur]) { a[cur+1] = a[cur]; } else { p = false; break; } } else { if(a[cur] == b[cur]) { } else { p = false; break; } } e ^= 1; cur++; } if(p) out.println("YES"); else out.println("NO"); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } int[] readInt(int size) { int[] arr = new int[size]; for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(next()); return arr; } long[] readLong(int size) { long[] arr = new long[size]; for(int i = 0; i < size; i++) arr[i] = Long.parseLong(next()); return arr; } int[][] read2dArray(int rows, int cols) { int[][] arr = new int[rows][cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) arr[i][j] = Integer.parseInt(next()); } 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
d0fcf456f19982192d834adc8f70546a
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.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.PriorityQueue; import java.util.StringTokenizer; public class D { public static void main(String[]args) throws IOException { Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int[]a=new int[n]; PriorityQueue<Integer>pq=new PriorityQueue<Integer>(); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); pq.add(a[i]); } boolean ok=true; if(n%2==1&&a[0]!=pq.poll())ok=false; for(int i=n%2;i<n;i+=2) { int f=pq.poll(),s=pq.poll(); if((a[i]!=f||a[i+1]!=s)&&(a[i]!=s||a[i+1]!=f)) { ok=false; } } out.println(ok?"YES":"NO"); } out.close(); } 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 boolean hasNext() {return st.hasMoreTokens();} public int nextInt() throws IOException {return Integer.parseInt(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} 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
0901496894aa76cc94357422d6a6377c
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 sc=new Scanner(System.in); int rows=Integer.parseInt(sc.nextLine()); while(rows>0){ int n=sc.nextInt(); int[] nums=new int[n]; for(int i=0;i<n;i++){ nums[i]=sc.nextInt(); } for(int i=n-1;i>0;i-=2){ if(nums[i]<nums[i-1]){ int temp=nums[i]; nums[i]=nums[i-1]; nums[i-1]=temp; } } boolean verify=true; for(int i=0;i<n-1;i++){ if(nums[i]>nums[i+1]){ verify=false; break; } } System.out.println(verify?"YES":"NO"); rows--; } } }
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
1a559bf1c423610b21ae71ac86cfebb6
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.*; import static java.lang.System.currentTimeMillis; /* * @author: Hivilsm * @createTime: 2022-04-27, 23:29:16 * @description: Platform */ public class Accepted { static FastReader in = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static Random rand = new Random(); public static void main(String[] args) { // long start = currentTimeMillis(); // int t = 3; int t = i(); while (t-- > 0){ boolean ans = sol(); if (ans){ out.println("YES"); }else{ out.println("NO"); } } // long end = currentTimeMillis(); // out.println(end - start); out.flush(); out.close(); } public static boolean sol() { int n = i(); int[] arr = inputI(n); int pre = 1000000; for (int i = n - 1; i > -1; i-=2){ int max = arr[i]; int min = arr[i]; if (i - 1 > -1){ max = Math.max(arr[i - 1], max); min = Math.min(arr[i - 1], min); } if (max > pre){ return false; } pre = min; } return true; } public static void swap(int[] nums, int l, int r){ int tmp = nums[l]; nums[l] = nums[r]; nums[r] = tmp; } public static void ranArr() { int n = 3, len = 10, val = 10; System.out.println(n); for (int i = 0; i < n; i++) { int cnt = rand.nextInt(len) + 1; System.out.println(cnt); for (int j = 0; j < cnt; j++) { System.out.print(rand.nextInt(val) + " "); } System.out.println(); } } static double fastPower(double x, int n) { if (x == 0) return 0; long b = n; double res = 1.0; if (b < 0) { x = 1 / x; b = -b; } while (b > 0) { if ((b & 1) == 1) res *= x; x *= x; b >>= 1; } return res; } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static double d() { return in.nextDouble(); } static String s() { return in.nextLine(); } static int[] inputI(int n) { int nums[] = new int[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextInt(); } return nums; } static long[] inputLong(int n) { long nums[] = new long[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextLong(); } return nums; } } class ListNode { int val; ListNode next; public ListNode() { } public ListNode(int val) { this.val = val; } } class TreeNode { int val; TreeNode left; TreeNode right; public TreeNode() { } public TreeNode(int val) { this.val = val; } } 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
f0687493a796b007991cbd5be9ff8349
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 dict { 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[] arr=new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); int[] arr2=Arrays.copyOf(arr, n); Arrays.sort(arr2); if(check(arr,arr2,0,n)) System.out.println("YES"); else System.out.println("NO"); } sc.close(); } public static boolean check(int[] source,int[] sorted, int pos,int len) { if(len<=0 || pos>=source.length) return true; if(len%2!=0) { if(source[pos]!=sorted[pos]) return false; else { pos++; len--; } } else { if(source[pos]!=sorted[pos] && source[pos+1]!=sorted[pos]) return false; if(source[pos+1]==sorted[pos]) { int temp=source[pos]; source[pos]=source[pos+1]; source[pos+1]=temp; } pos++; len--; } return (true && check(source,sorted,pos,len)); } }
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
34d585e2ba62386b6bf677f0b2458ef1
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
//---#ON_MY_WAY--- //---#THE_SILENT_ONE--- import static java.lang.Math.*; import java.io.*; import java.math.*; import java.util.*; public class A { static FastReader x = new FastReader(); static OutputStream outputStream = System.out; static PrintWriter out = new PrintWriter(outputStream); /*---------------------------------------CODE STARTS HERE-------------------------*/ public static void main(String[] args) throws NumberFormatException, IOException { long startTime = System.nanoTime(); int mod = 1000000007; int t = x.nextInt(); StringBuilder str = new StringBuilder(); while (t > 0) { int n = x.nextInt(); int a[] = readarr(n); int b[] = Arrays.copyOf(a, n); for(int i = n-1; i >= 1; i -= 2) { if(a[i]<a[i-1]) { int k = a[i]; a[i] = a[i-1]; a[i-1] = k; } } sortint(b); if(Arrays.equals(a, b)) str.append("YES"); else str.append("NO"); str.append("\n"); t--; } out.println(str); out.flush(); long endTime = System.nanoTime(); //System.out.println((endTime-startTime)/1000000000.0); } /*--------------------------------------------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; } char nextchar() { char ch = ' '; try { ch = (char) br.read(); } catch (IOException e) { e.printStackTrace(); } return ch; } } /*--------------------------------------------HELPER---------------------------------*/ static class pair implements Comparable<pair> { int x, y; public pair(int a, int b) { x = a; y = b; } @Override public int hashCode() { int hash = 3; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final pair other = (pair) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(A.pair o) { if(this.x==o.x) return this.y-o.y; return this.x-o.x; } } /*--------------------------------------------BOILER PLATE---------------------------*/ static int[] readarr(int n) { int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = x.nextInt(); } return arr; } static int[] sortint(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 long[] sortlong(long a[]) { ArrayList<Long> al = new ArrayList<>(); for (long i : a) { al.add(i); } Collections.sort(al); for (int i = 0; i < al.size(); i++) { a[i] = al.get(i); } return a; } static long pow(long x, long y) { long result = 1; while (y > 0) { if (y % 2 == 0) { x = x * x; y = y / 2; } else { result = result * x; y = y - 1; } } return result; } static long pow(long x, long y, long mod) { long result = 1; x %= mod; while (y > 0) { if (y % 2 == 0) { x = (x % mod * x % mod) % mod; y /= 2; } else { result = (result % mod * x % mod) % mod; y--; } } return result; } static int[] revsort(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) { al.add(i); } Collections.sort(al, Comparator.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = al.get(i); } return a; } static int[] gcd(int a, int b, int ar[]) { if (b == 0) { ar[0] = a; ar[1] = 1; ar[2] = 0; return ar; } ar = gcd(b, a % b, ar); int t = ar[1]; ar[1] = ar[2]; ar[2] = t - (a / b) * ar[2]; return ar; } static boolean[] esieve(int n) { boolean p[] = new boolean[n + 1]; Arrays.fill(p, true); for (int i = 2; i * i <= n; i++) { if (p[i] == true) { for (int j = i * i; j <= n; j += i) { p[j] = false; } } } return p; } static ArrayList<Integer> primes(int n) { boolean p[] = new boolean[n + 1]; ArrayList<Integer> al = new ArrayList<>(); Arrays.fill(p, true); int i = 0; for (i = 2; i * i <= n; i++) { if (p[i] == true) { al.add(i); for (int j = i * i; j <= n; j += i) { p[j] = false; } } } for (i = i; i <= n; i++) { if (p[i] == true) { al.add(i); } } return al; } static int etf(int n) { int res = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res /= i; res *= (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { res /= n; res *= (n - 1); } return res; } 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); } }
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
504535c0af1ae71b25950e713876c9f3
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 GFG { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int T=sc.nextInt(); while(T-->0){ int n=sc.nextInt(); int[] ar=new int[n]; for(int i=0;i<n;i++){ ar[i]=sc.nextInt(); } for(int i=n-1;i>=1;i-=2){ if(ar[i]<ar[i-1]){ int temp=ar[i]; ar[i]=ar[i-1]; ar[i-1]=temp; } } boolean flag=false; for(int i=0;i<n-1;i++){ if(ar[i]>ar[i+1]){ flag=true; break; } } if(flag){ 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
725a1563b035db29dd560b3b5349ee61
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 AACFCC { // public static long mod = (long) Math.pow(10, 9) + 7; public static long mod2 = 998244353; public int oo = 0; // public static HashMap<Integer, Integer> primenom; // public static HashMap<Integer, Integer> primeden; // public static HashMap<Integer, Integer> fin; public static int zz = -1; public static long ttt = 0; static HashMap<Integer, Integer>[] factors; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int p1 = 1; int t = Integer.parseInt(br.readLine()); while (t-- > 0) { int n=Integer.valueOf(br.readLine()); int[]arr=new int[n]; String[]s1=br.readLine().split(" "); for(int i=0;i<n;i++) { arr[i]=Integer.valueOf(s1[i]); } int[]a1=new int[n]; for(int i=0;i<n;i++) { if((n-i)%2==0) { a1[i]=Math.min(arr[i], arr[i+1]); a1[i+1]=Math.max(arr[i], arr[i+1]); i++; }else { a1[i]=arr[i]; } } int ans=1; for(int i=n-2;i>=0;i--) { if(a1[i+1]<a1[i]) { ans=0; } } if(ans==0) { pw.println("NO"); }else { pw.println("YES"); } } pw.close(); } } // private static void putBit(int ind, int val, int[] bit) { // // TODO Auto-generated method stub // for (int i = ind; i < bit.length; i += (i & -i)) { // bit[i] += val; // } // } // // private static int getSum(int ind, int[] bit) { // // TODO Auto-generated method stub // int ans = 0; // for (int i = ind; i > 0; i -= (i & -i)) { // ans += bit[i]; // } // return ans; // } // private static void product(long[] bin, int ind, int currIt, long[] power) { // // TODO Auto-generated method stub // long pre = 1; // if (ind > 1) { // pre = power(power[ind - 1] - 1, mod2 - 1); // } // long cc = power[ind] - 1; // // System.out.println(pre + " " + cc); // for (int i = ind; i < bin.length; i += (i & (-i))) { // bin[i] = (bin[i] * pre) % mod2; // bin[i] = (bin[i] * cc) % mod2; // } // } // // private static void add(long[] bin, int ind, int val) { // // TODO Auto-generated method stub // for (int i = ind; i < bin.length; i += (i & (-i))) { // bin[i] += val; // } // } // // private static long sum(long[] bin, int ind) { // // TODO Auto-generated method stub // long ans = 0; // for (int i = ind; i > 0; i -= (i & (-i))) { // ans += bin[i]; // } // return ans; // } // // private static long power(long a, long p) { // TODO Auto-generated method stub // long res = 1;while(p>0) // { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // }return res; // }} // private static void getFac(long n, PrintWriter pw) { // // TODO Auto-generated method stub // int a = 0; // while (n % 2 == 0) { // a++; // n = n / 2; // } // if (n == 1) { // a--; // } // for (int i = 3; i <= Math.sqrt(n); i += 2) { // while (n % i == 0) { // n = n / i; // a++; // } // } // if (n > 1) { // a++; // } // if (a % 2 == 0) { // pw.println("Bob"); // } else { // pw.println("Alice"); // } // //System.out.println(a); // return; // } // private static long power(long a, long p) { // // TODO Auto-generated method stub // long res = 1; // while (p > 0) { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // } // return res; // } // // private static void fac() { // fac[0] = 1; // // TODO Auto-generated method stub // for (int i = 1; i < fac.length; i++) { // if (i == 1) { // fac[i] = 1; // } else { // fac[i] = i * fac[i - 1]; // } // if (fac[i] > mod) { // fac[i] = fac[i] % mod; // } // } // } // // private static int getLower(Long long1, Long[] st) { // // TODO Auto-generated method stub // int left = 0, right = st.length - 1; // int ans = -1; // while (left <= right) { // int mid = (left + right) / 2; // if (st[mid] <= long1) { // ans = mid; // left = mid + 1; // } else { // right = mid - 1; // } // } // return ans; // } // private static long getGCD(long l, long m) { // // long t1 = Math.min(l, m); // long t2 = Math.max(l, m); // while (true) { // long temp = t2 % t1; // if (temp == 0) { // return t1; // } // t2 = t1; // t1 = temp; // } // } // private static int kmp(String str) { // // TODO Auto-generated method stub // // System.out.println(str); // int[] pi = new int[str.length()]; // pi[0] = 0; // for (int i = 1; i < str.length(); i++) { // int j = pi[i - 1]; // while (j > 0 && str.charAt(i) != str.charAt(j)) { // j = pi[j - 1]; // } // if (str.charAt(j) == str.charAt(i)) { // j++; // } // pi[i] = j; // System.out.print(pi[i]); // } // System.out.println(); // return pi[str.length() - 1]; // } // private static void getFac(long n, PrintWriter pw) { // // TODO Auto-generated method stub // int a = 0; // while (n % 2 == 0) { // a++; // n = n / 2; // } // if (n == 1) { // a--; // } // for (int i = 3; i <= Math.sqrt(n); i += 2) { // while (n % i == 0) { // n = n / i; // a++; // } // } // if (n > 1) { // a++; // } // if (a % 2 == 0) { // pw.println("Bob"); // } else { // pw.println("Alice"); // } // //System.out.println(a); // return; // } // private static long power(long a, long p) { // // TODO Auto-generated method stub // long res = 1; // while (p > 0) { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // } // return res; // } // // private static void fac() { // fac[0] = 1; // // TODO Auto-generated method stub // for (int i = 1; i < fac.length; i++) { // if (i == 1) { // fac[i] = 1; // } else { // fac[i] = i * fac[i - 1]; // } // if (fac[i] > mod) { // fac[i] = fac[i] % mod; // } // } // } // // private static int getLower(Long long1, Long[] st) { // // TODO Auto-generated method stub // int left = 0, right = st.length - 1; // int ans = -1; // while (left <= right) { // int mid = (left + right) / 2; // if (st[mid] <= long1) { // ans = mid; // left = mid + 1; // } else { // right = mid - 1; // } // } // return ans; // } // private static long getGCD(long l, long m) { // // long t1 = Math.min(l, m); // long t2 = Math.max(l, m); // while (true) { // long temp = t2 % t1; // if (temp == 0) { // return t1; // } // t2 = t1; // t1 = temp; // } // }
Java
["3\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
cf7c3d8fc55402fefc83c9e9a3c59a24
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.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; public class Main { static class Reader{ BufferedReader br; StringTokenizer st; public Reader(boolean f) throws IOException{ if(f) { br = new BufferedReader(new FileReader("input.txt")); }else{ br=new BufferedReader(new InputStreamReader(System.in)); } } String next(){ while(st==null || !st.hasMoreTokens()){ try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class Writer { private final BufferedWriter bw; public Writer(boolean f) throws IOException { if(f) { this.bw = new BufferedWriter(new FileWriter("output.txt")); }else{ this.bw = new BufferedWriter(new OutputStreamWriter(out)); } } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } static long power(long x, long y) { long temp; if( y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp*temp; else return x*temp*temp; } public static void main(String[] args){ try { Reader in=new Reader(false); Writer out =new Writer(false); int t = 1; t=in.nextInt(); while(t-- > 0) { int n = in.nextInt(); Integer[] arr = new Integer[n]; Integer[] brr = new Integer[n]; for (int i = 0; i < n; i++) { int a = in.nextInt(); arr[i]=a; brr[i]=a; } int j = 0; if(n%2==0){ j=0; }else{ j=1; } while(n>j){ if(arr[j]>arr[j+1]){ int temp = arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } j+=2; } Arrays.sort(brr); boolean flag = false; for (int i = 0; i < n; i++) { if(!Objects.equals(arr[i], brr[i])){ flag = true; } } if(!flag){ out.println("YES"); }else{ out.println("NO"); } } out.close(); } catch (Exception e) { 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 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
9b0f6baea4ed356d912d6ddeb2653af0
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 _1674D { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i=1;i<=t;i++) { int n = sc.nextInt(); int arr[] = new int[n]; for(int j=0;j<n;j++) { arr[j]=sc.nextInt(); } for(int j=0;j<n/2;j++) {//Swapping int el_1=arr[n-2*j-2];int el_2=arr[n-2*j-1]; arr[n-2*j-2]=Math.min(el_1, el_2); arr[n-2*j-1]=Math.max(el_1, el_2); } boolean flag=true; for(int j=1;j<n;j++) { if(arr[j-1]<=arr[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 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
d1578fe426372568bd991bcc3186abfb
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
7c51c0d468651b5c7e109304d818d63a
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.*; import java.io.*; public class Main { void solve_test() { int n = ri(); int[] a = new int[n]; for(int i = 0; i <n; i++) a[i] = ri(); for(int i = n % 2; i +1 < n; i+=2) { if(a[i] > a[i + 1]) { int tmp = a[i]; a[i] = a[i + 1]; a[i + 1] = tmp; } } for(int i = 0; i +1 < n; i++) { if(a[i] > a[i + 1]) { out.println("NO"); return; } } out.println("YES"); } void solve() { int tt = ri(); while(tt-- > 0) { solve_test(); } } public static void main(String[] args) { new Main().run(); } void run() { try { solve(); out.close(); } catch(Exception e) { e.printStackTrace(); System.exit(-1); } } String readLine() { try { return in.readLine(); } catch(Exception e) { throw new RuntimeException(e); } } String rs() { while(!tok.hasMoreTokens()) { tok = new StringTokenizer(readLine()); } return tok.nextToken(); } int ri() { return Integer.parseInt(rs()); } long rl() { return Long.parseLong(rs()); } BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer tok = new StringTokenizer(""); }
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 11
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
8c3afd4912dc56650e11c6916b1c15f5
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.informatic; import java.lang.*; import java.util.*; import java.io.*; public class Main { int[] a; String solve_test() { try { int n = ri(); a = new int[n]; for(int i =0 ; i <n; i++) { a[i] = ri(); } for(int i = n % 2; i < n && i + 1 < n; i += 2) { if(a[i] > a[i + 1]) swap(i, i +1); } for(int i = 0; i +1 < n; i++) { if(a[i] > a[i + 1]) return "NO"; } return "YES"; } catch(Exception e) { return e.toString(); } } void swap(int f, int s) { int tmp = a[f]; a[f] = a[s]; a[s] = tmp; } void solve() { int t = ri(); while(t-- > 0) { out.println(solve_test()); } } public static void main(String[] args) { new Main().run(); } void run() { try { solve(); out.close(); } catch(Exception e) { e.printStackTrace(); System.exit(-1); } } String readLine() { try { return in.readLine(); } catch(Exception e) { throw new RuntimeException(e); } } String rs() { while(!tok.hasMoreTokens()) { tok = new StringTokenizer(readLine()); } return tok.nextToken(); } int ri() { return Integer.parseInt(rs()); } long rl() { return Long.parseLong(rs()); } BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer tok = new StringTokenizer(""); }
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 11
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
72ec4d8cf9d8f626f77b511bea8f17b4
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 { static Scanner scan = new Scanner(System.in); static boolean[] was = new boolean[1000000]; static boolean ok = true; static long count = 0; static boolean no = false; public static void main(String[] args) { int q = scan.nextInt(); while ( q-- > 0 ) { solve(); } } public static void solve() { boolean ok = true; int n = scan.nextInt(); int[] arr = new int[n]; int[] brr = new int[n]; int[] crr = new int[n]; for ( int i = 0; i < n; ++i ) { arr[i] = scan.nextInt(); } if ( n > 1 ) { for ( int l = n % 2; l < n; l += 2 ) { if ( arr[l] > arr[l + 1] ) { swap( arr, l, l + 1 ); } } for ( int l = 0; l < n - 1; l++ ) { if ( arr[l] > arr[l + 1] ) { ok = false; break; } } } System.out.println(ok ? "YES" : "NO"); } public static void sort( int[] array ) { Arrays.sort(array); } public static void swap( int[] array, int x, int y ) { int temp = array[x]; array[x] = array[y]; array[y] = temp; } public static int maxOfArray( int[] array, int idx, int max ) { // max = Integer.MIN_VALUE; if ( idx == array.length ) { return max; } if ( array[idx] > max ) { max = array[idx]; } return maxOfArray( array, idx + 1, max ); } public static int[] insertion_sort( int[] array ) { for ( int l = 1; l < array.length; ++l ) { int value = array[l]; while ( array[l - 1] > value && l > 0 ) { swap( array, l - 1, l ); l--; } } return array; } public static int minOfArray( int[] array, int idx, int min ) { // min = Integer.MAX_VALUE if ( idx == array.length ) { return min; } if ( min > array[idx] ) { min = array[idx]; } return minOfArray( array, idx + 1, min ); } public static int findMaxOfSubArray(int[] arr, int k) { int max = Integer.MIN_VALUE; int currentSum = 0; // sliding window for ( int i = 0; i < arr.length; ++i ) { currentSum += arr[i]; if ( i >= k - 1 ) { max = Math.max(max, currentSum); currentSum -= arr[i - (k - 1)]; } } return max; } }
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 11
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
02608db7f07b770089201442fe1963af
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 Main { public static FastReader cin; public static PrintWriter out; /*static int []arr=new int [5005]; static int []he=new int[5005]; static int []e=new int [100005]; static int []ne=new int [100005]; static int []dfn=new int [5005]; static int []low=new int [5005]; static int []stack=new int [5005]; static int []contains=new int [5005];//是否在栈中 static int []size=new int [5005]; static int []num=new int [5005]; static boolean[]visited=new boolean[5005]; static int idx=0; static int cnt=0;//用于初始化low和dfn static int top=0;//栈顶 static int index=0;//强连通分量的个数 static int INF=0x3f3f3f3f;*/ public static void main(String[] args) throws Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); cin = new FastReader(); int u=cin.nextInt(); label:for(int Q=0;Q<u;Q++){ int n=cin.nextInt(); int []arr=new int [n]; for(int i=0;i<n;i++){ arr[i]=cin.nextInt(); } if(n%2==0){ for(int i=2;i<n;i+=2){ int premax=Math.max(arr[i-2],arr[i-1]); int nowmin=Math.min(arr[i],arr[i+1]); if(nowmin<premax){ out.println("NO"); continue label; } } out.println("YES"); }else{ for(int i=1;i<n;i+=2){ int premax=arr[0]; if(i!=1)premax=Math.max(arr[i-2],arr[i-1]); int nowmin=Math.min(arr[i],arr[i+1]); if(nowmin<premax){ out.println("NO"); continue label; } } out.println("YES"); } } out.close(); } static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } return str; } } }
Java
["3\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 11
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
6a9d3db811568545cdfba6fde084a6f8
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 Main { public static FastReader cin; public static PrintWriter out; /*static int []arr=new int [5005]; static int []he=new int[5005]; static int []e=new int [100005]; static int []ne=new int [100005]; static int []dfn=new int [5005]; static int []low=new int [5005]; static int []stack=new int [5005]; static int []contains=new int [5005];//是否在栈中 static int []size=new int [5005]; static int []num=new int [5005]; static boolean[]visited=new boolean[5005]; static int idx=0; static int cnt=0;//用于初始化low和dfn static int top=0;//栈顶 static int index=0;//强连通分量的个数 static int INF=0x3f3f3f3f;*/ public static void main(String[] args) throws Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); cin = new FastReader(); int u=cin.nextInt(); label:for(int Q=0;Q<u;Q++){ int n=cin.nextInt(); int []arr=new int [n]; for(int i=0;i<n;i++){ arr[i]=cin.nextInt(); } if(n%2==0){ for(int i=0;i<n;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]){ out.println("NO"); continue label; } } out.println("YES"); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } return str; } } }
Java
["3\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 11
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
8cd174d84c37902e6de1f6ed4bf09c9a
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 Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.nextLine(); for (int i = 0; i < n; i++) { in.nextLine(); String[] t = in.nextLine().split(" "); if(t.length <= 2) { System.out.println("YES"); } else { boolean possible = true; if(t.length % 2 == 0) { int a = Integer.parseInt(t[0]); int b = Integer.parseInt(t[1]); for(int j=2; j<t.length; j += 2) { if(a > Integer.parseInt(t[j]) || b > Integer.parseInt(t[j]) || a > Integer.parseInt(t[j+1]) || b > Integer.parseInt(t[j+1])) { possible = false; break; } a = Integer.parseInt(t[j]); b = Integer.parseInt(t[j+1]); } } else { int f = Integer.parseInt(t[0]); int a = Integer.parseInt(t[1]); int b = Integer.parseInt(t[2]); if(f > a || f > b) possible = false; if(possible) { for(int j=3; j<t.length; j += 2) { if(f > a || f > b || a > Integer.parseInt(t[j]) || b > Integer.parseInt(t[j]) || a > Integer.parseInt(t[j+1]) || b > Integer.parseInt(t[j+1])) { possible = false; break; } a = Integer.parseInt(t[j]); b = Integer.parseInt(t[j+1]); } } } if(possible) System.out.println("YES"); else System.out.println("NO"); } } 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 11
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
ad46640218ab32b86f1f1a302018276b
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 { // driver program to test above function public static void main (String[] args) throws java.lang.Exception { int testCase = sc.nextInt(), t=0; while(testCase-->0){ int n = sc.nextInt(); int[] arr = new int[n], arr2 = new int[n],arr3 = new int[n]; for(int i=0; i<n; i++){ arr[i] = sc.nextInt(); arr2[i] = arr[i]; arr3[i] = arr[i]; } if(n==1){ System.out.println("YES"); continue; } Arrays.sort(arr2); // for(int i=0; i<n-1; i+=2){ // if(arr[i]>arr[i+1]){ // int tt = arr[i]; // arr[i] = arr[i+1]; // arr[i+1] = tt; // } // } // //System.out.println(Arrays.toString(arr)); // if(Arrays.equals(arr,arr2)){ // System.out.println("YES"); // continue; // } for(int i=n-1; i>0; i-=2){ if(arr3[i]<arr3[i-1]){ int tt = arr3[i]; arr3[i] = arr3[i-1]; arr3[i-1] = tt; } } System.out.println(Arrays.equals(arr3,arr2)?"YES":"NO"); // Arrays.sort(arr2); // if(n%2==1) { // if(Arrays.equals(arr,arr2)){ // System.out.println("YES"); // }else{ // System.out.println("NO"); // } // continue; //// int x = n / 2; //// int index = 0; //// for (int i = x; i >= 0; i--) { //// arr3[index] = arr2[i]; //// index++; //// } //// //// x = n/2; //// //arr3[index] = arr2[x]; //// //index++; //// for(int i = n-1; i>x; i--){ //// arr3[index] = arr2[i]; //// index++; //// } // }else{ // int x = n / 2; // int index = 0; // for (int i = x - 1; i >= 0; i--) { // arr3[index] = arr2[i]; // index++; // } // x = n/2; // for(int i = n-1; i>=x; i--){ // arr3[index] = arr2[i]; // index++; // } // if(Arrays.equals(arr, arr3) || Arrays.equals(arr,arr2)){ // System.out.println("YES"); // }else{ // System.out.println("NO"); // } // } // //System.out.println(Arrays.toString(arr3)); } } // Fast Reader Class public static FastReader sc = new FastReader(); public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in));} String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } }return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String 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 11
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
59806074035facd7670d7041be80802d
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.Arrays; import java.util.StringTokenizer; public class D { static final FastReader sc = new FastReader(); static final PrintWriter out = new PrintWriter(System.out, true); private static boolean debug = System.getProperty("ONLINE_JUDGE") == null; private static void trace(Object... o) { if (debug) { System.err.println(Arrays.deepToString(o)); } } public static void main(String[] args) { 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 == 1) { out.println("YES"); continue; } int[] b = a.clone(); Arrays.sort(b); boolean ok = true; for (int i = 0; i < n - 1; i++) { if (i % 2 == n % 2 && 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; } out.println((ok) ? "YES" : "NO"); } } 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 11
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
ae107d6f16a39fdcb0794f57f68060cc
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.Arrays; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int count = Integer.parseInt(br.readLine()); for (int t = 0; t < count; t++) { br.readLine(); Integer[] arr = Arrays.stream(br.readLine().split(" ")).map(v -> Integer.parseInt(v)).toArray(Integer[]::new); boolean flag = true; int i = arr.length % 2 == 0 ? 0 : 1; if (i == 1) { int temp = Integer.MAX_VALUE; if (2 < arr.length) { temp = Math.min(arr[1], arr[2]); } else if (1 < arr.length) { temp = arr[1]; } if (arr[0] > temp) { System.out.println("NO"); continue; } } for (; i < arr.length; i = i + 2) { int max = 0; if (i + 1 < arr.length) { max = Math.max(arr[i], arr[i + 1]); } else { break; } int min = 0; if (i + 3 < arr.length) { min = Math.min(arr[i + 2], arr[i + 3]); } else if (i + 2 < arr.length) { min = arr[i + 2]; } else { break; } if (min < max) { 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 11
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
7fa5c6927f0e2371e48362d76da1cdab
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.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class cf1674D { // https://codeforces.com/problemset/problem/1674/D public static void main(String[] args) { Kattio io = new Kattio(); int t = io.nextInt(), n, prevmin; int[] arr; boolean yes; for (int i = 0; i < t; i++) { n = io.nextInt(); arr = new int[n]; for (int k = 0; k < n; k++) { arr[k] = io.nextInt(); } yes = true; prevmin = Integer.MAX_VALUE; //back to front for (int k = arr.length - 1; k > 0; k-=2) { if (Math.max(arr[k], arr[k-1]) > prevmin) { yes = false; break; } prevmin = Math.min(arr[k], arr[k-1]); } if (arr.length % 2 == 1 && arr[0] > prevmin) { yes = false; } io.println(yes ? "YES" : "NO"); } io.close(); } // Kattio static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in,System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(problemName+".out"); r = new BufferedReader(new FileReader(problemName+".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) {} return null; } public String nextLine() { try { st = null; return r.readLine(); } 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 11
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
38a7940580f917a0d68657ec8d567d77
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
/** * @description: * @author: dzx * @date: 2022/5/3 */ import java.io.*; import java.util.*; public class Main2 { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, FastScanner in, FastPrinter out) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextInt(); } System.out.println(solution(nums)); } } String solution(int[]nums) { int last = nums.length-1; Deque<Integer> left = new ArrayDeque<>(); Deque<Integer> right = new ArrayDeque<>(); left.offer(Integer.MAX_VALUE); right.offer(Integer.MAX_VALUE); while (last > 0) { int min = Math.min(nums[last], nums[last - 1]); int max = Math.max(nums[last], nums[last - 1]); if(max>left.peekLast()||max> right.peekLast()) return "NO"; left.offer(min); right.offer(max); last -= 2; } if(last==0&&(nums[0]>left.peekLast()||nums[0]>right.peekLast())) return "NO"; return "YES"; } } static class ru_ifmo_niyaz_arrayutils_ArrayUtils { static final long seed = System.nanoTime(); static final Random rand = new Random(seed); public static void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(int[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); int t = a[i]; a[i] = a[j]; a[j] = t; } } } static class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } } static class net_egork_misc_ArrayUtils { public static long sumArray(int[] array) { long result = 0; for (int element : array) result += element; return result; } } static class FastScanner extends BufferedReader { public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try { int ret = super.read(); // if (isEOF && ret < 0) { // throw new InputMismatchException(); // } // isEOF = ret == -1; return ret; } catch (IOException e) { throw new InputMismatchException(); } } public String next() { StringBuilder sb = new StringBuilder(); int c = read(); while (isWhiteSpace(c)) { c = read(); } if (c < 0) { return null; } while (c >= 0 && !isWhiteSpace(c)) { sb.appendCodePoint(c); c = read(); } return sb.toString(); } static boolean isWhiteSpace(int c) { return c >= 0 && c <= 32; } public int nextInt() { int c = read(); while (isWhiteSpace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int ret = 0; while (c >= 0 && !isWhiteSpace(c)) { if (c < '0' || c > '9') { throw new NumberFormatException("digit expected " + (char) c + " found"); } ret = ret * 10 + c - '0'; c = read(); } return ret * sgn; } public long nextLong() { return Long.parseLong(next()); } public String readLine() { try { return super.readLine(); } catch (IOException e) { return null; } } public int[] readIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } }
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 11
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
f930f898262b73f490cc3e89a3c3b4a5
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
/** * @description: * @author: dzx * @date: 2022/5/3 */ import java.io.*; import java.util.*; public class Main2 { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, FastScanner in, FastPrinter out) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextInt(); } System.out.println(solution(nums)); } } String solution(int[]nums) { int last = nums.length-1; Deque<Integer> left = new ArrayDeque<>(); Deque<Integer> right = new ArrayDeque<>(); left.offer(Integer.MAX_VALUE); right.offer(Integer.MAX_VALUE); while (last > 0) { int min = Math.min(nums[last], nums[last - 1]); int max = Math.max(nums[last], nums[last - 1]); if(min>left.peekLast()||min>right.peekLast()||max>left.peekLast()||max> right.peekLast()) return "NO"; left.offer(min); right.offer(max); last -= 2; } if(last==0&&(nums[0]>left.peekLast()||nums[0]>right.peekLast())) return "NO"; return "YES"; } } static class ru_ifmo_niyaz_arrayutils_ArrayUtils { static final long seed = System.nanoTime(); static final Random rand = new Random(seed); public static void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(int[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); int t = a[i]; a[i] = a[j]; a[j] = t; } } } static class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } } static class net_egork_misc_ArrayUtils { public static long sumArray(int[] array) { long result = 0; for (int element : array) result += element; return result; } } static class FastScanner extends BufferedReader { public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try { int ret = super.read(); // if (isEOF && ret < 0) { // throw new InputMismatchException(); // } // isEOF = ret == -1; return ret; } catch (IOException e) { throw new InputMismatchException(); } } public String next() { StringBuilder sb = new StringBuilder(); int c = read(); while (isWhiteSpace(c)) { c = read(); } if (c < 0) { return null; } while (c >= 0 && !isWhiteSpace(c)) { sb.appendCodePoint(c); c = read(); } return sb.toString(); } static boolean isWhiteSpace(int c) { return c >= 0 && c <= 32; } public int nextInt() { int c = read(); while (isWhiteSpace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int ret = 0; while (c >= 0 && !isWhiteSpace(c)) { if (c < '0' || c > '9') { throw new NumberFormatException("digit expected " + (char) c + " found"); } ret = ret * 10 + c - '0'; c = read(); } return ret * sgn; } public long nextLong() { return Long.parseLong(next()); } public String readLine() { try { return super.readLine(); } catch (IOException e) { return null; } } public int[] readIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } }
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 11
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
9e274947871e6546340aab7d175e70dc
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 Main2 { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, FastScanner in, FastPrinter out) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int[] nums = new int[n]; int[] order = new int[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextInt(); // nums[i][1] = i; } // Arrays.sort(nums); // for (int i = 0; i < n; i++) { // order[nums[i][1]] = i; // } System.out.println(solution(nums)); } } String solution(int[]nums) { int n = nums.length; for (int i = n%2; i < n-1; i+=2) { if (nums[i] > nums[i + 1]) { int tmp = nums[i]; nums[i] = nums[i + 1]; nums[i + 1] = tmp; } } for (int i = 1; i <n ; i++) { if (nums[i] < nums[i - 1]) { return "NO"; } } return "YES"; } } static class ru_ifmo_niyaz_arrayutils_ArrayUtils { static final long seed = System.nanoTime(); static final Random rand = new Random(seed); public static void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(int[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); int t = a[i]; a[i] = a[j]; a[j] = t; } } } static class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } } static class net_egork_misc_ArrayUtils { public static long sumArray(int[] array) { long result = 0; for (int element : array) result += element; return result; } } static class FastScanner extends BufferedReader { public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try { int ret = super.read(); // if (isEOF && ret < 0) { // throw new InputMismatchException(); // } // isEOF = ret == -1; return ret; } catch (IOException e) { throw new InputMismatchException(); } } public String next() { StringBuilder sb = new StringBuilder(); int c = read(); while (isWhiteSpace(c)) { c = read(); } if (c < 0) { return null; } while (c >= 0 && !isWhiteSpace(c)) { sb.appendCodePoint(c); c = read(); } return sb.toString(); } static boolean isWhiteSpace(int c) { return c >= 0 && c <= 32; } public int nextInt() { int c = read(); while (isWhiteSpace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int ret = 0; while (c >= 0 && !isWhiteSpace(c)) { if (c < '0' || c > '9') { throw new NumberFormatException("digit expected " + (char) c + " found"); } ret = ret * 10 + c - '0'; c = read(); } return ret * sgn; } public long nextLong() { return Long.parseLong(next()); } public String readLine() { try { return super.readLine(); } catch (IOException e) { return null; } } public int[] readIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } }
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 11
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
e261727b1b6b758ee04c2a1a714afa20
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.rajan.codeforces.level_1200_1300; import java.io.*; public class A_B_C_Sort { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); int tt = Integer.parseInt(reader.readLine()); while (tt-- > 0) { int n = Integer.parseInt(reader.readLine()); int[] a = new int[n]; String[] temp = reader.readLine().split("\\s+"); for (int i = 0; i < n; i++) a[i] = Integer.parseInt(temp[i]); for (int i = n % 2; i < n - 1; i += 2) { if (a[i + 1] < a[i]) swap(a, i, i + 1); } boolean isSorted = true; for (int i = 1; i < n & isSorted; i++) if (a[i] < a[i - 1]) isSorted = false; writer.write(isSorted ? "YES\n" : "NO\n"); } writer.flush(); } private static void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[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 11
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
b91375deef1eb16b36e32e72d95ac3f4
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 cp { //start public static void main(String[] args) { var q = inp(); while(q-->0) { var n = inp(); var a = inp(n); var b = a.clone(); Arrays.sort(b); var chk = true; for(int i = n-1; i > 0; --i) { if((a[i]!=b[i]||a[i-1]!=b[i-1])&&(a[i]!=b[i-1]||a[i-1]!=b[i])) { chk=false; }else --i; } out(chk?"YES":"NO",false); } } //end public static int m=(int)1e9+7; public static Scanner sc = new Scanner(System.in); public static int inp() { int n = sc.hasNextInt()?sc.nextInt():0; sc.nextLine(); return n; } public static int bs(int[] a, int k) { return Arrays.binarySearch(a,k); } public static int[] inp(int n) { int[] a = new int[n]; for(int i=0;i<n;++i) a[i]=sc.hasNextInt()?sc.nextInt():0; sc.nextLine(); return a; } public static String inp(boolean s) { return sc.hasNextLine()?sc.nextLine():""; } public static void out(String s, boolean chk) { if(!chk) System.out.println(s); else System.out.print(s+" "); } public static void nl() { System.out.print("\n"); } public static void out(int[] a) { for(int i=0;i<a.length;++i) System.out.print(a[i]+" "); System.out.print("\n"); } public static void out(long[] a) { for(int i=0;i<a.length;++i) System.out.print(a[i]+" "); System.out.print("\n"); } public static void out(int n, boolean chk) { if(!chk) System.out.println(n); else System.out.print(n); } public static void out(long n, boolean chk) { if(!chk) System.out.println(n); else System.out.print(n); } }
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 11
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
483a8640eacc691101c10949ea42059e
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 Printing { public static void main(String [] args) { Scanner scan = new Scanner(System.in); // String nums = scan.nextLine(); // int n = Integer.parseInt(nums); int n = scan.nextInt(); for (int w = 0; w < n; w++) { int len = scan.nextInt(); int [] arr = new int[len]; for (int i = 0; i < len; i++) { arr[i] = scan.nextInt(); } response2(arr, len); } } public static void response(int [] arr, int len) { int m = (1000000000); boolean b = false; int prev = m; for (int i = len - 1; i >= 0; i--){ if (arr[i] > m){ System.out.println("No"); return; } if (b){ m = Math.min(m, prev); m = Math.min(arr[i], m); b = false; } else{ prev = arr[i]; b = true; } } System.out.println("Yes"); } public static void response2(int [] arr, int len) { int [] b = new int[len]; for (int i = len - 1; i >= 0; i = i - 2) { if ( i != 0 && arr[i] < arr[i - 1]) { b[i] = arr[i - 1]; b[i - 1] = arr[i]; } else if (i != 0) { b[i] = arr[i]; b[i - 1] = arr[i - 1]; } else { b[i] = arr[i]; } } // for (int i = 0; i < b.length; i++) { // System.out.println(b[i]); // } array(b); } public static void array(int [] arr) { for (int i = 0; i < arr.length - 1; i++) { if (arr[i] > arr[i + 1]) { System.out.println("No"); return; } } 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 11
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
86beedcd19795fe01f748da95a5eccc5
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 Printing { public static void main(String [] args) { Scanner scan = new Scanner(System.in); // String nums = scan.nextLine(); // int n = Integer.parseInt(nums); int n = scan.nextInt(); for (int w = 0; w < n; w++) { int len = scan.nextInt(); int [] arr = new int[len]; for (int i = 0; i < len; i++) { arr[i] = scan.nextInt(); } response(arr, len); } } public static void response(int [] arr, int len) { int m = (1000000000); boolean b = false; int prev = m; for (int i = len - 1; i >= 0; i--){ if (arr[i] > m){ System.out.println("No"); return; } if (b){ m = Math.min(m, prev); m = Math.min(arr[i], m); b = false; } else{ prev = arr[i]; b = true; } } 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 11
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
0886204399dd343f4fa0682239c5d8db
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 Main{ public static void printArr(int[] arr) { for(int i = 0; i < arr.length ; ++i ) { System.out.print(arr[i]+ " "); } System.out.println(); } public static boolean isSorted(int[]arr) { int cur = arr[0]; for(int i=1; i<arr.length; ++i) { if(cur<=arr[i]) { cur = arr[i]; }else { 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[]a = new int[n]; int aSize = n; for(int i=0; i < n; ++i) { a[i] = sc.nextInt(); } for(int i=n%2; i<n-1; 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"); } } } }
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 11
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
8e395098cdc172a7d279075d9e3dfe36
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
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static long getRow(long r) { long sum=0; sum+=1; long temp = 1; for(long i=1,up=r,down=1;i<=r;i++,up--, down++){ temp=temp*up/down; sum+=temp; } return sum; } public static void main(String[] args) { FastReader kb = new FastReader(); long tt=kb.nextLong(); while(tt-->0) { long n=kb.nextLong(); long []arr=new long[(int)n]; for(int i=0;i<arr.length;i++) { arr[i]=kb.nextLong(); } boolean check=true; if(arr.length%2==0) { for(int i=0;i<arr.length-1;i+=2) { if(arr[i+1]<arr[i]) { swap(arr,i,i+1); } } } else{ for(int i=1;i<arr.length-1;i+=2) { if(arr[i+1]<arr[i]) { swap(arr,i,i+1); } } } for(int i=0;i<arr.length-1;i++) { if(arr[i+1]<arr[i]) { check=false; } } System.out.println((check)?"YES":"NO"); } } public static void swap(long []arr,int i,int j) { long temp=arr[i]; arr[i]=arr[j]; arr[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 11
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
1af8564d03c6dd6bbd0f3442b5bae562
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.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; public class cf { public static void process() throws IOException { //code here int a[],b,c,d,max1,max2,flag=0; b=sc.nextInt(); a=new int[b]; for(c=0;c<b;c++) { a[c]=sc.nextInt(); } for(c=b%2;c<b;c=c+2) { if(a[c]>a[c+1]) {d=a[c]; a[c]=a[c+1]; a[c+1]=d; } } for(c=0;c<b-1;c++) { if(a[c]>a[c+1]) {flag=1; break;} } if(flag==1) System.out.println("NO"); else System.out.println("YES"); } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353; private static int N = 0; private static void google(int tt) { System.out.print("Case #" + (tt) + ": "); } static FastScanner sc; static FastWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new FastWriter(System.out); } else { sc = new FastScanner("input.txt"); out = new FastWriter("output.txt"); } long s = System.currentTimeMillis(); int t = 1; t = sc.nextInt(); int TTT = 1; while (t-- > 0) { // google(TTT++); process(); } out.flush(); // tr(System.currentTimeMillis()-s+"ms"); } private static boolean oj = System.getProperty("ONLINE_JUDGE") != null; private static void tr(Object... o) { if (!oj) System.err.println(Arrays.deepToString(o)); } static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Integer.compare(this.x, o.x); } /* @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Pair)) return false; Pair key = (Pair) o; return x == key.x && y == key.y; } @Override public int hashCode() { int result = x; result = 31 * result + y; return result; } */ } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long sqrt(long z) { long sqz = (long) Math.sqrt(z); while (sqz * 1L * sqz < z) { sqz++; } while (sqz * 1L * sqz > z) { sqz--; } return sqz; } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } public static long gcd(long a, long b) { if (a > b) a = (a + b) - (b = a); if (a == 0L) return b; return gcd(b % a, a); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int lower_bound(int[] arr, int x) { int low = 0, high = arr.length - 1, mid = -1; int ans = -1; while (low <= high) { mid = (low + high) / 2; if (arr[mid] > x) { high = mid - 1; } else { ans = mid; low = mid + 1; } } return ans; } public static int upper_bound(int[] arr, int x) { int low = 0, high = arr.length - 1, mid = -1; int ans = arr.length; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) { ans = mid; high = mid - 1; } else { low = mid + 1; } } return ans; } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void reverseArray(int[] a) { int n = a.length; int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void reverseArray(long[] a) { int n = a.length; long arr[] = new long[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } //custom multiset (replace with HashMap if needed) public static void push(TreeMap<Integer, Integer> map, int k, int v) { //map[k] += v; if (!map.containsKey(k)) map.put(k, v); else map.put(k, map.get(k) + v); } public static void pull(TreeMap<Integer, Integer> map, int k, int v) { //assumes map[k] >= v //map[k] -= v int lol = map.get(k); if (lol == v) map.remove(k); else map.put(k, lol - v); } // compress Big value to Time Limit public static int[] compress(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for (int x : arr) ls.add(x); Collections.sort(ls); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int boof = 1; //min value for (int x : ls) if (!map.containsKey(x)) map.put(x, boof++); int[] brr = new int[arr.length]; for (int i = 0; i < arr.length; i++) brr[i] = map.get(arr[i]); return brr; } // Fast Writer public static class FastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter() { out = null; } public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastWriter write(char c) { return write((byte) c); } public FastWriter write(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if (x == Integer.MIN_VALUE) { return write((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if (x < 0) { write('-'); x = -x; } x += Math.pow(10, -precision) / 2; // if(x < 0){ x = 0; } write((long) x).write("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; write((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastWriter writeln(char c) { return write(c).writeln(); } public FastWriter writeln(int x) { return write(x).writeln(); } public FastWriter writeln(long x) { return write(x).writeln(); } public FastWriter writeln(double x, int precision) { return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for (int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for (long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte) '\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for (char[] line : map) write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c) { return writeln(c); } public FastWriter println(int x) { return writeln(x); } public FastWriter println(long x) { return writeln(x); } public FastWriter println(double x, int precision) { return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } // Fast Inputs static class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] readArray(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] readArrayLong(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public int[][] readArrayMatrix(int N, int M, int Index) { if (Index == 0) { int[][] res = new int[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) res[i][j] = (int) nextLong(); } return res; } int[][] res = new int[N][M]; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) res[i][j] = (int) nextLong(); } return res; } public long[][] readArrayMatrixLong(int N, int M, int Index) { if (Index == 0) { long[][] res = new long[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) res[i][j] = nextLong(); } return res; } long[][] res = new long[N][M]; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) res[i][j] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] readArrayDouble(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
Java
["3\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 11
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
fe3b92dfac967dc89a809fce20b62539
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 Main { static long mod = (int)1e9+7; static PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; O : while(t-->0) { int n=sc.nextInt(); int a[]=sc.readArray(n); int b[]=a.clone(); radixSort2(b); for(int i=n-1;i>0;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;i++){ if(b[i]!=a[i]){ out.println("NO"); continue O; } } out.println("YES"); } out.flush(); } static void printN() { System.out.println("NO"); } static void printY() { System.out.println("YES"); } static int findfrequencies(int a[],int n) { int count=0; for(int i=0;i<a.length;i++) { if(a[i]==n) { count++; } } return count; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } static int[] EvenOddArragement(int nums[]) { int i1=0,i2=nums.length-1; while(i1<i2){ while(nums[i1]%2==0 && i1<i2){ i1++; } while(nums[i2]%2!=0 && i2>i1){ i2--; } int temp=nums[i1]; nums[i1]=nums[i2]; nums[i2]=temp; } return nums; } static int gcd(int a, int b) { while (b != 0) { int t = a; a = b; b = t % b; } return a; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static int DigitSum(int n) { int r=0,sum=0; while(n>=0) { r=n%10; sum=sum+r; n=n/10; } return sum; } static boolean checkPerfectSquare(int number) { double sqrt=Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static boolean isPowerOfTwo(int n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static boolean isPrime2(int n) { if (n <= 1) { return false; } if (n == 2) { return true; } if (n % 2 == 0) { return false; } for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2) { if (n % i == 0) { return false; } } return true; } static String minLexRotation(String str) { int n = str.length(); String arr[] = new String[n]; String concat = str + str; for(int i=0;i<n;i++) { arr[i] = concat.substring(i, i + n); } Arrays.sort(arr); return arr[0]; } static String maxLexRotation(String str) { int n = str.length(); String arr[] = new String[n]; String concat = str + str; for (int i = 0; i < n; i++) { arr[i] = concat.substring(i, i + n); } Arrays.sort(arr); return arr[arr.length-1]; } static class P implements Comparable<P> { int i, j; public P(int i, int j) { this.i=i; this.j=j; } public int compareTo(P o) { return Integer.compare(i, o.i); } } static class pair{ int i,j; pair(int x,int y){ i=x; j=y; } } static int binary_search(int a[],int value) { int start=0; int end=a.length-1; int mid=start+(end-start)/2; while(start<=end) { if(a[mid]==value) { return mid; } if(a[mid]>value) { end=mid-1; } else { start=mid+1; } mid=start+(end-start)/2; } return -1; } }
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 11
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
0be5020f806949cbfa87bb5d87fe241c
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 sortt { public static void main(String[] args){ Scanner sc=new Scanner (System.in); int t=sc.nextInt(); for (int i=1;i<=t;i++){ int n=sc.nextInt(); int a[]=new int[n]; for (int j=0;j<n;j++){ a[j]=sc.nextInt(); } int flag=0; if (n%2==0){ for (int j=0;j<n-1;j+=2){ if (a[j]>a[j+1]){ int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } if(j>0){ if (a[j-1]>a[j]){ System.out.println("NO"); flag=1; break; } } } } else{ for (int j=1;j<n-1;j+=2){ if (a[j]>a[j+1]){ int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } if(j>0){ if (a[j-1]>a[j]){ System.out.println("NO"); flag=1; break; } } } } if (flag==0){ System.out.println("YES"); } } 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 11
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
df30794895c6d6f7052060978b445705
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 Scanner obj = new Scanner(System.in); public static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int len = obj.nextInt(); while (len-- != 0) { int n = obj.nextInt(); long[] a=new long[n]; for(int i=0;i<n;i++)a[i]=obj.nextLong(); for(int i=n%2;i<n-1;i+=2) { if(a[i]>a[i+1]) { long temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } } boolean ans=true; for(int i=1;i<n;i++) { if(a[i]<a[i-1])ans=false; } if(ans)out.println("YES"); else out.println("NO"); } out.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 11
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
53b0accfdec12b862b1ff18f1626597f
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) { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); while (test-- > 0) { int n = sc.nextInt(); Deque<Integer> arr=new LinkedList<>(); for (int i = 0; i < n; i++) { arr.add(sc.nextInt()); } Integer p = null; boolean isSorted = true; while (arr.size() > 0) { Integer next = null; if (arr.size() % 2 == 0) { int f = arr.poll(); int s = arr.poll(); if (f < s) { next = f; arr.offerFirst(s); } else { next = s; arr.offerFirst(f); } } else { next = arr.poll(); } if (p != null && p > next) { isSorted = false; break; } p = next; } if (isSorted) { print("YES"); } else { print("NO"); } } } public static void print(Object o) { System.out.println(o); } }
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 11
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
66a4b7d97d20cd4bbcb838539a22189e
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) { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); while (test-- > 0) { int n = sc.nextInt(); Deque<Integer> arr=new ArrayDeque<Integer>(); for (int i = 0; i < n; i++) { arr.add(sc.nextInt()); } Integer p = null; boolean isSorted = true; while (arr.size() > 0) { Integer next = null; if (arr.size() % 2 == 0) { int f = arr.poll(); int s = arr.poll(); if (f < s) { next = f; arr.offerFirst(s); } else { next = s; arr.offerFirst(f); } } else { next = arr.poll(); } if (p != null && p > next) { isSorted = false; break; } p = next; } if (isSorted) { print("YES"); } else { print("NO"); } } } public static void print(Object o) { System.out.println(o); } }
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 11
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
8ccbe120760c1ed9888723c04ce5d614
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 { void go() { int n = Reader.nextInt(); int[] A = new int[n]; Deque<Integer> d1 = new ArrayDeque<>(); Deque<Integer> d2 = new ArrayDeque<>(); for(int i = 0; i < n; i++) { A[i] = Reader.nextInt(); } int l = Integer.MAX_VALUE; int r = Integer.MAX_VALUE; int m = 0; String ans = "YES"; if(n < 3) { ans = "YES"; } else { for(int i = n - 1; i >= 0; i -= 2) { int x = A[i]; int y = i - 1 >= 0 ? A[i - 1] : 0; if(x <= l && x <= r && y <= l && y <= r) { l = x; r = y; } else { ans = "NO"; break; } } } Writer.println(ans); } void solve() { for(int T = Reader.nextInt(); T > 0; T--) go(); } void run() throws Exception { Reader.init(System.in); Writer.init(System.out); solve(); Writer.close(); } public static void main(String[] args) throws Exception { new D().run(); } public static class Reader { public static StringTokenizer st; public static BufferedReader br; public static void init(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = new StringTokenizer(""); } public static String next() { while(!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new InputMismatchException(); } } return st.nextToken(); } public static int nextInt() { return Integer.parseInt(next()); } public static long nextLong() { return Long.parseLong(next()); } public static double nextDouble() { return Double.parseDouble(next()); } } public static class Writer { public static PrintWriter pw; public static void init(OutputStream os) { pw = new PrintWriter(new BufferedOutputStream(os)); } public static void print(String s) { pw.print(s); } public static void print(char c) { pw.print(c); } public static void print(int x) { pw.print(x); } public static void print(long x) { pw.print(x); } public static void println(String s) { pw.println(s); } public static void println(char c) { pw.println(c); } public static void println(int x) { pw.println(x); } public static void flush() { pw.flush(); } public static void println(long x) { pw.println(x); } public static void close() { pw.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 11
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
04bd8b0f9dbe3adcee8bbdbccfb197bf
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.Arrays; import java.util.Random; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { FastScanner fs = new FastScanner(); int ti = fs.nextInt(); outer: while (ti-- > 0) { int n = fs.nextInt(); int[] a = fs.readArray(n); for (int i = n - 1; i >= 1; i -= 2) { if (a[i - 1] > a[i]) { int temp = a[i - 1]; a[i - 1] = a[i]; a[i] = temp; } } for (int i = 1; i < n; i++) { if (a[i] < a[i - 1]) { System.out.println("NO"); continue outer; } } System.out.println("YES"); } } static final int mod = 1_000_000_007; static void sort(long[] a) { Random random = new Random(); int n = a.length; 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); } static void sort(int[] a) { Random random = new Random(); 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 long add(long a, long b) { return (a + b) % mod; } static long sub(long a, long b) { return ((a - b) % mod + mod) % mod; } static long mul(long a, long b) { return (a * b) % mod; } static long exp(long base, long exp) { if (exp == 0) return 1; long half = exp(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials = new long[2_000_001]; static long[] invFactorials = new long[2_000_001]; static void precompFacts() { factorials[0] = invFactorials[0] = 1; for (int i = 1; i < factorials.length; i++) factorials[i] = mul(factorials[i - 1], i); invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2); for (int i = invFactorials.length - 2; i >= 0; i--) invFactorials[i] = mul(invFactorials[i + 1], i + 1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k])); } static boolean isPrime(int n) { if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long binpow(long a, long b, long m) { a %= m; long res = 1; while (b > 0) { if (b % 2 == 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } static int divCount(int n) { boolean hash[] = new boolean[n + 1]; Arrays.fill(hash, true); for (int p = 2; p * p < n; p++) if (hash[p] == true) for (int i = p * 2; i < n; i += p) hash[i] = false; int total = 1; for (int p = 2; p <= n; p++) { if (hash[p]) { int count = 0; if (n % p == 0) { while (n % p == 0) { n = n / p; count++; } total = total * (count + 1); } } } return total; } 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()); } } static class Pair { int a; int b; public Pair(int a, int b) { this.a = a; this.b = 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 11
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
a82a6a68e6f592671aabd9576ef3ddec
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 A { static PrintWriter pw; static Scanner sc; public static void main(String[] args) throws IOException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] arr = sc.nextIntArray(n); boolean ans = true; for (int i = (n % 2 == 0) ? 0 : 1; 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++) ans &= arr[i] >= arr[i - 1]; pw.println(ans ? "YES" : "NO"); } pw.flush(); } 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 Scanner(String s) throws IOException { br = new BufferedReader(new FileReader(new File(s))); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["3\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 11
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
12395d75365393c352e5731ff165cf97
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 Main { static final PrintWriter out =new PrintWriter(System.out); static final FastReader sc = new FastReader(); /* _oo0oo_ o8888888o 88" . "88 (| -_- |) 0\ = /0 ___/`---'\___ .' \\| |// '. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' |_/ | \ .-\__ '-' ___/-. / ___'. .' /--.--\ `. .'___ ."" '< `.___\_<|>_/___.' >' "". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `_. \_ __\ /__ _/ .-` / / =====`-.____`.___ \_____/___.-`___.-'===== `=---=' */ public static boolean sorted(int a[]) { int n=a.length,i; int b[]=new int[n]; for(i=0;i<n;i++) b[i]=a[i]; Arrays.sort(b); for(i=0;i<n;i++) { if(a[i]!=b[i]) return false; } return true; } public static void main (String[] args) throws java.lang.Exception { int tes=sc.nextInt(); while(tes-->0) { int n=sc.nextInt(); int a[]=new int[n]; int i; for(i=0;i<n;i++) a[i]=sc.nextInt(); for(i=n-1;i>0;i-=2) { if(a[i-1]>a[i]) { int temp=a[i-1]; a[i-1]=a[i]; a[i]=temp; } } //System.out.println(Arrays.toString(a)); if(sorted(a)) System.out.println("YES"); else System.out.println("NO"); } } public static int first(ArrayList<Integer> arr, int low, int high, int x, int n) { if (high >= low) { int mid = low + (high - low) / 2; if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x) return mid; else if (x > arr.get(mid)) return first(arr, (mid + 1), high, x, n); else return first(arr, low, (mid - 1), x, n); } return -1; } public static int last(ArrayList<Integer> arr, int low, int high, int x, int n) { if (high >= low) { int mid = low + (high - low) / 2; if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x) return mid; else if (x < arr.get(mid)) return last(arr, low, (mid - 1), x, n); else return last(arr, (mid + 1), high, x, n); } return -1; } public static int lis(int[] arr) { int n = arr.length; ArrayList<Integer> al = new ArrayList<Integer>(); al.add(arr[0]); for(int i = 1 ; i<n;i++) { int x = al.get(al.size()-1); if(arr[i]>=x) { al.add(arr[i]); }else { int v = upper_bound(al, 0, al.size(), arr[i]); al.set(v, arr[i]); } } return al.size(); } public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k) { Collections.sort(ar); int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (ar.get((int)mid) <k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return -1; } return s; } public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k) { Collections.sort(ar); int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (ar.get(mid) <=k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return -1; } return s; } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long mod=1000000007; long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\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 11
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
fb91d98a69dc908663e461ffbd9d3933
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 ABCD { public static boolean sortlist(int[] a, int x){ if(x%2==0){ for(int i=0; i<x; i=i+2){ if(a[i]>a[i+1]){ int t = a[i]; a[i] = a[i+1]; a[i+1] = t; } if(i>0){ if(a[i]<a[i-1]){ return false; } } } } else{ for(int i=1; i<x; i=i+2){ if(a[i]>a[i+1]){ int t = a[i]; a[i] = a[i+1]; a[i+1] = t; } if(i>0){ if(a[i]<a[i-1]){ return false; } } } } return true; } public static void main(String args[]){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int j=0; j<t; j++){ int n = sc.nextInt(); int[] a = new int[n]; for(int i=0; i<n; i++){ a[i] = sc.nextInt();; } if(sortlist(a, n)) 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 11
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
8a36eef78dd9586789c7adb5c781bba3
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 sol { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int ar[]= new int[n]; for(int i=0;i<n;i++){ ar[i]=sc.nextInt(); } int ans[]=Arrays.copyOf(ar,ar.length); Arrays.sort(ans); // for(int i=0;i<n;i++){ // if(i!=0&&ar[i]>ar[i+1]){ // int temp=ar[i]; // ar[i]=ar[i+1]; // ar[i+1]=temp; // } // } for(int i=n-1;i>0;i-=2){ if(ar[i-1]>ar[i]){ int temp =ar[i]; ar[i]=ar[i-1]; ar[i-1]=temp; } } if(Arrays.equals(ar,ans)){ 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 11
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
6ecb490c95d037ce687dbdcbe0990e9f
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.text.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter writer = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int arr[] = new int[n]; int arr2[] = new int[n]; for(int i = 0; i< n; i++) { int y = sc.nextInt(); arr[i] = y; arr2[i] = y; } Arrays.sort(arr2); int ind = 0; if(n%2==1)ind = 1; if(check(arr,arr2,ind))writer.println("YES"); else writer.println("NO"); } writer.flush(); writer.close(); } private static boolean check(int[] arr, int[] arr2, int ind) { if(ind==1 && arr[0] != arr2[0])return false; for(int i = ind; i < arr.length - 1; i+=2) { if((arr[i] == arr2[i] && arr[i+1] == arr2[i+1]) || (arr[i] == arr2[i+1] && arr[i+1] == arr2[i]))continue; return false; } return true; } private static long power (long a, long n, long p) { long res = 1; while(n!=0) { if(n%2==1) { res=(res*a)%p; n--; }else { a= (a*a)%p; n/=2; } } return res; } private static boolean isPrime(int c) { for (int i = 2; i*i <= c; i++) { if(c%i==0)return false; } return true; } private static int find(int a , int arr[]) { if(arr[a] != a) return arr[(int)a] = find(arr[(int)a], arr); return arr[(int)a]; } private static void union(int a, int b, int arr[]) { int aa = find(a,arr); int bb = find(b, arr); arr[aa] = bb; } private static int gcd(int a, int b) { if(a==0)return b; return gcd(b%a, a); } public static int[] readIntArray(int size, FastReader s) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = s.nextInt(); } return array; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class SegmentTree{ int size; long arr[]; SegmentTree(int n){ size = 1; while(size<n)size*=2; arr = new long[size*2]; } public void build(int input[]) { build(input, 0,0, size); } public void set(int i, int v) { set(i,v,0,0,size); } // sum from l + r-1 public long sum(int l, int r) { return sum(l,r,0,0,size); } private void build(int input[], int x, int lx, int rx) { if(rx-lx==1) { if(lx < input.length ) arr[x] = 1; return; } int mid = (lx+rx)/2; build(input, 2*x+1, lx, mid); build(input,2*x+2,mid,rx); arr[x] = arr[2*x+1] + arr[2*x+2]; } private void set(int i, int v, int x, int lx, int rx) { if(rx-lx==1) { arr[x] = v; return; } int mid = (lx+rx)/2; if(i < mid) set(i, v, 2*x+1, lx, mid); else set(i,v,2*x+2,mid,rx); arr[x] = arr[2*x+1] + arr[2*x+2]; } private long sum(int l, int r, int x, int lx, int rx) { if(lx>=r || rx <= l)return 0; if(lx>=l && rx <= r)return arr[x]; int mid = (lx+rx)/2; long s1 = sum(l,r,2*x+1,lx,mid); long s2 = sum(l,r,2*x+2,mid,rx); return s2+s1; } // int first_above(int v, int l, int x, int lx, int rx) { // if(arr[x].a<v)return -1; // if(rx<=l)return -1; // if(rx-lx==1)return lx; // int m = (lx+rx)/2; // int res = first_above(v,l,2*x+1,lx,m); // if(res==-1)res = first_above(v,l,2*x+2,m,rx); // return res; // // } } class Trie{ Trie arr[] = new Trie[26]; boolean isEnd; Trie(){ for(int i = 0; i < 26; i++)arr[i] = null; isEnd = false; } } class Pair implements Comparable<Pair>{ long a; long b; long c; Pair(long a, long b, long c){ this.a = a; this.b = b; this.c = c; } Pair(long a, long b){ this.a = a; this.b = b; this.c = 0; } @Override public boolean equals(Object obj) { if(this == obj) return true; if(obj == null || obj.getClass()!= this.getClass()) return false; Pair pair = (Pair) obj; return (pair.a == this.a && pair.b == this.b && pair.c == this.c); } @Override public int compareTo(Pair o) { // if(o.a != this.a) return Long.compare(this.a, o.a); // else return Long.compare(o.b, this.b); } @Override public String toString() { return this.a + " " + this.b; } }
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 11
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
89ae1ca33ce2145cbcbb9fcaf9d541ea
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 { // 1. In sorting questions try to think about all possibilities like starting from start, end, middle. // 2. Two pointers, brute force. // 3. In graph query questions try to solve it reversely or try to process all the queries in a single parse. // 4. If order does not matter then just sort the data if constraints allow. It'll never harm. // 5. In greedy problems if you are just overwhelmed by the possibilities and stuck, try to code whatever you come up with. // 6. Try to solve it from back or reversely. // 7. When can't prove something take help of contradiction. // 8. In array question try to think of first occurrence of something and last occurrence and then maybe answer is their difference or anything. // 9. x%y < y Pretty easy, right? // 10. Take a case where answer is true and find why it is true?? // 11. Problems asking to minimize the maximum can be solved with binary search usually. // 12. If questions ask about changing values please think if there is any value that can't be changed. public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter writer = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int arr[] = new int[n]; Arrays.setAll(arr, i-> sc.nextInt()); boolean ans = true; int copy [] = Arrays.copyOf(arr, n); Arrays.sort(copy); if(n%2==0) { for(int i = 0; i < n-1; i+=2) { if((arr[i] == copy[i] && arr[i+1] == copy[i+1]) || (arr[i] == copy[i+1] && arr[i+1] == copy[i]))continue; else ans = false; } }else { if(arr[0] == copy[0]) { for(int i = 1; i < n-1; i+=2) { if((arr[i] == copy[i] && arr[i+1] == copy[i+1]) || (arr[i] == copy[i+1] && arr[i+1] == copy[i]))continue; else ans = false; } }else ans = false; } if(ans)writer.println("YES"); else writer.println("NO"); } writer.flush(); writer.close(); } private static long power (long a, long n, long p) { long res = 1; while(n!=0) { if(n%2==1) { res=(res*a)%p; n--; }else { a= (a*a)%p; n/=2; } } return res; } private static boolean isPrime(int c) { for (int i = 2; i*i <= c; i++) { if(c%i==0)return false; } return true; } private static int find(int a , int arr[]) { if(arr[a] != a) return arr[a] = find(arr[a], arr); return arr[a]; } private static void union(int a, int b, int arr[]) { int aa = find(a,arr); int bb = find(b, arr); arr[aa] = bb; } private static long gcd(long a, long b) { if(a==0)return b; return gcd(b%a, a); } public static int[] readIntArray(int size, FastReader s) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = s.nextInt(); } return array; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class Pair implements Comparable<Pair>{ int a; int b; Pair(int a, int b){ this.a = a; this.b = b; } @Override public boolean equals(Object obj) { if(this == obj) return true; if(obj == null || obj.getClass()!= this.getClass()) return false; Pair pair = (Pair) obj; return (pair.a == this.a && pair.b == this.b); } @Override public int hashCode() { return Objects.hash(a,b); } @Override public int compareTo(Pair o) { if(this.a == o.a) { return Integer.compare(this.b, o.b); }else { return Integer.compare(this.a, o.a); } } @Override public String toString() { return this.a + " " + this.b; } }
Java
["3\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 11
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
293508b1ec4954a2cb7fac30a9269af3
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 { FastReader sc = new FastReader(); PrintWriter writer = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int[] aa = new int[n]; for (int i = 0; i < n; i++) aa[i] = sc.nextInt(); for (int i = n % 2; i < n; i += 2) if (aa[i] > aa[i + 1]) { int tmp = aa[i]; aa[i] = aa[i + 1]; aa[i + 1] = tmp; } boolean yes = true; for (int i = 1; i < n; i++) if (aa[i - 1] > aa[i]) { yes = false; break; } writer.println(yes ? "YES" : "NO"); } writer.flush(); writer.close(); } private static long power (long a, long n, long p) { long res = 1; while(n!=0) { if(n%2==1) { res=(res*a)%p; n--; }else { a= (a*a)%p; n/=2; } } return res; } private static boolean isPrime(int c) { for (int i = 2; i*i <= c; i++) { if(c%i==0)return false; } return true; } private static int find(int a , int arr[]) { if(arr[a] != a) return arr[a] = find(arr[a], arr); return arr[a]; } private static void union(int a, int b, int arr[]) { int aa = find(a,arr); int bb = find(b, arr); arr[aa] = bb; } private static long gcd(long a, long b) { if(a==0)return b; return gcd(b%a, a); } public static int[] readIntArray(int size, FastReader s) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = s.nextInt(); } return array; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class Pair implements Comparable<Pair>{ int a; int b; Pair(int a, int b){ this.a = a; this.b = b; } @Override public boolean equals(Object obj) { if(this == obj) return true; if(obj == null || obj.getClass()!= this.getClass()) return false; Pair pair = (Pair) obj; return (pair.a == this.a && pair.b == this.b); } @Override public int hashCode() { return Objects.hash(a,b); } @Override public int compareTo(Pair o) { if(this.a == o.a) { return Integer.compare(this.b, o.b); }else { return Integer.compare(this.a, o.a); } } @Override public String toString() { return this.a + " " + this.b; } }
Java
["3\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 11
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
c4c01124f185a3d9b50bb05851f68b28
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 JavaApplication3 { public static void main(String[]args){ Scanner in =new Scanner(System.in); int t=in.nextInt(); while(t-- != 0){ int n=in.nextInt(); int[]arr =new int[n]; boolean flag=true; for(int i=0;i<n;++i)arr[i]=in.nextInt(); if(n==1){System.out.println("YES");continue;} 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; } } for(int i=0;i<n-1;++i)if(arr[i]>arr[i+1]){flag=false;break;} if(flag==true)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 11
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
ae572b7b7d4eec7cc49380a925a80662
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.System.out; import static java.lang.Math.*; import static java.lang.System.*; import java.lang.reflect.Array; import java.util.*; import java.io.*; import java.math.*; public class Main{ static FastReader sc; static long mod = ((long) 1e9) + 7; // static FastWriter out; static FastWriter out; static int imax=Integer.MAX_VALUE; static int imin=Integer.MIN_VALUE; static long lmax=Long.MAX_VALUE; static long lmin=Long.MIN_VALUE; public static void main(String hi[]) throws IOException { // initializeIO(); out = new FastWriter(); sc = new FastReader(); long startTimeProg = System.currentTimeMillis(); long endTimeProg = Long.MAX_VALUE; int t = sc.nextInt(); // int t=1; // boolean[] seave=sieveOfEratosthenes((int)(1e5)); // int[] seave2=sieveOfEratosthenesInt((long)sqrt(1e9)); // debug(seave2); // int ti=1; while (t-- != 0) { int n=sc.nextInt(); int[] arr=readIntArray(n); for(int i=n-1;i>0;i=i-2){ if(arr[i]<arr[i-1]){ int tr=arr[i]; arr[i]=arr[i-1]; arr[i-1]=tr; } } if(isSorted(arr))print("yes"); else print("no"); } endTimeProg = System.currentTimeMillis(); // debug("[finished : " + (endTimeProg - startTimeProg) + ".ms ]"); // System.out.println(String.format("%.9f", max)); } private static int findright(char a,Map<Character,Integer> map){ int c=0; for(int i=1;i<26;i++){ char x='a'; if((a+i)>=123){ x=(char)('a'+(a+i)%123); }else{ x=(char)(a+i); } c++; if(map.containsKey(x))break; } return c; } private static int findleft(char a,Map<Character,Integer> map){ int c=0; for(int i=25;i>=1;i--){ char x='a'; if((a+i)>=123){ x=(char)('a'+(a+i)%123); }else{ x=(char)(a+i); } c++; if(map.containsKey(x))break; } return c; } private static String chk(int[] arr, int n) throws IOException { String y="yes",no="no"; int l=imax,r=imax; boolean mid=true; int m=imax; for(int i=n-1;i>=0;i--){ if(l>r){ int temp=l; l=r; r=temp; } if(mid){ if(l>=m){ l=m; }else if(r>=m){ r=m; } if(arr[i]>l||arr[i]>r)return no; m=arr[i]; }else{ if(l>=arr[i]){ l=arr[i]; }else if(r>=arr[i]){ r=arr[i]; }else{ return "no"; } } mid=!mid; } return y; } private static boolean isPeak(int[] arr,int i,int l,int r){ if(i==l||i==r)return false; return (arr[i+1]<arr[i]&&arr[i]>arr[i-1]); } private static long kadens(List<Long> li, int l, int r) { long max = Long.MIN_VALUE; long ans = 0; for (int i = l; i <= r; i++) { ans += li.get(i); max = max(ans, max); if (ans < 0) ans = 0; } return max; } public static boolean isNumeric(String strNum) { if (strNum == null) { return false; } try { double d = Double.parseDouble(strNum); } catch (NumberFormatException nfe) { return false; } return true; } private static boolean isSorted(List<Integer> li) { int n = li.size(); if (n <= 1) return true; for (int i = 0; i < n - 1; i++) { if (li.get(i) > li.get(i + 1)) return false; } return true; } private static boolean isSorted(int[] arr) { int n = arr.length; if (n <= 1) return true; for (int i = 0; i < n - 1; i++) { if (arr[i] >arr[i+1]) return false; } return true; } static boolean isPowerOfTwo(long x) { return x != 0 && ((x & (x - 1)) == 0); } private static boolean ispallindromeList(List<Integer> res) { int l = 0, r = res.size() - 1; while (l < r) { if (res.get(l) != res.get(r)) return false; l++; r--; } return true; } private static class Pair { int first = 0; int sec = 0; int[] arr; char ch; String s; Map<Integer, Integer> map; Pair(int first, int sec) { this.first = first; this.sec = sec; } Pair(int[] arr) { this.map = new HashMap<>(); for (int x : arr) this.map.put(x, map.getOrDefault(x, 0) + 1); this.arr = arr; } Pair(char ch, int first) { this.ch = ch; this.first = first; } Pair(String s, int first) { this.s = s; this.first = first; } } private static int sizeOfSubstring(int st, int e) { int s = e - st + 1; return (s * (s + 1)) / 2; } private static Set<Long> factors(long n) { Set<Long> res = new HashSet<>(); // res.add(n); for (long i = 1; i * i <= (n); i++) { if (n % i == 0) { res.add(i); if (n / i != i) { res.add(n / i); } } } return res; } private static void print(String s) throws IOException { out.println(s); } private static long fact(long n) { if (n <= 2) return n; return n * fact(n - 1); } private static long ncr(long n, long r) { return fact(n) / (fact(r) * fact(n - r)); } private static int lessThen(long[] nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = 0; while (l <= r) { int mid = l + ((r - l) / 2); if (nums[mid] <= val) { i = mid; l = mid + 1; } else { r = mid - 1; } } return i; } private static int lessThen(List<Long> nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = 0; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) <= val) { i = mid; l = mid + 1; } else { r = mid - 1; } } return i; } private static int lessThen(List<Integer> nums, int val, boolean work, int l, int r) { int i = -1; if (work) i = 0; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) <= val) { i = mid; l = mid + 1; } else { r = mid - 1; } } return i; } private static int greaterThen(List<Long> nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = r; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) >= val) { i = mid; r = mid - 1; } else { l = mid + 1; } } return i; } private static int greaterThen(List<Integer> nums, int val, boolean work) { int i = -1, l = 0, r = nums.size() - 1; if (work) i = r; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) >= val) { i = mid; r = mid - 1; } else { l = mid + 1; } } return i; } private static int greaterThen(long[] nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = r; while (l <= r) { int mid = l + ((r - l) / 2); if (nums[mid] >= val) { i = mid; r = mid - 1; } else { l = mid + 1; } } return i; } private static long gcd(long[] arr) { long ans = 0; for (long x : arr) { ans = gcd(x, ans); } return ans; } private static int gcd(int[] arr) { int ans = 0; for (int x : arr) { ans = gcd(x, ans); } return ans; } private static long sumOfAp(long a, long n, long d) { long val = (n * (2 * a + ((n - 1) * d))); return val / 2; } //geometrics private static double areaOftriangle(double x1, double y1, double x2, double y2, double x3, double y3) { double[] mid_point = midOfaLine(x1, y1, x2, y2); // debug(Arrays.toString(mid_point)+" "+x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+" "+y3); double height = distanceBetweenPoints(mid_point[0], mid_point[1], x3, y3); double wight = distanceBetweenPoints(x1, y1, x2, y2); // debug(height+" "+wight); return (height * wight) / 2; } private static double distanceBetweenPoints(double x1, double y1, double x2, double y2) { double x = x2 - x1; double y = y2 - y1; return (Math.pow(x, 2) + Math.pow(y, 2)); } public static boolean isPerfectSquareByUsingSqrt(long n) { if (n <= 0) { return false; } double squareRoot = Math.sqrt(n); long tst = (long) (squareRoot + 0.5); return tst * tst == n; } private static double[] midOfaLine(double x1, double y1, double x2, double y2) { double[] mid = new double[2]; mid[0] = (x1 + x2) / 2; mid[1] = (y1 + y2) / 2; return mid; } private static long sumOfN(long n) { return (n * (n + 1)) / 2; } private static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } /* Function to calculate x raised to the power y in O(logn)*/ static long power(long x, long y) { long temp; if (y == 0) return 1l; temp = power(x, y / 2); if (y % 2 == 0) return (temp * temp); else return (x * temp * temp); } private static StringBuilder reverseString(String s) { StringBuilder sb = new StringBuilder(s); int l = 0, r = sb.length() - 1; while (l <= r) { char ch = sb.charAt(l); sb.setCharAt(l, sb.charAt(r)); sb.setCharAt(r, ch); l++; r--; } return sb; } private static void swap(List<Integer> li, int i, int j) { int t = li.get(i); li.set(i, li.get(j)); li.set(j, t); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static String decimalToString(int x) { return Integer.toBinaryString(x); } private static String decimalToString(long x) { return Long.toBinaryString(x); } private static boolean isPallindrome(String s, int l, int r) { while (l < r) { if (s.charAt(l) != s.charAt(r)) return false; l++; r--; } return true; } private static boolean isSubsequence(String s, String t) { if (s == null || t == null) return false; Map<Character, List<Integer>> map = new HashMap<>(); //<character, index> //preprocess t for (int i = 0; i < t.length(); i++) { char curr = t.charAt(i); if (!map.containsKey(curr)) { map.put(curr, new ArrayList<Integer>()); } map.get(curr).add(i); } int prev = -1; //index of previous character for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (map.get(c) == null) { return false; } else { List<Integer> list = map.get(c); prev = lessThen(list, prev, false, 0, list.size() - 1); if (prev == -1) { return false; } prev++; } } return true; } private static StringBuilder removeLeadingZero(StringBuilder sb) { int i = 0; while (i < sb.length() && sb.charAt(i) == '0') i++; // debug("remove "+i); if (i == sb.length()) return new StringBuilder(); return new StringBuilder(sb.substring(i, sb.length())); } private static StringBuilder removeEndingZero(StringBuilder sb) { int i = sb.length() - 1; while (i >= 0 && sb.charAt(i) == '0') i--; // debug("remove "+i); if (i < 0) return new StringBuilder(); return new StringBuilder(sb.substring(0, i + 1)); } private static int stringToDecimal(String binaryString) { // debug(decimalToString(n<<1)); return Integer.parseInt(binaryString, 2); } private static int stringToInt(String s) { return Integer.parseInt(s); } private static String toString(long val) { return String.valueOf(val); } private static void debug(int[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(long[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(List<int[]> arr) { for (int[] a : arr) { err.println(Arrays.toString(a)); } } private static void debug(float[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(double[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(boolean[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } // private static void print() throws IOException { // out.println(s); // } private static void debug(String s) throws IOException { err.println(s); } private static int charToIntS(char c) { return ((((int) (c - '0')) % 48)); } private static void print(double s) throws IOException { out.println(s); } private static void print(float s) throws IOException { out.println(s); } private static void print(long s) throws IOException { out.println(s); } private static void print(int s) throws IOException { out.println(s); } private static void debug(double s) throws IOException { err.println(s); } private static void debug(float s) throws IOException { err.println(s); } private static void debug(long s) { err.println(s); } private static void debug(int s) { err.println(s); } private static boolean isPrime(long n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return false; } return true; } private static List<List<Integer>> readUndirectedGraph(int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { int x = sc.nextInt(); int y = sc.nextInt(); graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readUndirectedGraph(int[][] intervals, int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < intervals.length; i++) { int x = intervals[i][0]; int y = intervals[i][1]; graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int[][] intervals, int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < intervals.length; i++) { int x = intervals[i][0]; int y = intervals[i][1]; graph.get(x).add(y); // graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { int x = sc.nextInt(); int y = sc.nextInt(); graph.get(x).add(y); // graph.get(y).add(x); } return graph; } static String[] readStringArray(int n) { String[] arr = new String[n]; for (int i = 0; i < n; i++) { arr[i] = sc.next(); } return arr; } private static Map<Character, Integer> freq(String s) { Map<Character, Integer> map = new HashMap<>(); for (char c : s.toCharArray()) { map.put(c, map.getOrDefault(c, 0) + 1); } return map; } private static Map<Long, Integer> freq(long[] arr) { Map<Long, Integer> map = new HashMap<>(); for (long x : arr) { map.put(x, map.getOrDefault(x, 0) + 1); } return map; } private static Map<Integer, Integer> freq(int[] arr) { Map<Integer, Integer> map = new HashMap<>(); for (int x : arr) { map.put(x, map.getOrDefault(x, 0) + 1); } return map; } static boolean[] sieveOfEratosthenes(long n) { boolean prime[] = new boolean[(int) n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } static int[] sieveOfEratosthenesInt(long n) { boolean prime[] = new boolean[(int) n + 1]; Set<Integer> li = new HashSet<>(); for (int i = 1; i <= n; i++) { prime[i] = true; li.add(i); } for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) { li.remove(i); prime[i] = false; } } } int[] arr = new int[li.size()]; int i = 0; for (int x : li) { arr[i++] = x; } return arr; } public static long Kadens(List<Long> prices) { long sofar = 0; long max_v = 0; for (int i = 0; i < prices.size(); i++) { sofar += prices.get(i); if (sofar < 0) { sofar = 0; } max_v = Math.max(max_v, sofar); } return max_v; } public static int Kadens(int[] prices) { int sofar = 0; int max_v = 0; for (int i = 0; i < prices.length; i++) { sofar += prices[i]; if (sofar < 0) { sofar = 0; } max_v = Math.max(max_v, sofar); } return max_v; } static boolean isMemberAC(int a, int d, int x) { // If difference is 0, then x must // be same as a. if (d == 0) return (x == a); // Else difference between x and a // must be divisible by d. return ((x - a) % d == 0 && (x - a) / d >= 0); } static boolean isMemberAC(long a, long d, long x) { // If difference is 0, then x must // be same as a. if (d == 0) return (x == a); // Else difference between x and a // must be divisible by d. return ((x - a) % d == 0 && (x - a) / d >= 0); } private static void sort(int[] arr) { int n = arr.length; List<Integer> li = new ArrayList<>(); for (int x : arr) { li.add(x); } Collections.sort(li); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sortReverse(int[] arr) { int n = arr.length; List<Integer> li = new ArrayList<>(); for (int x : arr) { li.add(x); } Collections.sort(li, Collections.reverseOrder()); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sort(double[] arr) { int n = arr.length; List<Double> li = new ArrayList<>(); for (double x : arr) { li.add(x); } Collections.sort(li); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sortReverse(double[] arr) { int n = arr.length; List<Double> li = new ArrayList<>(); for (double x : arr) { li.add(x); } Collections.sort(li, Collections.reverseOrder()); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sortReverse(long[] arr) { int n = arr.length; List<Long> li = new ArrayList<>(); for (long x : arr) { li.add(x); } Collections.sort(li, Collections.reverseOrder()); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sort(long[] arr) { int n = arr.length; List<Long> li = new ArrayList<>(); for (long x : arr) { li.add(x); } Collections.sort(li); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static long sum(int[] arr) { long sum = 0; for (int x : arr) { sum += x; } return sum; } private static long sum(long[] arr) { long sum = 0; for (long x : arr) { sum += x; } return sum; } private static long evenSumFibo(long n) { long l1 = 0, l2 = 2; long sum = 0; while (l2 < n) { long l3 = (4 * l2) + l1; sum += l2; if (l3 > n) break; l1 = l2; l2 = l3; } return sum; } private static void initializeIO() { try { System.setIn(new FileInputStream("input")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); System.setErr(new PrintStream(new FileOutputStream("error.txt"))); } catch (Exception e) { System.err.println(e.getMessage()); } } private static int maxOfArray(int[] arr) { int max = Integer.MIN_VALUE; for (int x : arr) { max = Math.max(max, x); } return max; } private static long maxOfArray(long[] arr) { long max = Long.MIN_VALUE; for (long x : arr) { max = Math.max(max, x); } return max; } private static int[][] readIntIntervals(int n, int m) { int[][] arr = new int[n][m]; for (int j = 0; j < n; j++) { for (int i = 0; i < m; i++) { arr[j][i] = sc.nextInt(); } } return arr; } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static int[] readIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } return arr; } private static double[] readDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextDouble(); } return arr; } private static long[] readLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); } return arr; } private static void print(int[] arr) throws IOException { out.println(Arrays.toString(arr)); } private static void print(long[] arr) throws IOException { out.println(Arrays.toString(arr)); } private static void print(String[] arr) throws IOException { out.println(Arrays.toString(arr)); } private static void print(double[] arr) throws IOException { out.println(Arrays.toString(arr)); } private static void debug(String[] arr) { err.println(Arrays.toString(arr)); } private static void debug(Boolean[][] arr) { for (int i = 0; i < arr.length; i++) err.println(Arrays.toString(arr[i])); } private static void debug(int[] arr) { err.println(Arrays.toString(arr)); } private static void debug(long[] arr) { err.println(Arrays.toString(arr)); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { BufferedWriter bw; List<String> list = new ArrayList<>(); Set<String> set = new HashSet<>(); FastWriter() { bw = new BufferedWriter(new OutputStreamWriter(System.out)); } <T> void print(T obj) throws IOException { bw.write(obj.toString()); bw.flush(); } void println() throws IOException { print("\n"); } <T> void println(T obj) throws IOException { print(obj.toString() + "\n"); } void print(int[] arr) throws IOException { for (int x : arr) { print(x + " "); } println(); } void print(long[] arr) throws IOException { for (long x : arr) { print(x + " "); } println(); } void print(double[] arr) throws IOException { for (double x : arr) { print(x + " "); } println(); } void printCharN(char c, int n) throws IOException { for (int i = 0; i < n; i++) { print(c); } } } static class Dsu { int[] parent, size; Dsu(int n) { parent = new int[n + 1]; size = new int[n + 1]; for (int i = 0; i <= n; i++) { parent[i] = i; size[i] = 1; } } private int findParent(int u) { if (parent[u] == u) return u; return parent[u] = findParent(parent[u]); } private boolean union(int u, int v) { // System.out.println("uf "+u+" "+v); int pu = findParent(u); // System.out.println("uf2 "+pu+" "+v); int pv = findParent(v); // System.out.println("uf3 " + u + " " + pv); if (pu == pv) return false; if (size[pu] <= size[pv]) { parent[pu] = pv; size[pv] += size[pu]; } else { parent[pv] = pu; size[pu] += size[pv]; } return true; } } }
Java
["3\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 11
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
1dd45846cfb387a7186380fba53d086f
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
// D. A-B-C Sort // Codeforces Round #786 (Div. 3) // Codeforces // Solution by Anmol Sharma import java.util.*; import java.lang.*; import java.io.*; public class Main { public static long mod = 1000000007; public static long mod2 = 998244353; public static void main(String[] args) throws java.lang.Exception { Reader sc = new Reader(); FastNum in = new FastNum(System.in); Writer out = new Writer(System.out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int tests = in.nextInt(); for (int test = 1; test <= tests; test++) { int n = in.nextInt(); int[] a = in.nextIntArray(n); for(int i = n - 2; i >= 0; i -= 2) { if(a[i] > a[i + 1]) { int t = a[i]; a[i] = a[i + 1]; a[i + 1] = t; } } boolean flag = true; for(int i = 0; i < n - 1; i++) { if(a[i] > a[i + 1]) { flag = false; break; } } if(flag) { out.println("YES"); } else { out.println("NO"); } } out.flush(); } static long modSum(long p, long q) { if (q > 0) { return (p + q) % mod; } else { return (p + q + mod) % mod; } } static long invMod(long p, long q, long m) { long expo = m - 2; while (expo != 0) { if ((expo & 1) == 1) { p = (p * q) % m; } q = (q * q) % m; expo >>= 1; } return p; } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } public static long power(long a, long b) { if (b == 0) return 1; long answer = power(a, b / 2) % mod; answer = (answer * answer) % mod; if (b % 2 != 0) answer = (answer * a) % mod; return answer; } public static void swap(int x, int y) { int t = x; x = y; y = t; } public static long min(long a, long b) { if (a < b) return a; return b; } public static long divide(long a, long b) { return (a % mod * (power(b, mod - 2) % mod)) % mod; } public static long nCr(long n, long r) { long answer = 1; long k = min(r, n - r); for (long i = 0; i < k; i++) { answer = (answer % mod * (n - i) % mod) % mod; answer = divide(answer, i + 1); } return answer % mod; } public static boolean plaindrome(String str) { StringBuilder sb = new StringBuilder(); sb.append(str); return (str.equals((sb.reverse()).toString())); } public static class Pair { int a; int b; Pair(int s, int e) { a = s; b = e; } static void sort(Pair[] a) { Arrays.sort(a, (o1, o2) -> { if (o1.a == o2.a) { return o1.b - o2.b; } else { return o1.a - o2.a; } }); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return a == pair.a && b == pair.b; } @Override public int hashCode() { return Objects.hash(a, b); } } static class Assert { static void check(boolean e) { if (!e) { throw new AssertionError(); } } } static class FastNum implements AutoCloseable { InputStream is; byte buffer[] = new byte[1 << 16]; int size = 0; int pos = 0; FastNum(InputStream is) { this.is = is; } int nextChar() { if (pos >= size) { try { size = is.read(buffer); } catch (IOException e) { throw new IOError(e); } pos = 0; if (size == -1) { return -1; } } Assert.check(pos < size); int c = buffer[pos] & 0xFF; pos++; return c; } int nextInt() { int c = nextChar(); while (c == ' ' || c == '\r' || c == '\n' || c == '\t') { c = nextChar(); } if (c == '-') { c = nextChar(); Assert.check('0' <= c && c <= '9'); int n = -(c - '0'); c = nextChar(); while ('0' <= c && c <= '9') { int d = c - '0'; c = nextChar(); Assert.check(n > Integer.MIN_VALUE / 10 || n == Integer.MIN_VALUE / 10 && d <= -(Integer.MIN_VALUE % 10)); n = n * 10 - d; } return n; } else { Assert.check('0' <= c && c <= '9'); int n = c - '0'; c = nextChar(); while ('0' <= c && c <= '9') { int d = c - '0'; c = nextChar(); Assert.check( n < Integer.MAX_VALUE / 10 || n == Integer.MAX_VALUE / 10 && d <= Integer.MAX_VALUE % 10); n = n * 10 + d; } return n; } } char nextCh() { int c = nextChar(); while (c == ' ' || c == '\r' || c == '\n' || c == '\t') { c = nextChar(); } return (char) c; } long nextLong() { int c = nextChar(); while (c == ' ' || c == '\r' || c == '\n' || c == '\t') { c = nextChar(); } if (c == '-') { c = nextChar(); Assert.check('0' <= c && c <= '9'); long n = -(c - '0'); c = nextChar(); while ('0' <= c && c <= '9') { int d = c - '0'; c = nextChar(); Assert.check(n > Long.MIN_VALUE / 10 || n == Long.MIN_VALUE / 10 && d <= -(Long.MIN_VALUE % 10)); n = n * 10 - d; } return n; } else { Assert.check('0' <= c && c <= '9'); long n = c - '0'; c = nextChar(); while ('0' <= c && c <= '9') { int d = c - '0'; c = nextChar(); Assert.check( n < Long.MAX_VALUE / 10 || n == Long.MAX_VALUE / 10 && d <= Long.MAX_VALUE % 10); n = n * 10 + d; } return n; } } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } char[] nextCharArray(int n) { char[] arr = new char[n]; for (int i = 0; i < n; i++) { arr[i] = nextCh(); } return arr; } @Override public void close() { } } static class Writer extends PrintWriter { public Writer(java.io.Writer out) { super(out); } public Writer(java.io.Writer out, boolean autoFlush) { super(out, autoFlush); } public Writer(OutputStream out) { super(out); } public Writer(OutputStream out, boolean autoFlush) { super(out, autoFlush); } public void printArray(int[] arr) { for (int j : arr) { print(j); print(' '); } println(); } public void printArray(long[] arr) { for (long j : arr) { print(j); print(' '); } println(); } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
Java
["3\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 11
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
7b34495a11d87c5ca47d84b2548f1b26
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 bd3 { private static class pair implements Comparable<pair> { int low, high; pair(int low, int high) { this.low = low; this.high = high; } @Override public int hashCode() { // uses roll no to verify the uniqueness // of the object of Student class final int temp = 14; int ans = 1; ans = (temp * ans) + low +high; return ans; } public int compareTo(pair o) { return this.low-o.low; } // Equal objects must produce the same // hash code as long as they are equal @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.low != other.low && this.high !=other.high) { return false; } return true; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private InputReader.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); } } public static void main(String[] args) { InputReader in=new InputReader(System.in); PrintWriter out=new PrintWriter(System.out); int t = in.nextInt(); while (t-- != 0) { int n=in.nextInt(); int a[]=new int[n];int a1[]=new int[n]; for(int i=0;i<n;i++) { a[i]=in.nextInt();a1[i]=a[i]; }Arrays.sort(a1); boolean chk=true; for(int i=(n%2==0)?0: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;i++){ if(a[i]!=a1[i]){chk=false;break;}} if(chk) { out.println("YES"); }else{ out.println("NO"); } }out.close();} private static int solve(int n, int m) {return -1; } }
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 11
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
5776bb5bf6123fec41c8f71888903aa7
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 Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); while (n != 0) { int size = sc.nextInt(); int[] arr = new int[size]; for (int i = 0; i < arr.length; i++) arr[i] = sc.nextInt(); int[] b = new int[size]; for (int i = 0; i < arr.length; i++) b[i] = arr[i]; Arrays.sort(b); for (int i = arr.length-1; i >= 0; i-=2) { if (i != 0) { if (arr[i-1] > arr[i]) { int temp = arr[i-1]; arr[i-1] = arr[i]; arr[i] = temp; } } } boolean check = false; for (int i = 0; i < arr.length; i++) { if (arr[i] != b[i]) { System.out.println("NO"); check = true; break; } } if (!check)System.out.println("YES"); n--; } } }
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 11
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
9765d463bd51c2511ebca08a9f668447
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.util.Arrays; import java.util.StringTokenizer; public class ABCSort { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine().trim()); var sb = new StringBuilder(); while (t-- > 0) { int N = Integer.parseInt(br.readLine().trim()); int arr[] = nextIntArray(N, br); sb.append(solve(arr)).append("\n"); } br.close(); System.out.println(sb); } private static String solve(int arr[]) { int temp[] = Arrays.copyOf(arr, arr.length); sort(arr); for(int i = temp.length%2; i < temp.length; i += 2) { if(temp[i] > temp[i+1]) swap(temp, i, i+1); } for(int i = 0; i < temp.length; i++) if(temp[i] != arr[i]) return "NO"; return "YES"; } private static void swap(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } private static void sort(int arr[]) { Integer temp[] = Arrays.stream(arr).boxed().toArray(Integer[]::new); Arrays.sort(temp); for(int i = 0; i < temp.length; i++) arr[i] = temp[i]; } private static int[] nextIntArray(int N, BufferedReader br) throws Exception { StringTokenizer st = new StringTokenizer(br.readLine().trim(), " "); int arr[] = new int[N]; for(int i = 0; i < arr.length; 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 11
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
740d5dea925b1c14b4d6ace3943f3be3
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.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author @debanjandhar12 */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "@debanjandhar12", 1 << 29); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(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, InputReader in, OutputWriter out) { int n = in.readInt(); int[] a = in.readIntArray(n); for (int i = n - 1; i >= 1; i -= 2) { if (a[i] < a[i - 1]) { // swap int temp = a[i]; a[i] = a[i - 1]; a[i - 1] = temp; } } boolean ansPos = true; // chk if sorted for (int i = 1; i < n; i++) { if (a[i] < a[i - 1]) { ansPos = false; } } out.printLine(ansPos ? "YES" : "NO"); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = readInt(); } return array; } 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 readInt() { 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 readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } 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 11
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
43c44fa1281fb63944581d32ec2c3868
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 { private static String solve(int n, int[] arr) { if (n <= 2) return "YES"; int i = 0; if (n%2 == 1) i = 1; for (; 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 (i=0; i<n-1; ++i) { if (arr[i] > arr[i+1]) return "NO"; } return "YES"; } public static void main(String args[]) { FastReader fs = new FastReader(); int t = fs.nextInt(); while (t-- > 0) { int n = fs.nextInt(); int[] arr = fs.nextArr(n); System.out.println(solve(n, arr)); } } } class FastReader{ BufferedReader br; StringTokenizer st; FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } int[] nextArr(int n) { int[] a = new int[n]; for(int i=0; i<n; i++){ a[i] = 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 11
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
8c41fb2ed32e705bc74ceb5e89a27ee8
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 { private static String solve(int n, int[] arr) { var a = new ArrayList<Integer>(); int i = 0; if (n % 2 == 1) { a.add(arr[0]); i = 1; } for (; i<n-1; i += 2) { a.add(Math.min(arr[i], arr[i+1])); a.add(Math.max(arr[i], arr[i+1])); } for (i=0; i<a.size()-1; ++i) { if (a.get(i) > a.get(i+1)) return "NO"; } return "YES"; } public static void main(String args[]) { FastReader fs = new FastReader(); int t = fs.nextInt(); while (t-- > 0) { int n = fs.nextInt(); int[] arr = fs.nextArr(n); System.out.println(solve(n, arr)); } } } class FastReader{ BufferedReader br; StringTokenizer st; FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } int[] nextArr(int n) { int[] a = new int[n]; for(int i=0; i<n; i++){ a[i] = 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 11
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
c333ce209718ba93873f4bfdb82121da
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 D_A_B_C_Sort { public static void main(String[] args) { FastReader rd = new FastReader(); // StringBuilder bd = new StringBuilder(); int t = rd.nextInt(); while(t!=0) { int n = rd.nextInt(); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] = rd.nextInt(); } int i=0; if(n%2!=0) i=1; while(i<n-1) { if(a[i]>a[i+1]) { int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } i+=2; } boolean found = false; i=0; for(i=0;i<n-1;i++) { if(a[i]>a[i+1]) { found = true; break; } } if(found) outn("NO"); else outn("YES"); t--; } } public static void out(Object obj) { System.out.print(obj); } public static void outn(Object obj) { System.out.println(obj); } public static void outn() { System.out.println(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } // class Pair { // int first; // int second; // public Pair(int first, int second) { // this.first = first; // this.second = second; // } // } // class Compare { // static void compare(Pair arr[], int PRIORITY) { // Arrays.sort(arr, new Comparator<Pair>(){ // @Override // public int compare(Pair p1, Pair p2) { // if(PRIORITY == 1) return p1.second - p2.second; // else return p1.first - p2.first; // } // }); // } // }
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 11
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
8871c306e074c8718e129d237b68f788
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 D { FastScanner sc; PrintWriter out; public void solve() throws Exception { Locale.setDefault(Locale.US); sc = new FastScanner(System.in); out = new PrintWriter(System.out, true); int t = sc.nextInt(); for (; t > 0; t--) { var a = new LinkedList<Integer>(); var b = new LinkedList<Integer>(); var c = new LinkedList<Integer>(); int n = sc.nextInt(); for (int i = 0; i < n; i++) a.add(sc.nextInt()); var iter = b.listIterator(); while (!a.isEmpty()) { int x = a.pollLast(); if (b.size() % 2 == 0 || b.size() == 1) { iter.add(x); } else { iter.next(); int l = iter.next(); iter.previous(); int r = iter.previous(); if (r < l) iter.next(); iter.add(x); } centerIter(b.size(), iter); //out.println("pos: " + iter.nextIndex()); //out.println(b); } iter = b.listIterator(); centerIter(b.size(), iter); while (!b.isEmpty()) { //out.println("b: " + b); //out.println("pos: " + iter.nextIndex()); if (b.size() % 2 == 0) { int l = iter.previous(); iter.next(); int r = iter.next(); iter.previous(); //out.println("r: " + r); c.add(Math.min(l, r)); if (l < r) iter.previous(); iter.remove(); } else { c.add(iter.next()); iter.remove(); } centerIter(b.size(), iter); } //out.println("c: " + c); boolean ok = true; var cIter = c.iterator(); int prev = cIter.next(); while (cIter.hasNext()) { int x = cIter.next(); if (x < prev) { ok = false; break; } prev = x; } out.println(ok ? "YES" : "NO"); } out.close(); sc.close(); } void centerIter(int n, ListIterator<?> iter) { while (iter.nextIndex() > n/2) iter.previous(); while (iter.previousIndex() < n/2 - 1) iter.next(); } static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream in) throws Exception { br = new BufferedReader(new InputStreamReader(in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String line = ""; try { line = br.readLine(); } catch (Exception e) { e.printStackTrace(); } return line; } void close() throws Exception { br.close(); } } public static void main(String[] arg) throws Exception { new D().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 11
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
f3cafaf82c6c7a42992401731be1cbf7
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 Main4 { static int[] list = new int[222222]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); boolean flag; while (T-- > 0) { int n = sc.nextInt(); for (int i = 1; i <= n; i++) { list[i] = sc.nextInt(); } flag = true; for (int i = n - 2; i >= 1; i -= 2) { if (Math.min(list[i + 1], list[i + 2]) < Math.max(list[i - 1], list[i])) { flag = false; } } 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 11
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
4626103bff93868ae3a65b05bbffad71
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 D { public static void main(String[] args) { FastScanner sc = new FastScanner(); 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 >= 1; i -= 2){ int right = a[i]; int left = a[i - 1]; // System.out.println(left + " " + right); if(left > right){ 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 boolean check(int[] arr){ for(int i = 1; i < arr.length; i++){ if(arr[i] < arr[i - 1]) return false; } return true; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\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 11
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
1392a29f316d9f13d5620d2a259ed70e
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.ArrayList; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; public class D { static class RealScanner { 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()); } } public static final class Pair<T1, T2> { public T1 first; public T2 second; public Pair() { first = null; second = null; } public Pair(T1 firstValue, T2 secondValue) { first = firstValue; second = secondValue; } public Pair(Pair<T1, T2> pair) { first = pair.first; second = pair.second; } } public static void main(String[] args) { RealScanner sc = new RealScanner(); 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()); } List<Integer> my = new ArrayList<>(); my.addAll(list); Collections.sort(my); // System.out.println(list); // System.out.println(my); // if (n == 1) { // System.out.println("YES"); // continue; // } // int count = 1; // for (int i = 1; i < n; i++) { // if (list.get(i) < list.get(i - 1)) { // count++; // } // } // if (count == n) { // System.out.println("NO"); // continue; // } // count = 1; // for (int i = 1; i < n; i++) { // if (list.get(i) > list.get(i - 1)) { // count++; // } // } // if (count == n) { // System.out.println("NO"); // continue; // } // System.out.println("YES"); for (int i = n - 1; i >= 1; i -= 2) { if (list.get(i - 1) > list.get(i)) { int temp = list.get(i - 1); list.set(i - 1, list.get(i)); list.set(i, temp); } } if (my.equals(list)) { 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 11
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
a21619a0fff649257ffb5e731be91cb5
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 { static boolean cases = true; // Solution static void solve(int t) { int n = sc.nextInt(); int a[] = sc.readIntArray(n); int b[] = a.clone(); sort(b); 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; } } for (int i = 0; i < n; i++) { if (a[i] != b[i]) { System.out.println("NO"); return; } } System.out.println("YES"); } public static void main(String[] args) { int c = 1; for (int t = (cases ? sc.nextInt() : 0); t > 1; t--, c++) solve(c); solve(c); } static final int mod = 1_000_000_007; static long add(long a, long b) { return (a + b) % mod; } static long sub(long a, long b) { return ((a - b) % mod + mod) % mod; } static long mul(long a, long b) { return (a * b) % mod; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static 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[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } char[] readCharArray(int n) { char a[] = new char[n]; String s = sc.next(); for (int i = 0; i < n; i++) { a[i] = s.charAt(i); } 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()); } } private static final FastScanner sc = new FastScanner(); private static PrintWriter out = new PrintWriter(System.out); }
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 11
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
164a6d57b087179f693148fa2a2148e6
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 cf1674D { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tt = sc.nextInt(); while(tt-->0){ int n = sc.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } if(n <= 2){ System.out.println("YES"); continue; } for (int i = n-1; i > 0; i-=2) { if(arr[i]<arr[i-1]){ int t = arr[i]; arr[i] = arr[i-1]; arr[i-1] = t; } } boolean s = true; for (int i = 0; i < n-1; i++) { if(arr[i] > arr[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 11
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
85bba9159eab028a6e953a9dd9027a99
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.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeSet; public class Main { public static void main(String[]args) throws IOException { new Main().run(); } void run() throws IOException{ new solve().setIO(System.in, System.out).run(); } public class solve extends ioTask{ int t,n,s,k,i,j,len,h,m; int b,x,y; public void run() throws IOException { t=in.in(); for(int te=1;te<=t;te++) { n=in.in(); int[]a=new int[n]; for(int i=0;i<n;i++){ a[i]=in.in(); } for(int i=n-1;i>=1;i-=2) { if(a[i-1]>a[i]) { int tmp=a[i-1]; a[i-1]=a[i]; a[i]=tmp; } } boolean ans=true; for(int i=1;i<n;i++) { if(a[i-1]>a[i]) { ans=false; break; } } out.println(ans?"YES":"NO"); } out.close(); } } class In{ private StringTokenizer in=new StringTokenizer(""); private InputStream is; private BufferedReader bf; public In(File file) throws IOException { is=new FileInputStream(file); init(); } public In(InputStream is) throws IOException { this.is=is; init(); } private void init() throws IOException { bf=new BufferedReader(new InputStreamReader(is)); } boolean hasNext() throws IOException { return in.hasMoreTokens()||bf.ready(); } String ins() throws IOException { while(!in.hasMoreTokens()) { in=new StringTokenizer(bf.readLine()); } return in.nextToken(); } int in() throws IOException { return Integer.parseInt(ins()); } long inl() throws IOException { return Long.parseLong(ins()); } double ind() throws IOException { return Double.parseDouble(ins()); } } class Out{ PrintWriter out; private OutputStream os; private void init() { out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(os))); } public Out(File file) throws IOException { os=new FileOutputStream(file); init(); } public Out(OutputStream os) throws IOException { this.os=os; init(); } } class graph{ int[]to,nxt,head; int cnt; void init(int n) { cnt=1; for(int i=1;i<=n;i++) { head[i]=0; } } public graph(int n,int m) { to=new int[m+1]; nxt=new int[m+1]; head=new int[n+1]; cnt=1; } void add(int u,int v) { to[cnt]=v; nxt[cnt]=head[u]; head[u]=cnt++; } } abstract class ioTask{ In in; PrintWriter out; public ioTask setIO(File in,File out) throws IOException{ this.in=new In(in); this.out=new Out(out).out; return this; } public ioTask setIO(File in,OutputStream out) throws IOException{ this.in=new In(in); this.out=new Out(out).out; return this; } public ioTask setIO(InputStream in,OutputStream out) throws IOException{ this.in=new In(in); this.out=new Out(out).out; return this; } public ioTask setIO(InputStream in,File out) throws IOException{ this.in=new In(in); this.out=new Out(out).out; return this; } void run()throws IOException{ } } }
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 11
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
86c07e6f45305985279ed7caef1315b3
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 Main { public static void main(String args[]) { Scanner x=new Scanner(System.in); int t; t=x.nextInt(); while(t>0) { t--; int n=x.nextInt(); int[] a=new int[n]; int[] b=new int[n]; int i; for(i=0;i<n;i++) { a[i]=x.nextInt(); b[i]=a[i]; } if(n%2==1) { for(i=1;i<n-1;i+=2) { if(a[i]>a[i+1]) { int k=a[i]; a[i]=a[i+1]; a[i+1]=k; } }} else { for(i=0;i<n-1;i+=2) { if(a[i]>a[i+1]) { int k=a[i]; a[i]=a[i+1]; a[i+1]=k; }}} Arrays.sort(b); for(i=0;i<n;i++) { if(a[i]!=b[i]) break; } System.out.println((i==n || n==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 11
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
67f427b280d32bfa96f993a4e18944b6
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.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class A_Number_Transformation{ private static void swap(long[] x, int i, int j) { final long t = x[i]; x[i] = x[j]; x[j] = t; } public static void main (String[] args) throws java.lang.Exception { FastReader sc = new FastReader() ; int check = sc.nextInt(); for(int tp=0;tp<check;tp++) { int n= sc.nextInt(); long arr[]=new long [n]; for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); } if(n==1){System.out.println("YES");continue;} for(int i=n-1;i>0;i-=2) { if(arr[i]<arr[i-1]) { long tmp=arr[i]; arr[i]=arr[i-1]; arr[i-1]=tmp; } } int f=0; for(int i=1;i<n;i++) { if(arr[i]<arr[i-1]) { System.out.println("NO"); f=1; break; } } if(f==0) System.out.println("YES"); //HASHMAP MAP NO REPETITION //HashMap <Long,Long> hm = new HashMap<Long,Long> (); //h1.put(1,4); //h1.forEach((k,v)->System.out.println(v+" and "+k)); //System.out.println(h1.getOrDefault(100,1)); //ARRAYLIST ARRAY ALTERNATIVE //ArrayList<Long> al = new ArrayList<Long> (); // ArrayList<ArrayList<Long>> al = new ArrayList<> (); // for(int i=0;i<3;i++) // { // al.add(new ArrayList<>()); // } //al.add(sc.nextLong()); //al.forEach((x) -> System.out.println(x*x)); //al.removeIf(x->(x%2==0)); //ORDERED IN ASCENDING //TreeMap <Long,Long> tm = new TreeMap <Long,Long>(); //tm.put(10,8); //System.out.println(tm.subMap(2,5)); // Start here } } static final class Utils { private static class Shuffler { private static void shuffle(int[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } private static void shuffle(long[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } private static void swap(int[] x, int i, int j) { final int t = x[i]; x[i] = x[j]; x[j] = t; } private static void swap(long[] x, int i, int j) { final long t = x[i]; x[i] = x[j]; x[j] = t; } } public static void shuffleSort(int[] arr) { Shuffler.shuffle(arr); Arrays.sort(arr); } public static void shuffleSort(long[] arr) { Shuffler.shuffle(arr); Arrays.sort(arr); } private Utils() {} } static class FastReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } FastReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { final byte[] buf = new byte[1024]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextSign() throws IOException { byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } 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 char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public int[] nextIntArray(int n) throws IOException { final int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long[] nextLongArray(int n) throws IOException { final long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } }
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 11
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
e5b09008500145a1041d68bd5c696c7c
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.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Collections; import java.util.Scanner; import java.util.Set; import java.util.Scanner; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { FastReader sc = new FastReader(); long to = sc.nextLong(); while (to-- > 0) { long n= sc.nextLong(),m=0,u=0; long a[]=new long[(int) n]; for(int i=0;i<n;i++){ a[i]= sc.nextLong(); } if(n%2!=0) m=1; for(int i=(int)n-1;i>m;i-=2){ if(a[i]<a[i-1]){ long t = a[i]; a[i] = a[i-1]; a[i-1] = t; } } for(int i=0;i<(int)n-1;i++){ if(a[i]>a[i+1]){ u=1;break;} } if(u==1) System.out.println("NO"); else System.out.println("YES"); } } 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 11
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
944ae0f9676d050db8d022c50b06e05a
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.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { 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(); } int m=0; if(n%2!=0) { m=1; } for(int i=n-1;i>=m;i-=2) { if(arr[i]<arr[i-1]) { int p=arr[i]; arr[i]=arr[i-1]; arr[i-1]=p; } } int f=0; for(int i=0;i<n-1;i++) { if(arr[i]>arr[i+1]) { f=1; break; } } if(f==1) System.out.println("NO"); else System.out.println("YES"); } // your code goes here } }
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 11
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
4072064d8352f287434acf932a1a7a91
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 javax.management.openmbean.OpenDataException; public class Codeforces { final static int mod = 1000000007; final static String yes = "YES"; final static String no = "NO"; public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); int t = sc.nextInt(); outer: 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 < 3) { System.out.println(yes); continue outer; } for (int i = n - 1; i - 1 >= 0; i -= 2) { if (a[i] > a[i - 1]) { continue; } else { int temp = a[i]; a[i] = a[i - 1]; a[i - 1] = temp; } } // printArray(a); for (int i = n-2; i-1 >=0; i -= 2) { if (a[i] < a[i - 1]) { System.out.println(no); continue outer; } } System.out.println(yes); } } static void sortLong(long[] a) // check for long { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sortInt(int[] a) // check for int { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } public static boolean isSorted(int[] nums, int n) { for (int i = 1; i < n; i++) { if (nums[i] < nums[i - 1]) return false; } return true; } public static boolean isPalindrome(String s) { StringBuilder sb = new StringBuilder(s); return s.equals(sb.reverse().toString()); } static long kadane(long A[]) { long lsum = A[0], gsum = 0; gsum = Math.max(gsum, lsum); for (int i = 1; i < A.length; i++) { lsum = Math.max(lsum + A[i], A[i]); gsum = Math.max(gsum, lsum); } return gsum; } public static void sortByColumn(int arr[][], int col) { Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; else return -1; } }); } public static void backtrack(String[] letters, int index, String digits, StringBuilder build, List<String> result) { if (build.length() >= digits.length()) { result.add(build.toString()); return; } char[] key = letters[digits.charAt(index) - '2'].toCharArray(); for (int j = 0; j < key.length; j++) { build.append(key[j]); backtrack(letters, index + 1, digits, build, result); build.deleteCharAt(build.length() - 1); } } public static String get(String s, int k) { int n = s.length(); int rep = k % n == 0 ? k / n : k / n + 1; s = s.repeat(rep); return s.substring(0, k); } public static int diglen(Long y) { int a = 0; while (y != 0L) { y /= 10; a++; } return a; } static class FastReader { BufferedReader br; StringTokenizer st; // StringTokenizer() is used to read long strings 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 class Pair implements Comparable<Pair> { public final int index; public final int value; public Pair(int index, int value) { this.index = index; this.value = value; } @Override public int compareTo(Pair other) { // multiplied to -1 as the author need descending sort order return -1 * Integer.valueOf(this.value).compareTo(other.value); } } static String reverseString(String str) { StringBuilder input = new StringBuilder(); return input.append(str).reverse().toString(); } static void printArray(int[] nums) { for (int i = 0; i < nums.length; i++) { System.out.print(nums[i] + "->"); } System.out.println(); } static void printLongArray(long[] nums) { for (int i = 0; i < nums.length; i++) { System.out.print(nums[i] + "->"); } System.out.println(); } static long factorial(int n, int b) { if (n == b) return 1; return n * factorial(n - 1, b); } static int lcm(int ch, int b) { return ch * b / gcd(ch, b); } static int gcd(int ch, int b) { return b == 0 ? ch : gcd(b, ch % b); } static double ceil(double n, double k) { return Math.ceil(n / k); } static int sqrt(double n) { return (int) Math.sqrt(n); } }
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 11
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
713dcc81bd291638b88754dd75fbf1e5
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 IOException { int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int [] arr = sc.nextIntArray(n); for (int i = n%2; i < arr.length; i+=2) { if(arr[i] > arr[i+1]) { int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } } boolean flag = true; for (int i = 1; i < arr.length; i++) { if(arr[i-1]>arr[i] ) flag = false; } pw.println(flag ? "YES" : "NO"); } pw.close(); } private static int mergeAndCount(int[] arr, int l, int m, int r) { // Left subarray int[] left = Arrays.copyOfRange(arr, l, m + 1); // Right subarray int[] right = Arrays.copyOfRange(arr, m + 1, r + 1); int i = 0, j = 0, k = l, swaps = 0; while (i < left.length && j < right.length) { if (left[i] <= right[j]) arr[k++] = left[i++]; else { arr[k++] = right[j++]; swaps += (m + 1) - (l + i); } } while (i < left.length) arr[k++] = left[i++]; while (j < right.length) arr[k++] = right[j++]; return swaps; } private static int mergeSortAndCount(int[] arr, int l, int r) { // Keeps track of the inversion count at a // particular node of the recursion tree int count = 0; if (l < r) { int m = (l + r) / 2; // Total inversion count = left subarray count // + right subarray count + merge count // Left subarray count count += mergeSortAndCount(arr, l, m); // Right subarray count count += mergeSortAndCount(arr, m + 1, r); // Merge count count += mergeAndCount(arr, l, m, r); } return count; } static long getDiff(pair start, pair end) { return Math.abs(start.x - end.x) + Math.abs(start.y - end.y); } static HashMap Hash(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static HashMap Hash(char[] arr) { HashMap<Character, Integer> map = new HashMap<>(); for (char i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static boolean isPrime(int n) { if (n <= 1) return false; for (int i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } public static long combination(long n, long r) { return factorial(n) / (factorial(n - r) * factorial(r)); } static long factorial(Long n) { if (n == 0) return 1; return n * factorial(n - 1); } static boolean isPalindrome(char[] str, int i, int j) { // While there are characters to compare while (i < j) { // If there is a mismatch if (str[i] != str[j]) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } public static double log2(int N) { double result = (Math.log(N) / Math.log(2)); return result; } public static double log4(int N) { double result = (Math.log(N) / Math.log(4)); return result; } public static int setBit(int mask, int idx) { return mask | (1 << idx); } public static int clearBit(int mask, int idx) { return mask ^ (1 << idx); } public static boolean checkBit(int mask, int idx) { return (mask & (1 << idx)) != 0; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } public static String toString(int[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(long[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(ArrayList arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.size(); i++) { sb.append(arr.get(i) + " "); } return sb.toString().trim(); } public static String toString(int[][] arr) { StringBuilder sb = new StringBuilder(); for (int[] i : arr) { sb.append(toString(i) + "\n"); } return sb.toString(); } public static String toString(boolean[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(Integer[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(String[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(char[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
Java
["3\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 11
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
b784eebec0fac8dacd636f256fa837cf
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.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.sqrt; import static java.lang.System.out; import java.util.*; public class main { public static void main (String[] args) throws java.lang.Exception{ FastScanner sc = new FastScanner(); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int[] a = sc.readArray(n); int[] c = a.clone(); Arrays.sort(c); int flag = 1; if (n % 2 == 0) { for (int i = 0; i < n; i+=2) { if (c[i] != min(a[i], a[i+1]) || c[i+1] != max(a[i], a[i+1])){ flag = -1; break; } } } else { for (int i = 1; i < n; i+=2) { if (c[i] != min(a[i], a[i+1]) || c[i+1] != max(a[i], a[i+1])) { flag = -1; break; } } if (c[0] != a[0]) flag = -1; } out.println((flag == -1)?"NO":"YES"); } } public static int nearestElement(int target, int[] arr, String type) { // target, arr, type:{"just_smaller", any other string} // calculates the nearest element to the target in array arr. // if all elements are bigger than target, returns least ele // if all ele are smaller, then returns greatest ele. int lo = 0, hi = arr.length-1; while(lo <= hi) { int mid = lo + (hi-lo)/2; if (target == arr[mid]) return arr[mid]; else if (target < arr[mid]) hi = mid - 1; else lo = mid + 1; } if (lo == arr.length) return arr[arr.length-1]; if (hi == -1) return arr[0]; return (type.equals("just_smaller"))?arr[hi] : arr[lo]; } /*public static void sort(int[] a) { // Collections.sort uses O(NlogN) worst case. ArrayList<Integer> l = new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); }*/ public static 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 class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { 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 11
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
4043f07097481beefd77f40607013374
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.company; import java.io.*; import java.lang.reflect.Array; import java.util.*; public class Main{ static boolean[] primecheck; static ArrayList<Integer>[] adj; static int[] vis; static int[] parent = new int[101]; static int[] rank = new int[101]; static int mod = (int)1e9 + 7; static int dist = 0; static int maxNode = 0; public static void main(String[] args) { OutputStream outputStream = System.out; FastReader in = new FastReader(); PrintWriter out = new PrintWriter(outputStream); PROBLEM solver = new PROBLEM(); int t = 1; t = in.ni(); for (int i = 0; i < t; i++) { //out.print("Case #" + (i+1) + ": "); solver.solve(in, out); } out.close(); } static class PROBLEM { public void solve(FastReader in, PrintWriter out) { int n = in.ni(), a[] = in.ra(n); int[] b = Arrays.copyOf(a, n); b = mergeSort(b); for (int i = n-1; i > 0; i-=2) { if(a[i-1] > a[i]) swap(a, i, i-1); } for (int i = 0; i < n; i++) { if(a[i] != b[i]){ out.println("NO"); return; } } out.println("YES"); // if(e != o){ // if(n == 2){ // out.println(1); // out.println(0); // return; // } // if(e > o){ // for (int i = 0; i < n; i+=2) { // if(a[i] == 1){ // if(e > o){ // e--; rem--; // a[i] = -1; // }else break; // } // } // }else{ // for (int i = 1; i < n; i+=2) { // if(a[i] == 1){ // if(o > e){ // o--; rem--; // a[i] = -1; // }else break; // } // } // } // } // // out.println(rem); // for (int i = 0; i < n; i++) { // if(a[i] != -1) out.print(a[i] + " "); // } // // out.println(); // int n = in.ni(), c[] = in.ra(n), r[] = new int[n]; // long sum = 0; // String[] s = new String[n]; // for (int i = 0; i < n; i++) { // s[i] = in.nline(); // } // for (int i = 1; i < n; i++) { // if(s[i].compareTo(s[i-1]) < 0){ // if(i == 1){ // s[i-1] = reverse(s[i-1]); // if(s[i].compareTo(s[i-1]) < 0){ // s[i-1] = reverse(s[i-1]); // s[i] = reverse(s[i]); // if(s[i].compareTo(s[i-1]) < 0){ // out.println(-1); // return; // } // } // } // // // if(s[i].compareTo(s[i-1]) < 0){ // out.println(-1); // return; // } // sum += c[i]; // } // } // // out.println(sum); } } static String reverse(String s){ char[] ch = s.toCharArray(); for (int j = 0; j < ch.length / 2; j++) { char temp = ch[j]; ch[j] = ch[ch.length-j-1]; ch[ch.length-j-1] = temp; } return String.valueOf(ch); } static int[][] matrixMul(int[][] a, int[][] m){ if(a[0].length == m.length) { int[][] b = new int[a.length][m.length]; for (int i = 0; i < m.length; i++) { for (int j = 0; j < m.length; j++) { int sum = 0; for (int k = 0; k < m.length; k++) { sum += m[i][k] * m[k][j]; } b[i][j] = sum; } } return b; } return null; } static void dfs(int i, int d){ vis[i] = 1; if(d > dist){ maxNode = i; dist = d; } for(int j : adj[i]){ if (vis[j] == 0) dfs(j, d+1); } } static int find(int u){ if(u == parent[u]) return u; return parent[u] = find(parent[u]); } static void union(int u, int v){ int a = find(u), b = find(v); if(a == b) return; if(rank[a] > rank[b]){ parent[b] = a; rank[a] += rank[b]; }else{ parent[a] = b; rank[b] += rank[a]; } } static void dsu(){ for (int i = 0; i < 101; i++) { parent[i] = i; rank[i] = 1; } } static boolean isPalindrome(char[] s){ boolean b = true; for (int i = 0; i < s.length / 2; i++) { if(s[i] != s[s.length-1-i]){ b = false; break; } } return b; } static void output(boolean b, PrintWriter out){ if(b) out.println("YES"); else out.println("NO"); } static void pa(int[] a, PrintWriter out){ for (int j : a) { out.print(j + " "); } out.println(); } static void pa(long[] a, PrintWriter out){ for (long j : a) { out.print(j + " "); } out.println(); } public static void sortbyColumn(int arr[][], int col) { // Using built-in sort function Arrays.sort Arrays.sort(arr, new Comparator<int[]>() { @Override // Compare values according to columns public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; else return -1; } }); // End of function call sort(). } static boolean isPoT(long n){ return ((n&(n-1)) == 0); } static long sigmaK(long k){ return (k*(k+1))/2; } static void swap(int[] a, int l, int r) { int temp = a[l]; a[l] = a[r]; a[r] = temp; } static int binarySearch(int[] a, int l, int r, int x){ if(r>=l){ int mid = l + (r-l)/2; if(a[mid] == x) return mid; if(a[mid] > x) return binarySearch(a, l, mid-1, x); else return binarySearch(a,mid+1, r, x); } return -1; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long lcm(long a, long b){ return (a / gcd(a, b)) * b; } static int ceil(int a, int b){ return (a+b-1)/b; } static long ceil(long a, long b){ return (a+b-1)/b; } static boolean isSquare(double a) { boolean isSq = false; double b = Math.sqrt(a); double c = Math.sqrt(a) - Math.floor(b); if (c == 0) isSq = true; return isSq; } static long fast_pow(long a, long b) { //Jeel bhai OP if(b == 0) return 1L; long val = fast_pow(a, b / 2); if(b % 2 == 0) return val * val % mod; else return val * val % mod * a % mod; } static int exponentMod(int A, int B, int C) { // Base cases if (A == 0) return 0; if (B == 0) return 1; // If B is even long y; if (B % 2 == 0) { y = exponentMod(A, B / 2, C); y = (y * y) % C; } // If B is odd else { y = A % C; y = (y * exponentMod(A, B - 1, C) % C) % C; } return (int) ((y + C) % C); } // static class Pair implements Comparable<Pair>{ // // int x; // int y; // // Pair(int x, int y){ // this.x = x; // this.y = y; // } // // public int compareTo(Pair o){ // // int ans = Integer.compare(x, o.x); // if(o.x == x) ans = Integer.compare(y, o.y); // // return ans; // //// int ans = Integer.compare(y, o.y); //// if(o.y == y) ans = Integer.compare(x, o.x); //// //// return ans; // } // } static class Tuple implements Comparable<Tuple>{ int x, y, id; Tuple(int x, int y, int id){ this.x = x; this.y = y; this.id = id; } public int compareTo(Tuple o){ int ans = Integer.compare(x, o.x); if(o.x == x) ans = Integer.compare(y, o.y); return ans; } } public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } public int hashCode() { return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode()); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<U, V> p = (Pair<U, V>) o; return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y)); } public int compareTo(Pair<U, V> b) { int cmpU = x.compareTo(b.x); return cmpU != 0 ? cmpU : y.compareTo(b.y); } public int compareToY(Pair<U, V> b) { int cmpU = y.compareTo(b.y); return cmpU != 0 ? cmpU : x.compareTo(b.x); } public String toString() { return String.format("(%s, %s)", x.toString(), y.toString()); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } char nc() { return next().charAt(0); } boolean nb() { return !(ni() == 0); } // boolean nextBoolean(){return Boolean.parseBoolean(next());} String nline() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int[] ra(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = ni(); return array; } } private static int[] mergeSort(int[] array) { //array.length replaced with ctr int ctr = array.length; if (ctr <= 1) { return array; } int midpoint = ctr / 2; int[] left = new int[midpoint]; int[] right; if (ctr % 2 == 0) { right = new int[midpoint]; } else { right = new int[midpoint + 1]; } for (int i = 0; i < left.length; i++) { left[i] = array[i]; } for (int i = 0; i < right.length; i++) { right[i] = array[i + midpoint]; } left = mergeSort(left); right = mergeSort(right); int[] result = merge(left, right); return result; } private static int[] merge(int[] left, int[] right) { int[] result = new int[left.length + right.length]; int leftPointer = 0, rightPointer = 0, resultPointer = 0; while (leftPointer < left.length || rightPointer < right.length) { if (leftPointer < left.length && rightPointer < right.length) { if (left[leftPointer] < right[rightPointer]) { result[resultPointer++] = left[leftPointer++]; } else { result[resultPointer++] = right[rightPointer++]; } } else if (leftPointer < left.length) { result[resultPointer++] = left[leftPointer++]; } else { result[resultPointer++] = right[rightPointer++]; } } return result; } public static void Sieve(int n) { Arrays.fill(primecheck, true); primecheck[0] = false; primecheck[1] = false; for (int i = 2; i * i < n + 1; i++) { if (primecheck[i]) { for (int j = i * 2; j < n + 1; j += i) { primecheck[j] = false; } } } } }
Java
["3\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 11
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
7a2eb8f3a3a3f045476d467a209a09a1
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 a111 { public static void main(String []args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int []f=new int [n]; for(int i=0;i<n;i++) f[i]=s.nextInt(); for(int i=(n%2);i<n-1;i+=2) { if(f[i]>f[i+1]) { int a=f[i]; f[i]=f[i+1]; f[i+1]=a; } } boolean flag=true; for(int i=0;i<n-1;++i) { if(f[i]>f[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 11
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
a6e96c39f18aef3d767c6992f29d482d
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 a111 { public static void main(String []args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int []f=new int [n]; for(int i=0;i<n;i++) f[i]=s.nextInt(); for(int i=(n&1);i<n-1;i+=2) { if(f[i]>f[i+1]) { int a=f[i]; f[i]=f[i+1]; f[i+1]=a; } } boolean flag=true; for(int i=0;i<n-1;++i) { if(f[i]>f[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 11
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
3605491a15f65b30847b15f0a9a287ee
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.*; import static java.lang.Math.*; public class Main { static double pi = 3.141592653589; static int mod = (int) 1e9 + 7; public static void main(String[] args) throws IOException { int qwe = in.nextInt(); while (qwe-- > 0) { solve(); } out.close(); } public static void solve() { int x = -0x3f3f3f3f; int n = in.nextInt(); if ((n&1)==1) x = in.nextInt(); int[] num = new int[n/2*2]; for (int i = 0; i < n/2*2; i++) num[i] = in.nextInt(); for (int cnt = 0; cnt < n/2; cnt++) { int a = num[cnt*2]; int b = num[cnt*2+1]; int max = max(a, b); int min = min(a, b); if (min >= x) { x = max; } else { System.out.println("NO"); return; } } System.out.println("YES"); } // static int N = 1010; // static int n,m; // static int INF = 0x3f3f3f3f; // static int[][] g = new int[N][N]; // 存储每条边 邻接矩阵 // static int[] dist = new int[N]; // 存储1号点到每个点的最短距离 // static boolean[] st = new boolean[N]; // 存储每个点的最短路是否已经确定 // static void init(){ // Arrays.fill(dist, INF); // for (int i = 0; i <= n; i++) Arrays.fill(g[i],0x3f3f3f3f); // } // // 求x号点到y号点的最短路,如果不存在则返回-1 // static int dijkstra(int x, int y) { // dist[x] = 0; // for (int i = 1; i < n; i++) { // int t = -1; // 在还未确定最短路的点中,寻找距离最小的点 // for (int j = 1; j <= n; j++) // if (!st[j] && (t == -1 || dist[t] > dist[j])) // t = j; // // st[t] = true; // // 用t更新其他点的距离 // for (int j = 1; j <= n; j++) dist[j] = Math.min(dist[j], dist[t] + g[t][j]); // } // if (dist[y] == INF) return -1; // return dist[y]; // } // static int N = 1000 * 26; // static int[][] trie = new int[N][26];; // static int[] cnt = new int[N]; // static int idx; // static void insert_trie(String s) { // int p = 0; // for (int i = 0; i < s.length(); i++) { // int u = s.charAt(i) - 'a'; //如果是大写字母,则需要改成大写A // if (trie[p][u] == 0) trie[p][u] = ++idx; // p = trie[p][u]; // } // cnt[p]++; // } // static int search_trie(String s){ // int p = 0; // for (int i = 0; i < s.length(); i ++ ){ // int u = s.charAt(i) - 'a'; // if (trie[p][u] == 0) return 0; // p = trie[p][u]; // } // return cnt[p]; // } static void kmp(String s1, String s2) { //短字符串、长字符串 int n = s1.length(); //短字符串 int m = s2.length(); char[] p = (" " + s1).toCharArray();//短字符串 char[] s = (" " + s2).toCharArray(); // 构造ne数组 int[] ne = new int[n + 1]; for (int i = 2, j = 0; i <= n; i++) { while (j != 0 && p[i] != p[j + 1]) j = ne[j]; if (p[i] == p[j + 1]) j++; ne[i] = j; } // kmp匹配 for (int i = 1, j = 0; i <= m; i++) { while (j != 0 && s[i] != p[j + 1]) j = ne[j]; if (s[i] == p[j + 1]) j++; if (j == n) { // 匹配了n字符了即代表完全匹配了 out.print(i - n + " "); // 输出在s串中p出现的位置 j = ne[j]; // 完全匹配后继续搜索 } out.flush(); } } // static int N = 100010; // static int idx = 0; //节点位置 // static char[] str = new char[N]; // static int[] cnt = new int[N]; // static int[][] son = new int[N][26]; // public static void insert(char[] str) { // int p = 0; //从根节点出发 // for (char i : str) { // int u = i - 'a'; // if (son[p][u] == 0) son[p][u] = ++idx; // p = son[p][u]; // } // cnt[p]++; // } // public static int query(char[] str) { // int p = 0; // for (char i : str) { // int u = i - 'a'; // if (son[p][u] == 0) return 0; // p = son[p][u]; // } // return cnt[p]; // } // static int bsearch_l(int l, int r, long target) { // while (l < r) { // int mid = l + r >> 1; // if (num[mid] >= target) r = mid; // else l = mid + 1; // } // return l; // } // // static int bsearch_r(int l, int r, long target) { // while (l < r) { // int mid = l + r + 1 >> 1; // if (num[mid] <= target) l = mid; // else r = mid - 1; // } // return l; // } // static double bsearch_d(double l, double r) { // double eps = 1e-8; // while (Math.abs(r - l) > eps) { // double mid = (l + r) / 2; // if (check(mid)) r = mid; // else l = mid; // } // return l; // } // static boolean check(int mid) { // return true; // } public static long ksm(long n, long m) {//本质为n^m long res = 1, base = n; while (m != 0) { if ((m & 1) == 1) { //等价于m%2==1,也就是m为奇数时 res = mul(res, base) % mod; } base = mul(base, base) % mod;//m为偶数时,base自乘 m = m >> 1;//等价于m/2 } return res % mod; } static long mul(long a, long b) {//本质为a*b long ans = 0; while (b != 0) { if ((b & 1) == 1) { ans = (ans + a) % mod; } a = (a + a) % mod; b = b >> 1; } return ans % mod; } 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 long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static 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]; } } static InputReader in = new InputReader(System.in); 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()); } } }
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 11
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
1e4501c04bcdd7e3101930d3e776889a
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.lang.reflect.Array; import java.util.*; import java.util.stream.IntStream; import java.util.stream.Stream; public class Main { public static void main(String[] args) { in = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); try { int t = in.nextInt(); while(t-- > 0) { solve(); out.println();} // solve(); } finally { out.close(); } return; } public static void solve() { int n = in.nextInt(); Integer[] A = new Integer[n]; int[] a = new int[n]; for(int i = 0; i < n; i++) { A[i] = in.nextInt(); a[i] = A[i]; } Arrays.sort(A, Comparator.reverseOrder()); int[] sorted = new int[n]; for(int i = 0; i < n; i++) { sorted[i] = A[i]; } Vector<Integer> vec = new Vector<Integer>(); for(int i = n - 1; i - 1 >= 0; i -= 2) { if(a[i] <= a[i-1]) { vec.add(a[i-1]); vec.add(a[i]); } else { vec.add(a[i]); vec.add(a[i-1]); } } if(n % 2 != 0) { vec.add(a[0]); } for(int i = 0; i < n; i++) { if(sorted[i] != vec.get(i)) { out.print("NO"); return; } } out.print("YES"); return; } //-------------- Helper methods------------------- public static int[] fillArray(int n) { int[] array = new int[n]; for(int i = 0; i < n; i++) { array[i] = in.nextInt(); } return array; } public static char[] fillArray() { char[] array = in.next().toCharArray(); return array; } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; public static MyScanner in; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } void shuffleArray(int[] arr){ int n = arr.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = arr[i]; int randomPos = i + rnd.nextInt(n-i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } //-------------------------------------------------------- }
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 11
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
7d98277f7c0568440c16c1e0bd28bce1
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.io.*; public class Test { final static FastReader fr = new FastReader(); final static PrintWriter out = new PrintWriter(System.out) ; static long mod = (long)1e9 + 7; static long[] dp ; static void solve() { int n = fr.nextInt(); ArrayList<Long> al = new ArrayList<>() ; for (int i = 0 ; i < n ; i++) { al.add(fr.nextLong()) ; } ArrayList<Long> bl = new ArrayList<>(al) ; Collections.sort(bl); int i = 0 ; if (n%2 != 0) { if (al.get(0) != bl.get(0)) { out.println("NO"); return; } else i++ ; } for ( ; i < n-1 ; i += 2) { if (al.get(i) == bl.get(i) && al.get(i+1) == bl.get(i+1)) { continue; } else if (al.get(i) == bl.get(i+1) && al.get(i+1) == bl.get(i)){ continue; } out.println("NO"); return; } out.println("YES"); } public static void main(String[] args) { int t = 1 ; t = fr.nextInt() ; while (t-- > 0) { solve() ; } out.close() ; } static long getMax(long ... a) { long max = Long.MIN_VALUE ; for (long x : a) max = Math.max(x, max) ; return max ; } static long getMin(long ... a) { long max = Long.MAX_VALUE ; for (long x : a) max = Math.min(x, max) ; return max ; } static long fastPower(double a, long b) { double ans = 1 ; while (b > 0) { if ((b & 1) != 0) ans *= a ; a *= a ; b >>= 1 ; } return (long)(ans + 0.5) ; } static long fastPower(long a, long b, long mod) { long ans = 1 ; while (b > 0) { if ((b&1) != 0) ans = (ans%mod * a%mod) %mod; b >>= 1 ; a = (a%mod * a%mod)%mod ; } return ans ; } static int lower_bound(List<Integer> arr, int key) { int pos = Collections.binarySearch(arr, key) ; if (pos < 0) { pos = - (pos + 1) ; } return pos ; } static int upper_bound(List<Integer> arr, int key) { int pos = Collections.binarySearch(arr, key); pos++ ; if (pos < 0) { pos = -(pos) ; } return pos ; } static int upper_bound(int arr[], int key) { int start = 0 , end = arr.length ; while (start < end) { int mid = start + ((end - start) >> 1) ; if (arr[mid] <= key) start = mid + 1 ; else end = mid ; } return start ; } static int lower_bound(int[] arr, int key) { int start = 0 , end = arr.length -1; int ans = -1 ; while (start <= end) { int mid = start + ((end - start )>>1) ; if (arr[mid] == key) { ans = mid ; } if (arr[mid] >= key){ end = mid - 1 ; } else start = mid + 1 ; } return ans ; } static class Pair{ long x; long y ; Pair(long x, long y){ this.x = x ; this.y= y ; } } static long gcd(long a, long b) { if (b == 0) return a ; return gcd(b, a%b) ; } static long lcm(long a, long b) { long lcm = (a * b)/gcd(a, b) ; return lcm ; } static List<Long> seive(int n) { // all are false by default // false -> prime, true -> composite boolean[] nums = new boolean[n+1] ; for (int i = 2 ; i <= Math.sqrt(n); i++) { if (!nums[i]) { for (int j = i*i ; j <= n ; j += i) { nums[j] = true ; } } } ArrayList<Long>primes = new ArrayList<>() ; for (int i = 2 ; i <=n ; i++) { if (!nums[i]) primes.add((long) i) ; } return primes ; } static boolean isVowel(char ch) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') return true ; return false ; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
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 11
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
2e3212222ae23e7015c3f2fee874d2fd
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 Main { public static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while(t-->0){ solve(); }} public static void solve() { int n = sc.nextInt(); int arr[] = new int[n]; for(int i =0 ; i < n ; i++)arr[i]=sc.nextInt(); 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){ // System.out.print(val + " "); // } // System.out.println(); for(int i=0;i<n-1;i++){ if(temp[i] > temp[i+1]) flag = true; } if(flag) 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 11
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
151c9e2e61b466edc2d41866929903c6
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.ArrayList; import java.util.Collections; import java.util.Scanner; public class sol { public static void main(String[] args) { Scanner in = new Scanner(System.in); int tt = in.nextInt(); while(tt--> 0) { int n = in.nextInt(); ArrayList<Integer> a = new ArrayList<>(n); for (int i = 0; i < n; i++) { a.add(in.nextInt()); } for(int i = n % 2; i < n - 1; i+=2){ if(a.get(i) > a.get(i + 1)) { Collections.swap(a, i, i + 1); } } boolean poss = true; for(int i = 0; i < n - 1; i++) { if(a.get(i) > a.get(i + 1)) { poss = false; break; } } if(poss) 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 11
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
ec66de736a1bba205467cae5bb685275
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.StringTokenizer; public class Main { static int globalVariable = 123456789; static String author = "pl728 on codeforces"; public static void main(String[] args) { FastReader sc = new FastReader(); MathUtils mathUtils = new MathUtils(); ArrayUtils arrayUtils = new ArrayUtils(); int tc = sc.ni(); while (tc-- != 0) { int n = sc.ni(); int[] a = sc.readIntArray(n); int i; int currentMax; if (n % 2 == 1) { currentMax = a[0]; i = 1; } else { currentMax = Math.max(a[0], a[1]); i = 2; } boolean ans = true; while(i < n - 1) { if (a[i] < currentMax || a[i + 1] < currentMax) { ans = false; break; } currentMax = Math.max(a[i], a[i + 1]); i += 2; } if(ans) System.out.println("YES"); else System.out.println("NO"); } } static class FastReader { /** * Uses BufferedReader and StringTokenizer for quick java I/O * get next int, long, double, string * get int, long, double, string arrays, both primitive and wrapped object when array contains all elements * on one line, and we know the array size (n) * next: gets next space separated item * nextLine: returns entire line as space */ BufferedReader br; StringTokenizer st; public FastReader() { this.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(); } // to parse something else: // T x = T.parseT(fastReader.next()); public int ni() { return Integer.parseInt(next()); } public String ns() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public String[] readStringArrayLine(int n) { String line = ""; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line.split(" "); } public String[] readStringArrayLines(int n) { String[] result = new String[n]; for (int i = 0; i < n; i++) { result[i] = this.next(); } return result; } public int[] readIntArray(int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = this.ni(); } return result; } public long[] readLongArray(int n) { long[] result = new long[n]; for (int i = 0; i < n; i++) { result[i] = this.nl(); } return result; } public Integer[] readIntArrayObject(int n) { Integer[] result = new Integer[n]; for (int i = 0; i < n; i++) { result[i] = this.ni(); } return result; } public long nl() { return Long.parseLong(next()); } public char[] readCharArray(int n) { return this.ns().toCharArray(); } } static class MathUtils { public MathUtils() { } public long gcdLong(long a, long b) { if (a % b == 0) return b; else return gcdLong(b, a % b); } public long lcmLong(long a, long b) { return a * b / gcdLong(a, b); } } static class ArrayUtils { public ArrayUtils() { } public static int[] reverse(int[] a) { int n = a.length; int[] b = new int[n]; int j = n; for (int i = 0; i < n; i++) { b[j - 1] = a[i]; j = j - 1; } return b; } public int sumIntArrayInt(int[] a) { int ans = 0; for (int i = 0; i < a.length; i++) { ans += a[i]; } return ans; } public long sumLongArrayLong(int[] a) { long ans = 0; for (int i = 0; i < a.length; i++) { ans += a[i]; } return ans; } } public static int lowercaseToIndex(char c) { return (int) c - 97; } }
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 11
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
83076fe6bc9f32b203c58199bae44f6c
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 A_B_C_Sort { static FastScanner fs; static FastWriter fw; static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null; private static final int[][] kdir = new int[][]{{-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}}; private static final int[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; private static final int iMax = Integer.MAX_VALUE, iMin = Integer.MIN_VALUE; private static final long lMax = Long.MAX_VALUE, lMin = Long.MIN_VALUE; private static final int mod1 = (int) (1e9 + 7); private static final int mod2 = 998244353; public static void main(String[] args) throws IOException { fs = new FastScanner(); fw = new FastWriter(); int t = 1; t = fs.nextInt(); while (t-- > 0) { solve(); } fw.out.close(); } private static void solve() { int n = fs.nextInt(); int[] arr1 = new int[n], arr2 = new int[n]; for (int i = 0; i < n; i++) { int num = fs.nextInt(); arr1[i] = num; arr2[i] = num; } Arrays.sort(arr2); if ((n & 1) == 1 && arr1[0] != arr2[0]) { fw.out.println("NO"); return; } int idx = ((n & 1) == 1) ? 1 : 0; for (int i = idx; i < n; i += 2) { if (!(Math.min(arr1[i], arr1[i + 1]) == Math.min(arr2[i], arr2[i + 1]) && Math.max(arr1[i], arr1[i + 1]) == Math.max(arr2[i], arr2[i + 1]))) { fw.out.println("NO"); return; } } fw.out.println("YES"); } private static class UnionFind { private final int[] parent; private final int[] rank; UnionFind(int n) { parent = new int[n + 5]; rank = new int[n + 5]; for (int i = 0; i <= n; i++) { parent[i] = i; rank[i] = 0; } } private int find(int i) { if (parent[i] == i) return i; return parent[i] = find(parent[i]); } private void union(int a, int b) { a = find(a); b = find(b); if (a != b) { if (rank[a] < rank[b]) { int temp = a; a = b; b = temp; } parent[b] = a; if (rank[a] == rank[b]) rank[a]++; } } } private static class SCC { private final int n; private final List<List<Integer>> adjList; SCC(int _n, List<List<Integer>> _adjList) { n = _n; adjList = _adjList; } private List<List<Integer>> getSCC() { List<List<Integer>> ans = new ArrayList<>(); Stack<Integer> stack = new Stack<>(); boolean[] vis = new boolean[n]; for (int i = 0; i < n; i++) { if (!vis[i]) dfs(i, adjList, vis, stack); } vis = new boolean[n]; List<List<Integer>> rev_adjList = rev_graph(n, adjList); while (!stack.isEmpty()) { int curr = stack.pop(); if (!vis[curr]) { List<Integer> scc_list = new ArrayList<>(); dfs2(curr, rev_adjList, vis, scc_list); ans.add(scc_list); } } return ans; } private void dfs(int curr, List<List<Integer>> adjList, boolean[] vis, Stack<Integer> stack) { vis[curr] = true; for (int x : adjList.get(curr)) { if (!vis[x]) dfs(x, adjList, vis, stack); } stack.add(curr); } private void dfs2(int curr, List<List<Integer>> adjList, boolean[] vis, List<Integer> scc_list) { vis[curr] = true; scc_list.add(curr); for (int x : adjList.get(curr)) { if (!vis[x]) dfs2(x, adjList, vis, scc_list); } } } private static List<List<Integer>> rev_graph(int n, List<List<Integer>> adjList) { List<List<Integer>> ans = new ArrayList<>(); for (int i = 0; i < n; i++) ans.add(new ArrayList<>()); for (int i = 0; i < n; i++) { for (int x : adjList.get(i)) { ans.get(x).add(i); } } return ans; } private static class Calc_nCr { private final long[] fact; private final long[] invfact; private final int p; Calc_nCr(int n, int prime) { fact = new long[n + 5]; invfact = new long[n + 5]; p = prime; fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = (i * fact[i - 1]) % p; } invfact[n] = pow_with_mod(fact[n], p - 2, p); for (int i = n - 1; i >= 0; i--) { invfact[i] = (invfact[i + 1] * (i + 1)) % p; } } private long nCr(int n, int r) { if (r > n || n < 0 || r < 0) return 0; return (((fact[n] * invfact[r]) % p) * invfact[n - r]) % p; } } private static int random_between_two_numbers(int l, int r) { Random ran = new Random(); return ran.nextInt(r - l) + l; } private static long gcd(long a, long b) { return (b == 0 ? a : gcd(b, a % b)); } private static long lcm(long a, long b) { return ((a * b) / gcd(a, b)); } private static long pow(long a, long b) { long result = 1; while (b > 0) { if ((b & 1L) == 1) { result = (result * a); } a = (a * a); b >>= 1; } return result; } private static long pow_with_mod(long a, long b, int mod) { long result = 1; while (b > 0) { if ((b & 1L) == 1) { result = (result * a) % mod; } a = (a * a) % mod; b >>= 1; } return result; } private static long ceilDiv(long a, long b) { return ((a + b - 1) / b); } private static long getMin(long... args) { long min = lMax; for (long arg : args) min = Math.min(min, arg); return min; } private static long getMax(long... args) { long max = lMin; for (long arg : args) max = Math.max(max, arg); return max; } private static boolean isPalindrome(String s, int l, int r) { int i = l, j = r; while (j - i >= 1) { if (s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } private static List<Integer> primes(int n) { boolean[] primeArr = new boolean[n + 5]; Arrays.fill(primeArr, true); for (int i = 2; (i * i) <= n; i++) { if (primeArr[i]) { for (int j = i * i; j <= n; j += i) { primeArr[j] = false; } } } List<Integer> primeList = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (primeArr[i]) primeList.add(i); } return primeList; } private static int noOfSetBits(long x) { int cnt = 0; while (x != 0) { x = x & (x - 1); cnt++; } return cnt; } private static int sumOfDigits(long num) { int cnt = 0; while (num > 0) { cnt += (num % 10); num /= 10; } return cnt; } private static int noOfDigits(long num) { int cnt = 0; while (num > 0) { cnt++; num /= 10; } return cnt; } private static boolean isPerfectSquare(long num) { long sqrt = (long) Math.sqrt(num); return ((sqrt * sqrt) == num); } private static class Pair<U, V> { private final U first; private final V second; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return first.equals(pair.first) && second.equals(pair.second); } @Override public int hashCode() { return Objects.hash(first, second); } @Override public String toString() { return "(" + first + ", " + second + ")"; } private Pair(U ff, V ss) { this.first = ff; this.second = ss; } } private static void randomizeIntArr(int[] arr, int n) { Random r = new Random(); for (int i = (n - 1); i > 0; i--) { int j = r.nextInt(i + 1); swapInIntArr(arr, i, j); } } private static void randomizeLongArr(long[] arr, int n) { Random r = new Random(); for (int i = (n - 1); i > 0; i--) { int j = r.nextInt(i + 1); swapInLongArr(arr, i, j); } } private static void swapInIntArr(int[] arr, int a, int b) { int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } private static void swapInLongArr(long[] arr, int a, int b) { long temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } private static int[] readIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = fs.nextInt(); return arr; } private static long[] readLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = fs.nextLong(); return arr; } private static List<Integer> readIntList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(fs.nextInt()); return list; } private static List<Long> readLongList(int n) { List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(fs.nextLong()); return list; } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws IOException { if (checkOnlineJudge) this.br = new BufferedReader(new FileReader("src/input.txt")); else this.br = new BufferedReader(new InputStreamReader(System.in)); this.st = new StringTokenizer(""); } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException err) { err.printStackTrace(); } } return st.nextToken(); } public String nextLine() { if (st.hasMoreTokens()) { return st.nextToken("").trim(); } try { return br.readLine().trim(); } catch (IOException err) { err.printStackTrace(); } return ""; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } private static class FastWriter { PrintWriter out; FastWriter() throws IOException { if (checkOnlineJudge) out = new PrintWriter(new FileWriter("src/output.txt")); else out = new PrintWriter(System.out); } } }
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 11
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
0bfeb75538ec30da38ace58fac4a1180
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.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.*; public class weird_algrithm { static BufferedWriter output = new BufferedWriter( new OutputStreamWriter(System.out)); static BufferedReader sc = new BufferedReader(new InputStreamReader(System.in)); static int mod = 998244353; static String toReturn = ""; static int steps = Integer.MAX_VALUE; static int maxlen = 1000005; /*MATHEMATICS FUNCTIONS START HERE MATHS MATHS MATHS MATHS*/ static long gcd(long a, long b) { if(b == 0) return a; else return gcd(b, a % b); } static long powerMod(long x, long y, int mod) { if(y == 0) return 1; long temp = powerMod(x, y / 2, mod); temp = ((temp % mod) * (temp % mod)) % mod; if(y % 2 == 0) return temp; else return ((x % mod) * (temp % mod)) % mod; } static long modInverse(long n, int p) { return powerMod(n, p - 2, p); } static long nCr(int n, int r, int mod, long [] fact, long [] ifact) { return ((fact[n] % mod) * ((ifact[r] * ifact[n - r]) % mod)) % mod; } static boolean isPrime(long a) { if(a == 1) return false; else if(a == 2 || a == 3 || a== 5) return true; else if(a % 2 == 0 || a % 3 == 0) return false; for(int i = 5; i * i <= a; i = i + 6) { if(a % i == 0 || a % (i + 2) == 0) return false; } return true; } static int [] seive(int a) { int [] toReturn = new int [a + 1]; for(int i = 0; i < a; i++) toReturn[i] = 1; toReturn[0] = 0; toReturn[1] = 0; toReturn[2] = 1; for(int i = 2; i * i <= a; i++) { if(toReturn[i] == 0) continue; for(int j = 2 * i; j <= a; j += i) toReturn[j] = 0; } return toReturn; } static long [] fact(int a) { long [] arr = new long[a + 1]; arr[0] = 1; for(int i = 1; i < a + 1; i++) { arr[i] = (arr[i - 1] * i) % mod; } return arr; } static ArrayList<Integer> divisors(int n) { ArrayList<Integer> arr = new ArrayList<Integer>(); for(int i = 1; i * i <= n; i++) { if(n % i == 0) { arr.add(i); if(n / i == i) continue; arr.add(n / i); } } return arr; } /*MATHS MATHS MATHS MATHS MATHEMATICS FUNCTIONS END HERE */ /*SWAP FUNCTION START HERE SWAP SWAP SWAP SWAP */ static void swap(int i, int j, long[] arr) { long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static void swap(int i, int j, int[] arr) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static void swap(int i, int j, String [] arr) { String temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static void swap(int i, int j, char [] arr) { char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } /*SWAP SWAP SWAP SWAP SWAP FUNCTION END HERE*/ /*BINARY SEARCH METHODS START HERE * BINARY SEARCH * BINARY SEARCH * BINARY SEARCH * BINARY SEARCH */ static boolean BinaryCheck(long test, long [] arr, long health) { for(int i = 0; i <= arr.length - 1; i++) { if(i == arr.length - 1) health -= test; else if(arr[i + 1] - arr[i] > test) { health = health - test; }else { health = health - (arr[i + 1] - arr[i]); } if(health <= 0) return true; } return false; } static long binarySearchModified(long start1, long n, ArrayList<Long> arr, int a, long r) { long start = start1, end = n, ans = -1; while(start < end) { long mid = (start + end) / 2; //System.out.println(mid); if(arr.get((int)mid) + arr.get(a) <= r && mid != start1) { ans = mid; start = mid + 1; }else{ end = mid; } } //System.out.println(); return ans; } static int upper(int start, int end, ArrayList<Long> arr, long val) { while(start < end) { int mid = (start + end) / 2; if(arr.get(mid) <= val) start = mid + 1; else end = mid; } return start; } static int lower(int start, int end, ArrayList<Long> arr, long val) { while(start < end) { int mid = (start + end) / 2; if(arr.get(mid) >= val) end = mid; else start = mid + 1; } return start; } /*BINARY SEARCH * BINARY SEARCH * BINARY SEARCH * BINARY SEARCH * BINARY SEARCH BINARY SEARCH METHODS END HERE*/ /*RECURSIVE FUNCTION START HERE * RECURSIVE * RECURSIVE * RECURSIVE * RECURSIVE */ static int recurse(int x, int y, int n, int steps1, Integer [][] dp) { if(x > n || y > n) return 0; if(dp[x][y] != null) { return dp[x][y]; } else if(x == n || y == n) { return steps1; } return dp[x][y] = Math.max(recurse(x + y, y, n, steps1 + 1, dp), recurse(x, x + y, n, steps1 + 1, dp)); } /*RECURSIVE * RECURSIVE * RECURSIVE * RECURSIVE * RECURSIVE RECURSIVE FUNCTION END HERE*/ /*GRAPH FUNCTIONS START HERE * GRAPH * GRAPH * GRAPH * GRAPH * */ static class edge{ int from, to; long weight; public edge(int x, int y, long weight2) { this.from = x; this.to = y; this.weight = weight2; } } static class sort implements Comparator<pair>{ @Override public int compare(pair o1, pair o2) { // TODO Auto-generated method stub return (int)o1.a - (int)o2.a; } } static void addEdge(ArrayList<ArrayList<edge>> graph, int from, int to, long weight) { edge temp = new edge(from, to, weight); edge temp1 = new edge(to, from, weight); graph.get(from).add(temp); //graph.get(to).add(temp1); } static int ans = 0; static void topoSort(ArrayList<ArrayList<Integer>> graph, int vertex, boolean [] visited, ArrayList<Integer> toReturn) { if(visited[vertex]) return; visited[vertex] = true; for(int i = 0; i < graph.get(vertex).size(); i++) { if(!visited[graph.get(vertex).get(i)]) topoSort(graph, graph.get(vertex).get(i), visited, toReturn); } toReturn.add(vertex); } static boolean isCyclicDirected(ArrayList<ArrayList<Integer>> graph, int vertex, boolean [] visited, boolean [] reStack) { if(reStack[vertex]) return true; if(visited[vertex]) return false; reStack[vertex] = true; visited[vertex] = true; for(int i = 0; i < graph.get(vertex).size(); i++) { if(isCyclicDirected(graph, graph.get(vertex).get(i), visited, reStack)) return true; } reStack[vertex] = false; return false; } static int e = 0; static long mst(PriorityQueue<edge> pq, int nodes) { long weight = 0; int [] size = new int[nodes + 1]; Arrays.fill(size, 1); while(!pq.isEmpty()) { edge temp = pq.poll(); int x = parent(parent, temp.to); int y = parent(parent, temp.from); if(x != y) { //System.out.println(temp.weight); union(x, y, rank, parent, size); weight += temp.weight; e++; } } return weight; } static void floyd(long [][] dist) { // to find min distance between two nodes for(int k = 0; k < dist.length; k++) { for(int i = 0; i < dist.length; i++) { for(int j = 0; j < dist.length; j++) { if(dist[i][j] > dist[i][k] + dist[k][j]) { dist[i][j] = dist[i][k] + dist[k][j]; } } } } } static void dijkstra(ArrayList<ArrayList<edge>> graph, long [] dist, int src) { for(int i = 0; i < dist.length; i++) dist[i] = Long.MAX_VALUE / 2; dist[src] = 0; boolean visited[] = new boolean[dist.length]; PriorityQueue<pair> pq = new PriorityQueue<>(new sort()); pq.add(new pair(src, 0)); while(!pq.isEmpty()) { pair temp = pq.poll(); int index = (int)temp.a; for(int i = 0; i < graph.get(index).size(); i++) { if(dist[graph.get(index).get(i).to] > dist[index] + graph.get(index).get(i).weight) { dist[graph.get(index).get(i).to] = dist[index] + graph.get(index).get(i).weight; pq.add(new pair(graph.get(index).get(i).to, graph.get(index).get(i).weight)); } } } } static int parent1 = -1; static boolean ford(ArrayList<ArrayList<edge>> graph1, ArrayList<edge> graph, long [] dist, int src, int [] parent) { for(int i = 0; i < dist.length; i++) dist[i] = Long.MIN_VALUE / 2; dist[src] = 0; boolean hasNeg = false; for(int i = 0; i < dist.length - 1; i++) { for(int j = 0; j < graph.size(); j++) { int from = graph.get(j).from; int to = graph.get(j).to; long weight = graph.get(j).weight; if(dist[to] < dist[from] + weight) { dist[to] = dist[from] + weight; parent[to] = from; } } } for(int i = 0; i < graph.size(); i++) { int from = graph.get(i).from; int to = graph.get(i).to; long weight = graph.get(i).weight; if(dist[to] < dist[from] + weight) { parent1 = from; hasNeg = true; /* * dfs(graph1, parent1, new boolean[dist.length], dist.length - 1); * //System.out.println(ans); dfs(graph1, 0, new boolean[dist.length], parent1); */ //System.out.println(ans); if(ans == 2) break; else ans = 0; } } return hasNeg; } /*GRAPH FUNCTIONS END HERE * GRAPH * GRAPH * GRAPH * GRAPH */ /*disjoint Set START HERE * disjoint Set * disjoint Set * disjoint Set * disjoint Set */ static int [] rank; static int [] parent; static int parent(int [] parent, int x) { if(parent[x] == x) return x; else return parent[x] = parent(parent, parent[x]); } static boolean union(int x, int y, int [] rank, int [] parent, int [] setSize) { if(parent(parent, x) == parent(parent, y)) { return true; } if (rank[x] > rank[y]) { parent[y] = x; setSize[x] += setSize[y]; } else { parent[x] = y; setSize[y] += setSize[x]; if (rank[x] == rank[y]) rank[y]++; } return false; } /*disjoint Set END HERE * disjoint Set * disjoint Set * disjoint Set * disjoint Set */ /*INPUT START HERE * INPUT * INPUT * INPUT * INPUT * INPUT */ static int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(sc.readLine()); } static long nextLong() throws NumberFormatException, IOException { return Long.parseLong(sc.readLine()); } static long [] inputLongArr() throws NumberFormatException, IOException{ String [] s = sc.readLine().split(" "); long [] toReturn = new long[s.length]; for(int i = 0; i < s.length; i++) { toReturn[i] = Long.parseLong(s[i]); } return toReturn; } static int max = 0; static int [] inputIntArr() throws NumberFormatException, IOException{ String [] s = sc.readLine().split(" "); //System.out.println(s.length); int [] toReturn = new int[s.length]; for(int i = 0; i < s.length; i++) { toReturn[i] = Integer.parseInt(s[i]); } return toReturn; } /*INPUT * INPUT * INPUT * INPUT * INPUT * INPUT END HERE */ static long [] preCompute(int level) { long [] toReturn = new long[level]; toReturn[0] = 1; toReturn[1] = 16; for(int i = 2; i < level; i++) { toReturn[i] = ((toReturn[i - 1] % mod) * (toReturn[i - 1] % mod)) % mod; } return toReturn; } static class pair{ long a; long b; long d; public pair(long in, long y) { this.a = in; this.b = y; this.d = 0; } } static int [] nextGreaterBack(char [] s) { Stack<Integer> stack = new Stack<>(); int [] toReturn = new int[s.length]; for(int i = 0; i < s.length; i++) { if(!stack.isEmpty() && s[stack.peek()] >= s[i]) { stack.pop(); } if(stack.isEmpty()) { stack.push(i); toReturn[i] = -1; }else { toReturn[i] = stack.peek(); stack.push(i); } } return toReturn; } static int [] nextGreaterFront(char [] s) { Stack<Integer> stack = new Stack<>(); int [] toReturn = new int[s.length]; for(int i = s.length - 1; i >= 0; i--) { if(!stack.isEmpty() && s[stack.peek()] >= s[i]) { stack.pop(); } if(stack.isEmpty()) { stack.push(i); toReturn[i] = -1; }else { toReturn[i] = stack.peek(); stack.push(i); } } return toReturn; } static int lps(String s) { int max = 0; int [] lps = new int[s.length()]; lps[0] = 0; int j = 0; for(int i = 1; i < lps.length; i++) { j = lps[i - 1]; while(j > 0 && s.charAt(i) != s.charAt(j)) j = lps[j - 1]; if(s.charAt(i) == s.charAt(j)) { lps[i] = j + 1; max = Math.max(max, lps[i]); } } return max; } static int [][] vectors = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; static String dir = "DRUL"; static boolean check(int i, int j, boolean [][] visited) { if(i >= visited.length || j >= visited[0].length) return false; if(i < 0 || j < 0) return false; return true; } static void selectionSort(long arr[], long [] arr1, ArrayList<ArrayList<Integer>> ans) { int n = arr.length; for (int i = 0; i < n-1; i++) { int min_idx = i; for (int j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; else if(arr[j] == arr[min_idx]) { if(arr1[j] < arr1[min_idx]) min_idx = j; } if(i == min_idx) { continue; } ArrayList<Integer> p = new ArrayList<Integer>(); p.add(min_idx + 1); p.add(i + 1); ans.add(new ArrayList<Integer>(p)); swap(i, min_idx, arr); swap(i, min_idx, arr1); } } static int saved = Integer.MAX_VALUE; static String ans1 = ""; public static boolean isValid(int x, int y, String [] mat) { if(x >= mat.length || x < 0) return false; if(y >= mat[0].length() || y < 0) return false; return true; } public static void recurse3(ArrayList<Character> arr, int index, String s, int max, ArrayList<String> toReturn) { if(s.length() == max) { toReturn.add(s); return; } if(index == arr.size()) return; recurse3(arr, index + 1, s + arr.get(index), max, toReturn); recurse3(arr, index + 1, s, max, toReturn); } /* if(arr[i] > q) return Math.max(f(i + 1, q - 1) + 1, f(i + 1, q); else return f(i + 1, q) + 1 */ static void dfsDP(ArrayList<ArrayList<Integer>> graph, int src, int [] dp1, int [] dp2, int parent) { int sum1 = 0; int sum2 = 0; for(int x : graph.get(src)) { if(x == parent) continue; dfsDP(graph, x, dp1, dp2, src); sum1 += Math.min(dp1[x], dp2[x]); sum2 += dp1[x]; } dp1[src] = 1 + sum1; dp2[src] = sum2; System.out.println(src + " " + dp1[src] + " " + dp2[src]); } static int balanced = 0; static void dfs(ArrayList<ArrayList<ArrayList<Long>>> graph, long src, int [] dist, long sum1, long sum2, long parent, ArrayList<Long> arr, int index) { index = 0;//binarySearch(index, arr.size() - 1, arr, sum1); if(index < arr.size() && arr.get(index) <= sum1) { dist[(int)src] = index + 1; } else dist[(int)src] = index; for(ArrayList<Long> x : graph.get((int)src)) { if(x.get(0) == parent) continue; if(arr.size() != 0) arr.add(arr.get(arr.size() - 1) + x.get(2)); else arr.add(x.get(2)); dfs(graph, x.get(0), dist, sum1 + x.get(1), sum2, src, arr, index); arr.remove(arr.size() - 1); } } static int compare(String s1, String s2) { Queue<Character> q1 = new LinkedList<>(); Queue<Character> q2 = new LinkedList<Character>(); for(int i = 0; i < s1.length(); i++) { q1.add(s1.charAt(i)); q2.add(s2.charAt(i)); } int k = 0; while(k < s1.length()) { if(q1.equals(q2)) { break; } q2.add(q2.poll()); k++; } return k; } static long pro = 0; public static int len(ArrayList<ArrayList<Integer>> graph, int src, boolean [] visited ) { visited[src] = true; int max = 0; for(int x : graph.get(src)) { if(!visited[x]) { visited[x] = true; int len = len(graph, x, visited) + 1; //System.out.println(len); pro = Math.max(max * (len - 1), pro); max = Math.max(len, max); } } return max; } public static void recurse(int l, int [] ans) { if(l < 0) return; int r = (int)Math.sqrt(l * 2); int s = r * r; r = s - l; recurse(r - 1, ans); while(r <= l) { ans[r] = l; ans[l] = r; r++; l--; } } static boolean isSmaller(String str1, String str2) { // Calculate lengths of both string 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; } // Function for find difference of larger numbers static String findDiff(String str1, String str2) { // Before proceeding further, make sure str1 // is not smaller if (isSmaller(str1, str2)) { String t = str1; str1 = str2; str2 = t; } // Take an empty string for storing result String str = ""; // Calculate length of both string int n1 = str1.length(), n2 = str2.length(); // Reverse both of strings str1 = new StringBuilder(str1).reverse().toString(); str2 = new StringBuilder(str2).reverse().toString(); int carry = 0; // Run loop till small string length // and subtract digit of str1 to str2 for (int i = 0; i < n2; i++) { // Do school mathematics, compute difference of // current digits int sub = ((int)(str1.charAt(i) - '0') - (int)(str2.charAt(i) - '0') - carry); // If subtraction is less than zero // we add then we add 10 into sub and // take carry as 1 for calculating next step if (sub < 0) { sub = sub + 10; carry = 1; } else carry = 0; str += (char)(sub + '0'); } // subtract remaining digits of larger number for (int i = n2; i < n1; i++) { int sub = ((int)(str1.charAt(i) - '0') - carry); // if the sub value is -ve, then make it // positive if (sub < 0) { sub = sub + 10; carry = 1; } else carry = 0; str += (char)(sub + '0'); } // reverse resultant string return new StringBuilder(str).reverse().toString(); } static void solve() throws IOException { int n = nextInt(); int [] arr = inputIntArr(); for(int i = 0; i < arr.length - 3; i+=2) { if(i == 0 && n % 2 != 0) { if(arr[i] > arr[i + 1] || arr[i] > arr[i + 2]) { output.write("NO\n"); return; } i--; }else { if(arr[i] > arr[i + 2] || arr[i] > arr[i + 3]) { output.write("NO\n"); return; }else if(arr[i + 1] > arr[i + 2] || arr[i + 1] > arr[i + 3]) { output.write("NO\n"); return; } } } if(n == 3) { if(arr[0] > arr[1] || arr[0] > arr[2]) { output.write("NO\n"); return; } } output.write("YES\n"); } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub int t = Integer.parseInt(sc.readLine()); for(int i = 0; i < t; i++) solve(); output.flush(); } } class TreeNode { int val; int start; int end; public TreeNode(int val, int x, int y) { this.val = val; this.start = x; this.end = y; } public String toString() { return Integer.toString(this.val); } } /* 1 10 6 10 7 9 11 99 45 20 88 31 */
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 11
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
c9d00a2937fc77a3b2d93538df755cda
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 TimePass { public static String solve(int n,int arr[]) { int temp[]=new int[n]; for(int i=0;i<n;i++) { temp[i]=arr[i]; } Arrays.sort(temp); PriorityQueue<Integer>pq=new PriorityQueue<>((a,b)->(a-b)); int j=0; pq.add(arr[j++]); if(n%2==0) pq.add(arr[j++]); int i=0; while(i<n) { if(temp[i]!=pq.poll()) return "NO"; i++; if((n-i)%2==0) { while(j<n&&pq.size()<2) pq.add(arr[j++]); } } return "YES"; } public static void main (String[] args) throws java.lang.Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(System.out); int testCases=Integer.parseInt(br.readLine()); while(testCases-->0){ int n=Integer.parseInt(br.readLine()); String input[]=br.readLine().split(" "); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=Integer.parseInt(input[i]); } String ans=solve(n,arr); out.print(ans+" "); out.print('\n'); } out.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 11
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
54d8195068c88c197ae01bb741c1a5d9
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
//<———My cp———— import java.util.*; import java.io.*; public class A_Minimums_and_Maximums{ public static void main(String[] args) throws Exception{ FastReader fr = new FastReader(System.in); int t = fr.nextInt(); while(t-->0){ int n = fr.nextInt(); int[] a = new int[n]; int[] sorted = new int[n]; for(int i=0;i<n;i++){ a[i] = fr.nextInt(); sorted[i] = a[i]; } Arrays.sort(sorted); boolean possible = true; if(n%2==1){ possible = (a[0]==sorted[0]); for(int i = 2;i<a.length && possible;i+=2){ boolean match = false; if(a[i]==sorted[i] && a[i-1]==sorted[i-1]){ match = true; } if(a[i]==sorted[i-1] && a[i-1]==sorted[i]){ match = true; } possible = match; } }else{ for(int i = 1;i<a.length && possible;i+=2){ boolean match = false; if(a[i]==sorted[i] && a[i-1]==sorted[i-1]){ match = true; } if(a[i]==sorted[i-1] && a[i-1]==sorted[i]){ match = true; } possible = match; } } if(possible){ System.out.println("YES"); }else{ System.out.println("NO"); } } } public static void print(Object val){ System.out.print(val); } public static void println(Object val){ System.out.println(val); } public static int[] sort(int[] vals){ ArrayList<Integer> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static long[] sort(long[] vals){ ArrayList<Long> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static void reverseArray(long[] vals){ int startIndex = 0; int endIndex = vals.length-1; while(startIndex<=endIndex){ long temp = vals[startIndex]; vals[startIndex] = vals[endIndex]; vals[endIndex] = temp; startIndex++; endIndex--; } } public static void reverseArray(int[] vals){ int startIndex = 0; int endIndex = vals.length-1; while(startIndex<=endIndex){ int temp = vals[startIndex]; vals[startIndex] = vals[endIndex]; vals[endIndex] = temp; startIndex++; endIndex--; } } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } public static int GCD(int numA, int numB){ if(numA==0){ return numB; }else if(numB==0){ return numA; }else{ if(numA>numB){ return GCD(numA%numB,numB); }else{ return GCD(numA,numB%numA); } } } }
Java
["3\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 11
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
a8dca2711571d88a8ae32ce02dd1bad0
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.ArrayList; import java.util.Collections; import java.util.StringTokenizer; import java.util.TreeSet; public class D implements Runnable { public static void main(String[] args) { new Thread(null, new D(), "whatever", 1 << 26).start(); } FastScanner s = new FastScanner(System.in); StringBuilder sb = new StringBuilder(); public void run() { int t = s.nextInt(); for (int test = 0; test < t; test++) { test(); } System.out.print(sb); } void test() { // Read input int n = s.nextInt(); ArrayList<Integer> nums = new ArrayList<>(); TreeSet<Pair> pairs = new TreeSet<>(); for (int i = 0; i < n ; i++) { int next = s.nextInt(); nums.add(next); pairs.add(new Pair(next, i)); } Collections.sort(nums); // Process // Only decision we have is to decide where to place val in odd string from a->b // and also which "middle" val to take for b -> c // When going from b -> c, we can always pick the smaller value in middle first. // So, determining if b -> c is possible is easy by simulation. // When going from a -> b, the order doesn't matter because: // if we have ____x____, and we add y, then y can go on either side of x, // regardless, y and x are together in the middle, so either can be chosen. // Solution: Arbitrarily place vals for a->b // Simulate b->c, choosing smaller first. // The above is too complex. New attempt: /* order in is reversed on way out. so the lowest item must be one of the last 2 going backwards After removing the lowest item, the second lowest must be one of the last 2. rinse and repeat. */ for (int target : nums) { // If number is first or second, remove and continue. // otherwise, fail. if (pairs.size() == 1) { break; // Done, it's ok } Pair pairToRemove = null; Pair first = pairs.first(); Pair second = pairs.higher(first); if (first.num == target) { pairToRemove = first; } else if (pairs.size() % 2 == 0 && second.num == target) { pairToRemove = second; } if (pairToRemove == null) { sb.append("NO\n"); return; } else { pairs.remove(pairToRemove); } } sb.append("YES\n"); // Add output to string builder sb (with newline if needed). } class Pair implements Comparable<Pair>{ int num; int index; public Pair(int num, int index) { this.num = num; this.index = index; } @Override public int compareTo(Pair o) { int res = Integer.compare(this.index, o.index); if (res != 0) { return res; } return Integer.compare(num, o.num); // tie break } @Override public String toString() { return "Pair{" + "num=" + num + ", index=" + index + '}'; } } 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 11
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
e958d2c3d438b65b75e4e6cc10245941
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 temp { public static void main(String[] args){ Scanner scn= new Scanner(System.in); int t= scn.nextInt(); while(t-->0){ int n= scn.nextInt(); int [] arr= new int[n]; for(int i=0; i<n; i++)arr[i]=scn.nextInt(); for(int i=arr.length-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 check= false; for(int i=0; i<arr.length-1; i++){ if(arr[i]> arr[i+1]){ check= true; break; } } if(check)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 11
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
306785b29254faafb0c78aa3ea25a976
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.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; public class D_A_B_C_Sort { public static void main(String[] args) { OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); FastReader f = new FastReader(); int t = f.nextInt(); while(t-- > 0){ solve(f, out); } out.close(); } public static void solve(FastReader f, PrintWriter out) { int n = f.nextInt(); int arr[] = f.nextArray(n); if(n == 1 || n == 2) { out.println("YES"); return; } if(n%2 == 0) { for(int i = 3; i < n; i += 2) { if(min(arr[i], arr[i-1]) < max(arr[i-2], arr[i-3])) { out.println("NO"); return; } } } else { if(arr[0] > min(arr[1], arr[2])) { out.println("NO"); return; } for(int i = 4; i < n; i += 2) { if(min(arr[i], arr[i-1]) < max(arr[i-2], arr[i-3])) { out.println("NO"); return; } } } out.println("YES"); // ArrayList<Point> arrli = new ArrayList<>(); // for(int i = 0; i < n; i++) { // arrli.add(new Point(f.nextInt(), i)); // } // Collections.sort(arrli, (p1, p2) -> (p1.x-p2.x)); // if(n%2 == 0) { // for(int i = 1; i < n; i += 2) { // if(abs(arrli.get(i).ind-arrli.get(i-1).ind) != 1) { // out.println("NO"); // return; // } // } // out.println("YES"); // } else { // if(arrli.get(0).ind != 0) { // out.println("NO"); // return; // } // for(int i = 2; i < n; i += 2) { // if(abs(arrli.get(i).ind-arrli.get(i-1).ind) != 1) { // out.println("NO"); // return; // } // } // out.println("YES"); // } } public static void sort(int arr[]) { ArrayList<Integer> al = new ArrayList<>(); for(int i: arr) { al.add(i); } Collections.sort(al); for(int i = 0; i < arr.length; i++) { arr[i] = al.get(i); } } public static void allDivisors(int n) { for(int i = 1; i*i <= n; i++) { if(n%i == 0) { System.out.println(i + " "); if(i != n/i) { System.out.println(n/i + " "); } } } } public static boolean isPrime(int n) { if(n < 1) return false; if(n == 2 || n == 3) return true; if(n % 2 == 0 || n % 3 == 0) return false; for(int i = 5; i*i <= n; i += 6) { if(n % i == 0 || n % (i+2) == 0) { return false; } } return true; } public static int gcd(int a, int b) { int dividend = a > b ? a : b; int divisor = a < b ? a : b; while(divisor > 0) { int reminder = dividend % divisor; dividend = divisor; divisor = reminder; } return dividend; } public static int lcm(int a, int b) { int lcm = gcd(a, b); int hcf = (a * b) / lcm; return hcf; } public static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } boolean nextBoolean() { return Boolean.parseBoolean(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextArray(int n) { int[] a = new int[n]; for(int i=0; i<n; i++) { a[i] = nextInt(); } return a; } } } // class Point { // int x; // int ind; // public Point(int x, int ind) { // this.x = x; // this.ind = ind; // } // } /** Dec Char Dec Char Dec Char Dec Char --------- --------- --------- ---------- 0 NUL (null) 32 SPACE 64 @ 96 ` 1 SOH (start of heading) 33 ! 65 A 97 a 2 STX (start of text) 34 " 66 B 98 b 3 ETX (end of text) 35 # 67 C 99 c 4 EOT (end of transmission) 36 $ 68 D 100 d 5 ENQ (enquiry) 37 % 69 E 101 e 6 ACK (acknowledge) 38 & 70 F 102 f 7 BEL (bell) 39 ' 71 G 103 g 8 BS (backspace) 40 ( 72 H 104 h 9 TAB (horizontal tab) 41 ) 73 I 105 i 10 LF (NL line feed, new line) 42 * 74 J 106 j 11 VT (vertical tab) 43 + 75 K 107 k 12 FF (NP form feed, new page) 44 , 76 L 108 l 13 CR (carriage return) 45 - 77 M 109 m 14 SO (shift out) 46 . 78 N 110 n 15 SI (shift in) 47 / 79 O 111 o 16 DLE (data link escape) 48 0 80 P 112 p 17 DC1 (device control 1) 49 1 81 Q 113 q 18 DC2 (device control 2) 50 2 82 R 114 r 19 DC3 (device control 3) 51 3 83 S 115 s 20 DC4 (device control 4) 52 4 84 T 116 t 21 NAK (negative acknowledge) 53 5 85 U 117 u 22 SYN (synchronous idle) 54 6 86 V 118 v 23 ETB (end of trans. block) 55 7 87 W 119 w 24 CAN (cancel) 56 8 88 X 120 x 25 EM (end of medium) 57 9 89 Y 121 y 26 SUB (substitute) 58 : 90 Z 122 z 27 ESC (escape) 59 ; 91 [ 123 { 28 FS (file separator) 60 < 92 \ 124 | 29 GS (group separator) 61 = 93 ] 125 } 30 RS (record separator) 62 > 94 ^ 126 ~ 31 US (unit separator) 63 ? 95 _ 127 DEL */
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 11
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
4b81832fe6786603e5c9d45970d4ba71
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 ProblemA{ static long mod = 1000000007L; static MyScanner sc = new MyScanner(); //code here static List<char[]> ans = new ArrayList<>(); static void solve(){ int n = sc.nextInt(); int[] a = sc.readIntArray(n); if(n == 1) { out.println("YES"); return; } for(int i=n-1;i>=1;i-=2) { if(a[i]<a[i-1]) { swap(a,i,i-1); } } for(int i=0;i<n-1;i++) { if(a[i]> a[i+1]) { out.println("NO"); return; } } out.println("YES"); } static void swap(int[] a,int i,int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static int LowerBound(int a[], int x) { int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } static int UpperBound(int a[], int x) { int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } // code ends static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } static boolean isPali(String str){ int i = 0; int j = str.length()-1; while(i<j){ if(str.charAt(i)!=str.charAt(j)){ return false; } i++; j--; } return true; } static void priArr(int[] a) { for(int i=0;i<a.length;i++) { out.print(a[i] + " "); } out.println(); } static long gcd(long a,long b){ if(b==0) return a; return gcd(b,a%b); } static String reverse(String str){ char arr[] = str.toCharArray(); int i = 0; int j = arr.length-1; while(i<j){ char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } String st = new String(arr); return st; } static void reverse(int[] arr){ int i = 0; int j = arr.length-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static boolean isprime(int n){ if(n==1) return false; if(n==3 || n==2) return true; if(n%2==0 || n%3==0) return false; for(int i = 5;i*i<=n;i+= 6){ if(n%i== 0 || n%(i+2)==0){ return false; } } return true; } static class Pair implements Comparable<Pair>{ int val; int ind; int ans; Pair(int v,int f){ val = v; ind = f; } public int compareTo(Pair p){ return p.val - this.val; } } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); // int t= 1; while(t-- >0){ // solve(); solve(); } // Stop writing your solution here. ------------------------------------- out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int[] readIntArray(int n){ int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(next()); } return arr; } int[] reverse(int arr[]){ int n= arr.length; int i = 0; int j = n-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; j--;i++; } return arr; } long[] readLongArray(int n){ long arr[] = new long[n]; for(int i = 0;i<n;i++){ arr[i] = Long.parseLong(next()); } return arr; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } }
Java
["3\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 11
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
7cd7ba1e272ed181cf9f02f69ba517ca
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.util.Map.Entry; import java.io.*; import java.lang.*; /** * D_A-B-C_Sort */ public class D_A_B_C_Sort { private static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static int mod = 998244353; private static FastReader sc = new FastReader(); public static void main(String[] args) { int t = sc.nextInt(); while(t-->0) { solve(); } } private static void solve() { int n = sc.nextInt(); List<Integer> a = new LinkedList<>(); int z[] = new int[n]; for(int i=0; i<n; i++){ a.add(sc.nextInt()); z[i] = a.get(i); } Arrays.sort(z); for(int i=0; i<n/2; i++){ z[i] = z[i]+z[n-i-1]; z[n-i-1] = z[i] - z[n-i-1]; z[i] = z[i] - z[n-i-1];; } System.out.println(find(a, n, z)?"YES":"NO"); } private static boolean find(List<Integer> a, int n, int p[]) { if(n==0){ return true; } if((n&1)==1){ if(a.get(0)!=p[n-1]){ return false; } a.remove(0); n--; return find(a, n, p); }else{ if(a.get(0)!=p[n-1] && a.get(1)!=p[n-1]){ return false; } if(a.get(0)>a.get(1)){ a.set(1, a.get(0)); } a.remove(0); n--; return find(a, n, p); } } }
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 11
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
f8090516c23fbbb44da38be48d30cb6e
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; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int tc = 0; tc < t; ++tc) { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < a.length; ++i) { a[i] = sc.nextInt(); } System.out.println(solve(a) ? "YES" : "NO"); } sc.close(); } static boolean solve(int[] a) { for (int i = (a.length % 2 == 0) ? 0 : 1; i < a.length; i += 2) { if (a[i] > a[i + 1]) { int temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } } return IntStream.range(0, a.length - 1).allMatch(i -> a[i] <= a[i + 1]); } }
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 11
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
40e85bb43c17053fc03a21e0943f625e
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 roughCodeforces { public static boolean isok(long x, long h, long k){ long sum = 0; if(h > k){ long t1 = h - k; long t = t1 * k; sum += (k * (k + 1)) / 2; sum += t - (t1 * (t1 + 1) / 2); }else{ sum += (h * (h + 1)) / 2; } if(sum < x){ return true; } return false; } public static boolean binary_search(long[] a, long k){ long low = 0; long high = a.length - 1; long mid = 0; while(low <= high){ mid = low + (high - low) / 2; if(a[(int)mid] == k){ return true; }else if(a[(int)mid] < k){ low = mid + 1; }else{ high = mid - 1; } } return false; } public static long lowerbound(long a[], long ddp){ long low = 0; long high = a.length; long mid = 0; while(low < high){ mid = low + (high - low)/2; if(a[(int)mid] == ddp){ return mid; } if(a[(int)mid] < ddp){ low = mid + 1; }else{ high = mid; } } // if(low + 1 < a.length && a[(int)low + 1] <= ddp){ // low++; // } if(low == a.length && low != 0){ low--; return low; } if(a[(int)low] > ddp && low != 0){ low--; } return low; } public static long upperbound(long a[], long ddp){ long low = 0; long high = a.length; long mid = 0; while(low < high){ mid = low + (high - low) / 2; if(a[(int)mid] < ddp){ low = mid + 1;; }else{ high = mid; } } if(low == a.length){ return a.length - 1; } return low; } public static class pair{ long w; long h; public pair(long w, long h){ this.w = w; this.h = h; } } public static class trinary{ long a; long b; long c; public trinary(long a, long b, long c){ this.a = a; this.b = b; this.c = c; } } public static long lowerboundforpairs(pair a[], long pr){ long low = 0; long high = a.length; long mid = 0; while(low < high){ mid = low + (high - low)/2; if(a[(int)mid].w <= pr){ low = mid + 1; }else{ high = mid; } } // if(low + 1 < a.length && a[(int)low + 1] <= ddp){ // low++; // } // if(low == a.length && low != 0){ // low--; // return low; // } // if(a[(int)low].w > pr && low != 0){ // low--; // } return low; } public static pair[] sortpair(pair[] a){ Arrays.sort(a, new Comparator<pair>() { public int compare(pair p1, pair p2){ return (int)p1.w - (int)p2.w; } }); return a; } public static boolean ispalindrome(String s){ long i = 0; long j = s.length() - 1; boolean is = false; while(i < j){ if(s.charAt((int)i) == s.charAt((int)j)){ is = true; i++; j--; }else{ is = false; return is; } } return is; } public static void sort(long[] arr) { ArrayList<Long> a = new ArrayList<>(); for (long i : arr) { a.add(i); } Collections.sort(a); for (int i = 0; i < a.size(); i++) { arr[i] = a.get(i); } } public static void sortForObjecttypes(Long[] arr) { ArrayList<Long> a = new ArrayList<>(); for (Long i : arr) { a.add(i); } Collections.sort(a); for (int i = 0; i < a.size(); i++) { arr[i] = a.get(i); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } void readArr(int[] ar, int n) { for (int i = 0; i < n; i++) { ar[i] = nextInt(); } } } public static void solve(FastReader sc, PrintWriter w) throws Exception { long n = sc.nextLong(); long a[] = new long[(int)n]; long b[] = new long[(int)n]; for(long i = 0;i < n;i++){ a[(int)i] = sc.nextLong(); b[(int)i] = a[(int)i]; } Arrays.sort(b); if(n == 1){ System.out.println("YES"); }else{ boolean is = false; if(n % 2 == 0){ for(long i = n - 1;i >= 0;i -= 2){ if(a[(int)i] < a[(int)i - 1]){ long t = a[(int)i]; a[(int)i] = a[(int)i - 1]; a[(int)i - 1] = t; } } }else{ for(long i = n - 1;i >= 1;i -= 2){ if(a[(int)i] < a[(int)i - 1]){ long t = a[(int)i]; a[(int)i] = a[(int)i - 1]; a[(int)i - 1] = t; } } } is = false; for(long i = 0;i < n;i++){ // System.out.println(a[(int)i] + " " + b[(int)i]); if(a[(int)i] == b[(int)i]){ is = true; }else{ is = false; break; } } if(is){ System.out.println("YES"); }else{ System.out.println("NO"); } } } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter w = new PrintWriter(System.out); long o = sc.nextLong(); while (o-- > 0) { solve(sc, w); } 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 11
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
3da57339e197873188369bbf035e5177
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.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.security.cert.X509CRL; import java.util.*; import java.lang.*; import java.util.stream.Collector; import java.util.stream.Collectors; @SuppressWarnings("unused") public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static String OUTPUT = ""; //global //private final static long BASE = 998244353L; private final static int ALPHABET = (int)('z') - (int)('a') + 1; private final static int BASE = 1000000007; private final static int INF_I = (1<<31)-1; private final static long INF_L = (1l<<63)-1; private final static int MAXN = 100100; private final static int MAXK = 31; static void solve() { int ntest = readInt(); //out.println(shift('f',2)); for (int test=0;test<ntest;test++) { int N = readInt(); int[] A = readIntArray(N); for (int i=N%2;i+1<N;i+=2) { if (A[i]<=A[i+1]) continue; int tmp = A[i]; A[i] = A[i+1]; A[i+1] = tmp; } boolean isFine = true; for (int i=0;i+1<N;i++) isFine &= (A[i]<=A[i+1]); if (isFine) out.println("yes"); else out.println("no"); } } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); if (INPUT=="") { is = System.in; } else { File file = new File(INPUT); is = new FileInputStream(file); } if (OUTPUT == "") out = new PrintWriter(System.out); else out = new PrintWriter(OUTPUT); solve(); out.flush(); long G = System.currentTimeMillis(); } private static class Point<T extends Number & Comparable<T>> implements Comparable<Point<T>> { private T x; private T y; public Point(T x, T y) { this.x = x; this.y = y; } public T getX() {return x;} public T getY() {return y;} @Override public int compareTo(Point<T> o) { int cmp = x.compareTo(o.getX()); if (cmp==0) return y.compareTo(o.getY()); return cmp; } } private static class ClassComparator<T extends Comparable<T>> implements Comparator<T> { public ClassComparator() {} @Override public int compare(T a, T b) { return a.compareTo(b); } } private static class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> { public ListComparator() {} @Override public int compare(List<T> o1, List<T> o2) { for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) { int c = o1.get(i).compareTo(o2.get(i)); if (c != 0) { return c; } } return Integer.compare(o1.size(), o2.size()); } } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double readDouble() { return Double.parseDouble(readString()); } private static char readChar() { return (char)skip(); } private static String readString() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] readChar(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] readTable(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = readChar(m); return map; } private static int[] readIntArray(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = readInt(); return a; } private static long[] readLongArray(int n) { long[] a = new long[n]; for (int i=0;i<n;i++) a[i] = readLong(); return a; } private static int readInt() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long readLong() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
Java
["3\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 11
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
1132efdc025b6ea5a87688ea84133070
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 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 temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } boolean flag=true; for(int i=0;i<n-1;i++) { if(arr[i]>arr[i+1]) { flag=false; break; } } if(flag) { System.out.println("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 11
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
793828881f9c6d5b58370127c084bb98
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
//Utilities import java.io.*; import java.util.*; public class a { static int t; static int n; static int[] a, b; public static void main(String[] args) throws IOException { t = in.iscan(); outer : while (t-- > 0) { n = in.iscan(); a = new int[n]; b = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.iscan(); b[i] = a[i]; } UTILITIES.sort(b, true); if (n % 2 == 0) { for (int i = 0; i < n; i += 2) { int min = Math.min(a[i], a[i+1]), max = Math.max(a[i], a[i+1]); if (min != b[i] || max != b[i+1]) { out.println("NO"); continue outer; } } } else { if (a[0] != b[0]) { out.println("NO"); continue outer; } for (int i = 1; i < n; i += 2) { int min = Math.min(a[i], a[i+1]), max = Math.max(a[i], a[i+1]); if (min != b[i] || max != b[i+1]) { out.println("NO"); continue outer; } } } out.println("YES"); } out.close(); } static INPUT in = new INPUT(System.in); static PrintWriter out = new PrintWriter(System.out); private static class INPUT { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public INPUT (InputStream stream) { this.stream = stream; } public INPUT (String file) throws IOException { this.stream = new FileInputStream (file); } public int cscan () throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read (buf); } if (numChars == -1) return numChars; return buf[curChar++]; } public int iscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } int res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public String sscan () throws IOException { int c = cscan (); while (space (c)) c = cscan (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = cscan (); } while (!space (c)); return res.toString (); } public double dscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } double res = 0; while (!space (c) && c != '.') { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); res *= 10; res += c - '0'; c = cscan (); } if (c == '.') { c = cscan (); double m = 1; while (!space (c)) { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); m /= 10; res += (c - '0') * m; c = cscan (); } } return res * sgn; } public long lscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } long res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public boolean space (int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } public static class UTILITIES { static final double EPS = 10e-6; public static void sort(int[] a, boolean increasing) { ArrayList<Integer> arr = new ArrayList<Integer>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(long[] a, boolean increasing) { ArrayList<Long> arr = new ArrayList<Long>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(double[] a, boolean increasing) { ArrayList<Double> arr = new ArrayList<Double>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static int lower_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } public static void updateMap(HashMap<Integer, Integer> map, int key, int v) { if (!map.containsKey(key)) { map.put(key, v); } else { map.put(key, map.get(key) + v); } if (map.get(key) == 0) { map.remove(key); } } public static long gcd (long a, long b) { return b == 0 ? a : gcd (b, a % b); } public static long lcm (long a, long b) { return a * b / gcd (a, b); } public static long fast_pow_mod (long b, long x, int mod) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod; return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod; } public static long fast_pow (long b, long x) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow (b * b, x / 2); return b * fast_pow (b * b, x / 2); } public static long choose (long n, long k) { k = Math.min (k, n - k); long val = 1; for (int i = 0; i < k; ++i) val = val * (n - i) / (i + 1); return val; } public static long permute (int n, int k) { if (n < k) return 0; long val = 1; for (int i = 0; i < k; ++i) val = (val * (n - i)); return val; } // start of permutation and lower/upper bound template public static void nextPermutation(int[] nums) { //find first decreasing digit int mark = -1; for (int i = nums.length - 1; i > 0; i--) { if (nums[i] > nums[i - 1]) { mark = i - 1; break; } } if (mark == -1) { reverse(nums, 0, nums.length - 1); return; } int idx = nums.length-1; for (int i = nums.length-1; i >= mark+1; i--) { if (nums[i] > nums[mark]) { idx = i; break; } } swap(nums, mark, idx); reverse(nums, mark + 1, nums.length - 1); } public static void swap(int[] nums, int i, int j) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } public static void reverse(int[] nums, int i, int j) { while (i < j) { swap(nums, i, j); i++; j--; } } static int lower_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= cmp) high = mid; else low = mid + 1; } return low; } static int upper_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > cmp) high = mid; else low = mid + 1; } return low; } // end of permutation and lower/upper bound template } }
Java
["3\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 11
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
3decea85933fe42cccd375b3e9a17099
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.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class D { public static boolean sol(List<Integer> arr, int n) { for (int i = n%2; i < n; i += 2) { if (arr.get(i) > arr.get(i+1)) Collections.swap(arr, i, i+1); } for (int i = 0; i < n-1; ++i) if (arr.get(i) > arr.get(i + 1)) return false; return true; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int test_cases = sc.nextInt(); for (int t = 0; t < test_cases; ++t) { List<Integer> arr = new ArrayList<>(); int n = sc.nextInt(); for (int i = 0; i < n; ++i) arr.add(sc.nextInt()); String status = sol(arr, n) ? "YES" : "NO"; System.out.println(status); } } }
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 11
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