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
2a40f76db990ea2f91f58a9b3d182c29
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class cp { public static void main(String[] args) throws IOException { //Your Solve FastReader s = new FastReader(); // Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int p = 0;p < t;p++) { int n = s.nextInt(); int a[] = new int[n]; for(int i = 0;i < n;i++) { a[i] = s.nextInt(); } String str = s.next(); int countB=0; for(int i = 0;i < n;i++) { if(str.charAt(i) == 'B') { countB++; } } // int countr = 0; // int countb = 0; int flag = 0; int countR = n-countB; int R[] = new int[countR]; int B[] = new int[countB]; int indexR = 0; int indexB = 0; for(int i = 0;i < n;i++) { if(str.charAt(i) == 'R') { R[indexR] = a[i]; indexR++; }else { B[indexB] = a[i]; indexB++; } } shuffleArray(R); shuffleArray(B); Arrays.sort(R); Arrays.sort(B); indexR=0;indexB=0; for(int i = 0;i < countR;i++) { if(R[indexR]>countB+indexR+1) { flag = 1; break; } indexR++; } for(int i = 0;i < countB;i++) { if(B[indexB]<indexB+1) { flag = 1; break; } indexB++; } if(flag==1) { System.out.println("NO"); }else { System.out.println("YES"); } } } public static boolean palindrome(Vector<Integer> v) { int start = 0;int end = v.size()-1; while(start<end) { if(v.get(start) != v.get(end)) { return false; } start++; end--; } return true; } static void shuffleArray(int[] ar) { // If running on Java 6 or older, use `new Random()` on RHS here Random rnd = new Random(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap int a = ar[index]; ar[index] = ar[i]; ar[i] = a; } } /* Iterative Function to calculate (x^y) in O(log y) */ static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long getPairsCount(int n, long sum,long arr[]) { HashMap<Long, Integer> hm = new HashMap<>(); // Store counts of all elements in map hm for (int i = 0; i < n; i++) { // initializing value to 0, if key not found if (!hm.containsKey(arr[i])) hm.put(arr[i], 0); hm.put(arr[i], hm.get(arr[i]) + 1); } long twice_count = 0; // iterate through each element and increment the // count (Notice that every pair is counted twice) for (int i = 0; i < n; i++) { if (hm.get(sum - arr[i]) != null) twice_count += hm.get(sum - arr[i]); // if (arr[i], arr[i]) pair satisfies the // condition, then we need to ensure that the // count is decreased by one such that the // (arr[i], arr[i]) pair is not considered if (sum - arr[i] == arr[i]) twice_count--; } // return the half of twice_count return twice_count / 2; } public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<String, Integer> > list = new LinkedList<Map.Entry<String, Integer> >( hm.entrySet()); // Sort the list using lambda expression Collections.sort( list, (i1, i2) -> i1.getValue().compareTo(i2.getValue())); // put data from sorted list to hashmap HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>(); for (Map.Entry<String, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } public static String palin(String str) { StringBuilder ans = new StringBuilder(); for(int i = str.length()-1;i >= 0 ;i--) { ans.append(str.charAt(i)); } return ans.toString(); } public static String palin(StringBuilder str) { StringBuilder ans = new StringBuilder(); for(int i = str.length()-1;i >= 0 ;i--) { ans.append(str.charAt(i)); } return ans.toString(); } public static int getInt(char ch) { int ans = 0; if(ch >= '0' && ch <= '9') { ans = ch-'0'; }else if(ch >= 'A' && ch <= 'Z') { ans = ch-'A' + 10; }else if(ch >= 'a' && ch <= 'z') { ans = ch-'a' + 36; }else if(ch == '-') { ans = 62; }else { ans = 63; } return ans; } public static int getZeroes(int base64) { int count = 0; int iters = 0; while(iters != 6) { if(base64%2 == 0) { count++; } base64 /= 2; iters++; } return count; } static int lower_bound(long array[], long key,long start,long end) { // Initialize starting index and // ending index long low = start, high = end; long mid; // Till high does not crosses low while (low < high) { // Find the index of the middle element mid = low + (high - low) / 2; // If key is less than or equal // to array[mid], then find in // left subarray if (key <= array[(int)mid]) { high = mid; } // If key is greater than array[mid], // then find in right subarray else { low = mid + 1; } } // If key is greater than last element which is // array[n-1] then lower bound // does not exists in the array if (low < end && array[(int)low] < key) { low++; } // Returning the lower_bound index return (int)low; } static int upper_bound(long array[], long key,long start,long end) { // Initialize starting index and // ending index long low = start, high = end; long mid; // Till high does not crosses low while (low < high) { // Find the index of the middle element mid = low + (high - low) / 2; // If key is less than or equal // to array[mid], then find in // left subarray if (key >= array[(int)mid]) { low = mid+1; } // If key is greater than array[mid], // then find in right subarray else { high = mid; } } // If key is greater than last element which is // array[n-1] then lower bound // does not exists in the array if (low < end && array[(int)low] <= key) { low++; } // Returning the lower_bound index return (int)low; } public static long search1(long x,long k) { long start = 1,end = k; long mid = start + (end-start)/2; while(start < end) { mid = start + (end-start)/2; if(x>mid*(mid+1)/2) { start = mid+1; }else { end = mid; } } if(start<end && mid*(mid+1)/2 <= x) { start++; } return start; } public static long search2(long x,long k) { long start = 1,end = k; long mid = start + (end-start)/2; while(start < end) { mid = start + (end-start)/2; if(x>mid*(2*k-mid-1)/2) { start = mid+1; }else { end = mid; } } if(start<end && mid*(2*k-mid-1)/2 <= x) { start++; } return start; } public static int countGreater(int arr[], int k,int l, int r) { int leftGreater = r+1; int R = r; while (l<=r) { int m = l + (r - l) / 2; if (arr[m] >= k) { leftGreater = m; r = m - 1; } else { l = m + 1; } } return (R-leftGreater+1); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
067dfb30f9f55d39ec0309696517ad20
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Solution { static PrintWriter out; public static void main(String[] args) { FastReader in = new FastReader(); out = new PrintWriter(System.out); long t = in.nextLong(); long test = 1; while (test <= t) { //out.println("Case #" + test + ": " + solve(in)); solve(in); test++; } out.close(); } /*--------------------------------------------------------------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------------------------------------------------------------*/ private static void solve(FastReader in) { int n = in.nextInt(); int[] arr = in.readArray(n); String s = in.next(); PriorityQueue<Integer> b = new PriorityQueue<>(); PriorityQueue<Integer> r = new PriorityQueue<>(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'B') b.add(arr[i]); else r.add(arr[i]); } char[] ch = s.toCharArray(); Arrays.sort(ch); List<Integer> list = new ArrayList<>(); while (!b.isEmpty()) { list.add(b.poll()); } while (!r.isEmpty()) list.add(r.poll()); //System.out.println(list); int cnt = 1; String ans = "yes"; for (int i : list) { if (ch[cnt - 1] == 'B') { if (i < cnt) { ans = "no"; break; } } else { if (cnt < i) { ans = "no"; break; } } cnt++; } out.println(ans); } /*--------------------------------------------------------------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------------------------------------------------------------*/ 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); } private static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return this.x == o.x ? this.y - o.y : this.x - o.x; } } 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[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } 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
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
d6f83d995cab3ecb30d27361471cf360
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
//jai Shree Krishna import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.util.Collections; import java.util.Comparator; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.StringTokenizer; public class C{ // author: Tarun Verma static FastScanner sc = new FastScanner(); static int inf = Integer.MAX_VALUE; static long mod = 1000000007; static BufferedWriter output = new BufferedWriter( new OutputStreamWriter(System.out)); static PrintWriter out=new PrintWriter(System.out); /* Common Mistakes By Me * make sure to read the bottom part of question * special cases (n=1?) * In Game Theory Check your solution and consider all the solutions * Always initialise value to the declare array in local scope * don't use debugs in interactive problems * Always Reset vis,adj array upto n+1 otherwise can cause TLE */ static Comparator<pair> comp = new Comparator<pair>() { @Override public int compare(pair o1, pair o2) { if(o1.fr == o2.fr) { return o1.sc - o2.sc; } return o1.fr - o2.fr; } }; public static void solve() { int n = sc.nextInt(); int arr[] =sc.readArray(n); String s = sc.next(); for(int i= 0;i<n;i++) { if(arr[i] < 1 && s.charAt(i) == 'B') { System.out.println("NO"); return; } if(arr[i] > n && s.charAt(i) == 'R') { System.out.println("NO"); return; } } ArrayList<pair> a = new ArrayList<>(); for(int i =0;i<n;i++) { if(s.charAt(i) == 'B') { a.add(new pair(1, min(n, arr[i]))); } else { a.add(new pair(max(arr[i], 1), n)); } } Collections.sort(a, comp); // for(pair i: a) { // System.out.println(i.fr + " " + i.sc); // } int k = 1; for(pair i: a) { if(i.fr > k || i.sc < k ) { System.out.println("NO"); return; } k++; } System.out.println("YES"); } public static void main(String[] args) { int t = 1; t = sc.nextInt(); outer: for (int tt = 0; tt < t; tt++) { solve(); } } //////////////////////////////////////////////////////////////////////////////////// ////////////////////DO NOT BELOW THIS LINE ////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// static int max(int a, int b) { return Math.max(a, b);} static int min(int a, int b) {return Math.min(a, b);} static long max(long a, long b){return Math.max(a, b);} static long min(long a, long b) {return Math.min(a, b);} static void sort(int arr[]) { ArrayList<Integer> a = new ArrayList<Integer>(); for(int i: arr) a.add(i); Collections.sort(a); for(int i=0;i<arr.length;i++) arr[i] = a.get(i); } static void sort(long arr[]) { ArrayList<Long> a = new ArrayList<Long>(); for(long i: arr) a.add(i); Collections.sort(a); for(int i=0;i<arr.length;i++) arr[i] = a.get(i); } static int abs(int a) {return Math.abs(a);} static long abs(long a) {return Math.abs(a);} static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[][] read2dArray(int n, int m) { int arr[][] = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = nextInt(); } } return arr; } ArrayList<Integer> readArrayList(int n) { ArrayList<Integer> arr = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { int a = nextInt(); arr.add(a); } return arr; } long nextLong() { return Long.parseLong(next()); } } static class pair { int fr, sc; pair(int fr, int sc) { this.fr = fr; this.sc = sc; } } //////////////////////////////////////////////////////////////////////////////////// }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
cfec83661cfda1cdf3795aa6e2de42e5
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; public class solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testcases = sc.nextInt(); for(int i=0 ; i < testcases; i++){ int nums = sc.nextInt(); int a[] = new int[nums]; for(int j = 0; j < nums; j++){ a[j] = sc.nextInt(); } sc.nextLine(); String colors = sc.nextLine(); List<Integer> r = new ArrayList<Integer>(); List<Integer> b = new ArrayList<Integer>(); for(int j = 0; j < nums; j++){ if(colors.charAt(j) == 'B'){ b.add(a[j]); } else r.add(a[j]); } Collections.sort(r, Collections.reverseOrder()); Collections.sort(b); boolean flag = true; for(int j = 0; j < b.size(); j++){ if(b.get(j) < j+1) flag = false; } for(int j = 0; j < r.size() ; j++){ if(r.get(j) > nums - j) flag = false; } System.out.println(flag ? "YES" : "NO"); } sc.close(); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
88902e88c1471036e8bc78a622295891
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class P1607D { public static void main(String[] args) throws IOException { MyScanner scanner = new MyScanner(System.in); PrintWriter writer = new PrintWriter(System.out); int t = scanner.nextInt(); while (t > 0) { int n = scanner.nextInt(); int[] numbers = new int[n]; for (int i = 0; i < n; i++) { numbers[i] = scanner.nextInt(); } String colors = scanner.nextToken(); ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for (int i = 0; i < n; i++) { char c = colors.charAt(i); if (c == 'B') { blue.add(numbers[i]); } else { red.add(numbers[i]); } } Collections.sort(blue); Collections.sort(red); int bFront = 0; int rFront = 0; boolean solvable = true; for (int i = 1; i <= n; i++) { if (bFront < blue.size() && blue.get(bFront) < i) { solvable = false; break; } if (bFront < blue.size() && blue.get(bFront) == i) { bFront++; continue; } if (bFront < blue.size() && blue.get(bFront) > i) { bFront++; continue; } if (rFront < red.size() && red.get(rFront) < i) { rFront++; continue; } if (rFront < red.size() && red.get(rFront) == i) { rFront++; continue; } solvable = false; break; } if (solvable) { writer.println("YES"); } else { writer.println("NO"); } t--; } writer.flush(); } private static class MyScanner { private BufferedReader reader; private StringTokenizer st; public MyScanner(InputStream inputStream) { reader = new BufferedReader(new InputStreamReader(inputStream)); } private String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) { st = new StringTokenizer(reader.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
2139ade994aeb489c37d6cd226e5827d
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; public class mentor1 { public static boolean solve(int n, String color, int[] arr){ List<Integer> Barr = new ArrayList<Integer>(); List<Integer> Rarr = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { if(color.charAt(i) == 'B')Barr.add(arr[i]); else Rarr.add(arr[i]); } Barr.sort(Comparator.naturalOrder()); Rarr.sort(Comparator.reverseOrder()); for (int i = 0; i < Barr.size(); i++) { if(Barr.get(i)< i + 1)return false; } for (int i = 0; i < Rarr.size(); i++) { int expect = n-i; if(Rarr.get(i) > expect)return false; } return true; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); for (int i = 0; i < n; i++) { int m = input.nextInt(); int[] arr = new int[m]; for(int j = 0;j<m; j++)arr[j] = input.nextInt(); String color = input.next(); if(solve(m,color,arr)) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
e8f89e29e57db7f9546dcf15fc22a875
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import static java.lang.Math.*; import java.util.*; import java.io.*; public class Solution { public static boolean find(int[] a, char[] col) { int n = a.length; for (int i = 0; i < n; i++) { if ((col[i] == 'R' && a[i] > n) || (col[i] == 'B' && a[i] < 1)) { return false; } } ArrayList<Integer> red = new ArrayList<>(); ArrayList<Integer> blue = new ArrayList<>(); for (int i = 0; i < n; i++) { if (col[i] == 'B') blue.add(a[i]); else red.add(a[i]); } Collections.sort(blue); Collections.sort(red); int start = 1; for (int i: blue) { if (i >= start) { start++; } else { return false; } } for (int i: red) { if (i <= start) start++; else { return false; } } return true; } public static void main(String[] args) throws Exception { InputReader in = new InputReader(System.in); PrintWriter pw = new PrintWriter(System.out); int t = in.readInt(); for (int tt = 0; tt < t; tt++) { int n = in.readInt(); int[] a = in.readIntArray(n); char[] c = in.readString().toCharArray(); boolean res = find(a, c); if (res) pw.println("YES"); else pw.println("NO"); } pw.flush(); pw.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public 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 { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = readInt(); } return a; } public long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = readLong(); } return a; } public String next() { return readString(); } interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
caba0a6515443325f8e21c9bf13d5917
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class Main { static long mod = 1000000007; static long inv(long a, long b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; } static long mi(long a) { return inv(a, mod); } static InputReader sc = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] A = new int[n]; for (int i = 0; i < A.length; i++) { A[i] = sc.nextInt(); } String word = sc.next(); ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for (int i = 0; i < word.length(); i++) { if (word.charAt(i) == 'R') { red.add(A[i]); } else { blue.add(A[i]); } } Collections.sort(blue); Collections.sort(red); boolean possible = true; int a = 1; for (int i = 0; i < blue.size(); i++, a++) { if (blue.get(i) < a) { possible = false; break; } } for (int i = 0; i < red.size(); i++, a++) { if (red.get(i) > a) { possible = false; break; } } if (possible) out.println("YES"); else out.println("NO"); } out.flush(); out.close(); } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.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 = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return str; } } public static void shuffle(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; } } public static void shuffle(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; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
de5f6819cae6a748ad037c44f7a2d42f
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class Main { static long mod = 1000000007; static long inv(long a, long b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; } static long mi(long a) { return inv(a, mod); } static InputReader sc = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); public static <List> void main(String[] args) throws IOException { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] A = new int[n]; for (int i = 0; i < A.length; i++) { A[i] = sc.nextInt(); } String word = sc.next(); ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for (int i = 0; i < word.length(); i++) { if (word.charAt(i) == 'R') { red.add(A[i]); } else { blue.add(A[i]); } } Collections.sort(blue); Collections.sort(red); boolean possible = true; int a = 1; for (int i = 0; i < blue.size(); i++, a++) { if (blue.get(i) <= 0 || blue.get(i) < a) { possible = false; break; } } for (int i = 0; i < red.size(); i++, a++) { if (red.get(i) > n || red.get(i) > a) { possible = false; break; } } if (possible) out.println("YES"); else out.println("NO"); } out.flush(); out.close(); } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.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 = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return str; } } public static void shuffle(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; } } public static void shuffle(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; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
0ac2c8bddd239f163cff05488a46de88
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; import java.io.*; public class Main { // Graph // prefix sums //inputs // public static void main(String args[])throws Exception{ Input sc=new Input(); precalculates p=new precalculates(); StringBuilder sb=new StringBuilder(); int t=sc.readInt(); for(int f=0;f<t;f++){ int n=sc.readInt(); int a[]=sc.readArray(); String str=sc.readString(); //char ch[]=str.toCharArray(); int max1,max2; max1=max2=Integer.MIN_VALUE; int min1,min2; min1=min2=Integer.MAX_VALUE; ArrayList<Integer> lst1=new ArrayList<>(); ArrayList<Integer> lst2=new ArrayList<>(); boolean b=true; for(int i=0;i<n;i++){ if(str.charAt(i)=='B'){ lst1.add(a[i]); // if(a[i]<=0){ // b=false; // break; // } } } for(int i=0;i<n && b;i++){ if(str.charAt(i)=='R'){ lst2.add(a[i]); // if(a[i]>n){ // b=false; // break; // } } } Collections.sort(lst1); Collections.sort(lst2); int j=1; for(int i=0;i<lst1.size() && b;i++){ if(lst1.get(i)<j){ b=false; break; } j++; } for(int i=0;i<lst2.size() && b;i++){ if(lst2.get(i)>j){ b=false; break; } j++; } if(b) sb.append("YES\n"); else sb.append("NO\n"); // if(b) // sb.append("YES\n"); // else // sb.append("NO\n"); } System.out.print(sb); } } class pair implements Comparable<pair>{ int val; char ch; public pair(int val, char ch) { this.val = val; this.ch = ch; } @Override public int compareTo(pair o) { if(this.val==o.val){ return this.ch-o.ch; }else { return this.val-o.val; } } } class Input{ BufferedReader br; StringTokenizer st; Input(){ br=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); } public int[] readArray() throws Exception{ st=new StringTokenizer(br.readLine()); int a[]=new int[st.countTokens()]; for(int i=0;i<a.length;i++){ a[i]=Integer.parseInt(st.nextToken()); } return a; } public long[] readArrayLong() throws Exception{ st=new StringTokenizer(br.readLine()); long a[]=new long[st.countTokens()]; for(int i=0;i<a.length;i++){ a[i]=Long.parseLong(st.nextToken()); } return a; } public int readInt() throws Exception{ st=new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } public long readLong() throws Exception{ st=new StringTokenizer(br.readLine()); return Long.parseLong(st.nextToken()); } public String readString() throws Exception{ return br.readLine(); } public int[][] read2dArray(int n,int m)throws Exception{ int a[][]=new int[n][m]; for(int i=0;i<n;i++){ st=new StringTokenizer(br.readLine()); for(int j=0;j<m;j++){ a[i][j]=Integer.parseInt(st.nextToken()); } } return a; } } class precalculates{ public int[] prefixSumOneDimentional(int a[]){ int n=a.length; int dp[]=new int[n]; for(int i=0;i<n;i++){ if(i==0) dp[i]=a[i]; else dp[i]=dp[i-1]+a[i]; } return dp; } public int[] postSumOneDimentional(int a[]) { int n = a.length; int dp[] = new int[n]; for (int i = n - 1; i >= 0; i--) { if (i == n - 1) dp[i] = a[i]; else dp[i] = dp[i + 1] + a[i]; } return dp; } public int[][] prefixSum2d(int a[][]){ int n=a.length;int m=a[0].length; int dp[][]=new int[n+1][m+1]; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ dp[i][j]=a[i-1][j-1]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]; } } return dp; } public long pow(long a,long b){ long mod=1000000007; long ans=0; if(b<=0) return 1; if(b%2==0){ ans=pow(a,b/2)%mod; return ((ans%mod)*(ans%mod))%mod; }else{ return ((a%mod)*(ans%mod))%mod; } } } class GraphInteger{ HashMap<Integer,vertex> vtces; class vertex{ HashMap<Integer,Integer> children; public vertex(){ children=new HashMap<>(); } } public GraphInteger(){ vtces=new HashMap<>(); } public void addVertex(int a){ vtces.put(a,new vertex()); } public void addEdge(int a,int b,int cost){ if(!vtces.containsKey(a)){ vtces.put(a,new vertex()); } if(!vtces.containsKey(b)){ vtces.put(b,new vertex()); } vtces.get(a).children.put(b,cost); // vtces.get(b).children.put(a,cost); } public boolean isCyclicDirected(){ boolean isdone[]=new boolean[vtces.size()+1]; boolean check[]=new boolean[vtces.size()+1]; for(int i=1;i<=vtces.size();i++) { if (!isdone[i] && isCyclicDirected(i,isdone, check)) { return true; } } return false; } private boolean isCyclicDirected(int i,boolean isdone[],boolean check[]){ if(check[i]) return true; if(isdone[i]) return false; check[i]=true; isdone[i]=true; Set<Integer> set=vtces.get(i).children.keySet(); for(Integer ii:set){ if(isCyclicDirected(ii,isdone,check)) return true; } check[i]=false; return false; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
fdae74322e28d2994a986235b03c9a60
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
// हर हर महादेव import java.util.*; import java.lang.*; import java.io.*; import java.text.DecimalFormat; public final class Solution { static int inf = Integer.MAX_VALUE; static long mod = 1000000000 + 7; static void ne(Scanner sc, BufferedWriter op) throws Exception { int n=sc.nextInt(); ArrayList<Integer> blue= new ArrayList<>(); ArrayList<Integer> red= new ArrayList<>(); int [] arr= new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } String s=sc.next(); for(int i=0;i<n;i++){ char c=s.charAt(i); if(c=='R'){ red.add(arr[i]); }else{ blue.add(arr[i]); } } Collections.sort(blue); Collections.sort(red); int c1=0; int c2=0; boolean f=true; for(int i=1;i<=n;i++){ if(c1<blue.size()){ if(blue.get(c1)>=i){ c1++; }else if(c2<red.size()){ if(red.get(c2)<=i){ c2++; }else{ f=false; } }else{ f=false; } }else if(c2<red.size()){ if(red.get(c2)<=i){ c2++; }else{ f=false; } }else{ f=false; } } if(f){ op.write("YES\n"); }else{ op.write("NO\n"); } } static long gcd(long a, long b){ if(a==0) return b; return gcd(b%a,a); } static long lcm(long a, long b){ return (a*b)/gcd(a,b); } public static void main(String[] args) throws Exception { BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out)); // Reader sc = new Reader(); Scanner sc= new Scanner(System.in); int t = sc.nextInt(); while (t-->0){ne(sc, op);} // ne(sc,op); op.flush(); } static void print(Object o) { System.out.println(String.valueOf(o)); } static int[] toIntArr(String s){ int[] val= new int[s.length()]; for(int i=0;i<s.length();i++){ val[i]=s.charAt(i)-'a'; } return val; } static void sort(int[] arr){ ArrayList<Integer> list= new ArrayList<>(); for(int i=0;i<arr.length;i++){ list.add(arr[i]); } Collections.sort(list); for(int i=0;i<arr.length;i++){ arr[i]=list.get(i); } } static void sort(long[] arr){ ArrayList<Long> list= new ArrayList<>(); for(int i=0;i<arr.length;i++){ list.add(arr[i]); } Collections.sort(list); for(int i=0;i<arr.length;i++){ arr[i]=list.get(i); } } } // return -1 to put no ahed in array class pair { int xx; int yy; pair(int xx, int yy ) { this.xx = xx; this.yy = yy; } } class sortY implements Comparator<pair> { public int compare(pair p1, pair p2) { if (p1.yy > p2.yy) { return -1; } else if (p1.yy == p2.yy) { if (p1.xx > p2.xx) { return 1; } else if (p1.xx < p2.xx) { return -1; } return 0; } return 1; } } class sortX implements Comparator<pair> { public int compare(pair p1, pair p2) { if (p1.xx > p2.xx) { return 1; } else if (p1.xx == p2.xx) { if (p1.yy > p2.yy) { return 1; } else if (p1.yy < p2.yy) { return -1; } return 0; } return -1; } } class debug { static void print1d(long[] arr) { System.out.println(); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } static void print1d(int[] arr) { System.out.println(); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } static void print1d(boolean[] arr) { System.out.println(); for (int i = 0; i < arr.length; i++) { System.out.println(i + "= " + arr[i] + " "); } } static void print2d(int[][] arr) { System.out.println(); int n = arr.length; int n2 = arr[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < n2; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } static void print2d(long[][] arr) { System.out.println(); int n = arr.length; int n2 = arr[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < n2; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } static void printPair(ArrayList<pair> list) { if(list.size()==0){ System.out.println("empty list"); return; } System.out.println(); for(int i=0;i<list.size();i++){ System.out.println(list.get(i).xx+"-"+list.get(i).yy); } } } class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } class MultiTreeSet<E> { TreeMap<E, Integer> freqTreeMap = new TreeMap<E, Integer>(); int size; public MultiTreeSet() {} public MultiTreeSet(Collection<? extends E> c) { for(E element : c) add(element); } public int size() { return size; } public int disCount(){ return freqTreeMap.size(); } public void add(E element) { Integer freq = freqTreeMap.get(element); if(freq==null) freqTreeMap.put(element, 1); else freqTreeMap.put(element,freq+1); ++size; } public void remove(E element) { Integer freq = freqTreeMap.get(element); if(freq!=null) { if(freq==1) freqTreeMap.remove(element); else freqTreeMap.put(element, freq-1); --size; } } public int get(E element) { Integer freq = freqTreeMap.get(element); if(freq==null) return 0; return freq; } public boolean contains(E element) { return get(element)>0; } public void print(){ for(E k : freqTreeMap.keySet()){ System.out.print(k+"="+freqTreeMap.get(k)+" "); } System.out.println(); } public boolean isEmpty() { return size==0; } public E first() { return freqTreeMap.firstKey(); } public E last() { return freqTreeMap.lastKey(); } public E ceiling(E element) { return freqTreeMap.ceilingKey(element); } public E floor(E element) { return freqTreeMap.floorKey(element); } public E higher(E element) { return freqTreeMap.higherKey(element); } public E lower(E element) { return freqTreeMap.lowerKey(element); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
6f381e2396602e77ab161a9b48a4a0cc
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { static long mod = 1_000_000_007; public static void main(String[] args) { FastScanner f = new FastScanner(); PrintWriter p = new PrintWriter(System.out); int t=f.nextInt(); while(t-->0) { int n=f.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=f.nextInt(); } char c[]=f.next().toCharArray(); ArrayList<Integer> blue=new ArrayList<>(); ArrayList<Integer> red=new ArrayList<>(); for(int i=0;i<n;i++) { if(c[i]=='B') { blue.add(arr[i]); }else { red.add(arr[i]); } } Collections.sort(blue); Collections.sort(red); HashSet<Integer> hashSet=new HashSet<>(); int curr=1; for(int i=0;i<blue.size();i++) { int num=blue.get(i); if(curr<=num) hashSet.add(curr); curr++; } for(int i=0;i<red.size();i++) { int num=red.get(i); if(curr>=num) hashSet.add(curr); curr++; } if(hashSet.size()==n) { p.println("YES"); }else { p.println("NO"); } } p.close(); } //----------------------------------------------------------------------------------------------------------------------------------------------------------------- static int offx[] = { -1, -1, -1, 0, 1, 1, 1, 0 }; static int offy[] = { -1, 0, 1, 1, 1, 0, -1, -1 }; static 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; } } static long add(long x, long y) { x += y; if (x >= mod) return (x % mod); return x; } static long sub(long x, long y) { x -= y; if (x < 0) return (x + mod); return x; } static long mul(long x, long y) { return (x * y) % mod; } static long bin_pow(long x, long p) { if (p == 0) return 1; if ((p & 1) != 0) return mul(x, bin_pow(x, p - 1)); return bin_pow(mul(x, x), p / 2); } static long rev(long x) { return bin_pow(x, mod - 2); } static long div(long x, long y) { return mul(x, rev(y)); } // will work till array size of 65537---fastest sorting time static long[] radixSort(long[] f) { return radixSort(f, f.length); } static long[] radixSort(long[] f, int n) { long[] to = new long[n]; { int[] b = new int[65537]; for (int i = 0; i < n; i++) b[1 + (int) (f[i] & 0xffff)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < n; i++) to[b[(int) (f[i] & 0xffff)]++] = f[i]; long[] d = f; f = to; to = d; } { int[] b = new int[65537]; for (int i = 0; i < n; i++) b[1 + (int) (f[i] >>> 16 & 0xffff)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < n; i++) to[b[(int) (f[i] >>> 16 & 0xffff)]++] = f[i]; long[] d = f; f = to; to = d; } { int[] b = new int[65537]; for (int i = 0; i < n; i++) b[1 + (int) (f[i] >>> 32 & 0xffff)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < n; i++) to[b[(int) (f[i] >>> 32 & 0xffff)]++] = f[i]; long[] d = f; f = to; to = d; } { int[] b = new int[65537]; for (int i = 0; i < n; i++) b[1 + (int) (f[i] >>> 48 & 0xffff)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < n; i++) to[b[(int) (f[i] >>> 48 & 0xffff)]++] = f[i]; long[] d = f; f = to; to = d; } return f; } // Function to sort an array using quick sort algorithm. static void quickSort(long arr[], int low, int high) { if (low < high) { int p = partition(arr, low, high); quickSort(arr, low, p - 1); quickSort(arr, p + 1, high); } } static int partition(long arr[], int low, int high) { long pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } long temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } /// Ceils static long ceil(long x, long m) { long res = x / m; if (x % m != 0) { res++; } return res; } // ------------------------------------------------------------------------------------------------ // makes the prefix sum array static long[] prefixSum(long arr[], int n) { long psum[] = new long[n]; psum[0] = arr[0]; for (int i = 1; i < n; i++) { psum[i] = psum[i - 1] + arr[i]; } return psum; } // ------------------------------------------------------------------------------------------------ // makes the suffix sum array static long[] suffixSum(long arr[], int n) { long ssum[] = new long[n]; ssum[n - 1] = arr[n - 1]; for (int i = n - 2; i >= 0; i--) { ssum[i] = ssum[i + 1] + arr[i]; } return ssum; } //------------------------------------------------------------------------------------------ // BINARY EXPONENTIATION OF A NUMBER MODULO M FASTER METHOD WITHOUT RECURSIVE // OVERHEADS // static long m = (long) (1e9 + 7); static long binPower(long a, long n, long m) { if (n == 0) return 1; long res = 1; while (n > 0) { if ((n & 1) != 0) { res *= a; } a *= a; n >>= 1; } return res; } //------------------------------------------------------------------------------------------- // gcd static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } //------------------------------------------------------------------------------------------ // lcm static long lcm(long a, long b) { return a / gcd(a, b) * b; } // ------------------------------------------------------------------------------------------ // BRIAN KERNINGHAM TO CHECK NUMBER OF SET BITS // O(LOGn) static int setBits(int n) { int count = 0; while (n > 0) { n = n & (n - 1); count++; } return count; } //------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------ // 0 based indexing static boolean KthBitSet(int n, int k) { int mask = 1; mask = mask <<= k; if ((mask & n) != 0) return true; else return false; } //------------------------------------------------------------------------------------------ // EXTENDED EUCLIDEAN THEOREM // TO REPRESENT GCD IN TERMS OF A AND B // gcd(a,b) = a.x + b.y where x and y are integers static long x = -1; static long y = -1; static long gcdxy(long a, long b) { if (b == 0) { x = 1; y = 0; return a; } else { long d = gcdxy(b, a % b); long x1 = y; long y1 = x - (a / b) * y; x = x1; y = y1; return d; } } //------------------------------------------------------------------------------------------------- }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
db54dcec58c202e74d23c5c4c861cc17
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
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; outer :while(t-->0) { int n = sc.nextInt(); int arr[] = sc.readArray(n); char brr[] = sc.next().toCharArray(); ArrayList<Integer> red = new ArrayList<>(); ArrayList<Integer> blue = new ArrayList<>(); HashSet<Integer> set = new HashSet<>(); HashSet<Integer> red_set = new HashSet<>(); HashSet<Integer> blue_set = new HashSet<>(); for(int i=0;i<n;i++) { if(brr[i] == 'R') { red.add(arr[i]); red_set.add(arr[i]); } else { blue.add(arr[i]); blue_set.add(arr[i]); } set.add(arr[i]); } Collections.sort(red); Collections.sort(blue); if(blue.size()==0) { if(red.size() > 1 && red_set.size()==1) { if(red.get(0) > 1) { out.println("NO"); continue; } } } if(red.size() == 0) { if(blue.size() > 1 && blue_set.size()==1) { if(blue.get(0) < 1) { out.println("NO"); continue; } } } boolean flag = true; if(blue.size() > 0) { if(blue.get(0) >= 1) { blue.set(0 , 1); } else { out.println("NO"); continue; } } for(int i=1;i<blue.size();i++) { int ele = blue.get(i); int prev = blue.get(i - 1); if(ele != prev) { blue.set(i , (prev + 1)); } else { flag = false; break; } } if(flag == false) { out.println("NO"); } else { if(red.size() > 0) { boolean z = true; int key = 0; if(blue.size() > 0) { key = blue.get(blue.size()-1); } if(red.get(0) >= (key + 2)) { out.println("NO"); continue outer; } else { red.set(0 , (key + 1)); } for(int i=1;i<red.size();i++) { int ele = red.get(i); int prev = red.get(i - 1); if(ele >= (prev + 2)) { z = false; break; } else { red.set(i , (prev + 1)); } } if(z == false) { out.println("NO"); } else { out.println("YES"); } } else { out.println("YES"); } } } out.flush(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } static void reverse_sorted(int[] arr) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<arr.length;i++) { list.add(arr[i]); } Collections.sort(list , Collections.reverseOrder()); for(int i=0;i<arr.length;i++) { arr[i] = list.get(i); } } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value int l=-1,r=list.size(); while(l+1<r) { int m=(l+r)>>>1; if(list.get(m)<=x) l=m; else r=m; } return l+1; } public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<String, Integer> > list = new LinkedList<Map.Entry<String, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>(); for (Map.Entry<String, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static class Queue_Pair implements Comparable<Queue_Pair> { int first , second; public Queue_Pair(int first, int second) { this.first=first; this.second=second; } public int compareTo(Queue_Pair o) { return Integer.compare(o.first, first); } } static void leftRotate(char arr[], int d, int n) { for (int i = 0; i < d; i++) leftRotatebyOne(arr, n); } static void leftRotatebyOne(char arr[], int n) { int i; char temp = arr[0]; for (i = 0; i < n - 1; i++) arr[i] = arr[i + 1]; arr[n-1] = temp; } static void rightRotate(char arr[], int d, int n) { for (int i = 0; i < d; i++) rightRotatebyOne(arr, n); } static void rightRotatebyOne(char arr[], int n) { int i; char temp = arr[n - 1]; for (i = n - 1; i > 0; i--) arr[i] = arr[i - 1]; arr[0] = temp; } static boolean isPalindrome(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } static boolean palindrome_array(char arr[], int n) { // Initialise flag to zero. int flag = 0; // Loop till array size n/2. for (int i = 0; i <= n / 2 && n != 0; i++) { // Check if first and last element are different // Then set flag to 1. if (arr[i] != arr[n - i - 1]) { flag = 1; break; } } // If flag is set then print Not Palindrome // else print Palindrome. if (flag == 1) return false; else return true; } static boolean allElementsEqual(long[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]==arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } static boolean allElementsDistinct(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]!=arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } public static void reverse(int[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily int temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } public static void reverse_Long(long[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily long temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static boolean isReverseSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { return false; } } return true; } static int[] rearrangeEvenAndOdd(int arr[], int n) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); int[] array = list.stream().mapToInt(i->i).toArray(); return array; } static long[] rearrangeEvenAndOddLong(long arr[], int n) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); long[] array = list.stream().mapToLong(i->i).toArray(); return array; } static boolean isPrime(long n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (long i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static long getSum(long n) { long sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcdLong(long a, long b) { if (b == 0) return a; return gcdLong(b, a % b); } static void swap(int i, int j) { int temp = i; i = j; j = temp; } static int countDigit(int n) { return (int)Math.floor(Math.log10(n) + 1); } } class Pair { int first , second; Pair(int first , int second) { this.first = first; this.second = second; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
9121c9b7238b0e9696e62c38ffde154e
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; public class Main{ public static class SortByChar implements Comparator<Custom1> { public int compare(Custom1 a,Custom1 b) { if(a.x==b.x) { if(a.val>b.val) { return 1; } else if(a.val<b.val) { return -1; } return 0; } else if(a.x>b.x) { return 1; } else{ return -1; } } } public static class Custom1 { int val; char x; Custom1(int val,char x) { this.val=val; this.x=x; } } public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); while(n-- >0) { int size=sc.nextInt(); int[]a=new int[size]; ArrayList<Custom1> arr=new ArrayList<Custom1>(); for(int i=0;i<size;i++) { a[i]=sc.nextInt(); } String s=sc.next(); for(int i=0;i<size;i++) { arr.add(new Custom1(a[i],s.charAt(i))); } Collections.sort(arr,new SortByChar()); int i=1; int flag=0; while(i<=size && arr.get(i-1).x=='B') { if(arr.get(i-1).val>=i) { i++; } else{ flag=1; System.out.println("NO"); break; } } while(i<=size && arr.get(i-1).x=='R' && flag==0) { if(arr.get(i-1).val<=i) { i++; } else{ flag=1; System.out.println("NO"); break; } } if(flag==0) { System.out.println("YES"); } } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
070050d65f0dec754a8cdf7ccbbf4959
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader obj = new FastReader(); PrintWriter out = new PrintWriter(System.out); int l = obj.nextInt(); while (l-- != 0) { int n = obj.nextInt(); int[] num = new int[n]; for (int i = 0; i < n; i++) num[i] = obj.nextInt(); Vector<Integer> red = new Vector<>(); Vector<Integer> blue = new Vector<>(); String s = obj.next(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'R') red.add(num[i]); else blue.add(num[i]); } Collections.sort(blue); Collections.sort(red); int c = 1, f = 0; for (int i = 0; i < blue.size(); i++) { if (blue.get(i) < c) { f = 1; break; } c++; } for (int i = 0; i < red.size(); i++) { if (red.get(i) > c) { f = 1; break; } c++; } if (f == 0) out.println("YES"); else out.println("NO"); } out.flush(); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
f87dcb709e0bbff6158834089d5d5342
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import javax.swing.plaf.basic.BasicArrowButton; 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 { 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 T=Integer.parseInt(br.readLine()); StringBuilder stringBuilder=new StringBuilder(); while(T-->0) { int n=Integer.parseInt(br.readLine()); long[] arr=new long[n]; StringTokenizer stringTokenizer=new StringTokenizer(br.readLine()); for (int i=0;i<n;i++) arr[i]=Long.parseLong(stringTokenizer.nextToken()); String s=br.readLine().trim(); ArrayList<Long> reds=new ArrayList<>(); ArrayList<Long> blues=new ArrayList<>(); for (int i=0;i<n;i++){ if (s.charAt(i)=='B') blues.add(arr[i]); else reds.add(arr[i]); } Collections.sort(blues); Collections.sort(reds); boolean isPossible = true; int j = 1; for (int i = 0; i < blues.size(); i++) { if (blues.get(i) >= j) j++; else isPossible = false; } for (int i = 0; i < reds.size(); i++) { if (reds.get(i) <= j) j++; else isPossible = false; } // if (isPossible==true) // stringBuilder.append("YES\n"); // else // stringBuilder.append("NO\n"); System.out.println(isPossible ? "YES" :"NO"); } // System.out.println(stringBuilder); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
a8bbe88420de4c410a72bf1402165f18
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; public class BlueRedPermutation { 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 nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextArray(int n) { int[] a = new int[n]; for (int i=0; i<n; i++) { a[i] = this.nextInt(); } return a; } long[] nextArrayLong(int n) { long[] a = new long[n]; for (int i=0; i<n; i++) { a[i] = this.nextInt(); } return a; } } public static void solve(int n, int[] a, String s) { ArrayList<Integer> blueList = new ArrayList<Integer>(); ArrayList<Integer> redList = new ArrayList<Integer>(); for (int i=0; i<n; i++) { if (s.charAt(i) == 'B') { blueList.add(a[i]); } else { redList.add(a[i]); } } blueList.sort((c, d) -> c-d); redList.sort((c, d) -> c-d); int currentVal = 0; for (int val : blueList) { if (val >= currentVal+1) { currentVal++; } } for (int val : redList) { if (val <= currentVal+1) { currentVal++; } } if (currentVal == n) { System.out.println("YES"); } else { System.out.println("NO"); } } public static void main(String[] args) { FastReader in = new FastReader(); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int[] a = in.nextArray(n); String s = in.next(); solve(n, a, s); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
42042e3d0c4e55296a423ec7d5614468
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
//package com.company; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class D { private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int t = scanner.nextInt(); while (t-- > 0) { int n = scanner.nextInt(); int[] ar = new int[n]; for (int i = 0; i < n; i++) { ar[i] = scanner.nextInt(); } String s = scanner.next(); List<Integer> blue = new ArrayList<Integer>(); List<Integer> red = new ArrayList<Integer>(); for (int i = 0; i < s.length(); i++) { if(s.charAt(i) == 'B') { blue.add(ar[i]); } else { red.add(ar[i]); } } Collections.sort(blue); red.sort(Collections.reverseOrder()); boolean ok = true; for (int i = 0; i < blue.size(); i++) { if(blue.get(i) <= i) { ok = false; break; } } for (int i = 0; i <red.size() ; i++) { if(red.get(i) > n-i) { ok = false; break; } } System.out.println(ok ? "YES" : "NO"); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
5fc365ef62af7e6107551c01e85ec119
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
//package Codeforces; import java.util.*; import java.io.*; public class template{ 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 helper{ //reverse 1-d Array public 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; } } //gcd of two numbers public static int gcd(int a,int b) { if(b == 0) { return a; } return gcd(b,a%b); } //calculate ncr public static int ncr(int n,int r) { int num = n; int deno = r; int a1 = 1; int a2 = 1; for(int i=0;i<r;i++) { a1 *= num; a2 *= deno; num--; deno--; } int ans = a1/a2; return ans; } //isPalindrome public static boolean isPalindrome(String substring){ char ch[] = substring.toCharArray(); int i = 0; int j = ch.length-1; while(i<j){ char temp = ch[i]; ch[i] = ch[j]; ch[j] = temp; i++; j--; } String rev = String.valueOf(ch); if(rev.equals(substring)){ return true; } else{ return false; } } //isPowerOfTwo public static boolean isPowerOfTwo(long x) { /* First x in the below expression is for the case when x is 0 */ return x != 0 && ((x & (x - 1)) == 0); } //nextPowerOfTwo public static long nextPowerOfTwo(long n) { n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } } public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); helper help = new helper(); long test = sc.nextLong(); while(test-->0) { int n = sc.nextInt(); int arr[] = sc.readArray(n); String s = sc.next(); PriorityQueue<lavesh> pq = new PriorityQueue<>(Comparator.comparing(lavesh::getColor).thenComparing(lavesh::getValue)); for(int i=0;i<arr.length;i++) { int value = arr[i]; String ss = ""; ss += s.charAt(i); pq.add(new lavesh(value,ss)); } int index= 1; boolean flag = true; while(pq.size() != 0) { lavesh e = pq.peek(); if(e.value == index) { } else if(e.value < index && e.color.equals("R")) { } else if(e.value > index && e.color.equals("B")) { } else { flag = false; break; } index++; pq.poll(); } if(flag) { out.println("YES"); } else { out.println("NO"); } } out.flush(); } } class lavesh { int value; String color; lavesh(int value,String color){ this.value = value; this.color = color; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } } class pair { int value; int index; pair(int value,int index){ this.value = value; this.index = index; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
b53892542038768cb4204befca8bd6a5
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.io.*; public class Main { static long mod = 1000000007; static long max ; static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); int t = sc.nextInt(); while( t-- > 0) { int n = sc.nextInt(); int arr[] = new int[n]; ArrayList<Integer> red = new ArrayList<>(); ArrayList<Integer> blue = new ArrayList<>(); for( int i= 0 ;i< n ;i++) { arr[i] = sc.nextInt(); } char a[]= sc.next().toCharArray(); for( int i= 0 ;i < n ;i++) { if( a[i] == 'R') { red.add(arr[i]); } else { blue.add(arr[i]); } } Collections.sort(red); Collections.sort(blue); Queue<Integer> r = new LinkedList<>(); Queue<Integer> b = new LinkedList<>(); for( int i = 0 ;i < red.size(); i++) { r.add(red.get(i)); } for( int i = 0 ;i < blue.size(); i++) { b.add(blue.get(i)); } int i = 1; boolean check = true; while( i <= n && check) { if( b.isEmpty() && r.isEmpty()) { check = false; continue; } if( b.isEmpty()) { if( r.peek() > i) { check = false; } else { r.poll(); } } else if( r.isEmpty() ) { while( !b.isEmpty() && b.peek() < i) { b.poll(); } if(!b.isEmpty() && b.peek() >= i) { b.poll(); } else { check = false; } } else { while(!b.isEmpty() && b.peek() < i) { b.poll(); } if( b.isEmpty() && !r.isEmpty()) { if( r.peek() > i) { check = false; } else { r.poll(); } } else { if( b.peek() >= i && r.peek() <= i ) { if( b.peek() - i > n - r.peek()) { r.poll(); } else { b.poll(); } } else if( b.peek() >= i) { b.poll(); } else if( r.peek() <= i) { r.poll(); } else { check = false; } } } i++; } out.println(check?"YES":"NO"); } out.flush(); } public static boolean ifpowof2(long n ) { return ((n&(n-1)) == 0); } public static int[] nextLargerElement(int[] arr, int n) { Stack<Integer> stack = new Stack<>(); int rtrn[] = new int[n]; rtrn[n-1] = -1; stack.push( n-1); for( int i = n-2 ;i >= 0 ; i--){ int temp = arr[i]; int lol = -1; while( !stack.isEmpty() && arr[stack.peek()] <= temp){ if(arr[stack.peek()] == temp ) { lol = stack.peek(); } stack.pop(); } if( stack.isEmpty()){ if( lol != -1) { rtrn[i] = lol; } else { rtrn[i] = -1; } } else{ rtrn[i] = stack.peek(); } stack.push( i); } return rtrn; } @SuppressWarnings("unused") private static void mySort(int[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); int loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static long rightmostsetbit(long n) { return n&-n; } static long leftmostsetbit(long n) { long k = (long)(Math.log(n) / Math.log(2)); return 1 << k; } static HashMap<Long,Long> primefactor( long n){ HashMap<Long ,Long> hm = new HashMap<>(); long temp = 0; while( n%2 == 0) { temp++; n/=2; } if( temp!= 0) { hm.put( 2L, temp); } long c = (long)Math.sqrt(n); for( long i = 3 ; i <= c ; i+=2) { temp = 0; while( n% i == 0) { temp++; n/=i; } if( temp!= 0) { hm.put( i, temp); } } if( n!= 1) { hm.put( n , 1L); } return hm; } @SuppressWarnings("unused") private static ArrayList<Integer> allfactors(int abs) { HashMap<Integer,Integer> hm = new HashMap<>(); ArrayList<Integer> rtrn = new ArrayList<>(); for( int i = 2 ;i*i <= abs; i++) { if( abs% i == 0) { hm.put( i , 0); hm.put(abs/i, 0); } } for( int x : hm.keySet()) { rtrn.add(x); } if( abs != 0) { rtrn.add(abs); } return rtrn; } 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
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
28db21168da008b24ed897e30ee44780
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import javax.swing.text.Segment; import java.io.*; import java.math.*; import java.sql.Array; public class Main { 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; } } static long mod = (long)(1e9 + 7); static void sort(long[] arr ) { ArrayList<Long> al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr ) { ArrayList<Integer> al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i); } static void rvrs(int[] arr) { int i =0 , j = arr.length-1; while(i>=j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static void rvrs(long[] arr) { int i =0 , j = arr.length-1; while(i>=j) { long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static long mod_mul(long a , long b ,long mod) { return ((a%mod)*(b%mod))%mod; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static long modInverse(long a, long m) { long g = gcd(a, m); return power(a, m - 2, m); } static long lcm(long a , long b) { return (a*b)/gcd(a, b); } static int lcm(int a , int b) { return (int)((a*b)/gcd(a, b)); } static long power(long x, long y, long m){ if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (int)((p * (long)p) % m); if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); } static class Combinations{ private long[] z; private long[] z1; private long[] z2; public Combinations(long N , long mod) { z = new long[(int)N+1]; z1 = new long[(int)N+1]; z[0] = 1; for(int i =1 ; i<=N ; i++) z[i] = (z[i-1]*i)%mod; z2 = new long[(int)N+1]; z2[0] = z2[1] = 1; for (int i = 2; i <= N; i++) z2[i] = z2[(int)(mod % i)] * (mod - mod / i) % mod; z1[0] = z1[1] = 1; for (int i = 2; i <= N; i++) z1[i] = (z2[i] * z1[i - 1]) % mod; } long fac(long n) { return z[(int)n]; } long invrsNum(long n) { return z2[(int)n]; } long invrsFac(long n) { return invrsFac((int)n); } long ncr(long N, long R, long mod) { if(R<0 || R>N ) return 0; long ans = ((z[(int)N] * z1[(int)R]) % mod * z1[(int)(N - R)]) % mod; return ans; } } static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } void makeSet() { for (int i = 0; i < n; i++) { parent[i] = i; } } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } } } static int max(int a , int b) { if(b>a) return b; return a; } static long max(long a , long b) { if(b>a) return b; return a; } static int min(int a , int b) { if(b<a) return b; return a; } static long min(long a , long b) { if(b<a) return b; return a; } static int[] KMP(String str) { int n = str.length(); int[] kmp = new int[n]; for(int i = 1 ; i<n ; i++) { int j = kmp[i-1]; while(j>0 && str.charAt(i) != str.charAt(j)) { j = kmp[j-1]; } if(str.charAt(i) == str.charAt(j)) j++; kmp[i] = j; } return kmp; } /************************************************ Query **************************************************************************************/ /***************************************** Sparse Table ********************************************************/ static class SparseTable{ private long[][] st; SparseTable(long[] arr){ int n = arr.length; st = new long[n][25]; log = new int[n+2]; build_log(n+1); build(arr); } private void build(long[] arr) { int n = arr.length; for(int i = n-1 ; i>=0 ; i--) { for(int j = 0 ; j<25 ; j++) { int r = i + (1<<j)-1; if(r>=n) break; if(j == 0 ) st[i][j] = arr[i]; else st[i][j] = Math.max(st[i][j-1] , st[ i + ( 1 << (j-1) ) ][ j-1 ] ); } } } public long gcd(long a ,long b) { if(a == 0) return b; return gcd(b%a , a); } public long query(int l ,int r) { int w = r-l+1; int power = log[w]; return Math.max(st[l][power],st[r - (1<<power) + 1][power]); } private int[] log; void build_log(int n) { log[1] = 0; for(int i = 2 ; i<=n ; i++) { log[i] = 1 + log[i/2]; } } } /******************************************************** Segement Tree *****************************************************/ /** static class SegmentTree{ long[] tree; long[] arr; int n; SegmentTree(long[] arr){ this.n = arr.length; tree = new long[4*n+1]; this.arr = arr; buildTree(0, n-1, 1); } void buildTree(int s ,int e ,int index ) { if(s == e) { tree[index] = arr[s]; return; } int mid = (s+e)/2; buildTree( s, mid, 2*index); buildTree( mid+1, e, 2*index+1); tree[index] = Math.min(tree[2*index] , tree[2*index+1]); } long query(int si ,int ei) { return query(0 ,n-1 , si ,ei , 1 ); } private long query( int ss ,int se ,int qs , int qe,int index) { if(ss>=qs && se<=qe) return tree[index]; if(qe<ss || se<qs) return (long)(1e17); int mid = (ss + se)/2; long left = query( ss , mid , qs ,qe , 2*index); long right= query(mid + 1 , se , qs ,qe , 2*index+1); return Math.min(left, right); } public void update(int index , int val) { arr[index] = val; for(long e:arr) System.out.print(e+" "); update(index , 0 , n-1 , 1); } private void update(int id ,int si , int ei , int index) { if(id < si || id>ei) return; if(si == ei ) { tree[index] = arr[id]; return; } if(si > ei) return; int mid = (ei + si)/2; update( id, si, mid , 2*index); update( id , mid+1, ei , 2*index+1); tree[index] = Math.min(tree[2*index] ,tree[2*index+1]); } } */ /* ***************************************************************************************************************************************************/ static Scanner sc = new Scanner(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) throws IOException { int tc = 1; tc = sc.nextInt(); for(int i = 1 ; i<=tc ; i++) { // sb.append("Case #" + i + ": " ); // During KickStart && HackerCup TEST_CASE(); } System.out.println(sb); } static void TEST_CASE() { // B -- // R -- int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0 ; i<n ;i++) arr[i] = sc.nextInt(); char[] col = sc.next().toCharArray(); ArrayList<Integer> red = new ArrayList<>(); ArrayList<Integer> blue = new ArrayList<>(); for(int i = 0 ; i<n ; i++) { if(col[i] == 'R') red.add(arr[i]); else blue.add(arr[i]); } Collections.sort(red); Collections.sort(blue); int r = 0 , b = 0; int n1 = red.size() , n2 = blue.size(); int i = 1; // System.out.println(red); // System.out.println(blue); while(r<n1 || b< n2) { if(r<n1 && b<n2) { if(red.get(r) > i && blue.get(b) < i ) { sb.append("NO\n"); return; }else if(blue.get(b) >= i ) { b++; }else { r++; } }else if(r<n1) { if(red.get(r)<=i) { r++; }else { sb.append("NO\n"); return; } }else { if(blue.get(b)>=i) { b++; }else { sb.append("NO\n"); return; } } // System.out.println(r+" "+b+" "+i); i++; } sb.append("YES\n"); } } /*******************************************************************************************************************************************************/ /** */
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
f48c2d5fb1cb80c2a25b50270403e5d2
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class PROBD { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //BufferedReader br = new BufferedReader(new FileReader("src/input.txt")); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); for (int i = 0; i < t; i++) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); ArrayList<Integer> a = new ArrayList<Integer>(); for (int j = 0; j < n; j++) { a.add(Integer.parseInt(st.nextToken())); } ArrayList<Integer> r = new ArrayList<Integer>(); ArrayList<Integer> b = new ArrayList<Integer>(); st = new StringTokenizer(br.readLine()); String c = st.nextToken(); for (int j = 0; j < a.size(); j++) { if (Character.toString(c.charAt(j)).equals("R")) { r.add(a.get(j)); } else { b.add(a.get(j)); } } Collections.sort(r); Collections.sort(b); //System.out.println(r+" "+b); boolean v = true; for (int j = n; j > 0; j--) { if (r.size() > 0 && r.get(r.size()-1) <= j) { r.remove(r.size()-1); } else if (b.size() > 0 && b.get(b.size()-1) >= j) { b.remove(b.size()-1); } else { v = false; break; } } if (v) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
9f19e66683a937e76de14b18e496f3ca
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
//package currentContest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Random; import java.util.StringTokenizer; public class P4 { static int N = 1000001; // Array to store inverse of 1 to N static long[] factorialNumInverse = new long[N + 1]; // Array to precompute inverse of 1! to N! static long[] naturalNumInverse = new long[N + 1]; // Array to store factorial of first N numbers static long[] fact = new long[N + 1]; // Function to precompute inverse of numbers public static void InverseofNumber(int p) { naturalNumInverse[0] = naturalNumInverse[1] = 1; for (int i = 2; i <= N; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long) (p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse[0] = factorialNumInverse[1] = 1; // Precompute inverse of natural numbers for (int i = 2; i <= N; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to N public static void factorial(int p) { fact[0] = 1; // Precompute factorials for (int i = 1; i <= N; i++) { fact[i] = (fact[i - 1] * (long) i) % p; } } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R, int p) { // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p; return ans; } public static void main(String[] args) { // TODO Auto-generated method stub FastReader sc = new FastReader(); int t=sc.nextInt(); StringBuilder st=new StringBuilder(); while(t--!=0) { int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } String s=sc.nextLine(); ArrayList<Integer> ese=new ArrayList<Integer>(); ArrayList<Integer> kese=new ArrayList<Integer>(); int ca=0; int cb=0; for(int i=0;i<n;i++) { if(s.charAt(i)=='B') { ese.add(a[i]); cb++; }else { kese.add(a[i]); ca++; } } int[] ese1=new int[ese.size()]; for(int i=0;i<ese.size();i++) { ese1[i]=ese.get(i); } sort(ese1); int current =1; int flag=0; for(int x:ese1) { if(x<current) { flag=1; break; } current++; } if(flag==0) { int[] kese1=new int[kese.size()]; for(int i=0;i<kese.size();i++) { kese1[i]=kese.get(i); } sort(kese1); flag=0; for(int x:kese1) { if(x>current) { flag=1; break; } current++; } if(flag==0) st.append("YES\n"); else st.append("NO\n"); }else { st.append("NO\n"); } } System.out.println(st); } public static long gcd1(long a, long b) { if (a == 0) { return b; } return gcd1(b % a, a); } static FastReader sc = new FastReader(); public static void solvegraph() { int n = sc.nextInt(); int edge[][] = new int[n - 1][2]; for (int i = 0; i < n - 1; i++) { edge[i][0] = sc.nextInt() - 1; edge[i][1] = sc.nextInt() - 1; } ArrayList<ArrayList<Integer>> ad = new ArrayList<>(); for (int i = 0; i < n; i++) { ad.add(new ArrayList<Integer>()); } for (int i = 0; i < n - 1; i++) { ad.get(edge[i][0]).add(edge[i][1]); ad.get(edge[i][1]).add(edge[i][0]); } int parent[] = new int[n]; Arrays.fill(parent, -1); parent[0] = n; ArrayDeque<Integer> queue = new ArrayDeque<>(); queue.add(0); int child[] = new int[n]; Arrays.fill(child, 0); ArrayList<Integer> lv = new ArrayList<Integer>(); while (!queue.isEmpty()) { int toget = queue.getFirst(); queue.removeFirst(); child[toget] = ad.get(toget).size() - 1; for (int i = 0; i < ad.get(toget).size(); i++) { if (parent[ad.get(toget).get(i)] == -1) { parent[ad.get(toget).get(i)] = toget; queue.addLast(ad.get(toget).get(i)); } } lv.add(toget); } child[0]++; } static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[] = new Character[s.length()]; for (int i = 0; i < s.length(); i++) { ch[i] = s.charAt(i); } Arrays.sort(ch); StringBuffer st = new StringBuffer(""); for (int i = 0; i < s.length(); i++) { st.append(ch[i]); } return st.toString(); } public static long gcd(long a, long b) { if (a == 0) { return b; } return gcd1(b % a, a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
d3bcd81d46b0290853f3756d4938ce82
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
//package currentContest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Random; import java.util.StringTokenizer; public class P4 { static int N = 1000001; // Array to store inverse of 1 to N static long[] factorialNumInverse = new long[N + 1]; // Array to precompute inverse of 1! to N! static long[] naturalNumInverse = new long[N + 1]; // Array to store factorial of first N numbers static long[] fact = new long[N + 1]; // Function to precompute inverse of numbers public static void InverseofNumber(int p) { naturalNumInverse[0] = naturalNumInverse[1] = 1; for (int i = 2; i <= N; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long) (p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse[0] = factorialNumInverse[1] = 1; // Precompute inverse of natural numbers for (int i = 2; i <= N; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to N public static void factorial(int p) { fact[0] = 1; // Precompute factorials for (int i = 1; i <= N; i++) { fact[i] = (fact[i - 1] * (long) i) % p; } } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R, int p) { // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p; return ans; } public static void main(String[] args) { // TODO Auto-generated method stub FastReader sc = new FastReader(); int t=sc.nextInt(); StringBuilder st=new StringBuilder(); while(t--!=0) { int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } String s=sc.nextLine(); ArrayList<Integer> ese=new ArrayList<Integer>(); ArrayList<Integer> kese=new ArrayList<Integer>(); int ca=0; int cb=0; for(int i=0;i<n;i++) { if(s.charAt(i)=='B') { ese.add(a[i]); cb++; }else { kese.add(a[i]); ca++; } } Collections.sort(ese); int current =1; int flag=0; for(int x:ese) { if(x<current) { flag=1; break; } current++; } if(flag==0) { Collections.sort(kese); flag=0; for(int x:kese) { if(x>current) { flag=1; break; } current++; } if(flag==0) st.append("YES\n"); else st.append("NO\n"); }else { st.append("NO\n"); } } System.out.println(st); } public static long gcd1(long a, long b) { if (a == 0) { return b; } return gcd1(b % a, a); } static FastReader sc = new FastReader(); public static void solvegraph() { int n = sc.nextInt(); int edge[][] = new int[n - 1][2]; for (int i = 0; i < n - 1; i++) { edge[i][0] = sc.nextInt() - 1; edge[i][1] = sc.nextInt() - 1; } ArrayList<ArrayList<Integer>> ad = new ArrayList<>(); for (int i = 0; i < n; i++) { ad.add(new ArrayList<Integer>()); } for (int i = 0; i < n - 1; i++) { ad.get(edge[i][0]).add(edge[i][1]); ad.get(edge[i][1]).add(edge[i][0]); } int parent[] = new int[n]; Arrays.fill(parent, -1); parent[0] = n; ArrayDeque<Integer> queue = new ArrayDeque<>(); queue.add(0); int child[] = new int[n]; Arrays.fill(child, 0); ArrayList<Integer> lv = new ArrayList<Integer>(); while (!queue.isEmpty()) { int toget = queue.getFirst(); queue.removeFirst(); child[toget] = ad.get(toget).size() - 1; for (int i = 0; i < ad.get(toget).size(); i++) { if (parent[ad.get(toget).get(i)] == -1) { parent[ad.get(toget).get(i)] = toget; queue.addLast(ad.get(toget).get(i)); } } lv.add(toget); } child[0]++; } static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[] = new Character[s.length()]; for (int i = 0; i < s.length(); i++) { ch[i] = s.charAt(i); } Arrays.sort(ch); StringBuffer st = new StringBuffer(""); for (int i = 0; i < s.length(); i++) { st.append(ch[i]); } return st.toString(); } public static long gcd(long a, long b) { if (a == 0) { return b; } return gcd1(b % a, a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
272a40a6e19cf3dd1606a16a015f84ff
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; import java.util.Map.Entry; public class AMain { public static void main(String[] args) { QuickReader in = new QuickReader(System.in); try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));) { new AMain().solve(in, out); } } public void solve(QuickReader in, PrintWriter out) { int t = in.nextInt(); while(t-->0) { int n = in.nextInt(); int[] a= in.nextInts(n); char[] clr = in.next().toCharArray(); ArrayList<ArrayList<Integer> > rgh = new ArrayList<>(); for(int i=0;i<n;i++) rgh.add(new ArrayList<>()); boolean ok = true; for(int i=0;i<n;i++) if(clr[i] == 'B') { if(a[i]<1) ok = false; else rgh.get(0).add(a[i]-1); } else { if(a[i]>n) ok = false; else rgh.get(Math.max(0, a[i]-1)).add(n-1); } TreeMap<Integer, Integer> q = new TreeMap<>(); for(int i=0;i<n;i++) { for(int j: rgh.get(i)) q.put(j, q.getOrDefault(j, 0)+1); if(q.isEmpty() || q.firstKey() < i) { ok = false; break; } Entry<Integer, Integer> cur = q.firstEntry(); if(cur.getValue() == 1) q.remove(cur.getKey()); else q.put(cur.getKey(), cur.getValue()-1); } out.println(ok? "YES":"NO"); out.flush(); } } } class QuickReader { BufferedReader in; StringTokenizer token; public QuickReader(InputStream ins) { in=new BufferedReader(new InputStreamReader(ins)); token=new StringTokenizer(""); } public boolean hasNext() { while (!token.hasMoreTokens()) { try { String s = in.readLine(); if (s == null) return false; token = new StringTokenizer(s); } catch (IOException e) { throw new InputMismatchException(); } } return true; } public String next() { hasNext(); return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextInts(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } public long nextLong() { return Long.parseLong(next()); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
ee47879815f06251d60a5ee559cb0904
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class Main { void solveA() { String s = scanner.nextLine(); String t = scanner.nextLine(); int[] pos = new int[26]; for(int i = 0; i < 26; i++){ pos[s.charAt(i) - 'a'] = i; } long res = 0; for(int i = 1; i < t.length(); i++){ int a = pos[t.charAt(i-1)-'a']; int b = pos[t.charAt(i)-'a']; res += Math.abs(a-b); } out.println(res); } void solveB(){ long x = scanner.nextLong(), n = scanner.nextLong(); if(n % 4 == 0){ out.println(x); } else{ long mod = n % 4, res = x; for(long i = 1; i <= mod; i++){ long num = n / 4 * 4 + i; if(i == 1){ res += (x % 2 == 0? -1L:1L) * num; } else{ res += (x % 2 == 0? 1L:-1L) * num; } } out.println(res); } } void solveC(){ int n = scanner.nextInt(); long[] arr = scanner.nextLongArray(n, 0); if(n == 1){ out.println(arr[0]); return; } PriorityQueue<Long> pq = new PriorityQueue<>(); long offset = 0, res = Long.MIN_VALUE; for(long val: arr){ pq.offer(val); } while(!pq.isEmpty()){ long curr = pq.poll(); res = Math.max(res, curr - offset); offset += curr - offset; } out.println(res); } void solveD(){ int n = scanner.nextInt(); int[] arr = scanner.nextIntArray(n, 0); String str = scanner.nextLine(); int[] count = new int[n + 3]; List<int[]> intervals = new ArrayList<>(); for(int i = 0; i < n; i++){ char ch = str.charAt(i); if(ch == 'B' && arr[i] < 1){ out.println("NO"); return; } if(ch == 'R' && arr[i] > n){ out.println("NO"); return; } int l, r; if(ch == 'B'){ l = 1; r = Math.min(arr[i], n); } else{ l = Math.max(1, arr[i]); r = n; } intervals.add(new int[]{l, r}); count[l]++; count[r+1]--; //out.println(l + ", " + r); } // for(int i = 1; i < count.length; i++){ // count[i] += count[i-1]; // } // for(int i = 1; i <= n; i++){ // if(count[i] <= 0){ // out.println("NO"); // return; // } // } Collections.sort(intervals, new Comparator<int[]>() { @Override public int compare(int[] a, int[] b) { if(a[0] == b[0]){ return a[1] - b[1]; } return a[0] - b[0]; } }); for(int i = 1; i <= n; i++){ int[] interval = intervals.get(i-1); if(i < interval[0] || i > interval[1]){ out.println("NO"); return; } } out.println("YES"); } private static final boolean memory = false; private static final boolean singleTest = false; // read inputs and call solvers void run() { int numOfTests = singleTest? 1: scanner.nextInt(); for(int testIdx = 1; testIdx <= numOfTests; testIdx++){ solveD(); } out.flush(); out.close(); } // ----- runner templates ----- // public static void main(String[] args) { if(memory) { new Thread(null, new Runnable() { @Override public void run() { new Main().run(); } }, "go", 1 << 26).start(); } else{ new Main().run(); } } //------ input and output ------// public static MyScanner scanner = new MyScanner(); public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextIntArray(int n, int base){ int[] arr = new int[n + base]; for(int i = base; i < n + base; i++){ arr[i] = scanner.nextInt(); } return arr; } long[] nextLongArray(int n, int base){ long[] arr = new long[n + base]; for(int i = base; i < n + base; i++){ arr[i] = scanner.nextLong(); } return arr; } int[][] nextIntGrid(int n, int m, int base){ int[][] grid = new int[n + base][m + base]; for(int i = base; i < n + base; i++){ for(int j = base; j < m + base; j++){ grid[i][j] = scanner.nextInt(); } } return grid; } Map<Integer, List<Integer>> nextSimpleGraph(int numOfVertices, int numOfEdges, boolean directed){ Map<Integer, List<Integer>> g = new HashMap<>(); for(int i = 1; i <= numOfVertices; i++){ g.put(i, new ArrayList<>()); } for(int i = 1; i <= numOfEdges; i++){ int u = scanner.nextInt(); int v = scanner.nextInt(); g.get(u).add(v); if(!directed){g.get(v).add(u);} } return g; } } //------ debug and print functions ------// void debug(Object...o){ out.println(Arrays.deepToString(o)); } void print(Object...o) { for(int i = 0; i < o.length; i++){ out.print(Arrays.toString(o)); out.print(i == o.length-1? '\n':' '); } } void println(Object...o){ for(int i = 0; i < o.length; i++){ out.println(Arrays.toString(o)); } } void println(Object o){ out.println(o); } //------ sort primitive data types arrays ------// static void sort(int[] arr){ List<Integer> temp = new ArrayList<>(); for(int val: arr){temp.add(val);} Collections.sort(temp); for(int i = 0; i < arr.length; i++){arr[i] = temp.get(i);} } static void sort(long[] arr){ List<Long> temp = new ArrayList<>(); for(long val: arr){temp.add(val);} Collections.sort(temp); for(int i = 0; i < arr.length; i++){arr[i] = temp.get(i);} } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
4564df4f0f76d6127fbf2ca69eae83b4
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); String x=sc.next(); Vector<Integer> R=new Vector<>(); Vector<Integer> B=new Vector<>(); for(int i=0;i<n;i++){ if(x.charAt(i)=='B') R.add(a[i]); else B.add(a[i]); } Collections.sort(R); Collections.sort(B); boolean yes=true; for(int i=0;i<R.size();i++){ if(R.get(i)-i<1){System.out.println("NO");yes=false;break;} } if(yes) { int s=B.size(); for(int j=0;j<s;j++){ if(B.get(j)+s-j>n+1){System.out.println("NO");yes=false;break;} } } if(yes)System.out.println("YES"); } sc.close(); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
b319d4f189f48cd9392ded3513653716
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.io.*; public class blueredpermutation { static Scanner sc=new Scanner(System.in); static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) { long t=sc.nextLong(); while(t-->0){ solve(); } out.close(); } private static void solve() { int n=sc.nextInt(); int []arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } String s=sc.next(); ArrayList<Integer> red=new ArrayList<>(); ArrayList<Integer> blue=new ArrayList<>(); for(int i=0;i<n;i++){ if(s.charAt(i)=='B'){ blue.add(arr[i]); } else{ red.add(arr[i]); } } boolean flag=true; Collections.sort(blue); Collections.sort(red,Collections.reverseOrder()); for(int i=0;i<blue.size();i++){ if(blue.get(i)<(i+1)){ flag=false; break; } } if(flag){ for(int i=0;i<red.size();i++){ if(red.get(i)>(n-i)){ flag=false; break; } } } if(flag){ out.println("yes"); } else out.println("no"); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
856e16657893de8d645dbb367dcc5d3e
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.invoke.MethodHandles; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class c{ static int []f; //static int []f2; static int []size; //static int []size2; //static int []a=new int [500001]; static int max=Integer.MAX_VALUE; static long ans=0; static Set<Integer>set; static int k; static long mod= 998244353; public static void main(String []args) { MyScanner s=new MyScanner(); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int []f=new int [n]; for(int i=0;i<n;i++) f[i]=s.nextInt(); List<Integer>a=new ArrayList<>(); List<Integer>b=new ArrayList<>(); char []c=s.next().toCharArray(); for(int i=0;i<n;i++) { if(c[i]=='B') a.add(f[i]); else b.add(f[i]); } Collections.sort(a); Collections.sort(b); int k=1; int i=0,j=0; while(k<=n) { if(i<a.size()&&a.get(i)>=k) { k++;i++; } else if(j<b.size()&&b.get(j)<=k) { k++; j++; } else break; } if(k<=n) out.println("NO"); else out.println("YES"); } out.close(); } public static void dfs(char [][]a,char [][]b,int x,int y,int [][]v) { if(k==0)return ; b[x][y]='.'; k--; int []dx= {0,1,0,-1}; int []dy= {1,0,-1,0}; for(int i=0;i<4;i++) { int x1=x+dx[i]; int y1=y+dy[i]; if(x1<0||x1>=a.length||y1<0||y1>=a[0].length||v[x1][y1]==1||a[x1][y1]=='#') continue; v[x1][y1]=1; dfs(a,b,x1,y1,v); } } public static void swap(int []a) { int l=0,r=a.length-1; while(l<r) { int t=a[l]; a[l]=a[r]; a[r]=t; l++;r--; } } public static boolean is(int j) { for(int i=2;i<=(int )Math.sqrt(j);i++) { if(j%i==0)return false; } return true; } public static int find (int []father,int x) { if(x!=father[x]) x=find(father,father[x]); return father[x]; } public static void union(int []father,int x,int y,int []size) { x=find(father,x); y=find(father,y); if(x==y) return ; if(size[x]<size[y]) { int tem=x; x=y; y=tem; } father[y]=x; size[x]+=size[y]; return ; } public static void shufu(int []f) { for(int i=0;i<f.length;i++) { int k=(int)(Math.random()*(f.length)); int t=f[k]; f[k]=f[0]; f[0]=t; } } public static int gcd(int x,int y) { return y==0?x:gcd(y,x%y); } public static int lcm(int x,int y) { return x*y/gcd(x,y); } /* public static void buildertree(int k,int l,int r) { if(l==r) { f[k]=a[l]; return ; } int m=l+r>>1; buildertree(k+k,l,m); buildertree(k+k+1,m+1,r); f[k]= } public static void update(int u,int l,int r,int x,int c) { if(l==x && r==x) { f[u]=c; return; } int mid=l+r>>1; if(x<=mid)update(u<<1,l,mid,x,c); else if(x>=mid+1)update(u<<1|1,mid+1,r,x,c); f[u]=Math.max(f[u+u], f[u+u+1]); } public static int query(int k,int l,int r,int x,int y) { if(x==l&&y==r) { return f[k]; } int m=l+r>>1; if(y<=m) { return query(k+k,l,m,x,y); } else if(x>m)return query(k+k+1,m+1,r,x,y); else { int i=query(k+k,l,m,x,m),j=query(k+k+1,m+1,r,m+1,y); return Math.max(j, Math.max(i+j, i)); } } public static void calc(int k,int l,int r,int x,int z) { f[k]+=z; if(l==r) { return ; } int m=l+r>>1; if(x<=m) calc(k+k,l,m,x,z); else calc(k+k+1,m+1,r,x,z); } */ public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { try { final String USERDIR = System.getProperty("user.dir"); String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", ""); cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname; final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in"); br = new BufferedReader(new InputStreamReader(fin.exists() ? new FileInputStream(fin) : System.in)); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } public String next() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
a4de625dc0414241ff41ab9119bfd8fc
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class Solution { public static long gcd(long a,long b) { if(a==0) return b; return gcd(b%a,a); } public static long lcm(long a,long b) { return (a*b)/gcd(a,b); } public static long [] arrayInput(BufferedReader br,int n) throws java.lang.Exception { long out[]=new long[n]; String input[]=br.readLine().split(" "); for(int i=0;i<n;i++) { out[i]=Long.parseLong(input[i]); } return out; } public static long [] simpleInput(BufferedReader br,int n) throws java.lang.Exception{ long ans[]=new long[n]; String input[]=br.readLine().split(" "); for(int i=0;i<n;i++) { ans[i]=Long.parseLong(input[i]); } return ans; } public static void main(String[] args) throws java.lang.Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(System.out); int testCases=Integer.parseInt(br.readLine()); while(testCases-->0) { int n=(int)simpleInput(br, 1)[0]; long input[]=arrayInput(br, n); String s=br.readLine(); List<Long>blue=new ArrayList<>(); List<Long>red=new ArrayList<>(); for(int i=0;i<s.length();i++) { if(s.charAt(i)=='B') blue.add(input[i]); else red.add(input[i]); } Collections.sort(blue); Collections.sort(red); int next=1; for(int i=0;i<blue.size();i++) { if(blue.get(i)>=next) next++; } for(int i=0;i<red.size();i++) { if(red.get(i)<=next) next++; } if(next>n) out.println("YES"); else out.println("NO"); } out.close(); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
6cdb94382f2df896d49a126688e80f2b
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class Mainn { static long M = (long) (1e9 + 7); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if (st.hasMoreTokens()) { str = st.nextToken("\n"); } else { str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } public static void swap(long[] a, long i, long j) { long temp = a[(int) i]; a[(int) i] = a[(int) j]; a[(int) j] = (int) temp; } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static int lcm(int a, int b) { return (int) (a * b / gcd(a, b)); } public static String sortString(String inputString) { char[] tempArray = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } static boolean isSquare(int n) { int v = (int) Math.sqrt(n); return v * v == n; } static boolean PowerOfTwo(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 int power(long a, long b) { long res = 1; while (b > 0) { if (b % 2 == 1) { res = (res * a) % M; } a = ((a * a) % M); b = b / 2; } return (int) res; } public static boolean isPrime(int n) { for (int i = 2; i * i <= n; i++) if (n % i == 0) { return false; } return true; } static long computeXOR(long n) { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } static long binaryToInteger(String binary) { char[] numbers = binary.toCharArray(); long result = 0; for (int i = numbers.length - 1; i >= 0; i--) if (numbers[i] == '1') result += Math.pow(2, (numbers.length - i - 1)); return result; } static String reverseString(String str) { char ch[] = str.toCharArray(); String rev = ""; for (int i = ch.length - 1; i >= 0; i--) { rev += ch[i]; } return rev; } static int countFreq(int[] arr, int n) { HashMap<Integer, Integer> map = new HashMap<>(); int x = 0; for (int i = 0; i < n; i++) { map.put(arr[i], map.getOrDefault(arr[i], 0) + 1); } for (int i = 0; i < n; i++) { if (map.get(arr[i]) == 1) x++; } return x; } static void reverse(int[] arr, int l, int r) { int d = (r - l + 1) / 2; for (int i = 0; i < d; i++) { int t = arr[l + i]; arr[l + i] = arr[r - i]; arr[r - i] = t; } } static void sort(String[] s, int n) { for (int i = 1; i < n; i++) { String temp = s[i]; int j = i - 1; while (j >= 0 && temp.length() < s[j].length()) { s[j + 1] = s[j]; j--; } s[j + 1] = temp; } } static int sqr(int n) { double x = Math.sqrt(n); if ((int) x == x) return (int) x; else return (int) (x + 1); } static int set_bits_count(int num) { int count = 0; while (num > 0) { num &= (num - 1); count++; } return count; } static double factorial(double n) { double f = 1; for (int i = 1; i <= n; i++) { f = f * i; } return f; } static boolean palindrome(int arr[], int n) { boolean flag = true; for (int i = 0; i <= n / 2 && n != 0; i++) { if (arr[i] != arr[n - i - 1]) { flag = false; break; } } return flag; } public static boolean isSorted(int[] a) { if (a == null || a.length <= 1) { return true; } for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } public static boolean Consecutive(List<Integer> a, int[] b, int n, int m) { int i = 0, j = 0; while (i < n && j < m) { if (Objects.equals(a.get(i), b[j])) { i++; j++; if (j == m) return true; } else { i = i - j + 1; j = 0; } } return false; } static class Pair { int l; int r; public Pair(int l, int r) { this.l = l; this.r = r; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); 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(); } String s = sc.next(); List<Integer> blue = new ArrayList<Integer>(); List<Integer> red = new ArrayList<Integer>(); for (int i = 0; i < s.length(); i++) { if(s.charAt(i) == 'B') { blue.add(ar[i]); } else { red.add(ar[i]); } } Collections.sort(blue); red.sort(Collections.reverseOrder()); boolean ok = true; for (int i = 0; i < blue.size(); i++) { if(blue.get(i) <= i) { ok = false; break; } } for (int i = 0; i <red.size() ; i++) { if(red.get(i) > n-i) { ok = false; break; } } System.out.println(ok ? "YES" : "NO"); } } static void solve() throws IOException { } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
b6e49accac85add7b54839454851e686
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; public class MergeSort { static int[][] move = {{1, 1}, {1, 0}, {1, -1}, {0, 1}, {-1, 1}, {0, -1}, {-1, -1}, {0, -1}}; public static void main(String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); while (test-- > 0) { int n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); char[] c=sc.next().toCharArray(); Vector<Integer> l=new Vector<>(), r=new Vector<>(); for(int i=0;i<n;i++) (c[i] == 'B' ? l : r).add(a[i]); Collections.sort(l); r.sort(Collections.reverseOrder()); boolean ok = true; for(int i=0;i<l.size();i++) if (l.get(i) < i + 1) { ok = false; break; } for(int i=0;i<r.size();i++) if (r.get(i) > n - i) { ok = false; break; } System.out.print((ok ? "YES" : "NO")+'\n'); } } //////////Helper Functions//////////////////////////////////// private static boolean isValidPos(long x, long y, long i, long j) { return i < x && i >= 0 && j >= 0 && j < y; } private static boolean isPalindrome(char[] s) { int start = 0, last = s.length - 1; while (start < last) { if (s[start] != s[last]) return false; start++; last--; } return true; } private static void swap(int i, int j, long[] arr) { long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } private static void swap(int i, int j) { int temp = i; i = j; j = temp; } private static boolean isEven(int a) { return a % 2 == 0; } static int findGCD(int x, int y) { int r = 0, a, b; a = Math.max(x, y); b = Math.min(x, y); r = b; while (a % b != 0) { r = a % b; a = b; b = r; } return r; } static int findLcm(int a, int b){ return (a*b)/findGCD(a,b); } private static boolean primalityTest(int n) { if (n == 1) return false; for (int i = 2; i * i < n; i++) { if (n % i == 0) return false; } return true; } private static void swap(int i, int j, char[] arr) { char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public static Pair asFraction(long a, long b) { long gcd = gcd(a, b); return new Pair(a / gcd, b / gcd); } static class Pair { long x, y, z; Pair(long x, long y) { this.x = x; this.y = y; } Pair(long x, long y, long z) { this.x = x; this.y = y; this.z = z; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
7e1facbbacdd6dd0eb8a7af43f59253c
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
//package random; import java.util.*; import java.io.*; public class CF { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int q = Integer.parseInt(st.nextToken()); while(q-->0) { int n = Integer.parseInt(br.readLine()); int[] arr = new int[n]; st = new StringTokenizer(br.readLine()); for (int i=0; i<n; i++) { arr[i] = Integer.parseInt(st.nextToken()); } String s = br.readLine(); int r = 0; int b = 0; for (int i=0; i<n; i++) { if (s.charAt(i)=='B') { b++; } else { r++; } } boolean pos = true; if (b>0&&r>0) { int[] red = new int[r]; int[] blue = new int[b]; b = 0; r = 0; for (int i=0; i<n; i++) { if (s.charAt(i)=='R') { red[r] = arr[i]; r++; } else { blue[b] = arr[i]; b++; } } for (int i=0; i<r; i++) { int t = (int)Math.random()*r; int temp = red[t]; red[t] = red[i]; red[i] = temp; } for (int i=0; i<b; i++) { int t = (int)Math.random()*b; int temp = blue[t]; blue[t] = blue[i]; blue[i] = temp; } Arrays.sort(red); Arrays.sort(blue); for (int i=0; i<b; i++) { if (blue[i]<i+1) { pos = false; break; } } for (int i=b; i<n; i++) { if (red[i-b]>i+1) { pos = false; break; } } } else if (b>0) { for (int i=0; i<n; i++) { int t = (int)Math.random()*n; int temp = arr[t]; arr[t] = arr[i]; arr[i] = temp; } Arrays.sort(arr); for (int i=0; i<n; i++) { if (arr[i]<i+1) { pos = false; break; } } } else { for (int i=0; i<n; i++) { int t = (int)Math.random()*n; int temp = arr[t]; arr[t] = arr[i]; arr[i] = temp; } Arrays.sort(arr); for (int i=0; i<n; i++) { if (arr[i]>i+1) { pos = false; break; } } } if (pos) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
6e40d9599a9d256a26408275b8efb26a
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces{ static long mod = 1000000007L; // map.put(a[i],map.getOrDefault(a[i],0)+1); // map.putIfAbsent; static MyScanner sc = new MyScanner(); //<----------------------------------------------WRITE HERE-------------------------------------------> static void solve(){ int n = sc.nextInt(); int[] a= sc.readIntArray(n); char[] ca = sc.next().toCharArray(); Map<Integer,Integer> hash = new HashMap<>(); int blue = 0; int red = 0; int blueSpare = 0; int redSpare = 0; for(int i=0;i<n;i++) { if(ca[i] == 'B')blue++; else red++; } for(int i=0;i<n;i++) { if(ca[i] == 'B') { if(a[i] > blue) blueSpare++; else hash.put(a[i],hash.getOrDefault(a[i],0)+1); } else { if(a[i]<=blue) redSpare++; else hash.put(a[i],hash.getOrDefault(a[i],0)+1); } } // out.println(hash); // out.println(blueSpare+" "+redSpare); int tot = 0; for(int i=blue;i>=1;i--) { if(hash.containsKey(i)) tot+= hash.get(i); if(i == blue) tot+= blueSpare; if(tot == 0) { out.println("NO"); return; } // out.println(tot); tot--; } tot = 0; for(int i=blue+1;i<=n;i++) { if(hash.containsKey(i)) tot+= hash.get(i); if(i == blue+1) tot+= redSpare; if(tot == 0) { out.println("NO"); return; } // out.println(tot+" df"); tot--; } out.println("YES"); // out.println(blueSpare+" "+redSpare); } //<----------------------------------------------WRITE HERE-------------------------------------------> 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; } static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } static boolean isPali(String str){ int i = 0; int j = str.length()-1; while(i<j){ if(str.charAt(i)!=str.charAt(j)){ return false; } i++; j--; } return true; } static void priArr(int[] a) { for(int i=0;i<a.length;i++) { out.print(a[i] + " "); } out.println(); } static long gcd(long a,long b){ if(b==0) return a; return gcd(b,a%b); } static String reverse(String str){ char arr[] = str.toCharArray(); int i = 0; int j = arr.length-1; while(i<j){ char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } String st = new String(arr); return st; } static void reverse(int[] arr){ int i = 0; int j = arr.length-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static boolean isprime(int n){ if(n==1) return false; if(n==3 || n==2) return true; if(n%2==0 || n%3==0) return false; for(int i = 5;i*i<=n;i+= 6){ if(n%i== 0 || n%(i+2)==0){ return false; } } return true; } static class Pair implements Comparable<Pair>{ int val; int ind; Pair(int v,int f){ val = v; ind = f; } public int compareTo(Pair p){ if(this.val != p.val) return this.val-p.val; return this.ind - p.ind; } } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); // int t= 1; while(t-- >0){ solve(); } // Stop writing your solution here. ------------------------------------- out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int[] readIntArray(int n){ int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(next()); } return arr; } int[] reverse(int arr[]){ int n= arr.length; int i = 0; int j = n-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; j--;i++; } return arr; } long[] readLongArray(int n){ long arr[] = new long[n]; for(int i = 0;i<n;i++){ arr[i] = Long.parseLong(next()); } return arr; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
01273f06663c1599f3e9ed894f7913b2
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; public class BlueRedPermutation { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pr=new PrintWriter(System.out); int t=Integer.parseInt(br.readLine()); while(t!=0){ solve(br,pr); t--; } pr.flush(); pr.close(); } public static void solve(BufferedReader br,PrintWriter pr) throws IOException{ int n=Integer.parseInt(br.readLine()); String[] temp=br.readLine().split(" "); int[] nums=new int[n]; for(int i=0;i<n;i++){ nums[i]=Integer.parseInt(temp[i]); } String color=br.readLine(); PriorityQueue<Integer> R=new PriorityQueue<>((a,b)->(b-a)); PriorityQueue<Integer> B=new PriorityQueue<>(); for(int i=0;i<n;i++){ char c=color.charAt(i); if((c=='B'&&nums[i]<=0)||(c=='R'&&nums[i]>n)){ pr.println("NO"); return; } if(c=='B'){ B.add(nums[i]); } else if(c=='R'){ R.add(nums[i]); } } int current=1; while(B.size()!=0){ int min=B.poll(); if(min<current){ pr.println("NO"); return; } current++; } current=n; while(R.size()!=0){ int max=R.poll(); if(max>current){ pr.println("NO"); return; } current--; } pr.println("YES"); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
7b2ef485072e3cd478405b3807bd8cb1
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
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_Blue_Red_Permutation { 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); String s = f.nextLine(); ArrayList<Integer> r = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); for(int i = 0; i < n; i++) { if(s.charAt(i) == 'R') { r.add(arr[i]); } else { b.add(arr[i]); } } Collections.sort(r); Collections.sort(b); for(int i = r.size()-1; i >= 0; i--) { if(r.get(i) > n-(r.size()-1-i)) { out.println("NO"); return; } } for(int i = 0; i < b.size(); i++) { if(b.get(i) < (i+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; } } } /** 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
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
bf6edbe6fc8447f3f401c3e838dd14af
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class Q1607D { static int mod = (int) (1e9 + 7); static void solve() { int n = i(); long[] num = new long[n]; for(int i=0;i<n;i++){ num[i]=l(); } ArrayList<Long>red=new ArrayList<>(); ArrayList<Long>blue=new ArrayList<>(); char[]paint=s().toCharArray(); for(int i=0;i<n;i++){ if(paint[i]=='R'){ red.add(num[i]); }else{ blue.add(num[i]); } } Collections.sort(blue); Collections.sort(red); for(int i=0;i<blue.size();i++){ if(blue.get(i)>=(i+1)){ blue.set(i, (long) (i+1)); }else{ System.out.println("NO"); return; } } long last=blue.size(); for(int i=0;i<red.size();i++){ if(red.get(i)<=(i+1)+last){ red.set(i, (long)(i+1+last)); }else{ System.out.println("NO"); return; } } // System.out.println(red); // System.out.println(blue); System.out.println("YES"); } public static class pair{ } public static void main(String[] args) { int test = i(); while (test-- > 0) { solve(); } } // -----> POWER ---> long power(long x, long y) <---- // -----> LCM ---> long lcm(long x, long y) <---- // -----> GCD ---> long gcd(long x, long y) <---- // -----> SIEVE --> ArrayList<Integer> sieve(int N) <----- // -----> NCR ---> long ncr(int n, int r) <---- // -----> BINARY_SEARCH ---> int binary_Search(long[]arr,long x) <---- // -----> (SORTING OF LONG, CHAR,INT) -->long[] sortLong(long[] a2)<-- // ----> DFS ---> void dfs(ArrayList<ArrayList<Integer>>edges,int child,int // parent)<--- // ---> NODETOROOT --> ArrayList<Integer> // node2Root(ArrayList<ArrayList<Integer>>edges,int child,int parent,int tofind) // <-- // ---> LEVELS_TREE -->levels_Trees(ArrayList<HashSet<Integer>> edges, int // child, int parent,int[]level,int currLevel) <-- // ---> K_THPARENT --> int k_thparent(int node,int[][]parent,int k) <--- // ---> TWOPOWERITHPARENTFILL --> void twoPowerIthParentFill(int[][]parent) <--- // -----> (INPUT OF INT,LONG ,STRING) -> int i() long l() String s()<- // tempstart static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int Int() { 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 String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static InputReader in = new InputReader(System.in); public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
09cdc694dd755bb9723ac15e3e2e243e
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.io.*; public class Main { static String io[], ss; static int n, m, k, t, N = 200010, M = 100010, P = (int)1e6+7; static int[] a = new int[N], b = new int[N]; static void solve() throws Exception { n = ni(in.readLine()); io = in.readLine().split(" "); ss = in.readLine(); int n1 = 0, n2 = 0, h1 = 0, h2 = 0; for (int i = 0;i < n;i++) { char c = ss.charAt(i); int v = ni(io[i]); if (c == 'R') a[n1++] = v; else b[n2++] = v; } Arrays.sort(a, 0, n1); Arrays.sort(b, 0, n2); for (int i = 1;i <= n;i++){ //out.println(i+":"+a[h1]+" "+a[h2]); if (h2 < n2 && b[h2] >= i) ++h2; else{ if (h1 >= n1 || a[h1] > i){ out.println("NO"); return; }else ++h1; } } out.println("YES"); } public static void main(String[] args) throws Exception { t = Integer.parseInt(in.readLine()); while (t-- > 0) solve(); out.flush(); } static int ni() throws IOException{input.nextToken();return (int) input.nval;} static long nl() throws IOException{input.nextToken();return (long) input.nval;} static int ni(String x) {return Integer.parseInt(x);} static long nl(String x) {return Long.parseLong(x);} static int max(int a, int b) {return a > b ? a : b;} static int min(int a, int b) {return a < b ? a : b;} static long max(long a, long b) {return a > b ? a : b;} static long min(long a, long b) {return a < b ? a : b;} static int abs(int a) {return a > 0?a:-a;} static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer input = new StreamTokenizer(in); static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
b96f74085c24dd559e041f029bfec216
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; /** * @author atulanand */ public class Solution { static class Result { public boolean solve(int[] a, char[] b) { PriorityQueue<Integer> red = new PriorityQueue<>(Comparator.naturalOrder()); PriorityQueue<Integer> blue = new PriorityQueue<>(Comparator.naturalOrder()); for (int i = 0; i < a.length; i++) { if (b[i] == 'R') { red.add(a[i]); } else { blue.add(a[i]); } } for (int i = 1; i <= a.length; i++) { if (!blue.isEmpty() && blue.peek() >= i) { blue.poll(); continue; } if (!red.isEmpty() && red.peek() <= i) { red.poll(); continue; } return false; } return true; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = inputInt(br); Result result = new Result(); StringBuilder sb = new StringBuilder(); while (t-- > 0) { inputInt(br); sb.append(result.solve(inputIntArray(br), inputCharArray(br)) ? "YES" : "NO").append("\n"); } System.out.println(sb); } public static char[][] inputCharGrid(BufferedReader br, int rows) throws IOException { char[][] grid = new char[rows][]; for (int i = 0; i < grid.length; i++) { grid[i] = inputString(br).toCharArray(); } return grid; } public static int[][] inputIntGrid(BufferedReader br, int rows) throws IOException { int[][] grid = new int[rows][]; for (int i = 0; i < grid.length; i++) { grid[i] = inputIntArray(br); } return grid; } public static char[] inputCharArray(BufferedReader br) throws IOException { return inputString(br).toCharArray(); } public static String inputString(BufferedReader br) throws IOException { return br.readLine().trim(); } public static int inputInt(BufferedReader br) throws IOException { return Integer.parseInt(inputString(br)); } public static long inputLong(BufferedReader br) throws IOException { return Long.parseLong(inputString(br)); } public static int[] inputIntArray(BufferedReader br) throws IOException { String[] spec = inputString(br).split(" "); int[] arr = new int[spec.length]; for (int i = 0; i < spec.length; i++) arr[i] = Integer.parseInt(spec[i]); return arr; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
4209015f7290a3e526bc8f67fd5b229b
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; 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]; TreeSet<Integer> set = new TreeSet<>(); TreeMap<Integer, Integer> blue = new TreeMap<>(); TreeMap<Integer, Integer> red = new TreeMap<>(); for(int i = 0; i < n; i++) { set.add(i+1); arr[i] = sc.nextInt(); } String color = sc.next(); for(int i = 0; i < n; i++) { if(color.charAt(i) == 'R') { if(red.containsKey(arr[i])) red.put(arr[i], 1+red.get(arr[i])); else red.put(arr[i], 1); }else { if(blue.containsKey(arr[i])) blue.put(arr[i], 1+blue.get(arr[i])); else blue.put(arr[i], 1); } } boolean ans = true; for(Map.Entry<Integer, Integer> mape : blue.entrySet()) { int val = mape.getValue(); for(int i = 0; i < val; i++) { int vall = set.higher(0); if(vall <= mape.getKey()) { set.remove(vall); }else ans = false; } } for(Map.Entry<Integer, Integer> mape : red.entrySet()) { int val = mape.getValue(); for(int i = 0; i < val; i++) { int vall = set.higher(0); if(vall >= mape.getKey()) { set.remove(vall); }else ans = false; } } if(ans && set.isEmpty())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>{ long a; long b; Pair(long a, long 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 Long.compare(this.b, o.b); }else { return Long.compare(this.a, o.a); } } @Override public String toString() { return this.a + " " + this.b; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
3d36f9e808d5563f6cdbfd8b22597e54
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.lang.Exception; import java.util.ArrayList; import java.util.Collections; 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{ if(st.hasMoreTokens()) str=st.nextToken("\n"); else str=br.readLine(); }catch(IOException e){ e.printStackTrace(); } return str; } } static FastReader sc=new FastReader(); static void solve(){ int n=sc.nextInt(); int[] v=new int[n]; for(int i=0;i<n;i++){ v[i]=sc.nextInt(); } String colors=sc.next(); ArrayList<Integer> blue=new ArrayList<Integer>(n); ArrayList<Integer> red=new ArrayList<Integer>(n); for(int i=0;i<n;i++){ if(colors.charAt(i)=='B') blue.add(v[i]); else red.add(v[i]); } Collections.sort(blue); Collections.sort(red); int szb=blue.size(); int szr=red.size(); for(int i=0;i<szb;i++){ if(blue.get(i)<i+1){ System.out.println("NO"); return; } } for(int i=0;i<szr;i++){ if(red.get(i)>i+szb+1){ System.out.println("NO"); return; } } System.out.println("YES"); } public static void main(String args[])throws java.lang.Exception{ int t=sc.nextInt(); while(t-->0){ solve(); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
22714c4076f815694444961708eef9b6
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
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.util.*; public class Main { 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(); String str = scn.next(); ArrayList<Integer>b = new ArrayList<>(); ArrayList<Integer>r = new ArrayList<>(); boolean flag = false; for(int i = 0;i<arr.length;i++){ char ch = str.charAt(i); if(ch == 'B'){ if(arr[i]<0){ flag = true; break; } else{ b.add(arr[i]); } } else{ if(arr[i]>n){ flag = true; break; } else{ r.add(arr[i]); } } } Collections.sort(b); Collections.sort(r); int [] blue = new int[b.size()+1]; blue[0] = 0; for(int i = 1;i<blue.length;i++){ blue[i] = b.get(i-1); } int [] red = new int[r.size()+1]; red[0] = 0; for(int i = 1;i<red.length;i++){ red[i] = r.get(i-1); } for(int i = 1;i<blue.length;i++){ if(blue[i]>=i){ // nothing to do } else{ flag = true; break; } } for(int i = red.length-1 ;i>=1;i--){ if(red[i]<= i+b.size()){ // nothig to do; } else{ flag = true; break; } } if(flag){ System.out.println("NO"); } else{ System.out.println("YES"); } } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
81b214b355d6f1050e5a2dec433173b7
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.StringTokenizer; public class App { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void solve(FastReader reader, PrintWriter writer) { int n = reader.nextInt(); String array = reader.nextLine(); String colours = reader.nextLine(); int redLength = 0; for (int i = 0; i < n; i++) { if (colours.charAt(i) == 'R') redLength++; } int[] redArray = new int[redLength]; int[] blueArray = new int[n - redLength]; String[] split = array.split(" "); int redIndex = 0; int blueIndex = 0; for (int i = 0; i < n; i++) { if (colours.charAt(i) == 'R') { redArray[redIndex] = Integer.parseInt(split[i]); redIndex++; } else { blueArray[blueIndex] = Integer.parseInt(split[i]); blueIndex++; } } Arrays.sort(redArray); Arrays.sort(blueArray); int lastNumber = n + 1; int space = 0; for (int i = redLength - 1; i >= 0; i--) { if (redArray[i] > n) { writer.println("NO"); return; } if (lastNumber == redArray[i] && space == 0) { writer.println("NO"); return; } space += lastNumber - redArray[i] - 1; lastNumber = redArray[i]; } lastNumber = 0; space = 0; for (int i = 0; i < n - redLength; i++) { if (blueArray[i] < 0) { writer.println("NO"); return; } if (lastNumber == blueArray[i] && space == 0) { writer.println("NO"); return; } space += blueArray[i] - 1 - lastNumber; lastNumber = blueArray[i]; } writer.println("YES"); } public static void main(String[] args){ FastReader reader = new FastReader(); PrintWriter writer = new PrintWriter(System.out, true); int t = reader.nextInt(); // int t = 1; for (int i = 1; i <= t; i++) { solve(reader, writer); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
7f3fd608feca9e2d03ba67ee5db4a1ea
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.lang.reflect.Constructor; import java.util.*; public class CodeD { private static final boolean SHOULD_BUFFER_OUTPUT = true; static final SuperWriter sw = new SuperWriter(System.out); static final SuperScanner sc = new SuperScanner(); static class RepeatedInteger implements Comparable<RepeatedInteger> { int i; int position; RepeatedInteger(int i, int position) { this.i = i; this.position = position; } @Override public int compareTo(RepeatedInteger o) { if (i == o.i) { return Integer.compare(position, o.position); } return Integer.compare(i, o.i); } } public static void solve() { int n = sc.nextInt(); int[] a = sc.nextIntArray(n); TreeSet<RepeatedInteger> blueElements = new TreeSet<>(); TreeSet<RepeatedInteger> redElements = new TreeSet<>(); char[] colors = sc.next().toCharArray(); for (int i = 0; i < a.length; i++) { if (colors[i] == 'B') { blueElements.add(new RepeatedInteger(a[i], i)); } else { redElements.add(new RepeatedInteger(a[i], i)); } } for (int i = 1; i <= n; i++) { while (!blueElements.isEmpty() && blueElements.first().i < i) { blueElements.pollFirst(); } if (!blueElements.isEmpty() && blueElements.first().i >= i) { blueElements.pollFirst(); } else if (!redElements.isEmpty() && redElements.first().i <= i) { redElements.pollFirst(); } else { sw.printLine("NO"); return; } } sw.printLine("YES"); } public static void main() { int t = sc.nextInt(); for (int cc = 0; cc < t; cc++) { solve(); } } static class LineScanner extends Scanner { private StringTokenizer st; public LineScanner(String input) { st = new StringTokenizer(input); } @Override public String next() { return st.hasMoreTokens() ? st.nextToken() : null; } @Override public String nextLine() { throw new RuntimeException("not supported"); } public boolean hasNext() { return st.hasMoreTokens(); } private final ArrayList<Object> temp = new ArrayList<Object>(); private void fillTemp() { while (st.hasMoreTokens()) { temp.add(st.nextToken()); } } public String[] asStringArray() { fillTemp(); String[] answer = new String[temp.size()]; for (int i = 0; i < answer.length; i++) { answer[i] = (String) temp.get(i); } temp.clear(); return answer; } public int[] asIntArray() { fillTemp(); int[] answer = new int[temp.size()]; for (int i = 0; i < answer.length; i++) { answer[i] = Integer.parseInt((String) temp.get(i)); } temp.clear(); return answer; } public long[] asLongArray() { fillTemp(); long[] answer = new long[temp.size()]; for (int i = 0; i < answer.length; i++) { answer[i] = Long.parseLong((String) temp.get(i)); } temp.clear(); return answer; } public double[] asDoubleArray() { fillTemp(); double[] answer = new double[temp.size()]; for (int i = 0; i < answer.length; i++) { answer[i] = Double.parseDouble((String) temp.get(i)); } temp.clear(); return answer; } } static class SuperScanner extends Scanner { private InputStream stream; private byte[] buf = new byte[8096]; private int curChar; private int numChars; public SuperScanner() { this.stream = System.in; } public int read() { if (numChars == -1) return -1; if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) return -1; } return buf[curChar++]; } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public static boolean isLineEnd(int c) { return c == '\n' || c == -1; } private final StringBuilder sb = new StringBuilder(); @Override public String next() { int c = read(); while (isWhitespace(c)) { if (c == -1) { return null; } c = read(); } sb.setLength(0); do { sb.append((char) c); c = read(); } while (!isWhitespace(c)); return sb.toString(); } @Override public String nextLine() { sb.setLength(0); int c; while (true) { c = read(); if (!isLineEnd(c)) { sb.append((char) c); } else { break; } } if (c == -1 && sb.length() == 0) { return null; } else { if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\r') { return sb.substring(0, sb.length() - 1); } else { return sb.toString(); } } } public LineScanner nextLineScanner() { String line = nextLine(); if (line == null) { return null; } else { return new LineScanner(line); } } public LineScanner nextNonEmptyLineScanner() { while (true) { String line = nextLine(); if (line == null) { return null; } else if (!line.isEmpty()) { return new LineScanner(line); } } } @Override public int nextInt() { int c = read(); while (isWhitespace(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 = (res << 3) + (res << 1); res += c - '0'; c = read(); } while (!isWhitespace(c)); return res * sgn; } @Override public long nextLong() { int c = read(); while (isWhitespace(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = (res << 3) + (res << 1); res += c - '0'; c = read(); } while (!isWhitespace(c)); return res * sgn; } } static abstract class Scanner { public abstract String next(); public abstract String nextLine(); public int nextIntOrQuit() { Integer n = nextInteger(); if (n == null) { sw.close(); System.exit(0); } return n.intValue(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < res.length; i++) res[i] = nextInt(); return res; } public long[] nextLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < res.length; i++) res[i] = nextLong(); return res; } public double[] nextDoubleArray(int n) { double[] res = new double[n]; for (int i = 0; i < res.length; i++) res[i] = nextDouble(); return res; } public void sortIntArray(int[] array) { Integer[] vals = new Integer[array.length]; for (int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for (int i = 0; i < array.length; i++) array[i] = vals[i]; } public void sortLongArray(long[] array) { Long[] vals = new Long[array.length]; for (int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for (int i = 0; i < array.length; i++) array[i] = vals[i]; } public void sortDoubleArray(double[] array) { Double[] vals = new Double[array.length]; for (int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for (int i = 0; i < array.length; i++) array[i] = vals[i]; } public String[] nextStringArray(int n) { String[] vals = new String[n]; for (int i = 0; i < n; i++) vals[i] = next(); return vals; } public Integer nextInteger() { String s = next(); if (s == null) return null; return Integer.parseInt(s); } public int[][] nextIntMatrix(int n, int m) { int[][] ans = new int[n][]; for (int i = 0; i < n; i++) ans[i] = nextIntArray(m); return ans; } public char[][] nextGrid(int r) { char[][] grid = new char[r][]; for (int i = 0; i < r; i++) grid[i] = next().toCharArray(); return grid; } public static <T> T fill(T arreglo, int val) { if (arreglo instanceof Object[]) { Object[] a = (Object[]) arreglo; for (Object x : a) fill(x, val); } else if (arreglo instanceof int[]) Arrays.fill((int[]) arreglo, val); else if (arreglo instanceof double[]) Arrays.fill((double[]) arreglo, val); else if (arreglo instanceof long[]) Arrays.fill((long[]) arreglo, val); return arreglo; } public <T> T[] nextObjectArray(Class<T> clazz, int size) { @SuppressWarnings("unchecked") T[] result = (T[]) java.lang.reflect.Array.newInstance(clazz, size); for (int c = 0; c < 3; c++) { Constructor<T> constructor; try { if (c == 0) constructor = clazz.getDeclaredConstructor(Scanner.class, Integer.TYPE); else if (c == 1) constructor = clazz.getDeclaredConstructor(Scanner.class); else constructor = clazz.getDeclaredConstructor(); } catch (Exception e) { continue; } try { for (int i = 0; i < result.length; i++) { if (c == 0) result[i] = constructor.newInstance(this, i); else if (c == 1) result[i] = constructor.newInstance(this); else result[i] = constructor.newInstance(); } } catch (Exception e) { throw new RuntimeException(e); } return result; } throw new RuntimeException("Constructor not found"); } public Collection<Integer> wrap(int[] as) { ArrayList<Integer> ans = new ArrayList<Integer>(); for (int i : as) ans.add(i); return ans; } public int[] unwrap(Collection<Integer> collection) { int[] vals = new int[collection.size()]; int index = 0; for (int i : collection) vals[index++] = i; return vals; } int testCases = Integer.MIN_VALUE; boolean testCases() { if (testCases == Integer.MIN_VALUE) { testCases = nextInt(); } return --testCases >= 0; } } static class SuperWriter { private final PrintWriter writer; public SuperWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public SuperWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.flush(); writer.close(); } public void printLine(String line) { writer.println(line); if (!SHOULD_BUFFER_OUTPUT) { writer.flush(); } } public void printLine(int... vals) { if (vals.length == 0) { writer.println(); } else { writer.print(vals[0]); for (int i = 1; i < vals.length; i++) writer.print(" ".concat(String.valueOf(vals[i]))); writer.println(); } if (!SHOULD_BUFFER_OUTPUT) { writer.flush(); } } public void printLine(long... vals) { if (vals.length == 0) { writer.println(); } else { writer.print(vals[0]); for (int i = 1; i < vals.length; i++) writer.print(" ".concat(String.valueOf(vals[i]))); writer.println(); } if (!SHOULD_BUFFER_OUTPUT) { writer.flush(); } } public void printLine(double... vals) { if (vals.length == 0) { writer.println(); } else { writer.print(vals[0]); for (int i = 1; i < vals.length; i++) writer.print(" ".concat(String.valueOf(vals[i]))); writer.println(); } if (!SHOULD_BUFFER_OUTPUT) { writer.flush(); } } public void printLine(int prec, double... vals) { if (vals.length == 0) { writer.println(); } else { String precision = "%." + prec + "f"; writer.print(String.format(precision, vals[0]).replace(',', '.')); precision = " " + precision; for (int i = 1; i < vals.length; i++) writer.print(String.format(precision, vals[i]).replace(',', '.')); writer.println(); } if (!SHOULD_BUFFER_OUTPUT) { writer.flush(); } } public <E> void printLine(Collection<E> vals) { if (vals.size() == 0) { writer.println(); } else { int i = 0; for (E val : vals) { if (i++ == 0) { writer.print(val.toString()); } else { writer.print(" ".concat(val.toString())); } } writer.println(); } if (!SHOULD_BUFFER_OUTPUT) { writer.flush(); } } public void printSimple(String value) { writer.print(value); if (!SHOULD_BUFFER_OUTPUT) { writer.flush(); } } public boolean ifZeroExit(Number... values) { for (Number number : values) { if (number.doubleValue() != 0.0d || number.longValue() != 0) { return false; } } close(); System.exit(0); return true; } } public static void main(String[] args) { main(); sw.close(); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
bc3df2a0dc19641c72b3ba43b2da7de2
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; public class Hello { 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 index = 0; index < N; index++) { arr[index] = sc.nextInt(); } sc.nextLine(); String str = sc.nextLine(); List<Integer> numList1 = new ArrayList<>(); List<Integer> numList2 = new ArrayList<>(); int index = 0; for (char ch : str.toCharArray()) { if (ch == 'B') { numList1.add(arr[index]); } else { numList2.add(arr[index]); } index++; } Collections.sort(numList1); Collections.sort(numList2, Collections.reverseOrder()); boolean canForm = true; for (index = 0; index < numList1.size(); index++) { if (numList1.get(index) < index + 1) { canForm = false; } } for (index = 0; index < numList2.size(); index++) { if (numList2.get(index) > N - index) { canForm = false; } } System.out.println(canForm ? "YES" : "NO"); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
67c9a78c828534992fcb584e0e060ac8
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
/* "Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up." Just have Patience + 1... */ import java.util.*; import java.lang.*; import java.io.*; public class Solution { public static void main(String[] args) throws java.lang.Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new FastReader(); int test = sc.nextInt(); for (int t = 1; t <= test; t++) { solve(); } out.close(); } private static void solve() { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } char[] color = sc.next().toCharArray(); List<Integer> blues = new ArrayList<>(); List<Integer> reds = new ArrayList<>(); for (int i = 0; i < n; i++) { if (color[i] == 'B') { blues.add(arr[i]); }else { reds.add(arr[i]); } } Collections.sort(blues); Collections.sort(reds); int m = blues.size(); for (int i = 0; i < m; i++) { if (blues.get(i) < i + 1) { out.println("NO"); return; } } m = reds.size(); for (int i = m - 1; i >= 0; i--) { if (reds.get(i) > n) { out.println("NO"); return; } n--; } out.println("YES"); } public static FastReader sc; public static PrintWriter out; static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } return str; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
bdf9260a657d2d3bd003daca3cceb98d
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces { static long M = 1000000007; public static class Pair implements Comparable<Pair>{ int val; char color; public Pair(int v, char c) { val = v; color = c; } @Override public int compareTo(Pair o) { if(this.color != o.color) { // blues always to the left (lesser) if(this.color == 'B') { return -1; }else { return 1; } } return this.val - o.val; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int cases = Integer.parseInt(br.readLine()); o:while(cases-- > 0) { StringBuffer buf = new StringBuffer(); String[] str = br.readLine().split(" "); int n = Integer.parseInt(str[0]); int[] a = new int[n]; str = br.readLine().split(" "); for(int i=0; i<n; i++) { a[i] = Integer.parseInt(str[i]); } char[] colors = br.readLine().toCharArray(); // Blues can go to the left (decrease) and reds to right // if we start with smallest blues and fill greedily from left most empty places // and then do the same for reds, we will know yes or no // Why greedy? Take this - _ 4b _ 7b // if we send 7 to left most empty (_) then who fills the empty right to 4 ? Pair[] pairs = new Pair[n]; for(int i=0; i<n; i++) { pairs[i] = new Pair(a[i], colors[i]); } Arrays.sort(pairs); // now check if these nums can be brought to 1 - n boolean ans = true; for(int i=0; i<n; i++) { if(pairs[i].color == 'B' && pairs[i].val < (i+1)) { ans = false; } if(pairs[i].color == 'R' && pairs[i].val > (i+1)) { ans = false; } } if(ans) { System.out.println("YES"); }else { System.out.println("NO"); } } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
53f199d685034685c96380716428376c
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.io.*; public class RaceFikky { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out); int t = Integer.parseInt(f.readLine()); while (t-- > 0) { int n=Integer.parseInt(f.readLine()); StringTokenizer st = new StringTokenizer(f.readLine()); int[] arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=Integer.parseInt(st.nextToken()); } String s=f.readLine(); boolean good=true; ArrayList<Integer> b=new ArrayList<Integer>(); ArrayList<Integer> r=new ArrayList<Integer>(); for(int i=0;i<n;i++) { if(s.charAt(i)=='B') { b.add(arr[i]); } else { r.add(arr[i]); } } Collections.sort(b); Collections.sort(r); int count=1; for(int i:b) { if(i<count) { good=false; } count++; } for(int i:r) { if(i>count) { good=false; } count++; } pw.println(good?"YES":"NO"); } pw.close(); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
0220108dbf4e18b7a300ef61e32214c6
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; public class BRPerm { public static void main(String arg[]) { 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(); } ArrayList<Integer> red=new ArrayList<Integer>(); ArrayList<Integer> blue=new ArrayList<Integer>(); Sc.nextLine(); String S=Sc.nextLine(); for(int i=0;i<n;i++) { char ch=S.charAt(i); if(ch=='R') red.add(ar[i]); else blue.add(ar[i]); } Collections.sort(red); Collections.sort(blue); boolean check=true; for(int i=0;i<blue.size();i++) { if(blue.get(i)<i+1) { check=false; } } for(int i=red.size()-1,zz=n;i>=0;i--,zz--) { if(red.get(i)>zz) { check=false; } } if(check) { System.out.println("YES"); } else System.out.println("NO"); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
4316abc1a9350d040b9f17b9454bd9f2
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); ArrayList<Integer> r = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); ArrayList<Integer> vals = new ArrayList<>(); for (int j = 0; j < n; j++) { int val = sc.nextInt(); vals.add(val); } String o = sc.next(); for (int j = 0; j < n; j++) { if (o.charAt(j) == 'R') { r.add(vals.get(j)); } else { b.add(vals.get(j)); } } Collections.sort(r, Collections.reverseOrder()); Collections.sort(b); boolean ans = true; for (int j = 0; j < b.size(); j++) { if (b.get(j) <= j) { ans = false; break; } } for (int j = 0; j < r.size(); j++) { if (r.get(j) > n - j) { ans = false; break; } } pw.println(ans? "YES": "NO"); } pw.close(); } } class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } ArrayList<Integer> readList(int n) { ArrayList<Integer> arr = new ArrayList<>(); for (int i = 0; i < n; i++) { arr.add(nextInt()); } return arr; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
5173a6db7f6fcbee4f89d9151b0d86e9
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
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){ t--; int n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } String s=sc.next(); int nb=0; Map<Integer,Integer> mb=new HashMap<>(); Map<Integer,Integer> mr=new HashMap<>(); for(int i=0;i<n;i++){ char c=s.charAt(i); if(c=='B'){nb++;}} for(int i=0;i<n;i++){ char c=s.charAt(i); if(c=='B'){ int e=Math.min(a[i],nb); if(!mb.containsKey(e)){ mb.put(e,1); } else{ mb.put(e,mb.get(e)+1); } } else{ int e=Math.max(a[i],nb+1); if(!mr.containsKey(e)){ mr.put(e,1); } else{ mr.put(e,mr.get(e)+1); } } } boolean f=true; int inc=0; for(int i=nb;i>=1;i--){ if(!mb.containsKey(i)&&inc==0){ f=false; break; } if(!mb.containsKey(i)){ inc--; } else{ inc=inc+mb.get(i)-1; } } if(f){ inc=0; for(int i=nb+1;i<=n;i++){ if(!mr.containsKey(i)&&inc==0){ f=false; break; } if(!mr.containsKey(i)){ inc--; } else{ inc=inc+mr.get(i)-1; } } } if(f){ System.out.println("YES"); } else{ System.out.println("NO"); }}}}
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
0f4846dbad350821f051b30113abebf1
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.math.*; //import java.io.*; public class Experiment { static Scanner in=new Scanner(System.in); static int global=0; // static BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); static class Pair implements Comparable<Pair>{ String s;char ch;int swap; Pair(String s,char ch,int swap){ this.s=s; this.ch=ch; this.swap=swap; } public int compareTo(Pair o){ if(swap%2==0) return o.ch-this.ch; else return this.ch-o.ch; // Sort return this.first-o.first; pair on the basis of first parameter in ascending order // return o.first-this.first to sort on basis of first parameter in descending order // return this.second -o.second to Sort pair on the basis of second parameter in ascending order //return o.second-this.second to sort on basis of second parameter in descending order } } public static void sort(int ar[],int l,int r) { if(l<r) { int m=l+(r-l)/2; sort(ar,l,m); sort(ar,m+1,r); merge(ar,l,m,r); } } public static int hcf(int x,int y) { if(y==0) return x; return hcf(y,x%y); } public static void merge(int ar[],int l,int m,int r) { int n1=m-l+1;int n2=r-m; int L[]=new int[n1]; int R[]=new int[n2]; for(int i=0;i<n1;i++) L[i]=ar[l+i]; for(int i=0;i<n2;i++) R[i]=ar[m+i+1]; int i=0;int j=0;int k=l; while(i<n1 && j<n2) { if(L[i]>=R[j]) { ar[k++]=L[i++]; }else { ar[k++]=R[j++]; } } while(i<n1) ar[k++]=L[i++]; while(j<n2) ar[k++]=R[j++]; } public static int[] sort(int ar[]) { sort(ar,0,ar.length-1); //Array.sort uses O(n^2) in its worst case ,so better use merge sort return ar; } public static long rec(int n,int ar[],int start) { if(start>=n) { return 0; } long max=Integer.MIN_VALUE; // if(dp[start]!=-1) // return dp[start]; for(int i=start;i<n;i++) { int ele=ar[i]; long sum= ele+rec(n, ar, start+ele); if(sum>max)max=sum; } return max; } public static void func() { int n=in.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=in.nextInt(); String st=in.next(); ArrayList<Integer> blue=new ArrayList<Integer>(); ArrayList<Integer> red=new ArrayList<Integer>(); for(int i=0;i<n;i++) { if(st.charAt(i)=='R') red.add(arr[i]); else blue.add(arr[i]); } Collections.sort(red);Collections.sort(blue); if((red.size()!=0 && blue.size()!=0) && (red.get(red.size()-1)>n || blue.get(0)<1)) { System.out.println("NO");return; } int count=0;int i=0; for( i=1;i<=n;i++) { if(i-1>=blue.size()) break; int ele=blue.get(i-1); if(ele>=i) { count++; } } for( int j=i;j<=n;j++) { if(j-i>=red.size()) break; int ele=red.get(j-i); if(ele<=j) { count++; } } if(count==(red.size()+blue.size())) System.out.println("YES"); else System.out.println("NO"); } public static void main(String[] args) { int t=in.nextInt(); while(t-->0) { func(); global=0; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
261e3c64106ee47fc58740a92725f335
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; import java.math.BigDecimal; import java.math.*; // public class Main{ public static void main(String[] args) { TaskA solver = new TaskA(); // boolean[]prime=seive(3*100001); int t = in.nextInt(); for (int i = 1; i <= t ; i++) { solver.solve(1, in, out); } // solver.solve(1, in, out); out.flush(); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n= in.nextInt(); int[]arr=new int[n]; ArrayList<Integer>red=new ArrayList<Integer>(); ArrayList<Integer>blue=new ArrayList<Integer>(); for(int i=0;i<n;i++) { arr[i]=in.nextInt(); } char[]c=in.next().toCharArray(); for(int i=0;i<n;i++) { if(c[i]=='R') {red.add(arr[i]);} else {blue.add(arr[i]);} } Collections.sort(red,Collections.reverseOrder());Collections.sort(blue); for(int i=1;i<=blue.size();i++) { if( blue.get(i-1)<i) { println("NO");return; } } for(int i=0;i<red.size();i++) { if(red.get(i)>n-i) { println("NO");return; } } println("YES"); } } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) System .out.print(arr[i] + " "); System.out.println(" "); } static int dis(int a,int b,int c,int d) { return Math.abs(c-a)+Math.abs(d-b); } static long ceil(long a,long b) { return (a/b + Math.min(a%b, 1)); } static char[] rev(char[]ans,int n) { for(int i=ans.length-1;i>=n;i--) { ans[i]=ans[ans.length-i-1]; } return ans; } static int countStep(int[]arr,long sum,int k,int count1) { int count=count1; int index=arr.length-1; while(sum>k&&index>0) { sum-=(arr[index]-arr[0]); count++; if(sum<=k) { break; } index--; // println("c"); } if(sum<=k) { return count; } else { return Integer.MAX_VALUE; } } static long pow(long b, long e) { long ans = 1; while (e > 0) { if (e % 2 == 1) ans = ans * b % mod; e >>= 1; b = b * b % mod; } return ans; } static void sortDiff(Pair arr[]) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.first==p2.first) { return (p1.second-p1.first)-(p2.second-p1.first); } return (p1.second-p1.first)-(p2.second-p2.first); } }); } static void sortF(Pair arr[]) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.first==p2.first) { return p1.second-p2.second; } return p1.first - p2.first; } }); } static int[] shift (int[]inp,int i,int j){ int[]arr=new int[j-i+1]; int n=j-i+1; for(int k=i;k<=j;k++) { arr[(k-i+1)%n]=inp[k]; } for(int k=0;k<n;k++ ) { inp[k+i]=arr[k]; } return inp; } static long[] fac; static long mod = (long) 1000000007; static void initFac(long n) { fac = new long[(int)n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = (fac[i - 1] * i) % mod; } } static long nck(int n, int k) { if (n < k) return 0; long den = inv((int) (fac[k] * fac[n - k] % mod)); return fac[n] * den % mod; } static long inv(long x) { return pow(x, mod - 2); } static void sort(int[] a) { ArrayList<Integer> q = new ArrayList<>(); for (int i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static void sort(long[] a) { ArrayList<Long> q = new ArrayList<>(); for (long i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } public static int bfsSize(int source,LinkedList<Integer>[]a,boolean[]visited) { Queue<Integer>q=new LinkedList<>(); q.add(source); visited[source]=true; int distance=0; while(!q.isEmpty()) { int curr=q.poll(); distance++; for(int neighbour:a[curr]) { if(!visited[neighbour]) { visited[neighbour]=true; q.add(neighbour); } } } return distance; } public static Set<Integer>factors(int n){ Set<Integer>ans=new HashSet<>(); ans.add(1); for(int i=2;i*i<=n;i++) { if(n%i==0) { ans.add(i); ans.add(n/i); } } return ans; } public static int bfsSp(int source,int destination,ArrayList<Integer>[]a) { boolean[]visited=new boolean[a.length]; int[]parent=new int[a.length]; Queue<Integer>q=new LinkedList<>(); int distance=0; q.add(source); visited[source]=true; parent[source]=-1; while(!q.isEmpty()) { int curr=q.poll(); if(curr==destination) { break; } for(int neighbour:a[curr]) { if(neighbour!=-1&&!visited[neighbour]) { visited[neighbour]=true; q.add(neighbour); parent[neighbour]=curr; } } } int cur=destination; while(parent[cur]!=-1) { distance++; cur=parent[cur]; } return distance; } static int bs(int size,int[]arr) { int x = -1; for (int b = size/2; b >= 1; b /= 2) { while (!ok(arr)); } int k = x+1; return k; } static boolean ok(int[]x) { return false; } public static int solve1(ArrayList<Integer> A) { long[]arr =new long[A.size()+1]; int n=A.size(); for(int i=1;i<=A.size();i++) { arr[i]=((i%2)*((n-i+1)%2))%2; arr[i]%=2; } int ans=0; for(int i=0;i<A.size();i++) { if(arr[i+1]==1) { ans^=A.get(i); } } return ans; } public static String printBinary(long a) { String str=""; for(int i=31;i>=0;i--) { if((a&(1<<i))!=0) { str+=1; } if((a&(1<<i))==0 && !str.isEmpty()) { str+=0; } } return str; } public static String reverse(long a) { long rev=0; String str=""; int x=(int)(Math.log(a)/Math.log(2))+1; for(int i=0;i<32;i++) { rev<<=1; if((a&(1<<i))!=0) { rev|=1; str+=1; } else { str+=0; } } return str; } //////////////////////////////////////////////////////// static void sortS(Pair arr[]) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return p1.second - p2.second; } }); } static class Pair implements Comparable<Pair> { int first ;int second ; public Pair(int x, int y) { this.first = x ;this.second = y ; } @Override public boolean equals(Object obj) { if(obj == this)return true ; if(obj == null)return false ; if(this.getClass() != obj.getClass()) return false ; Pair other = (Pair)(obj) ; if(this.first != other.first)return false ; if(this.second != other.second)return false ; return true ; } @Override public int hashCode() { return this.first^this.second ; } @Override public String toString() { String ans = "" ;ans += this.first ; ans += " "; ans += this.second ; return ans ; } @Override public int compareTo(Main.Pair o) { { if(this.first==o.first) { return this.second-o.second; } return this.first - o.first; } } } ////////////////////////////////////////////////////////////////////////// static int nD(long num) { String s=String.valueOf(num); return s.length(); } static int CommonDigits(int x,int y) { String s=String.valueOf(x); String s2=String.valueOf(y); return 0; } static int lcs(String str1, String str2, int m, int n) { int L[][] = new int[m + 1][n + 1]; int i, j; // Following steps build L[m+1][n+1] in // bottom up fashion. Note that L[i][j] // contains length of LCS of str1[0..i-1] // and str2[0..j-1] for (i = 0; i <= m; i++) { for (j = 0; j <= n; j++) { if (i == 0 || j == 0) L[i][j] = 0; else if (str1.charAt(i - 1) == str2.charAt(j - 1)) L[i][j] = L[i - 1][j - 1] + 1; else L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]); } } // L[m][n] contains length of LCS // for X[0..n-1] and Y[0..m-1] return L[m][n]; } ///////////////////////////////// boolean IsPowerOfTwo(int x) { return (x != 0) && ((x & (x - 1)) == 0); } //////////////////////////////// static long power(long a,long b,long m ) { long ans=1; while(b>0) { if(b%2==1) { ans=((ans%m)*(a%m))%m; b--; } else { a=(a*a)%m;b/=2; } } return ans%m; } /////////////////////////////// public static boolean repeatedSubString(String string) { return ((string + string).indexOf(string, 1) != string.length()); } static int search(char[]c,int start,int end,char x) { for(int i=start;i<end;i++) { if(c[i]==x) {return i;} } return -2; } //////////////////////////////// static long gcd(long a, long b) { while (b != 0) { long t = b; b = a % b; a = t; } return a; } static long fac(long a) { if(a== 0L || a==1L)return 1L ; return a*fac(a-1L) ; } static ArrayList al() { ArrayList<Integer>a =new ArrayList<>(); return a; } static HashSet h() { return new HashSet<Integer>(); } static void debug(int[][]a) { int n= a.length; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { out.print(a[i][j]+" "); } out.print("\n"); } } static void debug(int[]a) { out.println(Arrays.toString(a)); } static void debug(ArrayList<Integer>a) { out.println(a.toString()); } static boolean[]seive(int n){ boolean[]b=new boolean[n+1]; for (int i = 2; i <= n; i++) b[i] = true; for(int i=2;i*i<=n;i++) { if(b[i]) { for(int j=i*i;j<=n;j+=i) { b[j]=false; } } } return b; } static int longestIncreasingSubseq(int[]arr) { int[]sizes=new int[arr.length]; Arrays.fill(sizes, 1); int max=1; for(int i=1;i<arr.length;i++) { for(int j=0;j<i;j++) { if(arr[j]<arr[i]) { sizes[i]=Math.max(sizes[i],sizes[j]+1); max=Math.max(max, sizes[i]); } } } return max; } public static ArrayList primeFactors(long n) { ArrayList<Long>h= new ArrayList<>(); // Print the number of 2s that divide n if(n%2 ==0) {h.add(2L);} // n must be odd at this point. So we can // skip one element (Note i = i +2) for (long i = 3; i <= Math.sqrt(n); i+= 2) { if(n%i==0) {h.add(i);} } if (n > 2) h.add(n); return h; } static boolean Divisors(long n){ if(n%2==1) { return true; } for (long i=2; i<=Math.sqrt(n); i++){ if (n%i==0 && i%2==1){ return true; } } return false; } static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(outputStream); public static void superSet(int[]a,ArrayList<String>al,int i,String s) { if(i==a.length) { al.add(s); return; } superSet(a,al,i+1,s); superSet(a,al,i+1,s+a[i]+" "); } public static long[] makeArr() { long size=in.nextInt(); long []arr=new long[(int)size]; for(int i=0;i<size;i++) { arr[i]=in.nextInt(); } return arr; } public static long[] arr(int n) { long []arr=new long[n+1]; for(int i=1;i<n+1;i++) { arr[i]=in.nextLong(); } return arr; } public static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } static void println(long x) { out.println(x); } static void print(long c) { out.print(c); } static void print(int c) { out.print(c); } static void println(int x) { out.println(x); } static void print(String s) { out.print(s); } static void println(String s) { out.println(s); } public static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } public static void reverse(int[] array) { int n = array.length; for (int i = 0; i < n / 2; i++) { int temp = array[i]; array[i] = array[n - i - 1]; array[n - i - 1] = temp; } } public static long nCr(int n,int k) { long ans=1L; k=k>n-k?n-k:k; for( int j=1;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } static int searchMax(int index,long[]inp) { while(index+1<inp.length &&inp[index+1]>inp[index]) { index+=1; } return index; } static int searchMin(int index,long[]inp) { while(index+1<inp.length &&inp[index+1]<inp[index]) { index+=1; } return index; } public class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } static class Pairr implements Comparable<Pairr>{ private int index; private int cumsum; private ArrayList<Integer>indices; public Pairr(int index,int cumsum) { this.index=index; this.cumsum=cumsum; indices=new ArrayList<Integer>(); } public int compareTo(Pairr other) { return Integer.compare(cumsum, other.cumsum); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
34f1689351e783f8f6b2e09a5a74cc26
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import javax.management.MBeanRegistration; import java.io.*; import java.util.*; public class Main { private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private FastWriter wr; private Reader rd; public final int MOD = 1000000007; /************************************************** FAST INPUT IMPLEMENTATION *********************************************/ class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader( new InputStreamReader(System.in)); } public Reader(String path) throws FileNotFoundException { br = new BufferedReader( new InputStreamReader(new FileInputStream(path))); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public int ni() throws IOException { return rd.nextInt(); } public long nl() throws IOException { return rd.nextLong(); } char nc() throws IOException { return rd.next().charAt(0); } public String ns() throws IOException { return rd.nextLine(); } public Double nd() throws IOException { return rd.nextDouble(); } public int[] nai(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } public long[] nal(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nl(); } return arr; } /************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/ public static class FastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter() { out = null; } public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastWriter write(char c) { return write((byte) c); } public FastWriter write(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if (x == Integer.MIN_VALUE) { return write((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if (x < 0) { write('-'); x = -x; } x += Math.pow(10, -precision) / 2; // if(x < 0){ x = 0; } write((long) x).write("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; write((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastWriter writeln(char c) { return write(c).writeln(); } public FastWriter writeln(int x) { return write(x).writeln(); } public FastWriter writeln(long x) { return write(x).writeln(); } public FastWriter writeln(double x, int precision) { return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for (int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for (long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte) '\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for (char[] line : map) write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c) { return writeln(c); } public FastWriter println(int x) { return writeln(x); } public FastWriter println(long x) { return writeln(x); } public FastWriter println(double x, int precision) { return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } /********************************************************* USEFUL CODE **************************************************/ boolean[] SAPrimeGenerator(int n) { // TC-N*LOG(LOG N) //Create Prime Marking Array and fill it with true value boolean[] primeMarker = new boolean[n + 1]; Arrays.fill(primeMarker, true); primeMarker[0] = false; primeMarker[1] = false; for (int i = 2; i <= n; i++) { if (primeMarker[i]) { // we start from 2*i because i*1 must be prime for (int j = 2 * i; j <= n; j += i) { primeMarker[j] = false; } } } return primeMarker; } private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } class Pair<F, S, K> { private F first; private S second; private K third; Pair(F first, S second, K third) { this.first = first; this.second = second; this.third = third; } public F getFirst() { return first; } public S getSecond() { return second; } @Override public String toString() { return "Pair{" + "first=" + first + ", second=" + second + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<F, S, K> pair = (Pair<F, S, K>) o; return first == pair.first && second == pair.second && third == pair.third; } @Override public int hashCode() { return Objects.hash(first, second, third); } } public static void main(String[] args) throws IOException { new Main().run(); } public void run() throws IOException { if (oj) { rd = new Reader(); wr = new FastWriter(System.out); } else { File input = new File("input.txt"); File output = new File("output.txt"); if (input.exists() && output.exists()) { rd = new Reader(input.getPath()); wr = new FastWriter(output.getPath()); } else { rd = new Reader(); wr = new FastWriter(System.out); oj = true; } } long s = System.currentTimeMillis(); solve(); wr.flush(); tr(System.currentTimeMillis() - s + "ms"); } /*************************************************************************************************************************** *********************************************************** MAIN CODE ****************************************************** ****************************************************************************************************************************/ boolean[] sieve; public void solve() throws IOException { int t = 1; t = ni(); while (t-- > 0) { go(); } } /********************************************************* MAIN LOGIC HERE ****************************************************/ public void go() throws IOException { int n=ni(); int[] arr=nai(n); String st=ns(); boolean flag=true; ArrayList<Integer> red=new ArrayList<>(); ArrayList<Integer> blue=new ArrayList<>(); for(int i=0;i<n;i++){ if(st.charAt(i)=='R'){ red.add(arr[i]); }else { blue.add(arr[i]); } } Collections.sort(red,Collections.reverseOrder()); Collections.sort(blue); int val=n; for(int i=0;i<red.size();i++){ if(red.get(i)<=val){ val--; }else { flag=false; break; } } val=1; for(int i=0;i<blue.size();i++){ if(blue.get(i)>=val){ val++; }else { flag=false; break; } } if(flag){ wr.println("YES"); }else { wr.println("NO"); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
472f9162ae46e6b2cf30f565df044e98
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class P1607C { public static void main(String[] args) { Scanner read = new Scanner(System.in); int t = read.nextInt(); for (int it = 0; it < t; it++) { int n = read.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = read.nextInt(); } char[] c = read.next().toCharArray(); int b = 0; int r = 0; for (char ch : c) { if (ch == 'B') { b++; } else { r++; } } int[] blues = new int[b]; int[] reds = new int[r]; b = 0; r = 0; for (int i = 0; i < n; i++) { if (c[i] == 'B') { blues[b] = a[i]; b++; } else { reds[r] = a[i]; r++; } } Arrays.sort(blues); Arrays.sort(reds); boolean bool = true; for (int i = b - 1; i >= 0; i--) { if (blues[i] <= i) { bool = false; break; } } for (int i = 0; i < r; i++) { if (reds[i] > i + b + 1) { bool = false; break; } } if (bool) { System.out.println("YES"); } else { System.out.println("NO"); } } read.close(); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
07e5ca5e292c1c1970316172a91957c2
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
//package practice; import java.io.*;import java.util.*; public class Main { public static void main(String[] args) throws IOException { FastReader fr=new FastReader(); int tt=1; tt=fr.nextInt(); while(tt-->0) { int n=fr.nextInt(); long a[]=new long[n]; for(int i=0;i<n;i++) { a[i]=fr.nextLong(); } String s=fr.next(); List<Long> b=new ArrayList<>(); List<Long> r=new ArrayList<>(); for(int i=0;i<s.length();i++) { if(s.charAt(i)=='B') b.add(a[i]); else r.add(a[i]); } Collections.sort(b); Collections.sort(r,Collections.reverseOrder()); boolean ans=true; for(int i=0;i<b.size();i++) { if(b.get(i)<(i+1)) { ans=false; break; } } for(int i=0;i<r.size();i++) { if(r.get(i)>(n-i)) { ans=false; break; } } if(ans) { System.out.println("YES"); } else { System.out.println("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
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 11
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
1723c30c29a9c7f9ef7efb80237d3032
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; /** * * @author eslam */ public class Solution { // Beginning of the solution static Kattio input = new Kattio(); static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); static ArrayList<ArrayList<Integer>> powerSet = new ArrayList<>(); static ArrayList<LinkedList<Integer>> allprem = new ArrayList<>(); static ArrayList<LinkedList<String>> allprems = new ArrayList<>(); static ArrayList<Long> luc = new ArrayList<>(); static long mod = (long) (1e9 + 7); static int grid[][] = {{0, 0, 1, -1, 1, 1, -1, -1}, {1, -1, 0, 0, 1, -1, 1, -1}}; static long dp[][][]; static double cmp = 1e-7; static final double pi = 3.14159265359; static boolean c = true; public static void main(String[] args) throws IOException { // Kattio input = new Kattio("input"); // BufferedWriter log = new BufferedWriter(new FileWriter(f)); int test = input.nextInt(); loop: for (int co = 1; co <= test; co++) { int n = input.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = input.nextInt(); } TreeSet<Integer> s1 = new TreeSet<>(); TreeSet<Integer> s2 = new TreeSet<>(); HashMap<Integer, Integer> h1 = new HashMap<>(); HashMap<Integer, Integer> h2 = new HashMap<>(); String b = input.next(); for (int i = 0; i < n; i++) { if (b.charAt(i) == 'B') { s2.add(a[i]); h2.put(a[i], h2.get(a[i]) == null ? 1 : h2.get(a[i]) + 1); } else { s1.add(a[i]); h1.put(a[i], h1.get(a[i]) == null ? 1 : h1.get(a[i]) + 1); } } for (int i = 1; i <= n; i++) { Integer ce = s2.ceiling(i); if (ce != null) { h2.put(ce, h2.get(ce) - 1); if (h2.get(ce) == 0) { s2.remove(ce); } } else { ce = s1.floor(i); if (ce == null) { log.write("NO\n"); continue loop; } h1.put(ce, h1.get(ce) - 1); if (h1.get(ce) == 0) { s1.remove(ce); } } } log.write("YES\n"); } log.flush(); } static int bs1(ArrayList<Integer> a, int v) { int max = a.size() - 1; int min = 0; int ans = -1; while (max >= min) { int mid = (max + min) / 2; if (a.get(mid) > v) { max = mid - 1; } else { min = mid + 1; ans = mid; } } return ans; } static int bs2(ArrayList<Integer> a, int v) { int max = a.size() - 1; int min = 0; int ans = a.size(); while (max >= min) { int mid = (max + min) / 2; if (a.get(mid) < v) { min = mid + 1; } else { max = mid - 1; ans = mid; } } return ans; } static class temp { int x; int y; int z; int ind; public temp(int x, int y, int z, int ind) { this.x = x; this.y = y; this.z = z; this.ind = ind; } public String toString() { return x + " " + " " + y + " " + z + " " + ind; } } static long f(long x) { return (long) ((x * (x + 1) / 2) % mod); } static long Sub(long x, long y) { long z = x - y; if (z < 0) { z += mod; } return z; } static long add(long a, long b) { a += b; if (a >= mod) { a -= mod; } return a; } static long mul(long a, long b) { return (long) ((long) a * b % mod); } static long powlog(long base, long power) { if (power == 0) { return 1; } long x = powlog(base, power / 2); x = mul(x, x); if ((power & 1) == 1) { x = mul(x, base); } return x; } static long modinv(long x) { return fast_pow(x, mod - 2, mod); } static long Div(long x, long y) { return mul(x, modinv(y)); } static Comparator<temp> cmpTemp() { Comparator<temp> c = new Comparator<temp>() { @Override public int compare(temp o1, temp o2) { if (o1.x > o2.x) { return 1; } else if (o1.x < o2.x) { return -1; } else { if (o1.y > o2.y) { return 1; } else if (o1.y < o2.y) { return -1; } else { if (o1.z > o2.z) { return 1; } else if (o1.z < o2.z) { return -1; } else { if (o1.ind > o2.ind) { return 1; } else if (o1.ind < o2.ind) { return -1; } else { return 0; } } } } } }; return c; } static void floodFill(int r, int c, int a[][], boolean vi[][], int w[][], int d) { vi[r][c] = true; for (int i = 0; i < 4; i++) { int nr = grid[0][i] + r; int nc = grid[1][i] + c; if (isValid(nr, nc, a.length, a[0].length)) { if (Math.abs(a[r][c] - a[nr][nc]) <= d && !vi[nr][nc]) { floodFill(nr, nc, a, vi, w, d); } } } } static boolean isdigit(char ch) { return ch >= '0' && ch <= '9'; } static boolean lochar(char ch) { return ch >= 'a' && ch <= 'z'; } static boolean cachar(char ch) { return ch >= 'A' && ch <= 'Z'; } static class Pa { long x; long y; public Pa(long x, long y) { this.x = x; this.y = y; } } static long sqrt(long v) { long max = (long) 4e9; long min = 0; long ans = 0; while (max >= min) { long mid = (max + min) / 2; if (mid * mid > v) { max = mid - 1; } else { ans = mid; min = mid + 1; } } return ans; } static long cbrt(long v) { long max = (long) 3e6; long min = 0; long ans = 0; while (max >= min) { long mid = (max + min) / 2; if (mid * mid > v) { max = mid - 1; } else { ans = mid; min = mid + 1; } } return ans; } static void prefixSum2D(long arr[][]) { for (int i = 0; i < arr.length; i++) { prefixSum(arr[i]); } for (int i = 0; i < arr[0].length; i++) { for (int j = 1; j < arr.length; j++) { arr[j][i] += arr[j - 1][i]; } } } public static long baseToDecimal(String w, long base) { long r = 0; long l = 0; for (int i = w.length() - 1; i > -1; i--) { long x = (w.charAt(i) - '0') * (long) Math.pow(base, l); r = r + x; l++; } return r; } static int bs(int v, ArrayList<Integer> a) { int max = a.size() - 1; int min = 0; int ans = 0; while (max >= min) { int mid = (max + min) / 2; if (a.get(mid) >= v) { ans = a.size() - mid; max = mid - 1; } else { min = mid + 1; } } return ans; } static Comparator<tri> cmpTri() { Comparator<tri> c = new Comparator<tri>() { @Override public int compare(tri o1, tri o2) { if (o1.x > o2.x) { return 1; } else if (o1.x < o2.x) { return -1; } else { if (o1.y > o2.y) { return 1; } else if (o1.y < o2.y) { return -1; } else { if (o1.z > o2.z) { return 1; } else if (o1.z < o2.z) { return -1; } else { return 0; } } } } }; return c; } static Comparator<pair> cmpPair2() { Comparator<pair> c = new Comparator<pair>() { @Override public int compare(pair o1, pair o2) { if (o1.y > o2.y) { return 1; } else if (o1.y < o2.y) { return -1; } else { if (o1.x > o2.x) { return -1; } else if (o1.x < o2.x) { return 1; } else { return 0; } } } }; return c; } static Comparator<pair> cmpPair() { Comparator<pair> c = new Comparator<pair>() { @Override public int compare(pair o1, pair o2) { if (o1.x > o2.x) { return -1; } else if (o1.x < o2.x) { return 1; } else { if (o1.y > o2.y) { return 1; } else if (o1.y < o2.y) { return -1; } else { return 0; } } } }; return c; } static class rec { long x1; long x2; long y1; long y2; public rec(long x1, long y1, long x2, long y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } public long getArea() { return (x2 - x1) * (y2 - y1); } } static long sumOfRange(int x1, int y1, int x2, int y2, long a[][]) { return (a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1]) + a[x1 - 1][y1 - 1]; } public static int[][] bfs(int i, int j, String w[]) { Queue<pair> q = new ArrayDeque<>(); q.add(new pair(i, j)); int dis[][] = new int[w.length][w[0].length()]; for (int k = 0; k < w.length; k++) { Arrays.fill(dis[k], -1); } dis[i][j] = 0; while (!q.isEmpty()) { pair p = q.poll(); int cost = dis[p.x][p.y]; for (int k = 0; k < 4; k++) { int nx = p.x + grid[0][k]; int ny = p.y + grid[1][k]; if (isValid(nx, ny, w.length, w[0].length())) { if (dis[nx][ny] == -1 && w[nx].charAt(ny) == '.') { q.add(new pair(nx, ny)); dis[nx][ny] = cost + 1; } } } } return dis; } public static void graphRepresintion(ArrayList<Integer>[] a, int q) throws IOException { for (int i = 0; i < a.length; i++) { a[i] = new ArrayList<>(); } while (q-- > 0) { int x = input.nextInt(); int y = input.nextInt(); a[x].add(y); a[y].add(x); } } public static boolean isValid(int i, int j, int n, int m) { return (i > -1 && i < n) && (j > -1 && j < m); } // present in the left and right indices public static int[] swap(int data[], int left, int right) { // Swap the data int temp = data[left]; data[left] = data[right]; data[right] = temp; // Return the updated array return data; } // Function to reverse the sub-array // starting from left to the right // both inclusive public static int[] reverse(int data[], int left, int right) { // Reverse the sub-array while (left < right) { int temp = data[left]; data[left++] = data[right]; data[right--] = temp; } // Return the updated array return data; } // Function to find the next permutation // of the given integer array public static boolean findNextPermutation(int data[]) { // If the given dataset is empty // or contains only one element // next_permutation is not possible if (data.length <= 1) { return false; } int last = data.length - 2; // find the longest non-increasing suffix // and find the pivot while (last >= 0) { if (data[last] < data[last + 1]) { break; } last--; } // If there is no increasing pair // there is no higher order permutation if (last < 0) { return false; } int nextGreater = data.length - 1; // Find the rightmost successor to the pivot for (int i = data.length - 1; i > last; i--) { if (data[i] > data[last]) { nextGreater = i; break; } } // Swap the successor and the pivot data = swap(data, nextGreater, last); // Reverse the suffix data = reverse(data, last + 1, data.length - 1); // Return true as the next_permutation is done return true; } // public static pair[] dijkstra(int node, ArrayList<pair> a[]) { // PriorityQueue<tri> q = new PriorityQueue<>(new Comparator<tri>() { // @Override // public int compare(tri o1, tri o2) { // if (o1.y > o2.y) { // return 1; // } else if (o1.y < o2.y) { // return -1; // } else { // return 0; // } // } // }); // q.add(new tri(node, 0, -1)); // pair distance[] = new pair[a.length]; // while (!q.isEmpty()) { // tri p = q.poll(); // int cost = p.y; // if (distance[p.x] != null) { // continue; // } // distance[p.x] = new pair(p.z, cost); // ArrayList<pair> nodes = a[p.x]; // for (pair node1 : nodes) { // if (distance[node1.x] == null) { // tri pa = new tri(node1.x, cost + node1.y, p.x); // q.add(pa); // } // } // } // return distance; // } public static String revs(String w) { String ans = ""; for (int i = w.length() - 1; i > -1; i--) { ans += w.charAt(i); } return ans; } public static boolean isPalindrome(String w) { for (int i = 0; i < w.length() / 2; i++) { if (w.charAt(i) != w.charAt(w.length() - i - 1)) { return false; } } return true; } public static void getPowerSet(Queue<Integer> a) { int n = a.poll(); if (!a.isEmpty()) { getPowerSet(a); } int s = powerSet.size(); for (int i = 0; i < s; i++) { ArrayList<Integer> ne = new ArrayList<>(); ne.add(n); for (int j = 0; j < powerSet.get(i).size(); j++) { ne.add(powerSet.get(i).get(j)); } powerSet.add(ne); } ArrayList<Integer> p = new ArrayList<>(); p.add(n); powerSet.add(p); } public static int getlo(int va) { int v = 1; while (v <= va) { if ((va&v) != 0) { return v; } v <<= 1; } return 0; } static long fast_pow(long a, long p, long mod) { long res = 1; while (p > 0) { if (p % 2 == 0) { a = (a * a) % mod; p /= 2; } else { res = (res * a) % mod; p--; } } return res; } public static int countPrimeInRange(int n, boolean isPrime[]) { int cnt = 0; Arrays.fill(isPrime, true); for (int i = 2; i * i <= n; i++) { if (isPrime[i]) { for (int j = i * 2; j <= n; j += i) { isPrime[j] = false; } } } for (int i = 2; i <= n; i++) { if (isPrime[i]) { cnt++; } } return cnt; } public static void create(long num) { luc.add(num); if (num > power(10, 9)) { return; } create(num * 10 + 4); create(num * 10 + 7); } public static long ceil(long a, long b) { return (a + b - 1) / b; } public static long round(long a, long b) { if (a < 0) { return (a - b / 2) / b; } return (a + b / 2) / b; } public static void allPremutationsst(LinkedList<String> l, boolean visited[], ArrayList<String> st) { if (l.size() == st.size()) { allprems.add(l); } for (int i = 0; i < st.size(); i++) { if (!visited[i]) { visited[i] = true; LinkedList<String> nl = new LinkedList<>(); for (String x : l) { nl.add(x); } nl.add(st.get(i)); allPremutationsst(nl, visited, st); visited[i] = false; } } } public static void allPremutations(LinkedList<Integer> l, boolean visited[], int a[]) { if (l.size() == a.length) { allprem.add(l); } for (int i = 0; i < a.length; i++) { if (!visited[i]) { visited[i] = true; LinkedList<Integer> nl = new LinkedList<>(); for (Integer x : l) { nl.add(x); } nl.add(a[i]); allPremutations(nl, visited, a); visited[i] = false; } } } public static int binarySearch(long[] a, long value) { int l = 0; int r = a.length - 1; while (l <= r) { int m = (l + r) / 2; if (a[m] == value) { return m; } else if (a[m] > value) { r = m - 1; } else { l = m + 1; } } return -1; } public static void reverse(int l, int r, char ch[]) { for (int i = 0; i < r / 2; i++) { char c = ch[i]; ch[i] = ch[r - i - 1]; ch[r - i - 1] = c; } } public static long logK(long v, long k) { int ans = 0; while (v > 1) { ans++; v /= k; } return ans; } public static long power(long a, long p) { long res = 1; while (p > 0) { if (p % 2 == 0) { a = (a * a); p /= 2; } else { res = (res * a); p--; } } return res; } public static long get(long max, long x) { if (x == 1) { return max; } int cnt = 0; while (max > 0) { cnt++; max /= x; } return cnt; } public static int numOF0(long v) { long x = 1; int cnt = 0; while (x <= v) { if ((x & v) == 0) { cnt++; } x <<= 1; } return cnt; } public static int log2(double n) { int cnt = 0; while (n > 1) { n /= 2; cnt++; } return cnt; } public static int[] bfs(int node, ArrayList<Integer> a[]) { Queue<Integer> q = new LinkedList<>(); q.add(node); int distances[] = new int[a.length]; Arrays.fill(distances, -1); distances[node] = 0; while (!q.isEmpty()) { int parent = q.poll(); ArrayList<Integer> nodes = a[parent]; int cost = distances[parent]; for (Integer node1 : nodes) { if (distances[node1] == -1) { q.add(node1); distances[node1] = cost + 1; } } } return distances; } public static void primeFactors(int n, HashMap<Integer, ArrayList<Integer>> h, int ind) { boolean ca = true; while (n % 2 == 0) { if (ca) { if (h.get(2) == null) { h.put(2, new ArrayList<>()); } h.get(2).add(ind); ca = false; } n /= 2; } for (int i = 3; i <= Math.sqrt(n); i += 2) { ca = true; while (n % i == 0) { if (ca) { if (h.get(i) == null) { h.put(i, new ArrayList<>()); } h.get(i).add(ind); ca = false; } n /= i; } if (n < i) { break; } } if (n > 2) { if (h.get(n) == null) { h.put(n, new ArrayList<>()); } h.get(n).add(ind); } } // end of solution public static BigInteger ff(long n) { if (n <= 1) { return BigInteger.ONE; } long t = n - 1; BigInteger b = new BigInteger(t + ""); BigInteger ans = new BigInteger(n + ""); while (t > 1) { ans = ans.multiply(b); b = b.subtract(BigInteger.ONE); t--; } return ans; } public static long factorial(long n) { if (n <= 1) { return 1; } long t = n - 1; while (t > 1) { n = mod((mod(n, mod) * mod(t, mod)), mod); t--; } return n; } public static long rev(long n) { long t = n; long ans = 0; while (t > 0) { ans = ans * 10 + t % 10; t /= 10; } return ans; } public static boolean isPalindrome(int n) { int t = n; int ans = 0; while (t > 0) { ans = ans * 10 + t % 10; t /= 10; } return ans == n; } static class tri { int x, y; long z; public tri(int x, int y, long z) { this.x = x; this.y = y; this.z = z; } @Override public String toString() { return x + " " + y + " " + z; } } static boolean isPrime(long num) { if (num == 1) { return false; } if (num == 2) { return true; } if (num % 2 == 0) { return false; } if (num == 3) { return true; } for (long i = 3; i * i <= num; i += 2) { if (num % i == 0) { return false; } } return true; } public static void prefixSum(long[] a) { for (int i = 1; i < a.length; i++) { a[i] = a[i] + a[i - 1]; } } public static void suffixSum(long[] a) { for (int i = a.length - 2; i > -1; i--) { a[i] = a[i] + a[i + 1]; } } static long mod(long a, long b) { long r = a % b; return r < 0 ? r + b : r; } public static long binaryToDecimal(String w) { long r = 0; long l = 0; for (int i = w.length() - 1; i > -1; i--) { long x = (w.charAt(i) - '0') * (long) Math.pow(2, l); r = r + x; l++; } return r; } public static String decimalAnyBase(long n, long base) { String w = ""; while (n > 0) { w = n % base + w; n /= base; } return w; } public static String decimalToBinary(long n) { String w = ""; while (n > 0) { w = n % 2 + w; n /= 2; } return w; } public static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } public static void print(long[] a) throws IOException { for (int i = 0; i < a.length; i++) { log.write(a[i] + " "); } log.write("\n"); } public static void read(int[] a) { for (int i = 0; i < a.length; i++) { a[i] = input.nextInt(); } } static class gepair { long x; long y; public gepair(long x, long y) { this.x = x; this.y = y; } @Override public String toString() { return x + " " + y; } } static class pair { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } @Override public String toString() { return x + " " + y; } } static class pai { long x; int y; public pai(long x, int y) { this.x = x; this.y = y; } @Override public String toString() { return x + " " + y; } } public static long LCM(long x, long y) { return x / GCD(x, y) * y; } public static long GCD(long x, long y) { if (y == 0) { return x; } return GCD(y, x % y); } public static void simplifyTheFraction(int a, int b) { long GCD = GCD(a, b); System.out.println(a / GCD + " " + b / GCD); } 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)); } // returns null if no more input String nextLine() { String str = ""; try { str = r.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public String next() { try { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(r.readLine()); } return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 17
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
d39284f60fd9dc8700fff30ea2ff990d
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.util.function.*; import java.io.*; // you can compare with output.txt and expected out public class Round753D { MyPrintWriter out; MyScanner in; // final static long FIXED_RANDOM; // static { // FIXED_RANDOM = System.currentTimeMillis(); // } final static String IMPOSSIBLE = "IMPOSSIBLE"; final static String POSSIBLE = "POSSIBLE"; final static String YES = "YES"; final static String NO = "NO"; private void initIO(boolean isFileIO) { if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) { try{ in = new MyScanner(new FileInputStream("input.txt")); out = new MyPrintWriter(new FileOutputStream("output.txt")); } catch(FileNotFoundException e){ e.printStackTrace(); } } else{ in = new MyScanner(System.in); out = new MyPrintWriter(new BufferedOutputStream(System.out)); } } public static void main(String[] args){ // Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); // the code for the deep recursion (more than 100k or 3~400k or so) // Thread t = new Thread(null, new RoundEdu130F(), "threadName", 1<<28); // t.start(); // t.join(); Round753D sol = new Round753D(); sol.run(); } private void run() { boolean isDebug = false; boolean isFileIO = true; boolean hasMultipleTests = true; initIO(isFileIO); int t = hasMultipleTests? in.nextInt() : 1; for (int i = 1; i <= t; ++i) { if(isDebug){ out.printf("Test %d\n", i); } getInput(); solve(); printOutput(); } in.close(); out.close(); } // use suitable one between matrix(2, n), matrix(n, 2), transposedMatrix(2, n) for graph edges, pairs, ... int n; int[] a; String s; void getInput() { n = in.nextInt(); a = in.nextIntArray(n); s = in.next(); } void printOutput() { out.printlnAns(ans); } boolean ans; void solve(){ ArrayList<Integer> reds = new ArrayList<>(); ArrayList<Integer> blues = new ArrayList<>(); for(int i=0; i<n; i++) { if(s.charAt(i) == 'R') reds.add(a[i]); else blues.add(a[i]); } Collections.sort(reds, Collections.reverseOrder()); Collections.sort(blues); int ub = n; for(int i=0; i<reds.size(); i++) { if(reds.get(i) > ub) { ans = false; return; } ub--; } int lb = 1; for(int i=0; i<blues.size(); i++) { if(blues.get(i) < lb) { ans = false; return; } lb++; } ans = true; } // Optional<T> solve() // return Optional.empty(); static class Pair implements Comparable<Pair>{ final static long FIXED_RANDOM = System.currentTimeMillis(); int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } @Override public int hashCode() { // http://xorshift.di.unimi.it/splitmix64.c long x = first; x <<= 32; x += second; x += FIXED_RANDOM; x += 0x9e3779b97f4a7c15l; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9l; x = (x ^ (x >> 27)) * 0x94d049bb133111ebl; return (int)(x ^ (x >> 31)); } @Override public boolean equals(Object obj) { // if (this == obj) // return true; // if (obj == null) // return false; // if (getClass() != obj.getClass()) // return false; Pair other = (Pair) obj; return first == other.first && second == other.second; } @Override public String toString() { return "[" + first + "," + second + "]"; } @Override public int compareTo(Pair o) { int cmp = Integer.compare(first, o.first); return cmp != 0? cmp: Integer.compare(second, o.second); } } public static class MyScanner { BufferedReader br; StringTokenizer st; // 32768? public MyScanner(InputStream is, int bufferSize) { br = new BufferedReader(new InputStreamReader(is), bufferSize); } public MyScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); // br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt"))); } public void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[][] nextMatrix(int n, int m) { return nextMatrix(n, m, 0); } int[][] nextMatrix(int n, int m, int offset) { int[][] mat = new int[n][m]; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { mat[i][j] = nextInt()+offset; } } return mat; } int[][] nextTransposedMatrix(int n, int m){ return nextTransposedMatrix(n, m, 0); } int[][] nextTransposedMatrix(int n, int m, int offset){ int[][] mat = new int[m][n]; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { mat[j][i] = nextInt()+offset; } } return mat; } int[] nextIntArray(int len) { return nextIntArray(len, 0); } int[] nextIntArray(int len, int offset){ int[] a = new int[len]; for(int j=0; j<len; j++) a[j] = nextInt()+offset; return a; } long[] nextLongArray(int len) { return nextLongArray(len, 0); } long[] nextLongArray(int len, int offset){ long[] a = new long[len]; for(int j=0; j<len; j++) a[j] = nextLong()+offset; return a; } String[] nextStringArray(int len) { String[] s = new String[len]; for(int i=0; i<len; i++) s[i] = next(); return s; } } public static class MyPrintWriter extends PrintWriter{ public MyPrintWriter(OutputStream os) { super(os); } // public <T> void printlnAns(Optional<T> ans) { // if(ans.isEmpty()) // println(-1); // else // printlnAns(ans.get()); // } public void printlnAns(OptionalInt ans) { println(ans.orElse(-1)); } public void printlnAns(long ans) { println(ans); } public void printlnAns(int ans) { println(ans); } public void printlnAns(boolean[] ans) { for(boolean b: ans) printlnAns(b); } public void printlnAns(boolean ans) { if(ans) println(YES); else println(NO); } public void printAns(long[] arr){ if(arr != null && arr.length > 0){ print(arr[0]); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]); } } } public void printlnAns(long[] arr){ printAns(arr); println(); } public void printAns(int[] arr){ if(arr != null && arr.length > 0){ print(arr[0]); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]); } } } public void printlnAns(int[] arr){ printAns(arr); println(); } public <T> void printAns(ArrayList<T> arr){ if(arr != null && arr.size() > 0){ print(arr.get(0)); for(int i=1; i<arr.size(); i++){ print(" "); print(arr.get(i)); } } } public <T> void printlnAns(ArrayList<T> arr){ printAns(arr); println(); } public void printAns(int[] arr, int add){ if(arr != null && arr.length > 0){ print(arr[0]+add); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]+add); } } } public void printlnAns(int[] arr, int add){ printAns(arr, add); println(); } public void printAns(ArrayList<Integer> arr, int add) { if(arr != null && arr.size() > 0){ print(arr.get(0)+add); for(int i=1; i<arr.size(); i++){ print(" "); print(arr.get(i)+add); } } } public void printlnAns(ArrayList<Integer> arr, int add){ printAns(arr, add); println(); } public void printlnAnsSplit(long[] arr, int split){ if(arr != null){ for(int i=0; i<arr.length; i+=split){ print(arr[i]); for(int j=i+1; j<i+split; j++){ print(" "); print(arr[j]); } println(); } } } public void printlnAnsSplit(int[] arr, int split){ if(arr != null){ for(int i=0; i<arr.length; i+=split){ print(arr[i]); for(int j=i+1; j<i+split; j++){ print(" "); print(arr[j]); } println(); } } } public <T> void printlnAnsSplit(ArrayList<T> arr, int split){ if(arr != null && !arr.isEmpty()){ for(int i=0; i<arr.size(); i+=split){ print(arr.get(i)); for(int j=i+1; j<i+split; j++){ print(" "); print(arr.get(j)); } println(); } } } } static private void permutateAndSort(long[] a) { int n = a.length; Random R = new Random(System.currentTimeMillis()); for(int i=0; i<n; i++) { int t = R.nextInt(n-i); long temp = a[n-1-i]; a[n-1-i] = a[t]; a[t] = temp; } Arrays.sort(a); } static private void permutateAndSort(int[] a) { int n = a.length; Random R = new Random(System.currentTimeMillis()); for(int i=0; i<n; i++) { int t = R.nextInt(n-i); int temp = a[n-1-i]; a[n-1-i] = a[t]; a[t] = temp; } Arrays.sort(a); } static private int[][] constructChildren(int n, int[] parent, int parentRoot){ int[][] childrens = new int[n][]; int[] numChildren = new int[n]; for(int i=0; i<parent.length; i++) { if(parent[i] != parentRoot) numChildren[parent[i]]++; } for(int i=0; i<n; i++) { childrens[i] = new int[numChildren[i]]; } int[] idx = new int[n]; for(int i=0; i<parent.length; i++) { if(parent[i] != parentRoot) childrens[parent[i]][idx[parent[i]]++] = i; } return childrens; } static private int[][][] constructDirectedNeighborhood(int n, int[][] e){ int[] inDegree = new int[n]; int[] outDegree = new int[n]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; outDegree[u]++; inDegree[v]++; } int[][] inNeighbors = new int[n][]; int[][] outNeighbors = new int[n][]; for(int i=0; i<n; i++) { inNeighbors[i] = new int[inDegree[i]]; outNeighbors[i] = new int[outDegree[i]]; } for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; outNeighbors[u][--outDegree[u]] = v; inNeighbors[v][--inDegree[v]] = u; } return new int[][][] {inNeighbors, outNeighbors}; } static private int[][] constructNeighborhood(int n, int[][] e) { int[] degree = new int[n]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; degree[u]++; degree[v]++; } int[][] neighbors = new int[n][]; for(int i=0; i<n; i++) neighbors[i] = new int[degree[i]]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; neighbors[u][--degree[u]] = v; neighbors[v][--degree[v]] = u; } return neighbors; } static private void drawGraph(int[][] e) { makeDotUndirected(e); try { final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot") .redirectOutput(new File("graph.png")) .start(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } static private void makeDotUndirected(int[][] e) { MyPrintWriter out2 = null; try { out2 = new MyPrintWriter(new FileOutputStream("graph.dot")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } out2.println("strict graph {"); for(int i=0; i<e.length; i++){ out2.println(e[i][0] + "--" + e[i][1] + ";"); } out2.println("}"); out2.close(); } static private void makeDotDirected(int[][] e) { MyPrintWriter out2 = null; try { out2 = new MyPrintWriter(new FileOutputStream("graph.dot")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } out2.println("strict digraph {"); for(int i=0; i<e.length; i++){ out2.println(e[i][0] + "->" + e[i][1] + ";"); } out2.println("}"); out2.close(); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 17
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
ec8231a31e8d3534542ed22fc46509dd
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; public class Problem3 { 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]; PriorityQueue<Integer> blue= new PriorityQueue<Integer>(); PriorityQueue<Integer> red= new PriorityQueue<Integer>(); for(int i=0;i<n;i++) { ar[i]=sc.nextInt(); } char[] colors = sc.next().toCharArray(); for(int i=0;i<colors.length;i++) { if(colors[i] == 'R') red.add(ar[i]); else blue.add(ar[i]); } int i=1; while(i<=n) { if(blue.size()>0 && blue.peek()>=i) blue.poll(); else if(red.size()>0 && red.peek()<=i) red.poll(); else break; i++; } if(i>n) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 17
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
e52bc02096312d4dbc2213632a243a6a
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class CF753D { public static void solve() { int q = in.nextInt(); while (q-- > 0) { int n = in.nextInt(); int[] inputArr = new int[n]; for (int i = 0; i < n; i++) { inputArr[i] = in.nextInt(); } ArrayList<Integer> numDecs = new ArrayList<>(); ArrayList<Integer> numIncs = new ArrayList<>(); char[] colors = in.next().toCharArray(); for (int i = 0; i < n; i++) { if (colors[i] == 'B') { numDecs.add(inputArr[i]); } else { numIncs.add(inputArr[i]); } } Collections.sort(numDecs); Collections.sort(numIncs); boolean ans = true; HashSet<Integer> decIndexUsed = new HashSet<>(); HashSet<Integer> incIndexUsed = new HashSet<>(); for (int i = 0; i < n; i++) { int index = i + 1; int decIndex = searchRightDec(numDecs, index); int incIndex = searchLeftInc(numIncs, index); // int rightVal = numDecs.get(decIndex); // int leftVal = numIncs.get(incIndex); if (decIndex != -1) { numDecs.remove(decIndex); } else if (incIndex != -1) { // pw.println(numIncs); // pw.println(incIndex + " " + numIncs.get(incIndex)); numIncs.remove(incIndex); } else { ans = false; break; } } pw.println(ans ? "YES" : "NO"); // pw.println(); } } public static int searchRightDec(ArrayList<Integer> al, int min) { int L = 0, R = al.size() - 1; int ans = -1; while (L <= R) { int mid = L + (R - L) / 2; int num = al.get(mid); if (num >= min) { ans = mid; R = mid - 1; } else { L = mid + 1; } } return ans; } public static int searchLeftInc(ArrayList<Integer> al, int max) { int L = 0, R = al.size() - 1; int ans = -1; while (L <= R) { int mid = L + (R - L) / 2; int num = al.get(mid); if (num <= max) { ans = mid; L = mid + 1; } else { R = mid - 1; } } return ans; } public static int getNumber(String s) { return Integer.parseInt(s.substring(0, s.length() - 1)); } public static char getColor(String s) { return s.charAt(s.length() - 1); } static class Pair { int num; boolean inc; Pair(int n, boolean i) { num = n; inc = i; } } // region CP-BOILERPLATE // CP BOILERPLATE TEMPLATE static InputReader in; static PrintWriter pw; public static void main(String[] args) throws IOException { in = new InputReader(); pw = new PrintWriter(System.out); solve(); pw.close(); } static class InputReader { private BufferedReader br; private StringTokenizer st; public InputReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public InputReader(String fileName) throws FileNotFoundException { br = new BufferedReader(new FileReader(fileName)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } } //endregion }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 17
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
bbe9a546c8710febf28c51260d30083a
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
/* * Everything is Hard * Before Easy * Jai Mata Dii */ import java.util.*; import java.io.*; public class Main { static class FastReader{ BufferedReader br;StringTokenizer st;public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException e){e.printStackTrace();}}return st.nextToken();}int nextInt(){ return Integer.parseInt(next());}long nextLong(){return Long.parseLong(next());}double nextDouble(){return Double.parseDouble(next());}String nextLine(){String str = ""; try{str = br.readLine(); } catch (IOException e) {e.printStackTrace();} return str; }} static long mod = (long)(1e9+7); // static long mod = 998244353; // static Scanner sc = new Scanner(System.in); static FastReader sc = new FastReader(); static PrintWriter out = new PrintWriter(System.out); public static void main (String[] args) { int ttt = 1; ttt = sc.nextInt(); z :for(int tc=1;tc<=ttt;tc++){ int n = sc.nextInt(); long a[] = new long[n]; for(int i=0;i<n;i++) { a[i] = sc.nextLong(); } char s[] = sc.next().toCharArray(); // if(tc == 65) { // System.out.println(n); // for(long val : a) { // System.out.print(val+" "); // } // System.out.println(); // for(char c : s) { // System.out.print(c+""); // } // System.out.println(); // continue; // } boolean is = true; for(int i=0;i<n;i++) { if(a[i]<1 && s[i] == 'B') { is = false; } if(a[i]>n && s[i] == 'R') { is = false; } } if(!is) { out.write("NO\n"); continue; } ArrayList<pair> l = new ArrayList<>(); for(int i=0;i<n;i++) { l.add(new pair(a[i], s[i])); } Collections.sort(l, new Comparator<pair>() { @Override public int compare(pair o1, pair o2) { return (int) (o1.val-o2.val); } }); int bef = 0, rig = 0; int i = 0, j = n-1; int cur = 1; int req = 0; int standBy = 0; while(cur<=n) { bef = 0; while(i<=j && l.get(i).val<=cur) { if(l.get(i).c == 'R') { rig++; } else if(l.get(i).val == cur){ bef++; } i++; } if(bef == 0 && rig == 0) { req++; } else if(bef == 0) { rig--; standBy++; } else if(rig == 0) { bef--; int min = Math.min(bef, req); bef -= min; req -= min; min = Math.min(standBy, bef); standBy -= min; rig += min; } else { if(req<=bef-1) { bef--; bef -= req; req = 0; int min = Math.min(standBy, bef); standBy -= min; rig += min; } else { req -= bef; rig--; standBy++; } } cur++; } while(i<=j) { if(l.get(i).c == 'B') req--; i++; } if(req>0) { out.write("NO\n"); } else{ out.write("YES\n"); } } out.close(); } static long pow(long a, long b){long ret = 1;while(b>0){if(b%2 == 0){a = (a*a)%mod;b /= 2;}else{ret = (ret*a)%mod;b--;}}return ret%mod;} static long gcd(long a,long b){if(b==0) return a; return gcd(b,a%b); } private static void sort(int[] a) {List<Integer> k = new ArrayList<>();for(int val : a) k.add(val);Collections.sort(k);for(int i=0;i<a.length;i++) a[i] = k.get(i);} private static void ini(List<Integer>[] tre2){for(int i=0;i<tre2.length;i++){tre2[i] = new ArrayList<>();}} private static void init(List<int[]>[] tre2){for(int i=0;i<tre2.length;i++){tre2[i] = new ArrayList<>();}} private static void sort(long[] a) {List<Long> k = new ArrayList<>();for(long val : a) k.add(val);Collections.sort(k);for(int i=0;i<a.length;i++) a[i] = k.get(i);} } class pair{ long val; char c; pair(long val, char c){ this.val = val; this.c = c; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
3c0b4b9846cb42e5b53e8491cdb84ac2
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.StringTokenizer; /* operations: blue = minus 1 red = plus 1 ---------------- 3 1 3 1 3 R B R R B 1 1 3 3 3 B R R R B 1 2 3 4 5 (answer) B R B R R ---------------- 1 2 3 4 5 B R B R R 5 1 5 1 5 R B R R B 1 1 5 5 5 B R R R B impossible (answer) ---------------- -2 -1 4 0 R R R R -2 -1 0 4 R R R R 1 2 3 4 (answer) R R R R ---------------- 1 2 5 2 B R B R 1 2 2 5 B R R B 1 2 3 4 (answer) B R R B ---------------- n = 10 3 4 4 5 5 6 6 7 8 8 B R R B R R B B R B 3 5 6 7 8 4 4 5 6 8 B B B B B R R R R R 1 2 3 4 5 6 7 8 9 10 B B B B B R R R R R 1 2 3 4 5 6 7 8 9 10 B B B R R B B R R R ---------------- n = 10 1 1 2 2 2 5 7 7 8 10 B R B R R R R R R B distance of numbers from 1 and n 1 2 3 4 5 6 7 8 9 10 B B R R R R R R R B ---------------- n = 4 ..., -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, ... assumptions: if its color is blue, we shouldn't decrease if it's 1 if its color is red, we shouldn't increase if it's n there can only be one element which is 1 and blue there can only be one element which is n and red there should be no negative or zero with color blue (all x <= 0 should be R) there should be no x > n with color red (all x > n should be B) 1 9 2 8 2 4 20 8 14 14 20 BBRRBRRRR */ public class Main { public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int T = 1; T = fs.nextInt(); for (int tc = 1; tc <= T; tc++) { int n = fs.nextInt(); int[] a = fs.readArray(n); char[] colors = fs.next().toCharArray(); ArrayList<Element> arr = new ArrayList<>(); for (int i = 0; i < n; i++) { arr.add(new Element(a[i], colors[i])); } Collections.sort(arr, new Comparator<Element>() { @Override public int compare(Element o1, Element o2) { if (o1.color == o2.color) { if (o1.num < o2.num) { return -1; } if (o1.num == o2.num) { return 0; } return 1; } if (o1.color == 'B') { return -1; } return 1; } }); boolean valid = true; for (int i = 0; i < arr.size(); i++) { int currentNum = i + 1; boolean checker = false; if (arr.get(i).color == 'B') { if (1 <= currentNum && currentNum <= arr.get(i).num) { checker = true; } } else { if (currentNum >= arr.get(i).num) { checker = true; } } if (!checker) { valid = false; break; } } out.println(valid ? "YES" : "NO"); } out.close(); } static class Element { int num; char color; Element(int num, char color) { this.num = num; this.color = color; } } static void sort(int[] a) { ArrayList<Integer> arr = new ArrayList<>(); for (int x : a) { arr.add(x); } Collections.sort(arr); for (int i = 0; i < a.length; i++) { a[i] = arr.get(i); } } static void swap(int[] a, int x, int y) { int temp = a[x]; a[x] = a[y]; a[y] = temp; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if (st.hasMoreTokens()) { str = st.nextToken("\n"); } else { str = br.readLine(); } } catch(IOException e) { e.printStackTrace(); } return str; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
50ae0c485ca7b3123427c1ee7264e394
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.StringTokenizer; /* operations: blue = minus 1 red = plus 1 ---------------- 3 1 3 1 3 R B R R B 1 1 3 3 3 B R R R B 1 2 3 4 5 (answer) B R B R R ---------------- 1 2 3 4 5 B R B R R 5 1 5 1 5 R B R R B 1 1 5 5 5 B R R R B impossible (answer) ---------------- -2 -1 4 0 R R R R -2 -1 0 4 R R R R 1 2 3 4 (answer) R R R R ---------------- 1 2 5 2 B R B R 1 2 2 5 B R R B 1 2 3 4 (answer) B R R B ---------------- n = 10 3 4 4 5 5 6 6 7 8 8 B R R B R R B B R B 3 5 6 7 8 4 4 5 6 8 B B B B B R R R R R 1 2 3 4 5 6 7 8 9 10 B B B B B R R R R R 1 2 3 4 5 6 7 8 9 10 B B B R R B B R R R ---------------- n = 10 1 1 2 2 2 5 7 7 8 10 B R B R R R R R R B distance of numbers from 1 and n 1 2 3 4 5 6 7 8 9 10 B B R R R R R R R B ---------------- n = 4 ..., -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, ... assumptions: if its color is blue, we shouldn't decrease if it's 1 if its color is red, we shouldn't increase if it's n there can only be one element which is 1 and blue there can only be one element which is n and red there should be no negative or zero with color blue (all x <= 0 should be R) there should be no x > n with color red (all x > n should be B) 1 9 2 8 2 4 20 8 14 14 20 BBRRBRRRR */ public class Main { public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int T = 1; T = fs.nextInt(); for (int tc = 1; tc <= T; tc++) { int n = fs.nextInt(); int[] a = fs.readArray(n); char[] colors = fs.next().toCharArray(); ArrayList<Element> arr = new ArrayList<>(); for (int i = 0; i < n; i++) { arr.add(new Element(a[i], colors[i])); } Collections.sort(arr, new Comparator<Element>() { @Override public int compare(Element o1, Element o2) { if (o1.color == o2.color) { if (o1.num < o2.num) { return -1; } if (o1.num == o2.num) { return 0; } return 1; } if (o1.color == 'B') { return -1; } return 1; } }); // for (Element el : arr) { // System.out.print(el.num + " "); // } // System.out.println(); // for (Element el : arr) { // System.out.print(el.color + " "); // } // System.out.println(); boolean valid = true; for (int i = 0; i < arr.size(); i++) { int currentNum = i + 1; boolean checker = false; if (arr.get(i).color == 'B') { if (1 <= currentNum && currentNum <= arr.get(i).num) { checker = true; } } else { if (currentNum >= arr.get(i).num) { checker = true; } } if (!checker) { valid = false; break; } } out.println(valid ? "YES" : "NO"); } out.close(); } static class Element { int num; char color; Element(int num, char color) { this.num = num; this.color = color; } } static void sort(int[] a) { ArrayList<Integer> arr = new ArrayList<>(); for (int x : a) { arr.add(x); } Collections.sort(arr); for (int i = 0; i < a.length; i++) { a[i] = arr.get(i); } } static void swap(int[] a, int x, int y) { int temp = a[x]; a[x] = a[y]; a[y] = temp; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if (st.hasMoreTokens()) { str = st.nextToken("\n"); } else { str = br.readLine(); } } catch(IOException e) { e.printStackTrace(); } return str; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
90f734060b396ac5c353a8629ff74376
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.StringTokenizer; /* operations: blue = minus 1 red = plus 1 ---------------- 3 1 3 1 3 R B R R B 1 1 3 3 3 B R R R B 1 2 3 4 5 (answer) B R B R R ---------------- 1 2 3 4 5 B R B R R 5 1 5 1 5 R B R R B 1 1 5 5 5 B R R R B impossible (answer) ---------------- -2 -1 4 0 R R R R -2 -1 0 4 R R R R 1 2 3 4 (answer) R R R R ---------------- 1 2 5 2 B R B R 1 2 2 5 B R R B 1 2 3 4 (answer) B R R B ---------------- n = 10 3 4 4 5 5 6 6 7 8 8 B R R B R R B B R B 3 5 6 7 8 4 4 5 6 8 B B B B B R R R R R 1 2 3 4 5 6 7 8 9 10 B B B B B R R R R R 1 2 3 4 5 6 7 8 9 10 B B B R R B B R R R ---------------- n = 10 1 1 2 2 2 5 7 7 8 10 B R B R R R R R R B distance of numbers from 1 and n 1 2 3 4 5 6 7 8 9 10 B B R R R R R R R B ---------------- n = 4 ..., -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, ... assumptions: if its color is blue, we shouldn't decrease if it's 1 if its color is red, we shouldn't increase if it's n there can only be one element which is 1 and blue there can only be one element which is n and red there should be no negative or zero with color blue (all x <= 0 should be R) there should be no x > n with color red (all x > n should be B) 4 6 7 7 8 9 3 6 8 B B B B B B R R R 1 9 2 8 2 4 20 8 14 14 20 BBRRBRRRR */ public class Main { public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int T = 1; T = fs.nextInt(); for (int tc = 1; tc <= T; tc++) { int n = fs.nextInt(); int[] a = fs.readArray(n); char[] colors = fs.next().toCharArray(); ArrayList<Element> arr = new ArrayList<>(); for (int i = 0; i < n; i++) { arr.add(new Element(a[i], colors[i])); } Collections.sort(arr, new Comparator<Element>() { @Override public int compare(Element o1, Element o2) { if (o1.color == o2.color) { if (o1.num < o2.num) { return -1; } if (o1.num == o2.num) { return 0; } return 1; } if (o1.color == 'B') { return -1; } return 1; } }); // for (Element el : arr) { // System.out.print(el.num + " "); // } // System.out.println(); // for (Element el : arr) { // System.out.print(el.color + " "); // } // System.out.println(); boolean valid = true; for (int i = 0; i < arr.size(); i++) { int currentNum = i + 1; boolean checker = false; if (arr.get(i).color == 'B') { if (1 <= currentNum && currentNum <= arr.get(i).num) { checker = true; } } else { if (currentNum >= arr.get(i).num) { checker = true; } } if (!checker) { valid = false; break; } } out.println(valid ? "YES" : "NO"); } out.close(); } static class Element { int num; char color; Element(int num, char color) { this.num = num; this.color = color; } } static void sort(int[] a) { ArrayList<Integer> arr = new ArrayList<>(); for (int x : a) { arr.add(x); } Collections.sort(arr); for (int i = 0; i < a.length; i++) { a[i] = arr.get(i); } } static void swap(int[] a, int x, int y) { int temp = a[x]; a[x] = a[y]; a[y] = temp; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if (st.hasMoreTokens()) { str = st.nextToken("\n"); } else { str = br.readLine(); } } catch(IOException e) { e.printStackTrace(); } return str; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
ec7bd408817aef5b89a479d2995c8665
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.StringTokenizer; public class CodeForces { static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readLongArray(int n) { long[] a =new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } static void sort (int[]a){ ArrayList<Integer> b = new ArrayList<>(); for(int i:a)b.add(i); Collections.sort(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static void sortReversed (int[]a){ ArrayList<Integer> b = new ArrayList<>(); for(int i:a)b.add(i); Collections.sort(b); Collections.reverse(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static void sort (long[]a){ ArrayList<Long> b = new ArrayList<>(); for(long i:a)b.add(i); Collections.sort(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static ArrayList<Integer> sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } ArrayList<Integer> ans = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (prime[i] == true) ans.add(i); } return ans; } static int binarySearchSmallerOrEqual(int arr[], int key) { int n = arr.length; int left = 0, right = n; int mid = 0; while (left < right) { mid = (right + left) >> 1; if (arr[mid] == key) { while (mid + 1 < n && arr[mid + 1] == key) mid++; break; } else if (arr[mid] > key) right = mid; else left = mid + 1; } while (mid > -1 && arr[mid] > key) mid--; return mid; } public static int binarySearchStrictlySmaller(int[] arr, int target) { int start = 0, end = arr.length-1; if(end == 0) return -1; if (target > arr[end]) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } static int binarySearch(int arr[], int x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } static int binarySearch(long arr[], long x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } static void init(int[]arr,int val){ for(int i=0;i<arr.length;i++){ arr[i]=val; } } static void init(long[]arr,long val){ for(int i=0;i<arr.length;i++){ arr[i]=val; } } public static void main(String[] args) { FastScanner fs = new FastScanner(); StringBuilder sb = new StringBuilder(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); for (int w=0;w<t;w++){ int n = fs.nextInt(); int[] arr = fs.readArray(n); char []arr2 = fs.next().toCharArray(); ArrayList<Item> items = new ArrayList<>(); for (int i=0;i<n;i++){ items.add(new Item(arr[i],arr2[i]=='B')); } Collections.sort(items, new Comparator<Item>(){ @Override public int compare(Item o1, Item o2) { return o1.val-o2.val; } }); Collections.sort(items, new Comparator<Item>(){ @Override public int compare(Item o1, Item o2) { return -Boolean.compare(o1.isMinus,o2.isMinus); } }); boolean res= true; for (int i=0;i<n;i++){ if(items.get(i).isMinus){ if(items.get(i).val<i+1){ res=false; break; } } else { if(items.get(i).val>i+1){ res=false; break; } } } out.print(res?"YES":"NO"); out.print("\n"); } out.close(); } static class Item{ int val; boolean isMinus;//isBlue public Item(int val , boolean isMin){ this.val=val; this.isMinus=isMin; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
448d4fdbb175280f8b4a55b210fa2d75
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
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.Collections; import java.util.PriorityQueue; import java.util.StringTokenizer; public class a { public static void main(String[] args) throws Exception { PrintWriter pw = new PrintWriter(System.out); Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { PriorityQueue<Integer> b = new PriorityQueue<Integer>(); PriorityQueue<Integer> r = new PriorityQueue<Integer>(/* Collections.reverseOrder() */); int res = 0, j = 1, f = 1; int n = sc.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); String s = sc.next(); for (int i = 0; i < n; i++) { if (s.charAt(i) == 'B') b.add(arr[i]); else r.add(arr[i]); } while (!b.isEmpty()) { if (b.poll() < j++) f = 0; } // j=1; while (!r.isEmpty()) { if (r.poll() > j++) f = 0; } if (f == 1) pw.println("YES"); else pw.println("NO"); } pw.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 int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
61122901c9c1989df8b242954da7a3af
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
// package faltu; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.Map.Entry; public class Main { static long LowerBound(long[] a2, long x) { // x is the target value or key int l=-1,r=a2.length; while(l+1<r) { int m=(l+r)>>>1; if(a2[m]>=x) r=m; else l=m; } return r; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static long getClosest(long val1, long val2,long target) { if (target - val1 >= val2 - target) return val2; else return val1; } static void ruffleSort(long[] a) { int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { long oi=r.nextInt(n), temp=a[i]; a[i]=a[(int)oi]; a[(int)oi]=temp; } Arrays.sort(a); } static void ruffleSort(int[] a){ int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n), temp=a[i]; a[i]=a[oi]; a[oi]=temp; } Arrays.sort(a); } int ceilIndex(int input[], int T[], int end, int s){ int start = 0; int middle; int len = end; while(start <= end){ middle = (start + end)/2; if(middle < len && input[T[middle]] < s && s <= input[T[middle+1]]){ return middle+1; }else if(input[T[middle]] < s){ start = middle+1; }else{ end = middle-1; } } return -1; } public static int findIndex(long arr[], long t) { if (arr == null) { return -1; } int len = arr.length; int i = 0; while (i < len) { if (arr[i] == t) { return i; } else { i = i + 1; } } return -1; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } public static int[] swap(int a[], int left, int right) { int temp = a[left]; a[left] = a[right]; a[right] = temp; return a; } public static void swap(long x,long max1) { long temp=x; x=max1; max1=temp; } public static int[] reverse(int a[], int left, int right) { // Reverse the sub-array while (left < right) { int temp = a[left]; a[left++] = a[right]; a[right--] = temp; } return a; } static int lowerLimitBinarySearch(ArrayList<Integer> A,int B) { int n =A.size(); int first = 0,second = n; while(first <second) { int mid = first + (second-first)/2; if(A.get(mid) > B) { second = mid; }else { first = mid+1; } } if(first < n && A.get(first) < B) { first++; } return first; //1 index } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // *******----segement tree implement---***** // -------------START-------------------------- void buildTree (int[] arr,int[] tree,int start,int end,int treeNode) { if(start==end) { tree[treeNode]=arr[start]; return; } buildTree(arr,tree,start,end,2*treeNode); buildTree(arr,tree,start,end,2*treeNode+1); tree[treeNode]=tree[treeNode*2]+tree[2*treeNode+1]; } void updateTree(int[] arr,int[] tree,int start,int end,int treeNode,int idx,int value) { if(start==end) { arr[idx]=value; tree[treeNode]=value; return; } int mid=(start+end)/2; if(idx>mid) { updateTree(arr,tree,mid+1,end,2*treeNode+1,idx,value); } else { updateTree(arr,tree,start,mid,2*treeNode,idx,value); } tree[treeNode]=tree[2*treeNode]+tree[2*treeNode+1]; } // disjoint set implementation --start static void makeSet(int n) { parent=new int[n]; rank=new int[n]; for(int i=0;i<n;i++) { parent[i]=i; rank[i]=0; } } static void union(int u,int v) { u=findpar(u); v=findpar(v); if(rank[u]<rank[v])parent[u]=v; else if(rank[v]<rank[u])parent[v]=u; else { parent[v]=u; rank[u]++; } } private static int findpar(int node) { if(node==parent[node])return node; return parent[node]=findpar(parent[node]); } static int parent[]; static int rank[]; // *************end static void presumbit(int[][]prebitsum) { for(int i=1;i<=200000;i++) { int z=i; int j=0; while(z>0) { if((z&1)==1) { prebitsum[i][j]+=(prebitsum[i-1][j]+1); }else { prebitsum[i][j]=prebitsum[i-1][j]; } z=z>>1; j++; } } } static boolean coprime(int a, long l){ return (gcd(a, l) == 1); } static Long MOD=(long) (1e9+7); static int prebitsum[][]; static ArrayList<Integer>arr; static boolean[] vis; static ArrayList<ArrayList<Integer>>adj; public static void main(String[] args) throws IOException { // sieve(); // prebitsum=new int[200001][18]; // presumbit(prebitsum); FastReader s = new FastReader(); long tt = s.nextLong(); while(tt-->0) { int n=s.nextInt(); ArrayList<pair>a=new ArrayList<>(); long[]num=new long[n]; for(int i=0;i<n;i++)num[i]=s.nextLong(); char[] ch=s.next().toCharArray(); for(int i=0;i<n;i++) { a.add(new pair(num[i],ch[i])); // System.out.println(a.get(i).x+" "+a.get(i).ch); } Collections.sort(a,new Comparator<pair>() { public int compare(pair oth1,pair oth) { if(oth1.ch != oth.ch) return oth1.ch-oth.ch; return (int) (oth1.x-oth.x); } }); boolean f=true; for(int i=0;i<n;i++) { if(a.get(i).ch=='B'&&a.get(i).x<i+1) f=false; if(a.get(i).ch=='R'&&a.get(i).x>i+1)f=false; } if(!f)System.out.println("NO"); else System.out.println("YES"); } } static void DFSUtil(int v, boolean[] visited) { visited[v] = true; Iterator<Integer> it = adj.get(v).iterator(); while (it.hasNext()) { int n = it.next(); if (!visited[n]) DFSUtil(n, visited); } } static long DFS(int n) { boolean[] visited = new boolean[n+1]; long cnt=0; for (int i = 1; i <= n; i++) { if (!visited[i]) { DFSUtil(i, visited); cnt++; } } return cnt; } public static String revStr(String str){ String input = str; StringBuilder input1 = new StringBuilder(); input1.append(input); input1.reverse(); return input1.toString(); } public static String sortString(String inputString){ char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } static long myPow(long n, long i){ if(i==0) return 1; if(i%2==0) return (myPow(n,i/2)%MOD * myPow(n,i/2)%MOD)%MOD; return (n%MOD* myPow(n,i-i)%MOD)%MOD; } static void palindromeSubStrs(String str) { HashSet<String>set=new HashSet<>(); char[]a =str.toCharArray(); int n=str.length(); int[][]dp=new int[n][n]; for(int g=0;g<n;g++){ for(int i=0,j=g;j<n;j++,i++){ if(!set.contains(str.substring(i,i+1))&&g==0) { dp[i][j]=1; set.add(str.substring(i,i+1)); } else { if(!set.contains(str.substring(i,j+1))&&isPalindrome(str,i,j)) { dp[i][j]=1; set.add(str.substring(i,j+1)); } } } } int ans=0; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { System.out.print(dp[i][j]+" "); if(dp[i][j]==1)ans++; } System.out.println(); } System.out.println(ans); } static boolean isPalindrome(String str,int i,int j) { while (i < j) { if (str.charAt(i) != str.charAt(j)) return false; i++; j--; } return true; } static boolean sign(long num) { return num>0; } static boolean isSquare(long x){ if(x==1)return true; long y=(long) Math.sqrt(x); return y*y==x; } static long power1(long a,long b) { if(b == 0){ return 1; } long ans = power(a,b/2); ans *= ans; if(b % 2!=0){ ans *= a; } return ans; } static void swap(StringBuilder sb,int l,int r) { char temp = sb.charAt(l); sb.setCharAt(l,sb.charAt(r)); sb.setCharAt(r,temp); } // function to reverse the string between index l and r static void reverse(StringBuilder sb,int l,int r) { while(l < r) { swap(sb,l,r); l++; r--; } } // function to search a character lying between index l and r // which is closest greater (just greater) than val // and return it's index static int binarySearch(StringBuilder sb,int l,int r,char val) { int index = -1; while (l <= r) { int mid = (l+r)/2; if (sb.charAt(mid) <= val) { r = mid - 1; } else { l = mid + 1; if (index == -1 || sb.charAt(index) >= sb.charAt(mid)) index = mid; } } return index; } // this function generates next permutation (if there exists any such permutation) from the given string // and returns True // Else returns false static boolean nextPermutation(StringBuilder sb) { int len = sb.length(); int i = len-2; while (i >= 0 && sb.charAt(i) >= sb.charAt(i+1)) i--; if (i < 0) return false; else { int index = binarySearch(sb,i+1,len-1,sb.charAt(i)); swap(sb,i,index); reverse(sb,i+1,len-1); return true; } } private static int lps(int m ,int n,String s1,String s2,int[][]mat) { for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { if(s1.charAt(i-1)==s2.charAt(j-1))mat[i][j]=1+mat[i-1][j-1]; else mat[i][j]=Math.max(mat[i-1][j],mat[i][j-1]); } } return mat[m][n]; } static String lcs(String X, String Y, int m, int n) { int[][] L = new int[m+1][n+1]; // Following steps build L[m+1][n+1] in bottom up fashion. Note // that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] for (int i=0; i<=m; i++) { for (int j=0; j<=n; j++) { if (i == 0 || j == 0) L[i][j] = 0; else if (X.charAt(i-1) == Y.charAt(j-1)) L[i][j] = L[i-1][j-1] + 1; else L[i][j] = Math.max(L[i-1][j], L[i][j-1]); } } // Following code is used to print LCS int index = L[m][n]; int temp = index; // Create a character array to store the lcs string char[] lcs = new char[index+1]; lcs[index] = '\u0000'; // Set the terminating character // Start from the right-most-bottom-most corner and // one by one store characters in lcs[] int i = m; int j = n; while (i > 0 && j > 0) { // If current character in X[] and Y are same, then // current character is part of LCS if (X.charAt(i-1) == Y.charAt(j-1)) { // Put current character in result lcs[index-1] = X.charAt(i-1); // reduce values of i, j and index i--; j--; index--; } // If not same, then find the larger of two and // go in the direction of larger value else if (L[i-1][j] > L[i][j-1]) i--; else j--; } return String.valueOf(lcs); // Print the lcs // System.out.print("LCS of "+X+" and "+Y+" is "); // for(int k=0;k<=temp;k++) // System.out.print(lcs[k]); } static long lis(long[] aa2, int n) { long lis[] = new long[n]; int i, j; long max = 0; for (i = 0; i < n; i++) lis[i] = 1; for (i = 1; i < n; i++) for (j = 0; j < i; j++) if (aa2[i] >= aa2[j] && lis[i] <= lis[j] + 1) lis[i] = lis[j] + 1; for (i = 0; i < n; i++) if (max < lis[i]) max = lis[i]; return max; } static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) { if (str.charAt(i) != str.charAt(j)) return false; i++; j--; } return true; } static boolean issafe(int i, int j, int r,int c, char ch) { if (i < 0 || j < 0 || i >= r || j >= c|| ch!= '1')return false; else return true; } static long power(long a, long b) { a %=MOD; long out = 1; while (b > 0) { if((b&1)!=0)out = out * a % MOD; a = a * a % MOD; b >>= 1; a*=a; } return out; } static long[] sieve; public static void sieve() { int nnn=(int) 1e6+1; long nn=(int) 1e6; sieve=new long[(int) nnn]; int[] freq=new int[(int) nnn]; sieve[0]=0; sieve[1]=1; for(int i=2;i<=nn;i++) { sieve[i]=1; freq[i]=1; } for(int i=2;i*i<=nn;i++) { if(sieve[i]==1) { for(int j=i*i;j<=nn;j+=i) { if(sieve[j]==1) { sieve[j]=0; } } } } } } class decrease implements Comparator<Long> { // Used for sorting in ascending order of // roll number public int compare(long a, long b) { return (int) (b - a); } @Override public int compare(Long o1, Long o2) { // TODO Auto-generated method stub return (int) (o2-o1); } } class pair{ long x; long y; long c; char ch; public pair(long x,long y) { this.x=x; this.y=y; } public pair(long x,char ch) { this.x=x; this.ch=ch; } public pair(long x,long y,long c) { this.x=x; this.y=y; this.c=c; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
ee858fb8d1ad973803dd0c7d533bff6e
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
/* stream Butter! eggyHide eggyVengeance I need U xiao rerun when */ import static java.lang.Math.*; import java.util.*; import java.io.*; import java.math.*; public class x1607D { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int[] vals = readArr(N, infile, st); char[] colors = infile.readLine().toCharArray(); Unit[] arr = new Unit[N]; for(int i=0; i < N; i++) arr[i] = new Unit(vals[i], colors[i]); Arrays.sort(arr); String res = "YES\n"; for(int i=0; i < N; i++) if(arr[i].tag == 'B' && arr[i].val < i+1 || arr[i].tag == 'R' && arr[i].val > i+1) res = "NO\n"; sb.append(res); } System.out.print(sb); } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } } class Unit implements Comparable<Unit> { public char tag; public int val; public Unit(int a, char c) { val = a; tag = c; } public int compareTo(Unit oth) { if(tag != oth.tag) return tag-oth.tag; return val-oth.val; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
c1cbbff3f7862ec7ca64423238704f0d
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.text.CollationElementIterator; import java.util.*; public class CFD { static CFD.Reader sc = new CFD.Reader(); static BufferedWriter bw; static int team1; static int team2; static int minkicks; public CFD() { } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); process: for (int i = 0; i < t; i++) { int n = sc.nextInt(); int[] ar = new int[n]; for (int j = 0; j < n; j++) { ar[j] = sc.nextInt(); } String s = sc.next(); List<Integer> redn = new ArrayList<>(); List<Integer> bluen = new ArrayList<>(); int[] red = new int[n + 1]; int[] blue = new int[n + 1]; for (int j = 0; j < n; j++) { char ch = s.charAt(j); if (ch == 'R') { redn.add(ar[j]); if (ar[j] <= n && ar[j] >= 1) { red[ar[j]]++; } else if (ar[j] < 1) { red[1]++; } } else { bluen.add(ar[j]); if (ar[j] <= n && ar[j] >= 1) { blue[ar[j]]++; } else if (ar[j] > n) { blue[n]++; } } } Collections.sort(bluen); Collections.sort(redn); for (int j = 0; j < bluen.size(); j++) { if(bluen.get(j)<(j+1)){ System.out.println("NO"); continue process; } } for (int j = 0; j < redn.size(); j++) { if(redn.get(j)>(bluen.size()+j+1)){ System.out.println("NO"); continue process; } } System.out.println("YES"); } bw.flush(); bw.close(); } public static int checker(int a) { if (a % 2 == 0) { return 1; } else { byte b; do { b = 2; } while (a + b != (a ^ b)); return b; } } public static boolean checkPrime(int n) { if (n != 0 && n != 1) { for (int j = 2; j * j <= n; ++j) { if (n % j == 0) { return false; } } return true; } else { return false; } } public static void dfs1(List<List<Integer>> g, boolean[] visited, Stack<Integer> stack, int num) { visited[num] = true; Iterator var4 = ((List) g.get(num)).iterator(); while (var4.hasNext()) { Integer integer = (Integer) var4.next(); if (!visited[integer]) { dfs1(g, visited, stack, integer); } } stack.push(num); } public static void dfs2(List<List<Integer>> g, boolean[] visited, List<Integer> list, int num, int c, int[] raj) { visited[num] = true; Iterator var6 = ((List) g.get(num)).iterator(); while (var6.hasNext()) { Integer integer = (Integer) var6.next(); if (!visited[integer]) { dfs2(g, visited, list, integer, c, raj); } } raj[num] = c; list.add(num); } public static int inputInt() throws IOException { return sc.nextInt(); } public static long inputLong() throws IOException { return sc.nextLong(); } public static double inputDouble() throws IOException { return sc.nextDouble(); } public static String inputString() throws IOException { return sc.readLine(); } public static void print(String a) throws IOException { bw.write(a); } public static void printSp(String a) throws IOException { bw.write(a + " "); } public static void println(String a) throws IOException { bw.write(a + "\n"); } public static long getAns(int[] ar, int c, long[][] dp, int i, int sign) { if (i < 0) { return 1L; } else if (c <= 0) { return 1L; } else { dp[i][c] = Math.max(dp[i][c], Math.max((long) ar[i] * getAns(ar, c - 1, dp, i - 1, sign), getAns(ar, c, dp, i - 1, 1))); return dp[i][c]; } } public static long power(long a, long b, long c) { long ans; for (ans = 1L; b != 0L; b /= 2L) { if (b % 2L == 1L) { ans *= a; ans %= c; } a *= a; a %= c; } return ans; } public static long power1(long a, long b, long c) { long ans; for (ans = 1L; b != 0L; b /= 2L) { if (b % 2L == 1L) { ans = multiply(ans, a, c); } a = multiply(a, a, c); } return ans; } public static long multiply(long a, long b, long c) { long res = 0L; for (a %= c; b > 0L; b /= 2L) { if (b % 2L == 1L) { res = (res + a) % c; } a = (a + a) % c; } return res % c; } public static long totient(long n) { long result = n; for (long i = 2L; i * i <= n; ++i) { if (n % i == 0L) { while (n % i == 0L) { n /= i; } result -= result / i; } } if (n > 1L) { result -= result / n; } return result; } public static long gcd(long a, long b) { return b == 0L ? a : gcd(b, a % b); } public static boolean[] primes(int n) { boolean[] p = new boolean[n + 1]; p[0] = false; p[1] = false; int i; for (i = 2; i <= n; ++i) { p[i] = true; } for (i = 2; i * i <= n; ++i) { if (p[i]) { for (int j = i * i; j <= n; j += i) { p[j] = false; } } } return p; } public String LargestEven(String S) { int[] count = new int[10]; int num; for (num = 0; num < S.length(); ++num) { ++count[S.charAt(num) - 48]; } num = -1; for (int i = 0; i <= 8; i += 2) { if (count[i] > 0) { num = i; break; } } StringBuilder ans = new StringBuilder(); for (int i = 9; i >= 0; --i) { int j; if (i != num) { for (j = 1; j <= count[i]; ++j) { ans.append(i); } } else { for (j = 1; j <= count[num] - 1; ++j) { ans.append(num); } } } if (num != -1 && count[num] > 0) { ans.append(num); } return ans.toString(); } static { bw = new BufferedWriter(new OutputStreamWriter(System.out)); team1 = 0; team2 = 0; minkicks = 10; } public static class Score { int l; String s; public Score(int l, String s) { this.l = l; this.s = s; } } public static class Ath { int r1; int r2; int r3; int r4; int r5; int index; public Ath(int r1, int r2, int r3, int r4, int r5, int index) { this.r1 = r1; this.r2 = r2; this.r3 = r3; this.r4 = r4; this.r5 = r5; this.index = index; } } static class Reader { private final int BUFFER_SIZE = 65536; private DataInputStream din; private byte[] buffer; private int bufferPointer; private int bytesRead; public Reader() { this.din = new DataInputStream(System.in); this.buffer = new byte[65536]; this.bufferPointer = this.bytesRead = 0; } public Reader(String file_name) throws IOException { this.din = new DataInputStream(new FileInputStream(file_name)); this.buffer = new byte[65536]; this.bufferPointer = this.bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; int cnt; byte c; for (cnt = 0; (c = this.read()) != -1 && c != 10; buf[cnt++] = (byte) c) { } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c; for (c = this.read(); c <= 32; c = this.read()) { } boolean neg = c == 45; if (neg) { c = this.read(); } do { ret = ret * 10 + c - 48; } while ((c = this.read()) >= 48 && c <= 57); return neg ? -ret : ret; } public long nextLong() throws IOException { long ret = 0L; byte c; for (c = this.read(); c <= 32; c = this.read()) { } boolean neg = c == 45; if (neg) { c = this.read(); } do { ret = ret * 10L + (long) c - 48L; } while ((c = this.read()) >= 48 && c <= 57); return neg ? -ret : ret; } public double nextDouble() throws IOException { double ret = 0.0D; double div = 1.0D; byte c; for (c = this.read(); c <= 32; c = this.read()) { } boolean neg = c == 45; if (neg) { c = this.read(); } do { ret = ret * 10.0D + (double) c - 48.0D; } while ((c = this.read()) >= 48 && c <= 57); if (c == 46) { while ((c = this.read()) >= 48 && c <= 57) { ret += (double) (c - 48) / (div *= 10.0D); } } return neg ? -ret : ret; } private void fillBuffer() throws IOException { this.bytesRead = this.din.read(this.buffer, this.bufferPointer = 0, 65536); if (this.bytesRead == -1) { this.buffer[0] = -1; } } private byte read() throws IOException { if (this.bufferPointer == this.bytesRead) { this.fillBuffer(); } return this.buffer[this.bufferPointer++]; } public void close() throws IOException { if (this.din != null) { this.din.close(); } } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
bfc591a70349a1be7419a716eb4b9c05
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
//package practice; import java.io.*; import java.util.*; public class D1607 { static final int MOD = 1000000007; static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static int fasterScanner() { try { boolean in = false; int res = 0; for (; ; ) { int b = System.in.read() - '0'; if (b >= 0) { in = true; res = 10 * res + b; } else if (in) { return res; } } } catch (IOException e) { throw new Error(e); } } public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = fasterScanner(); while (t-- > 0) { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } String s = sc.next(); NumColor[] nodes = new NumColor[n]; for (int i = 0; i < n; i++) { nodes[i] = new NumColor(arr[i], s.charAt(i)); } Arrays.sort(nodes, (NumColor o1, NumColor o2) -> { if (o1.clr == o2.clr) { return o1.val - o2.val; } return o1.clr - o2.clr; }); String ans = "Yes"; for (int i = 0; i < n; i++) { if (nodes[i].clr == 'B' && nodes[i].val < i + 1) { ans = "No"; break; } if (nodes[i].clr == 'R' && nodes[i].val > i + 1) { ans = "No"; break; } } out.println(ans); } out.close(); } static class NumColor { int val; char clr; NumColor(int val, char clr) { this.val = val; this.clr = clr; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } char nextChar() { return next().charAt(0); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
ca2f52d8b050000f5bede623413b3501
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Practice { static Scanner scn = new Scanner(System.in); static StringBuilder sb = new StringBuilder(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] HastaLaVistaLa) { int t = scn.nextInt(); // int t = 1; for(int tests = 0; tests < t; tests++) solve(); out.println(sb); out.close(); } public static void solve() { int n = scn.nextInt(); int[][] a = new int[n][2]; for(int i = 0; i < n; i++) a[i][0] = scn.nextInt(); char[] ch = scn.next().toCharArray(); for(int i = 0; i < n; i++) { if(ch[i] == 'B') a[i][1] = -1; else a[i][1] = 1; } boolean okay = true; for(int i = 0; i < n; i++) { if(a[i][0] > n && a[i][1] == 1) okay = false; if(a[i][0] <= 0 && a[i][1] == -1) okay = false; } if(!okay) { sb.append("NO\n"); return; } int[] left = new int[n + 1], right = new int[n + 1]; for(int i = 0; i < n; i++) { if(a[i][0] <= 0) a[i][0] = 1; if(a[i][0] > n) a[i][0] = n; if(a[i][1] == 1) right[a[i][0]]++; else left[a[i][0]]++; } int sum = 0; for(int i = 1; i <= n; i++) { sum += left[i]; if(sum > i) okay = false; } sum = 0; for(int i = n; i >= 1; i--) { sum += right[i]; if(sum > (n - i + 1)) okay = false; } if(!okay) { sb.append("NO\n"); }else { sb.append("YES\n"); } } public static void sort(int[] a) { List<Integer> l = new ArrayList<>(); for(int i : a) l.add(i); Collections.sort(l); int s = 0; for(int i : l) a[s++] = i; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
076889f1a349d7575e24482d64a524b3
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Practice { static Scanner scn = new Scanner(System.in); static StringBuilder sb = new StringBuilder(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] HastaLaVistaLa) { int t = scn.nextInt(); // int t = 1; for(int tests = 0; tests < t; tests++) solve(); out.println(sb); out.close(); } public static void solve() { int n = scn.nextInt(); int[][] a = new int[n][2]; for(int i = 0; i < n; i++) a[i][0] = scn.nextInt(); char[] ch = scn.next().toCharArray(); for(int i = 0; i < n; i++) { if(ch[i] == 'B') a[i][1] = -1; else a[i][1] = 1; } boolean okay = true; for(int i = 0; i < n; i++) { if(a[i][0] > n && a[i][1] == 1) okay = false; if(a[i][0] <= 0 && a[i][1] == -1) okay = false; } if(!okay) { sb.append("NO\n"); return; } int[] left = new int[n + 1], right = new int[n + 1]; for(int i = 0; i < n; i++) { if(a[i][0] <= 0) a[i][0] = 1; if(a[i][0] > n) a[i][0] = n; if(a[i][1] == 1) right[a[i][0]]++; else left[a[i][0]]++; } for(int i = 1; i <= n; i++) { int canL = (i); int canR = (n - i + 1); if(left[i] > canL) okay = false; if(right[i] > canR) okay = false; } int sum = 0; for(int i = 1; i <= n; i++) { sum += left[i]; if(sum > i) okay = false; } sum = 0; for(int i = n; i >= 1; i--) { sum += right[i]; if(sum > (n - i + 1)) okay = false; } if(!okay) { sb.append("NO\n"); }else { sb.append("YES\n"); } } public static void sort(int[] a) { List<Integer> l = new ArrayList<>(); for(int i : a) l.add(i); Collections.sort(l); int s = 0; for(int i : l) a[s++] = i; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
9d723dc20407708fc1c5e344a3a163e4
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; public class Solution { 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[] arr = new int[n]; for (int i = 0; i < n;) arr[i++] = in.nextInt(); char[] color = in.next().toCharArray(); int[][] nums = divide(arr, color); boolean res = checkRed(nums[0], nums[1].length + 1) && checkBlue(nums[1]); if (res) System.out.println("YES"); else System.out.println("NO"); } } private static int[][] divide(int[] nums, char[] color) { int[][] res = new int[2][]; // count int red = 0; for (char c: color) { if (c == 'R') red++; } // divide res[0] = new int[red]; res[1] = new int[nums.length - red]; for (int pr = 0, pb = 0, p = 0; p < nums.length;) { if (color[p] == 'R') res[0][pr++] = nums[p++]; else res[1][pb++] = nums[p++]; } // sort Arrays.sort(res[0]); Arrays.sort(res[1]); return res; } private static boolean checkRed(int[] nums, int p) { for (int n: nums) { if (n > p++) return false; } return true; } private static boolean checkBlue(int[] nums) { int p = 1; for (int n: nums) { if (n < p++) return false; } return true; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
63b693656ea0a43b93b629c74cdd2287
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; //import javafx.util.*; public class Main { static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static int INF = Integer.MAX_VALUE; static int NINF = Integer.MIN_VALUE; public static void main (String[] args) throws java.lang.Exception { int t = i(); while(t-- > 0){ int n = i(); int[] arr = input(n); char[] ch = in.nextLine().toCharArray(); ArrayList<Integer> red = new ArrayList<>(); ArrayList<Integer> blue = new ArrayList<>(); for(int i = 0;i < n;i++){ if(ch[i] == 'R'){ red.add(arr[i]); }else{ blue.add(arr[i]); } } boolean check = true; Collections.sort(blue); Collections.sort(red); for(int i = 0;i < blue.size();i++){ if(blue.get(i) < i+1){ check = false; break; } } int j = n - red.size() + 1; for(int i = 0;i < red.size();i++){ if(red.get(i) > j++){ check = false; break; } } if(check){ out.println("YES"); }else{ out.println("NO"); } } out.close(); } public static int sum(int n){ int sum = 0; for(int i = 2;i < n;i++){ if(isPrime(i)){ sum += i; } } return sum; } public static boolean isPrime(long n) { // Check if number is less than // equal to 1 if (n <= 1l) return false; // Check if number is 2 else if (n == 2l) return true; // Check if n is a multiple of 2 else if (n % 2l == 0l) return false; // If not, then just check the odds for (long i = 3l; i <= (long)Math.sqrt(n); i += 2l) { if (n % i == 0l) return false; } return true; } public static void sort(int [] arr){ ArrayList<Integer> ls = new ArrayList<>(); for(int x : arr){ ls.add(x); } Collections.sort(ls); for(int i = 0;i < arr.length;i++){ arr[i] = ls.get(i); } } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } } class Pair implements Comparable<Pair>{ int x; int y; Pair(int x, int y){ this.x = x; this.y = y; } @Override public int compareTo(Pair obj) { return (this.x - obj.x); } } 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
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
3db55d7ef62bdbb6d844dbdb0c89b59a
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class Practice1 { static long[] sort(long[] arr) { int n=arr.length; ArrayList<Long> al=new ArrayList<>(); for(int i=0;i<n;i++) { al.add(arr[i]); } Collections.sort(al); for(int i=0;i<n;i++) { arr[i]=al.get(i); } return arr; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long nCr(int n, int r) { // int x=1000000007; long dp[][]=new long[2][r+1]; for(int i=0;i<=n;i++){ for(int j=0;j<=i&&j<=r;j++){ if(i==0||j==0||i==j){ dp[i%2][j]=1; }else { // dp[i%2][j]=(dp[(i-1)%2][j]+dp[(i-1)%2][j-1])%x; dp[i%2][j]=(dp[(i-1)%2][j]+dp[(i-1)%2][j-1]); } } } return dp[n%2][r]; } public static class UnionFind { private final int[] p; public UnionFind(int n) { p = new int[n]; for (int i = 0; i < n; i++) { p[i] = i; } } public int find(int x) { return x == p[x] ? x : (p[x] = find(p[x])); } public void union(int x, int y) { x = find(x); y = find(y); if (x != y) { p[x] = y; } } } public static boolean ispalin(String str) { int n=str.length(); for(int i=0;i<n/2;i++) { if(str.charAt(i)!=str.charAt(n-i-1)) { return false; } } return true; } static class Pair{ int val; int pos; Pair(int val,int pos){ this.val=val; this.pos=pos; } } static long power(long N,long R) { long x=1000000007; if(R==0) return 1; if(R==1) return N; long temp= power(N,R/2)%x; // (a*b)%p = (a%p*b%p)*p temp=(temp*temp)%x; if(R%2==0){ return temp%x; }else{ return (N*temp)%x; } } public static String binary(int n) { StringBuffer ans=new StringBuffer(); int a=4; while(a-->0) { int temp=(n&1); if(temp!=0) { ans.append('1'); }else { ans.append('0'); } n =n>>1; } ans=ans.reverse(); return ans.toString(); } public static int find(String[][] arr,boolean[][] vis,int dir,int i,int j) { if(i<0||i>=arr.length||j<0||j>=arr[0].length) return 0; if(vis[i][j]==true) return 0; if(dir==1&&arr[i][j].charAt(0)=='1') return 0; if(dir==2&&arr[i][j].charAt(1)=='1') return 0; if(dir==3&&arr[i][j].charAt(2)=='1') return 0; if(dir==4&&arr[i][j].charAt(3)=='1') return 0; vis[i][j]=true; int a=find(arr,vis,1,i+1,j); int b=find(arr,vis,2,i-1,j); int c=find(arr,vis,3,i,j+1); int d=find(arr,vis,4,i,j-1); return 1+a+b+c+d; } static ArrayList<Integer> allDivisors(int n) { ArrayList<Integer> al=new ArrayList<>(); int i=2; while(i*i<=n) { if(n%i==0) al.add(i); if(n%i==0&&i*i!=n) al.add(n/i); i++; } return al; } static int[] sort(int[] arr) { int n=arr.length; ArrayList<Integer> al=new ArrayList<>(); for(int i=0;i<n;i++) { al.add(arr[i]); } Collections.sort(al); for(int i=0;i<n;i++) { arr[i]=al.get(i); } return arr; } public static void main (String[] args) { PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); // out.print(); //out.println(); FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } String str=sc.nextLine();// blue -> 1 && red -> 0 ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); boolean bol=true; for(int i=0;i<n;i++) { if(str.charAt(i)=='B') { blue.add(arr[i]); if(arr[i]<=0) bol=false; }else { red.add(arr[i]); if(arr[i]>n) bol=false; } } Collections.sort(blue); Collections.sort(red); int b=blue.size(); int r=red.size(); int i=0,j=0,val=1; while(i<b&&j<r) { if(blue.get(i)>=val) { i++; }else if(red.get(j)<=val) { j++; }else { bol=false; break; } val++; } while(i<b) { if(blue.get(i)>=val) { i++; }else { bol=false; break; } val++; } while(j<r) { if(red.get(j)<=val) { j++; }else { bol=false; break; } val++; } if(bol==true) { 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; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
9825eb8c69c0053a93b9124bbbe49ba2
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
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 { //FastScanner sc = new FastScanner(); Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int T = sc.nextInt(); for(int t = 0; t<T; t++){ int N = sc.nextInt(); int nums[] = new int[N]; for(int i = 0; i<N; i++)nums[i] = sc.nextInt(); sc.nextLine(); String type = sc.nextLine(); List<Integer> B = new ArrayList(); List<Integer> R = new ArrayList(); boolean arr[] = new boolean[N]; for(int i = 0; i<N; i++){ if(type.charAt(i) == 'B')B.add(nums[i]); else R.add(nums[i]); } Collections.sort(B); Collections.sort(R); int j = 0; for(int i = 0;i <N; i++){ if(j < B.size()){ if(B.get(j) >= i + 1){ arr[i] = true; j++; } } } j = 0; boolean flag = false; for(int i = 0; i <N; i++){ if(j < R.size() && !arr[i]){ if(R.get(j) <= i + 1){ arr[i] = true; j++; } } if(!arr[i])flag = true; } if(flag)out.println("NO"); else out.println("YES"); } out.flush(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
6b30688c84ad4e7c42aede71192d7d3f
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
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 { //FastScanner sc = new FastScanner(); Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int T = sc.nextInt(); for(int t = 0; t<T; t++){ int N = sc.nextInt(); int nums[] = new int[N]; for(int i = 0; i<N; i++)nums[i] = sc.nextInt(); sc.nextLine(); String type = sc.nextLine(); List<Integer> B = new ArrayList(); List<Integer> R = new ArrayList(); boolean arr[] = new boolean[N]; for(int i = 0; i<N; i++){ if(type.charAt(i) == 'B')B.add(nums[i]); else R.add(nums[i]); } Collections.sort(B); Collections.sort(R); int j = 0; for(int i = 0;i <N; i++){ if(j < B.size()){ if(B.get(j) >= i + 1){ arr[i] = true; j++; } } } j = 0; boolean flag = false; for(int i = 0; i <N; i++){ if(j < R.size() && !arr[i]){ if(R.get(j) <= i + 1){ arr[i] = true; j++; } } if(!arr[i])flag = true; } if(flag)out.println("NO"); else out.println("YES"); } out.flush(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
8efc5e5b9ebd1b9b19a7415e0927c408
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.io.*; public class D { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); while(T-- > 0) { int n = in.nextInt(); int[] a = new int[n]; for(int j=0;j<n;j++) a[j] = in.nextInt(); char[] s = in.next().toCharArray(); List<Integer> blue = new ArrayList<>(); List<Integer> red = new ArrayList<>(); for(int j=0;j<n;j++) { if(s[j] == 'B') blue.add(a[j]); else red.add(a[j]); } Collections.sort(blue); Collections.sort(red); boolean p = true; int cur = 1; for(int val : blue) { if(val<cur) { p = false; break; } else cur++; } for(int val : red) { if(val>cur) { p = false; break; } else cur++; } if(p) System.out.println("yes"); else System.out.println("no"); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
4338fca88187a6b4c764ae616d67d283
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=input.nextInt(); while(T-->0) { int n=input.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=input.nextInt(); } ArrayList<Pair> list=new ArrayList<>(); String s=input.next(); int f=0; for(int i=0;i<n;i++) { int v=a[i]; if(s.charAt(i)=='B') { int l=1,r=v; if(l>r) { f=1; break; } else { list.add(new Pair(l,r)); } } else { int l=v,r=n; if(l>r) { f=1; break; } else { l=Math.max(l,1); list.add(new Pair(l,r)); } } } if(f==1) { out.println("NO"); } else { Collections.sort(list, new Comparator<Pair>() { @Override public int compare(Pair o1, Pair o2) { if(o1.l==o2.l) return o1.r-o2.r; else return o1.l-o2.l; } }); int in=0; for(int i=1;i<=n;i++) { int l=list.get(in).l; int r=list.get(in).r; if(!(i>=l && i<=r)) { f=1; break; } in++; } if(f==1) { out.println("NO"); } else { out.println("YES"); } } } out.close(); } static class Pair { int l,r; Pair(int l,int r) { this.l=l; this.r=r; } } 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
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
01a932ff6779f68ffa0e8d65e1c6d4d5
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.io.*; public class code { // remember long, to reformat ctrl + shift +f static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws Exception { int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int []vals=new int[n]; boolean numLine[]=new boolean[n+1]; for(int i=0;i<n;i++)vals[i]=sc.nextInt(); String s=sc.nextLine(); ArrayList<Integer>b=new ArrayList<Integer>(); ArrayList<Integer>r=new ArrayList<Integer>(); for(int i=0;i<n;i++) { if(s.charAt(i)=='B' && vals[i]>0 )b.add(vals[i]); else if( s.charAt(i)=='R' && vals[i]<=n)r.add(vals[i]); } Collections.sort(b); Collections.sort(r); int small=1; for(int i=0;i<b.size();i++) { int y=b.get(i); if(y<small)continue; numLine[small]=true; small++; } // pw.println(Arrays.toString(numLine)); int large=n; for(int i=r.size()-1;i>=0;i--) { int y=r.get(i); if(y>large)continue; // y=Math.max(large, y); numLine[large]=true; large--; } //pw.print(Arrays.toString(numLine)); boolean can=true; for(int i=1;i<=n;i++) { if(numLine[i]==false) { pw.println("no"); can=false; break; } } if(can)pw.println("yes"); } pw.close(); } // --------------------stuff ---------------------- static class pair implements Comparable<pair> { int v; int w; public pair(int v,int w) { this.v = v; this.w = w; } public int compareTo(pair p) { return this.w- p.w;// increasing order!! //return Double.compare(v*w, p.w*p.v); } } 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
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
c7b7a7c1485e0a641a67b9351d9d5f7e
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; public class Soltion{ public static void main(String []args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); Integer[] arr = new Integer[n]; for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); } String s = sc.next(); List<Integer> blue = new ArrayList<>(); List<Integer> red = new ArrayList<>(); for(int i=0;i<s.length();i++){ if(s.charAt(i)=='B'){ blue.add(arr[i]); } else{ red.add(arr[i]); } } Collections.sort(blue); Collections.sort(red); int p=1,q=n; boolean flag = true; for(int i=red.size()-1;i>=0;i--){ if(red.get(i)>q){ flag = false; break; } q--; } for(int i=0;i<blue.size();i++){ if(blue.get(i)<p){ flag = false; break; } p++; } System.out.println(flag? "Yes" : "No"); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
8cc7b6142d89b0539245f77d5958668d
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
//package CodeforcesContests; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class D { static class Pair implements Comparator<Pair> { int first; int second; Pair(int m, int t) { first = m; second = t; } Pair() {} @Override public int compare(Pair o1, Pair o2) { return (o1.first - o2.first); } @Override public String toString() { return "Pair{" + "first=" + first + ", second=" + second + '}'; } } public static void main (String[] Z) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder op = new StringBuilder(); StringTokenizer stz; int T = Integer.parseInt(br.readLine()); while(T-- > 0) { int n = Integer.parseInt(br.readLine()); stz = new StringTokenizer(br.readLine()); String color = br.readLine(); int[] arr = new int[n]; ArrayList<Integer> red = new ArrayList<>(); ArrayList<Integer> blu = new ArrayList<>(); boolean atlest = true; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(stz.nextToken()); if(color.charAt(i) == 'R') { if(arr[i] > n) { atlest = false; break; } else { red.add(arr[i]); } } else { if(arr[i] < 1) { atlest = false; break; } else { blu.add(arr[i]); } } } Collections.sort(red); Collections.sort(blu); ArrayList<Integer> perm = new ArrayList<>(); boolean yes = true; int curr = 1; for (int i = 0; i < blu.size(); i++, ++curr) { if(curr <= blu.get(i)) { perm.add(curr); } else { break; } } // System.out.println(blu); // System.out.println(red); for (int i = 0; i < red.size() ; i++, ++curr) { if (curr >= red.get(i)) { perm.add(curr); } else { break; } } for (int i = 0; i < perm.size(); i++) { if(perm.get(i) != (i+1)) { yes = false; break; } } // System.out.println(perm); if(yes && perm.size() == n) op.append("YES\n"); else op.append("NO\n"); } System.out.println(op); // END OF CODE } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
bf87167558141aa3058014c12bfe27fa
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.io.*; public class Challenge1 { static boolean vis[]; static HashMap<Integer, Integer> h; public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); long[] arr = new long[n]; for (int i = 0; i < arr.length; i++) { arr[i] = sc.nextLong(); } String s = sc.nextLine(); int r = 0; int b = 0; for (int i = 0; i < arr.length; i++) { if(s.charAt(i)=='R') { r++; } else { b++; } } if(b==0) { Arrays.sort(arr); boolean f = true; long curr = n; for (int i = n-1; i >= 0; i--) { if(arr[i]>curr) { f = false; break; } curr--; } if(f) { pw.println("YES"); } else { pw.println("NO"); } } else { if(r==0) { Arrays.sort(arr); boolean f = true; long curr = 1; for (int i = 0; i<n; i++) { if(arr[i]<curr) { f = false; break; } curr++; } if(f) { pw.println("YES"); } else { pw.println("NO"); } } else { Long[] arrB = new Long[b]; Long[] arrR = new Long[r]; int c1 = 0; int c2 = 0; for (int i = 0; i < arr.length; i++) { if(s.charAt(i)=='R') { arrR[c1++] = arr[i]; } else { arrB[c2++] = arr[i]; } } Arrays.sort(arrR); Arrays.sort(arrB); long curr = 1; boolean f = true; for (int i = 0; i <arrB.length; i++) { if(arrB[i]<curr) { f = false; break; } curr++; } for (int i = 0; i <arrR.length; i++) { if(arrR[i]>curr) { f = false; break; } curr++; } if(f) { pw.println("YES"); } else { pw.println("NO"); } } } } pw.flush(); } public static long fact(long n) { if (n == 1) return 1; return n * fact(n - 1); } public static long pow(long n, long pow) { if (pow == 0) return 1 % 998244353; long res = (pow((n % 998244353) * (n % 998244353), pow / 2)) % 998244353; if (pow % 2 == 1) return (res % 998244353) * (n % 998244353); return res % 998244353; } public static long GCD(long a, long b) { if (a == 0) return b; return GCD(b % a, a); } public static int primeFactors(int n) { // Print the number of 2s that divide n int x = 1; while (n % 2 == 0) { x++; n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i += 2) { // While i divides n, print i and divide n int res = 1; while (n % i == 0) { res++; n /= i; } x = x * res; } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) x = x * 2; return x; } } class item implements Comparable<item> { int n; int c; public item(int n, int c) { this.n = n; this.c = c; } @Override public int compareTo(item o) { return c - o.c; } } class segTree { int[] sTree; int iden; int n; public segTree(int N) { n = 1; while (n < N) n <<= 1; sTree = new int[n << 1]; } public int q(int l, int r) { return query(1, l, r, 1, n); } public int query(int curr, int qL, int qR, int l, int r) { if (l >= qL && r <= qR) { return sTree[curr]; } if (r < qL || l > qR) { return 0; } int mid = (l + r) >> 1; int left = query((curr << 1), qL, qR, l, mid); int right = query((curr << 1) + 1, qL, qR, mid + 1, r); return left + right; } public void update(int i, int val) { i = i + n - 1; sTree[i] = val; i >>= 1; while (i >= 1) { sTree[i] = sTree[i << 1] + sTree[(i << 1) + 1]; i >>= 1; } } } class edge implements Comparable<edge> { int from; int dest; long cost; public edge(int from, int dest, long cost) { this.from = from; this.dest = dest; this.cost = cost; } public String toString() { return from + " " + dest + " " + cost; } @Override public int compareTo(edge o) { if (cost < o.cost) return -1; if (cost > o.cost) return 1; return 0; } } class pair { int a; int b; public pair(int a, int b) { this.a = a; this.b = b; } } class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
766661002b071d9c0a883be7faa44faa
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.io.*; public class Main{ static Scanner sc = new Scanner(new BufferedInputStream(System.in)); static int sc(){return sc.nextInt();} static double scd(){return sc.nextDouble();} static char[] carr(){return sc.next().toCharArray();} static long scl(){return sc.nextLong();} static String scs(){return sc.next();} static char scf(){return sc.next().charAt(0);} static char[] chars(){String s = sc.next();char[] c = new char[s.length()+1];for(int i = 1;i<=s.length();i++){c[i] = s.charAt(i-1);}return c;} // static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // static int toi(String s){return Integer.parseInt(s);} // static double tod(String s) {return Double.parseDouble(s);} // static long tol(String s) {return Long.parseLong(s);} // static int[] geti() throws Exception{String[] s = sarr();int[] a = new int[s.length];for(int i = 0;i<s.length;i++) {a[i] = Integer.parseInt(s[i]);}return a;} // static long[] getl() throws Exception{String[] s = sarr();long[] a = new long[s.length];for(int i = 0;i<s.length;i++) {a[i] = Long.parseLong(s[i]);}return a;} // static double[] getd() throws Exception{String[] s = sarr();double[] a = new double[s.length];for(int i = 0;i<s.length;i++) {a[i] = Double.parseDouble(s[i]);}return a;} // static char[] carr() throws Exception{return br.readLine().toCharArray();} // static char[] chars() throws Exception{char[] c = carr();char[] res = new char[c.length+1];System.arraycopy(c, 0, res, 1, c.length);return res;} // static String[] sarr() throws Exception{return br.readLine().split(" ");} // static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); // static int sc() throws Exception{in.nextToken();return (int)in.nval;} // static double scd() throws Exception{in.nextToken();return in.nval;} static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static void print(Object o){out.print(o);} static void println(Object o){out.println(o);} static void println(){out.println();} static void Y(){println("YES");} static void N(){println("NO");} static final int N = (int)2e6+10,M = 2010,P = 131,MOD = (int)1e9+7; static int n; static int[] dx = new int[]{0,-1,0,1},dy = new int[]{-1,0,1,0}; static int INF= 0x3f3f3f3f; // static int[] h = new int[N],e = new int[N],ne = new int[N],w = new int[N]; // static int idx; static int[] a = new int[N]; public static void main(String[] args) throws Exception{ int t = sc(); while(t-->0){ n = sc(); for(int i = 0;i<n;i++) a[i] = Math.min(Math.max(0,sc()),n+1); char[] c = carr(); int blen = 0,rlen = 0; for(int i = 0;i<n;i++) { if(c[i] == 'B') blen++; else rlen++; } int[] h1 = new int[blen+1]; int[] h2 = new int[n+2]; for(int i = 0;i<n;i++){ if(c[i] == 'B') h1[Math.min(blen,a[i])]++; else h2[Math.max(blen+1,a[i])]++; } long sum1 = 0; boolean f = true; for(int i = blen;i>0;i--){ sum1+=h1[i]; if(sum1<=blen-i) { f = false; break; } } sum1 = 0; for(int i = blen+1;i<=n;i++){ sum1+=h2[i]; if(sum1<=i-blen-1){ f = false; break; } } println(f?"YES":"NO"); } out.close(); } static class D{int x,y;D(int x,int y){this.x = x;this.y = y;}} }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
555726982446de8d2c22a5b395eb27a6
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
/******** * @author Brennan Cox * ********/ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class Main { public Main() { FastScanner input = new FastScanner(System.in); StringBuilder output = new StringBuilder(); int t = input.nextInt(); for (int i = 0; i < t; i++) { int n = input.nextInt(); ArrayList<Integer> blue = new ArrayList<>(); //blue container ArrayList<Integer> red = new ArrayList<>(); //red container { int[] arr = new int[n]; for (int j = 0; j < arr.length; j++) { arr[j] = input.nextInt(); } char[] arrChar = input.next().toCharArray(); for (int j = 0; j < arrChar.length; j++) { if (arrChar[j] == 'B') { blue.add(arr[j]); } else { red.add(arr[j]); } } } //sort Collections.sort(blue); Collections.sort(red); //double pointer int bPoint = 0; int rPoint = 0; boolean canDo = true; /* * The idea is that because all blue tokens can decrease to * any value smaller than themselves you want to take the * smallest blue values and make those fit first (wherever they can) * * then you want to try and fill the gaps that blue cannot with red * tokens because they can increase to any greater value * * if neither blue nor red can fill the gaps then * because we are starting at the least blue values and * least red values then that gap is un-fillable by either color * <-1 <-5 7-> 8-> * the hole ^^ in this line is where 6 should be located */ for (int j = 1; j <= n && canDo; j++) { if (bPoint < blue.size() && blue.get(bPoint) >= j) { bPoint++; } else if (rPoint < red.size() && red.get(rPoint) <= j) { rPoint++; } else { canDo = false; } } output.append(canDo ? "YES":"NO"); output.append("\n"); } System.out.println(output); } public static void main(String[] args) { new Main(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner(InputStream in) { this(new InputStreamReader(in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next());} } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
baa221b80dc3becbc8dd4efe0fc0cd75
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
/******** * @author Brennan Cox * ********/ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class Main { public Main() { FastScanner input = new FastScanner(System.in); StringBuilder output = new StringBuilder(); int t = input.nextInt(); for (int i = 0; i < t; i++) { int n = input.nextInt(); ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); { int[] arr = new int[n]; for (int j = 0; j < arr.length; j++) { arr[j] = input.nextInt(); } char[] arrChar = input.next().toCharArray(); for (int j = 0; j < arrChar.length; j++) { if (arrChar[j] == 'B') { blue.add(arr[j]); } else { red.add(arr[j]); } } } Collections.sort(blue); Collections.sort(red); int bPoint = 0; int rPoint = 0; boolean canDo = true; for (int j = 1; j <= n && canDo; j++) { if (bPoint < blue.size() && blue.get(bPoint) >= j) { bPoint++; } else if (rPoint < red.size() && red.get(rPoint) <= j) { rPoint++; } else { canDo = false; } } output.append(canDo ? "YES":"NO"); output.append("\n"); } System.out.println(output); } public static void main(String[] args) { new Main(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner(InputStream in) { this(new InputStreamReader(in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next());} } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
a3429d9d62e0082b87aa5742aea18359
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import static java.lang.Math.*; import java.io.*; import java.math.*; import java.util.*; public class cf2 { 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) { int t = x.nextInt(); StringBuilder str = new StringBuilder(); while (t > 0) { int n = x.nextInt(); int a[] =new int[n]; HashSet<Integer> hs = new HashSet<>(); for(int i=0;i<n;i++) { a[i] =x.nextInt(); } String s =x.next(); ArrayList<Integer> b= new ArrayList<>(); ArrayList<Integer> r= new ArrayList<>(); for(int i=0;i<n;i++) { if(s.charAt(i)=='B') { b.add(a[i]); }else { r.add(a[i]); } } Collections.sort(b);Collections.sort(r); int st =1; boolean pos=true; int l=n; for(int i=0;i<b.size();i++) { if(st<=b.get(i)) { hs.add(st); st++; }else { pos=false; break; } } if(pos) { for(int i=r.size()-1;i>=0;i--) { if(l>=r.get(i)) { hs.add(l); l--; }else { pos=false; break; } } } if(pos) { System.out.println("YES"); }else { System.out.println("NO"); } str.append("\n"); t--; } out.println(str); out.flush(); } /*--------------------------------------------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; } } /*--------------------------------------------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 int pow(int x, int y) { int 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 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
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
02aa884e43d7ca4db98e5b5f716c9781
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.PriorityQueue; public class HelloWorld { public static void main(String []args) throws java.io.IOException{ BufferedReader scan = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(scan.readLine()); while(t-- > 0) { int n = Integer.parseInt(scan.readLine()); String[] input = scan.readLine().split(" "); String colors = scan.readLine(); PriorityQueue<Integer> blueQ = new PriorityQueue<>(); PriorityQueue<Integer> redQ = new PriorityQueue<>(); for(int i=0; i<n; i++) { if(colors.charAt(i) == 'R') redQ.add(Integer.parseInt(input[i])); else blueQ.add(Integer.parseInt(input[i])); } boolean flag = true; int target = 1; while(!blueQ.isEmpty() || !redQ.isEmpty()) { if(!blueQ.isEmpty() && !redQ.isEmpty()) { if(blueQ.peek() >= target) blueQ.poll(); else if(redQ.peek() <= target) redQ.poll(); else { flag = false; break; } } else if(!blueQ.isEmpty()) { if(blueQ.peek() >= target) blueQ.poll(); else { flag = false; break; } } else { if(redQ.peek() <= target) redQ.poll(); else { flag = false; break; } } target++; } if(flag) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
f38c552da06f26f65572f4e5f760c636
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; import java.util.Stack; import java.util.stream.Stream; public class CasimirString { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws NumberFormatException, IOException { int cases = Integer.parseInt(reader.readLine()); while(cases-- > 0) { String[] firstLine = reader.readLine().split(" "); int n = Integer.parseInt(firstLine[0]); //int m = Integer.parseInt(firstLine[1]); int[] arr = convertToIntPrimitiveArray(reader.readLine().split(" ")); String str = reader.readLine(); List<Integer> increase = new ArrayList<>(); List<Integer> decrease = new ArrayList<>(); for(int i=0;i<str.length();i++) { if(str.charAt(i) == 'R') { increase.add(arr[i]); }else { decrease.add(arr[i]); } } Collections.sort(increase); Collections.sort(decrease); //System.out.println(increase); //System.out.println(decrease); int j = 0; int k = 0; boolean poss = true; for(int i=1;i<=n;i++) { if(j<decrease.size() && i<=decrease.get(j)) { // if(i > decrease.get(j)) { // poss = false; // break; // } j++; }else if(k<increase.size() && i>=increase.get(k)){ // if(i < increase.get(k)) { // poss = false; // break; // } k++; }else { poss = false; break; } } if(poss) { printYes(); }else { printNo(); } } out.flush(); } public static long func(long start, long end, long total, long k) { long mid = 0; while(start < end) { mid = start + (end-start)/2; long val = 0; if(mid > k) { long temp = 2*k - 1 - mid; val = ((k)*(k+1))/2 + (k*(k-1))/2 - (temp*(temp+1))/2; }else { val = (mid*(mid+1))/2; } //System.out.println(mid + " " + val); if(val < total) { start = mid+1; }else { end = mid; } } if(start >= 2*k) { return start-1; } return start; } public static int[] convertToIntPrimitiveArray(String[] str) { return Stream.of(str).mapToInt(Integer::parseInt).toArray(); } public static Integer[] convertToIntArray(String[] str) { Integer[] arr = new Integer[str.length]; for(int i=0;i<str.length;i++) { arr[i] = Integer.parseInt(str[i]); } return arr; } public static Long[] convertToLongArray(String[] str) { Long[] arr = new Long[str.length]; for(int i=0;i<str.length;i++) { arr[i] = Long.parseLong(str[i]); } return arr; } public static void printYes() throws IOException { out.append("YES" + "\n"); } public static void printNo() throws IOException { out.append("NO" + "\n"); } public static void printNumber(long num) throws IOException { out.append(num + "\n"); } public static long hcf(long a, long b) { if(b==0) return a; return hcf(b, a%b); } public static int findSet(int[] parent, int[] rank, int v) { if(v==parent[v]) { return v; } parent[v] = findSet(parent, rank, parent[v]); return parent[v]; } public static void unionSet(int[] parent, int[] rank, int a, int b) { a = findSet(parent, rank, a); b = findSet(parent, rank, b); if(a == b) { return; } if(rank[a] < rank[b]) { int temp = a; a = b; b = temp; } parent[b] = a; if(rank[a] == rank[b]) { rank[a]++; } } public static void makeSet(int[] parent, int[] rank, int v) { parent[v] = v; rank[v] = 0; } public long modularDivision(long a, long b, int mod) { a %= mod; long res = 1L; while(b > 0) { if((b&1)==1) { res = (res * a)%mod; } a = (a*a)%mod; b >>= 1; } return res; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
250f1415261bc5cf5e0defe6507fca89
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; import java.util.Stack; import java.util.stream.Stream; public class CasimirString { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws NumberFormatException, IOException { int cases = Integer.parseInt(reader.readLine()); while(cases-- > 0) { String[] firstLine = reader.readLine().split(" "); int n = Integer.parseInt(firstLine[0]); Integer[] arr = convertToIntArray(reader.readLine().split(" ")); String color = reader.readLine(); List<Integer> increase = new ArrayList<>(); List<Integer> decrease = new ArrayList<>(); for(int i=0;i<color.length();i++) { if(color.charAt(i) == 'R') { increase.add(arr[i]); }else { decrease.add(arr[i]); } } Collections.sort(increase); Collections.sort(decrease); int j = 0; int k = 0; boolean poss = true; for(int i=1;i<=n;i++) { if(j<decrease.size()) { if(i > decrease.get(j)) { poss = false; break; } j++; }else { if(i < increase.get(k)) { poss = false; break; } k++; } } if(poss) { printYes(); }else { printNo(); } } out.flush(); } public static Integer[] convertToIntArray(String[] str) { Integer[] arr = new Integer[str.length]; for(int i=0;i<str.length;i++) { arr[i] = Integer.parseInt(str[i]); } return arr; } public static Long[] convertToLongArray(String[] str) { Long[] arr = new Long[str.length]; for(int i=0;i<str.length;i++) { arr[i] = Long.parseLong(str[i]); } return arr; } public static void printYes() throws IOException { out.append("YES" + "\n"); } public static void printNo() throws IOException { out.append("NO" + "\n"); } public static void printNumber(long num) throws IOException { out.append(num + "\n"); } public static long hcf(long a, long b) { if(b==0) return a; return hcf(b, a%b); } public static int findSet(int[] parent, int[] rank, int v) { if(v==parent[v]) { return v; } parent[v] = findSet(parent, rank, parent[v]); return parent[v]; } public static void unionSet(int[] parent, int[] rank, int a, int b) { a = findSet(parent, rank, a); b = findSet(parent, rank, b); if(a == b) { return; } if(rank[a] < rank[b]) { int temp = a; a = b; b = temp; } parent[b] = a; if(rank[a] == rank[b]) { rank[a]++; } } public static void makeSet(int[] parent, int[] rank, int v) { parent[v] = v; rank[v] = 0; } } class Node { StringBuilder sb; int index; Node(StringBuilder sb, int index) { this.sb = sb; this.index = index; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
cdda673a45a107ac9c5a6a18d11da0ba
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; public class BlueRed { public static void solve(FastScanner fs) { int num = fs.nextInt(); int[] arr = new int[num]; ArrayList<Integer> redArr = new ArrayList<>(); ArrayList<Integer> blueArr = new ArrayList<>(); String[] line = fs.nextLine().split(" "); for(int i=0;i<num;i++){ arr[i] = Integer.parseInt(line[i]); } String[] line1 = fs.nextLine().split(""); for(int i=0;i<num;i++){ String str = line1[i]; if(str.equals("R")){ redArr.add(arr[i]); } else{ blueArr.add(arr[i]); } } Collections.sort(redArr); Collections.sort(blueArr); PrintWriter writer = new PrintWriter(System.out,true); int start =1; int bluePointer =0; int redPointer=0; while(start<=num){ if(bluePointer< blueArr.size() && start<=blueArr.get(bluePointer)){ start+=1; bluePointer+=1; } else if(redPointer<redArr.size() && start>=redArr.get(redPointer)){ start+=1; redPointer+=1; } else{ writer.println("NO"); return; } } writer.println("YES"); } public static void main(String args[]) { FastScanner fs = new FastScanner(); int t = 1; t = fs.nextInt(); while (t-- > 0) { solve(fs); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
128aaf683f1586f4d1e7066a621a9e38
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
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; /** * @author Micgogi * on 11/17/2021 4:51 PM * Rahul Gogyani */ public class D1607 { public static void main(String args[]) { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- != 0) { int n = sc.nextInt(); Long a[] = new Long[n]; for (int i = 0; i < a.length; i++) { a[i] = sc.nextLong(); } String s = sc.nextLine(); List<Long> blue = new ArrayList<>(); List<Long> red = new ArrayList<>(); for (int i = 0; i < a.length; i++) { if (s.charAt(i) == 'B') blue.add(a[i]); else red.add(a[i]); } Collections.sort(blue); Collections.sort(red); boolean flag = true; int current = 1; for (int i = 0; i < blue.size(); i++) { if (blue.get(i) < current) { flag = false; } current++; } for (int i = 0; i < red.size(); i++) { if (red.get(i) > current) { flag = false; } current++; } System.out.println(flag ? "YES" : "NO"); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in), 32768); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[][] read2DArray(int n) { int[][] a = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i][j] = nextInt(); } } return a; } int[][] read2DArray(int n, int m) { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextInt(); } } return a; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
6eef1f1c34a7ead06442cf2780c6a2a6
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.util.*; public class problem1607d { 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(); } Pair arr[] = new Pair[n]; String color = sc.next(); for(int i=0; i<n; i++){ arr[i] = new Pair(a[i], color.charAt(i)); } Arrays.sort(arr); boolean ans = true; for(int i=0; i<n; i++){ boolean found = false; if(arr[i].color == 'B' && arr[i].val >= i+1){ found = true; } else if(arr[i].color == 'R' && arr[i].val <= i+1){ found = true; } if(found == false){ ans = false; break; } } if(ans){ System.out.println("Yes"); } else{ System.out.println("No"); } } } } class Pair implements Comparable<Pair> { int val; char color; public Pair(int val, char c){ this.val = val; this.color = c; } public int compareTo(Pair p){ if(this.color == 'B' && p.color == 'R'){ return -1; } else if(this.color == 'R' && p.color == 'B'){ return 1; } else{ return this.val - p.val; } } } class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
90bb1c8d23a37268d1508b7897d38b31
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.awt.Container; import java.awt.Point; import java.io.*; import java.util.*; //import javax.naming.directory.NoSuchAttributeException; public class Main { static FastScanner scr=new FastScanner(); // static Scanner scr=new Scanner(System.in); static PrintStream out=new PrintStream(System.out); static StringBuilder sb=new StringBuilder(); 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(); } long gcd(long a,long b){ if(b==0) { return a; } return gcd(b,a%b); } int gcd(int a,int b) { if(a*b==0) { return a+b; } return gcd(b,a%b); } long modPow(long base,long exp) { if(exp==0) { return 1; } if(exp%2==0) { long res=(modPow(base,exp/2)); return (res*res); } return (base*modPow(base,exp-1)); } long []reverse(long[] count){ long b[]=new long[count.length]; int index=0; for(int i=count.length-1;i>=0;i--) { b[index++]=count[i]; } return b; } int []reverse(int[] count){ int b[]=new int[count.length]; int index=0; for(int i=count.length-1;i>=0;i--) { b[index++]=count[i]; } return b; } int nextInt() { return Integer.parseInt(next()); } long getMax(long a[],int n) { long max=Long.MIN_VALUE; for(int i=0;i<n;i++) { max=Math.max(a[i], max); } return max; } long[] readLongArray(int n) { long [] a=new long [n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; }int[] readArray(int n) { int [] a=new int [n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } 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; } } static class triplet{ long x; long y; long z; triplet(long x,long y,long z){ this.x=x; this.y=y; this.z=z; } } static class pair{ int x; int y; pair( int x,int y){ this.x=x; this.y=y; } } static Solve s=new Solve(); public static void main(String []args) { int t=scr.nextInt(); // int t=1; // s.preprocess(200000+1); while(t-->0) { s.solve(); } out.println(sb); } static class Solve { int MAX=Integer.MAX_VALUE; int MIN=Integer.MIN_VALUE; ArrayList<Integer>list[]; long mod=998244353; long time[]; boolean visited[]; void solve() { int n=scr.nextInt(); int []a=scr.readArray(n); char c[]=scr.next().toCharArray(); ArrayList<Integer>red=new ArrayList<>(); ArrayList<Integer>blue=new ArrayList<>(); for(int i=0;i<n;i++) { if(c[i]=='R')red.add(a[i]); else blue.add(a[i]); } boolean good=true; int x=1; Collections.sort(blue); Collections.sort(red); for(int i=0;i<blue.size();i++) { if(blue.get(i)<x) { good=false; } x++; } for(int i=0;i<red.size();i++) { if(red.get(i)>x) { good=false; } x++; } String s=good?"YES":"NO"; out.println(s); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
bf42dd85903c8a01e3baff85ca235a58
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.io.*; public class D { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); while(T-- > 0) { int n = in.nextInt(); int[] a = new int[n]; for(int j=0;j<n;j++) a[j] = in.nextInt(); char[] s = in.next().toCharArray(); List<Integer> blue = new ArrayList<>(); List<Integer> red = new ArrayList<>(); for(int j=0;j<n;j++) { if(s[j] == 'B') blue.add(a[j]); else red.add(a[j]); } Collections.sort(blue); Collections.sort(red); boolean p = true; int cur = 1; for(int val : blue) { if(val<cur) { p = false; break; } else cur++; } for(int val : red) { if(val>cur) { p = false; break; } else cur++; } if(p) System.out.println("yes"); else System.out.println("no"); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
cdd8f30154085983071f6b860d40916e
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.TreeSet; public class Main { static int t; static int n; static int[] a; static ArrayList<Integer> blue, red; public static void main(String[]args) throws IOException{ String[]data; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); t = Integer.valueOf(br.readLine()); while((t--)!=0){ blue = new ArrayList<>(); red = new ArrayList<>(); n = Integer.valueOf(br.readLine()); data = br.readLine().split(" "); a = new int[n]; for(int i = 0 ; i < n ; ++i) a[i] = Integer.valueOf(data[i]); char[]s = br.readLine().toCharArray(); for(int i = 0 ; i < n ; ++i) if(s[i] == 'B') blue.add(a[i]); else red.add(a[i]); blue.sort((e1,e2)-> e1-e2); red.sort((e1,e2)-> e2-e1); System.out.println(solve() ? "YES" : "NO"); } } private static boolean solve() { int index = 1; for(int e : blue){ if(e < index) return false; index++; } System.out.println(""); index = n; for(int e : red){ if(e > index) return false; index--; } return true; } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
79c5296d216bbbb0f7ebd1bfe778428e
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
// Main Code at the Bottom import java.util.*; import java.io.*; import java.sql.Time; public class Main implements Runnable{ //Fast IO class static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { boolean env=System.getProperty("ONLINE_JUDGE") != null; //env=true; if(!env) { try { br=new BufferedReader(new FileReader("src\\input.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } } else br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long MOD=(long)1e9+7; //debug static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); //Global variables and functions class Pair{ char color; long val; Pair(char c, long v) { color = c; val = v; } } //Main function(The main code starts from here) public void run() { int test=1; test=sc.nextInt(); while(test-->0) { int n = sc.nextInt(); long a[] = new long[n]; for(int i = 0; i < n; i++) { a[i] = sc.nextLong(); } char s[] = sc.nextLine().toCharArray(); Pair x[] = new Pair[n]; for(int i = 0; i < n; i++) { x[i] = new Pair(s[i], a[i]); } Arrays.parallelSort(x, new Comparator<Pair>() { public int compare(Pair p1, Pair p2) { if(p1.color == p2.color) return Long.compare(p1.val, p2.val); if(p1.color == 'B') return -1; return 1; } }); String ans = "YES"; int i = 0; for(; i < n; i++) { if(x[i].color =='R') break; if(x[i].val <= i) ans = "NO"; } for(; i < n; i++) { if(x[i].val > i + 1) { ans = "NO"; } } out.println(ans); } out.flush(); out.close(); } public static void main (String[] args) throws java.lang.Exception { new Thread(null, new Main(), "anything", (1<<28)).start(); } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
23b24fc3098a332e72ea674c100e5346
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { // -- static variables --- // static FastReader sc = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static int mod = (int) 1000000007; public static void main(String[] args) throws Exception { int t=sc.nextInt(); while(t-->0) Main.go(); // out.println(); out.flush(); } // >>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<< // static class pair{ int x;char y; pair(int x,char y){ this.x=x; this.y=y; } } static void go() throws Exception { int n=sc.nextInt(); int a[]=sc.intArray(n); char c[]=sc.next().toCharArray(); pair[] p=new pair[n]; HashSet<Integer> set=new HashSet<>(); ArrayList<Integer> aa=new ArrayList<>(); ArrayList<Integer> bb=new ArrayList<>(); for(int i=0;i<n;i++) { if(c[i]=='R') { aa.add(a[i]); }else { bb.add(a[i]); } } Collections.sort(aa); Collections.reverse(aa); int temp=n; for(int i=0;i<aa.size();i++) { if(aa.get(i)>n) { out.println("NO"); return; }else { if(aa.get(i)<=temp) { set.add(temp); temp-=1; } } } Collections.sort(bb); int st=1; for(int i=0;i<bb.size();i++) { if(bb.get(i)<1) { out.println("NO"); return; }else { if(bb.get(i)>=st) { set.add(st); st+=1; } } } if(set.size()==n) { out.println("yes"); }else out.println("no"); } static long lcm(long a,long b) { return a*b/gcd(a,b); } // >>>>>>>>>>> Code Ends <<<<<<<<< // // --For Rounding--// static double round(double value, int places) { if (places < 0) throw new IllegalArgumentException(); BigDecimal bd = new BigDecimal(Double.toString(value)); bd = bd.setScale(places, RoundingMode.HALF_UP); return bd.doubleValue(); } // ----Greatest Common Divisor-----// static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } // --- permutations and Combinations ---// static long fact[]; static long invfact[]; static long ncr(int n, int k) { if (k < 0 || k > n) { return 0; } long x = fact[n]; long y = fact[k]; long yy = fact[n - k]; long ans = (x / y); ans = (ans / yy); return ans; } // ---sieve---// static int prime[] = new int[1000006]; // static void sieve() { // Arrays.fill(prime, 1); // prime[0] = 0; // prime[1] = 0; // for (int i = 2; i * i <= 1000005; i++) { // if (prime[i] == 1) // for (int j = i * i; j <= 1000005; j += i) { // prime[j] = 0; // } // } // } // ---- Manual sort ------// static void sort(long[] a) { ArrayList<Long> aa = new ArrayList<>(); for (long i : a) { aa.add(i); } Collections.sort(aa); for (int i = 0; i < a.length; i++) a[i] = aa.get(i); } static void sort(int[] a) { ArrayList<Integer> aa = new ArrayList<>(); for (int i : a) { aa.add(i); } Collections.sort(aa); for (int i = 0; i < a.length; i++) a[i] = aa.get(i); } // --- Fast exponentiation ---// static long pow(long x, long y) { long res = 1l; while (y != 0) { if (y % 2 == 1) { res = (x * res); } y /= 2; x = (x * x); } return res; } // >>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<< // static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] intArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); return a; } long[] longArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = sc.nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
dabede4d08aff3386865c892616ca293
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
//package Codeforces; import java.util.*; public class Problem3 { 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]; PriorityQueue<Integer> blue= new PriorityQueue<Integer>(); PriorityQueue<Integer> red= new PriorityQueue<Integer>(); for(int i=0;i<n;i++) { ar[i]=sc.nextInt(); } char[] colors = sc.next().toCharArray(); for(int i=0;i<colors.length;i++) { if(colors[i] == 'R') red.add(ar[i]); else blue.add(ar[i]); } int i=1; while(i<=n) { if(blue.size()>0 && blue.peek()>=i) blue.poll(); else if(red.size()>0 && red.peek()<=i) red.poll(); else break; i++; } if(i>n) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output
PASSED
f01ca337360816a8442becf92c65fd1c
train_108.jsonl
1635863700
You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.
256 megabytes
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { public static void main (String[] args) throws java.lang.Exception { InputReader sc=new InputReader(System.in); int t = sc.readInt(); PrintWriter pw=new PrintWriter(System.out); while(t-->0){ int n = sc.readInt(); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i]=sc.readInt(); } String c = sc.readLine(); ArrayList<Integer> b = new ArrayList<>(); ArrayList<Integer> r = new ArrayList<>(); for(int i=0;i<c.length();i++){ if(c.charAt(i)=='R') r.add(a[i]); else{ b.add(a[i]); } } Collections.sort(r); Collections.sort(b); if(r.size()>0 && r.get(r.size()-1)>n){ pw.println("NO"); continue; } int j=0; int k=0; int i=1; for(;i<=n;i++){ if(j<b.size() && b.get(j)>=i){ j++; } else if(k<r.size() && r.get(k)<=i){ k++; } else{ pw.println("NO"); break; } } if(i==n+1) pw.println("YES"); } pw.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public 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 { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String readLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"]
1 second
["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"]
NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.
Java 8
standard input
[ "greedy", "math", "sortings" ]
c3df88e22a17492d4eb0f3239a27d404
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.
1,300
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
standard output