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
7cf1b1e67f22e6ffa8f0265a4aeeb05b
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 Solution { public static void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int[] A = new int[n]; for (int i = 0; i < n; i++) { A[i] = in.nextInt(); } String st = in.next(); List<Integer> blue = new ArrayList<>(n); List<Integer> red = new ArrayList<>(); for (int i = 0; i < n; i++) { if (st.charAt(i) == 'R') { if (A[i] <= n) { red.add(A[i]); } } else { if (A[i] >= 1) { blue.add(A[i]); } } } Collections.sort(blue); Collections.sort(red); int rp = 0, rl = red.size(), bp = 0, bl = blue.size(); boolean ans = true; for (int i = 1; i <= n; i++) { if (bp < bl && blue.get(bp) >= i) { bp++; } else if (rp < rl && red.get(rp) <= i) { rp++; } else { ans = false; } } out.println(ans ? "YES" : "NO"); } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); solve(in, out); out.close(); } static 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()); } String nextLine() throws Exception{ String str = ""; try{ str = reader.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } 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
76538a20b4950be1aba11c0084de4372
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 static java.lang.Math.*; public class D { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = in.nextInt(); for(int tt = 0; tt < t; tt++) { int n = in.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = in.nextLong(); char[] s = in.next().toCharArray(); if (solve(arr, n, s))pw.println("YES"); else pw.println("NO"); } pw.close(); } static boolean solve(long[] arr, int n, char[] s) { ArrayList<Long> B = new ArrayList<>(); ArrayList<Long> R = new ArrayList<>(); for(int i = 0; i < n; i++) { if (s[i] == 'B') B.add(arr[i]); else R.add(arr[i]); } Collections.sort(B); Collections.sort(R); // debug(B); long last = n; for (int i = R.size() - 1; i >= 0; i--) { long v = R.get(i); if (v > last) { return false; } last--; } long first = 1; int size = B.size(); for (int i = 0; i < size; i++) { long v = B.get(i); // debug(v, first); if (v < first) return false; first++; } return true; } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
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
98cd94b3148740b802c0c7a3ff2a95c5
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.*; /* I can't live without doing CP! Never Give UP! Consistency is the key! */ public class Coder { static StringBuffer str=new StringBuffer(); static long x,n; static List<Long> blue; static List<Long> red; static void solve(){ Collections.sort(blue); Collections.sort(red); int l=0, r=blue.size()-1; int l1=0, r1=red.size()-1; for(int i=1;i<=n;i++){ if(l>=0 && l<=r && r>=0){ if(blue.get(l)>=i){ l++; continue; } if(blue.get(r)>=i){ r--; continue; } } if(l1>=0 && l1<=r1 && r1>=0){ if(red.get(l1)<=i){ l1++; continue; } if(red.get(r1)<=i){ r1--; continue; } } str.append("NO\n");return; } str.append("YES\n"); } public static void main(String[] args) throws java.lang.Exception { BufferedReader bf; PrintWriter pw; boolean lenv=false; if(lenv){ bf = new BufferedReader( new FileReader("input.txt")); pw=new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); }else{ bf = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new OutputStreamWriter(System.out)); } int q = Integer.parseInt(bf.readLine().trim()); while (q-- > 0) { n=Integer.parseInt(bf.readLine().trim()); blue=new ArrayList<>(); red=new ArrayList<>(); String s[]=bf.readLine().trim().split("\\s+"); char c[]=bf.readLine().trim().toCharArray(); for(int i=0;i<n;i++){ if(c[i]=='B') blue.add(Long.parseLong(s[i])); else red.add(Long.parseLong(s[i])); } solve(); } pw.print(str); pw.flush(); // System.out.print(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
52b56adc38af589bf57dc09ab4f3566d
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.util.Arrays; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Collections; import java.util.TreeMap; import java.util.PriorityQueue; import java.util.LinkedList; import java.util.Iterator; public class First { public static void main(String[] args) { FastScanner fs = new FastScanner(); int T = fs.nextInt(); for (int tt = 0; tt < T; tt++) { solve(fs); } } static void solve(FastScanner fs) { int n=fs.nextInt(); int[] arr=takeInt(n, fs); char[] colors=fs.next().toCharArray(); // for(char c: colors) // p(c); // pn(""); int start=1; int end=n; ArrayList<Pair> ranges=new ArrayList<>(); for(int i=0;i<n;++i) { if(colors[i]=='R') { if(arr[i]>n) continue; if(arr[i]<1) ranges.add(new Pair(1, n)); else ranges.add(new Pair(arr[i], n)); } else { if(arr[i]<1) continue; if(arr[i]>n) ranges.add(new Pair(1, n)); else ranges.add(new Pair(1, arr[i])); } } Collections.sort(ranges); // pn(ranges); HashSet<Integer> set=new HashSet<>(); for(Pair range: ranges) { if(start>=range.l && start<=range.r) { set.add(start); start++; } else { pn("NO"); return; } } if(set.size()==n) pn("YES"); else pn("NO"); } static int MOD=(int)(1e9+7); static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } static void pn(Object o) { System.out.println(o); } static void p(Object o) { System.out.print(o); } static void flush() { System.out.flush(); } static void debugInt(int[] arr) { for(int i=0;i<arr.length;++i) System.out.print(arr[i]+" "); System.out.println(); } static void debugIntInt(int[][] arr) { for(int i=0;i<arr.length;++i) { for(int j=0;j<arr[0].length;++j) System.out.print(arr[i][j]+" "); System.out.println(); } System.out.println(); } static void debugLong(long[] arr) { for(int i=0;i<arr.length;++i) System.out.print(arr[i]+" "); System.out.println(); } static void debugLongLong(long[][] arr) { for(int i=0;i<arr.length;++i) { for(int j=0;j<arr[0].length;++j) System.out.print(arr[i][j]+" "); System.out.println(); } System.out.println(); } static long[] takeLong(int n, FastScanner fs) { long[] arr=new long[n]; for(int i=0;i<n;++i) arr[i]=fs.nextLong(); return arr; } static long[][] takeLongLong(int m, int n, FastScanner fs) { long[][] arr=new long[m][n]; for(int i=0;i<m;++i) for(int j=0;j<n;++j) arr[i][j]=fs.nextLong(); return arr; } static int[] takeInt(int n, FastScanner fs) { int[] arr=new int[n]; for(int i=0;i<n;++i) arr[i]=fs.nextInt(); return arr; } static int[][] takeIntInt(int m, int n, FastScanner fs) { int[][] arr=new int[m][n]; for(int i=0;i<m;++i) for(int j=0;j<n;++j) arr[i][j]=fs.nextInt(); return arr; } 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()); } } } class Pair implements Comparable<Pair> { int l; int r; Pair(int L, int R) { l=L; r=R; } public int compareTo(Pair o) { if(this.l!=o.l) return this.l-o.l; else return this.r-o.r; } public String toString() { return l+":"+r; } }
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
d7d8c13a8f8cdccee711721d62e91a87
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 SolutionB { public static long gcd(long a, long b){ if(b==0){ return a; } return gcd(b, a%b); } public static long gcdSum(long b){ long a = 0; long temp = b; while(temp!=0){ a = a + temp%10; temp = temp/10; } return gcd(a,b); } public static class Pair{ Long a; Long b; public Pair(Long a, Long b) { this.a = a; this.b = b; } } public static long factorial (long n){ if(n==0) return 1; else if(n==1) return n; return n * factorial(n-1); } public static long lcm (long n){ if(n<3) return n; return lcmForBig(n,n-1); } private static long lcmForBig(long n, long l) { if(l==1) return n; n = (n * l) / gcd(n, l); return lcmForBig(n, l-1); } public static void main(String[] args){ Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int i =0;i<t;i++) { int n = s.nextInt(); int arr [] = new int[n]; ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for(int j=0;j<n;j++){ int num = s.nextInt(); arr[j]=num; } String color = s.next(); for(int j=0;j<n;j++){ if(color.charAt(j)=='B'){ blue.add(arr[j]); } else{ red.add(arr[j]); } } Collections.sort(blue); String ans = "YES"; int counter = 0; for(int j=0;j<blue.size();j++){ int current = blue.get(j); if (current<1){ ans="NO"; break; } if(current>counter){ counter++; } else{ ans="NO"; break; } } if(ans=="NO"){ System.out.println(ans); } else{ int tempCounter = n+1; Collections.sort(red); for(int j=red.size()-1;j>=0;j--){ int current = red.get(j); if(current>=tempCounter){ ans="NO"; break; } else{ tempCounter--; } } if(tempCounter-counter!=1) System.out.println("NO"); else System.out.println(ans); } } return; } }
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
bc5296dbd19fb54b96bc81cd4b642c7b
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 { static int M = 998244353; static Random rng = new Random(); private static boolean testCase(int n, int[] a, String colors) { boolean[] contains = new boolean[n]; int any = 0, rem = n, idx; List<Integer> fromLeft = new ArrayList<>(), fromRight = new ArrayList<>(); for (int i = 0; i < n; i++) { if (colors.charAt(i) == 'B') { if (a[i] <= 0) { return false; } else if (a[i] >= n) { any++; } else { fromLeft.add(a[i]); } } else { if (a[i] > n) { return false; } else if (a[i] <= 1) { any++; } else { fromRight.add(a[i]); } } } sort(fromLeft); sort(fromRight); //System.out.println(fromLeft); //System.out.println(fromRight); idx = 1; for (int i = 0; i < fromLeft.size(); i++) { //try to assign a[i] to idx //System.out.println(String.format("Assign %d to %d", a[i], idx)); if (fromLeft.get(i) < idx) { return false; } else { contains[idx - 1] = true; rem--; idx++; } } idx = n; for (int i = fromRight.size() - 1; i >= 0; i--) { //System.out.println(String.format("Assign %d to %d", a[i], idx)); if (idx < fromRight.get(i)) { return false; } else { contains[idx - 1] = true; rem--; idx--; } } return any >= rem; } public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc. //in.nextLine(); for (int tt = 1; tt <= t; ++tt) { int n = in.nextInt(); int[] a = in.readArray(n); String colors = in.next(); out.println(testCase(n, a, colors) ? "YES" : "NO"); } out.close(); } private 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(); } boolean hasNext() { return st.hasMoreTokens(); } char[] readCharArray(int n) { char[] arr = new char[n]; try { br.read(arr); br.readLine(); } catch (IOException e) { e.printStackTrace(); } return arr; } 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()); } } private static void sort(int[] arr) { int temp, idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } private static void sort(long[] arr) { long temp; int idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } private static <T> void sort(T[] arr) { T temp; int idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } private static <T> void sort(T[] arr, Comparator<? super T> cmp) { T temp; int idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr, cmp); } private static <T extends Comparable<? super T>> void sort(List<T> list) { T temp; int idx; for (int i = list.size() - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = list.get(i); list.set(i, list.get(idx)); list.set(idx, temp); } Collections.sort(list); } private static <T> void sort(List<T> list, Comparator<? super T> cmp) { T temp; int idx; for (int i = list.size() - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = list.get(i); list.set(i, list.get(idx)); list.set(idx, temp); } Collections.sort(list, cmp); } }
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
ce9e46c20dc02ed49eef5a0cd01ab4ef
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 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
9d72a3f73ffe38643ff32464ef1b3b3e
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.*; import static java.util.Arrays.sort; public class CodeforcesTemp { static Reader scan = new Reader(); static FastPrinter out = new FastPrinter(); public static void main(String[] args) throws IOException { int tt = scan.nextInt(); // int tt = 1; StringBuilder sb=new StringBuilder(); outer: for (int tc = 1; tc <= tt; tc++) { int n= scan.nextInt(); int[] arr= scan.nextIntArray(n); String s= scan.next(); ArrayList<Integer> inc=new ArrayList<>(); ArrayList<Integer> dec=new ArrayList<>(); for (int i = 0; i < n; i++) { if(s.charAt(i)=='R'){ inc.add(arr[i]); }else dec.add(arr[i]); } Collections.sort(inc); Collections.sort(dec); int val=n; for (int i = inc.size()-1; i >=0 ; i--) { if(val>=inc.get(i)){ --val; continue ; }else{ sb.append("NO");sb.append("\n"); continue outer; } } for (int i = dec.size()-1; i >=0 ; i--) { if(val<=dec.get(i)){ --val; continue ; }else{ sb.append("NO");sb.append("\n"); continue outer; } } sb.append("YES");sb.append("\n"); } out.println(sb.toString()); out.flush(); out.close(); } static class Reader { private final InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public Reader(InputStream in) { this.in = in; } public Reader() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("not number")); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("not number")); } } } } throw new ArithmeticException( String.format(" overflows long.")); } n = n * 10 + digit; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(long length) { long[] array = new long[(int) length]; for (int i = 0; i < length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public Integer[] nextIntegerArray(int length) { Integer[] array = new Integer[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public int[][] nextIntArrayMulti(int length, int width) { int[][] arrays = new int[width][length]; for (int i = 0; i < length; i++) { for (int j = 0; j < width; j++) arrays[j][i] = this.nextInt(); } return arrays; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width) { long[][] mat = new long[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width) { int[][] mat = new int[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width) { double[][] mat = new double[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width) { char[][] mat = new char[height][width]; for (int h = 0; h < height; h++) { String s = this.next(); for (int w = 0; w < width; w++) { mat[h][w] = s.charAt(w); } } return mat; } } static class FastPrinter extends PrintWriter { public FastPrinter(PrintStream stream) { super(stream); } public FastPrinter() { super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if (x < 0) { sb.append('-'); x = -x; } x += Math.pow(10, -n) / 2; sb.append((long) x); sb.append("."); x -= (long) x; for (int i = 0; i < n; i++) { x *= 10; sb.append((int) x); x -= (int) x; } return sb.toString(); } @Override public void print(float f) { super.print(dtos(f, 20)); } @Override public void println(float f) { super.println(dtos(f, 20)); } @Override public void print(double d) { super.print(dtos(d, 20)); } @Override public void println(double d) { super.println(dtos(d, 20)); } public void printArray(int[] array, String separator) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(int[] array) { this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n - 1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(long[] array, String separator) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(long[] array) { this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n - 1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map) { this.printArray(array, " ", map); } public void printMatrix(int[][] arr) { for (int i = 0; i < arr.length; i++) { this.printArray(arr[i]); } } public void printCharMatrix(char[][] arr, int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { this.print(arr[i][j] + " "); } this.println(); } } } static Random __r = new Random(); static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] arr, int i, int j) { while (i < j) { char temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; i++; j--; } } static void reverse(int[] arr, int i, int j) { while (i < j) { int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; i++; j--; } } static void reverse(long[] arr, int i, int j) { while (i < j) { long temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; i++; j--; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } 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; } private static int getlowest(int l) { if (l >= 1000000000) return 1000000000; if (l >= 100000000) return 100000000; if (l >= 10000000) return 10000000; if (l >= 1000000) return 1000000; if (l >= 100000) return 100000; if (l >= 10000) return 10000; if (l >= 1000) return 1000; if (l >= 100) return 100; if (l >= 10) return 10; return 1; } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } }
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
d56802e9765c8b51ab74991cc2d8995d
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 solve() { int n = in.nextInt(); int[] arr = in.na(n); String st = in.next(); int[] blues = new int[n]; int[] reds = new int[n]; int cb = 0, cr = 0; for(int i = 0; i < n; i++) { if(st.charAt(i) == 'B') { blues[cb++] = arr[i]; } else { reds[cr++] = arr[i]; } } Arrays.sort(blues, 0, cb); Arrays.sort(reds, 0, cr); boolean possible = true; for(int i = 1; i <= cb; i++) { if(blues[i - 1] < i) { possible = false; } } for(int i = cb + 1, j = 0; i <= n; i++, j++) { if(reds[j] > i) { possible = false; } } out.yesNo(possible); } public static void main(String[] args) { in = new Reader(); out = new Writer(); int t = in.nextInt(); while(t-->0) solve(); out.exit(); } static Reader in; static Writer out; static class Reader { private BufferedReader br; private StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } public Reader(String f) { try { br = new BufferedReader(new FileReader(f)); } catch (IOException e) { e.printStackTrace(); } } public int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public double[] nd(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); return a; } public long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[] nca() { return next().toCharArray(); } public String[] ns(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public int nextInt() { ensureNext(); return Integer.parseInt(st.nextToken()); } public double nextDouble() { ensureNext(); return Double.parseDouble(st.nextToken()); } public Long nextLong() { ensureNext(); return Long.parseLong(st.nextToken()); } public String next() { ensureNext(); return st.nextToken(); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); return null; } } private void ensureNext() { if (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } } } static class Writer { private PrintWriter out; public Writer(){ out = new PrintWriter(System.out); } public Writer(String f){ try { out = new PrintWriter(new FileWriter(f)); } catch (IOException e) { e.printStackTrace(); } } public void yesNo(boolean condition) { println(condition?"YES":"NO"); } public void printArray(int[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); } public void printlnArray(int[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); out.println(); } public void printArray(long[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); } public void printlnArray(long[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); out.println(); } public void print(Object o) { out.print(o.toString()); } public void println(Object o) { out.println(o.toString()); } public void println() { out.println(); } public void flush() { out.flush(); } public void exit() { 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 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
28cbd7bf86d704b41b81bba1f73e4008
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.Arrays; import java.util.Comparator; import java.util.List; import java.util.StringTokenizer; public class Main { static FastReader fr; static int defMod = 1000000007; static int arrForIndexSort[]; static Integer map1[]; static Integer map2[]; static int globalVal; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Pair{ int first; int second; Pair(int first, int second){ this.first = first; this.second = second; } @Override public boolean equals(Object b) { Pair a = (Pair)b; if(this.first == a.first && this.second==a.second) { return true; } return false; } } class PairSorter implements Comparator<Main.Pair>{ public int compare(Pair a, Pair b) { if(a.first!=b.first) { return a.first-b.first; } return a.second-b.second; } } static class DoublePair{ double first; double second; DoublePair(double first, double second){ this.first = first; this.second = second; } @Override public boolean equals(Object b) { Pair a = (Pair)b; if(this.first == a.first && this.second==a.second) { return true; } return false; } } class DoublePairSorter implements Comparator<Main.DoublePair>{ public int compare(DoublePair a, DoublePair b) { if(a.second>b.second) { return 1; } else if(a.second<b.second) { return -1; } return 0; } } class IndexSorter implements Comparator<Integer>{ public int compare(Integer a, Integer b) { //desc if(arrForIndexSort[b]==arrForIndexSort[a]) { return b-a; } return arrForIndexSort[b]-arrForIndexSort[a]; } } class ListSorter implements Comparator<List>{ public int compare(List a, List b) { return b.size()-a.size(); } } static class DisjointSet{ int[] dsu; public DisjointSet(int n) { makeSet(n); } public void makeSet(int n) { dsu = new int[n+1]; //*** 1 Based indexing *** for(int i=1;i<=n;i++) { dsu[i] = -1; } } public int find(int i) { while(dsu[i] > 0) { i = dsu[i]; } return i; } public void union(int i, int j) { int iRep = find(i); int jRep = find(j); if(iRep == jRep) { return; } if(dsu[iRep]>dsu[jRep]) { dsu[jRep] += dsu[iRep]; dsu[iRep] = jRep; } else { dsu[iRep] += dsu[jRep]; dsu[jRep] = iRep; } } } static class SegmentTree{ int[] tree; int[] originalArr; public SegmentTree(int[] a) { int n = a.length; tree = new int[4*n]; build(1, a, 0, n-1); originalArr = a; } public void build(int node, int[] a, int start, int end){ if(start == end) { tree[node] = a[start]; return; } int mid = (start+end)/2; build(2*node, a, start, mid); build(2*node+1, a, mid+1, end); tree[node] = Math.min(tree[2*node], tree[2*node+1]); } public void update(int node, int start, int end, int idx, int val) { if(start == end) { tree[node] = originalArr[idx] = val; return; } int mid = (start+ end)/2; if(idx<=mid) { update(2*node, start, mid, idx, val); } else { update(2*node+1, mid+1, end, idx, val); } tree[node] = Math.min(tree[2*node], tree[2*node+1]); } public int query(int node, int start, int end, int l, int r) { if(r<start || l>end) { return Integer.MAX_VALUE; } if(start>=l && end<=r) { return tree[node]; } int mid = (start+end)/2; int p1 = query(2*node, start, mid, l, r); int p2 = query(2*node+1, mid+1, end, l, r); return Math.min(p1, p2); } } //1 Based Indexing static class BIT{ long[] tree; public BIT(int n) { tree = new long[n]; } public BIT(int[] a) { this(a.length); for(int i=1;i<tree.length;i++) { add(i, a[i]); } } public void add(int i, int val) { for(int j=i;j<tree.length;j += j&(-j)) { tree[j] += val; } } public long sum(int i) { long ret = 0; for(int j=i;j>0;j -= j&(-j)) { ret += tree[j]; } return ret; } public long query(int l, int r) { long lSum = 0; if(l>1) { lSum = sum(l-1); } long rSum = sum(r); return rSum-lSum; } } static void shuffle(int a[]) { for (int i = 0; i < a.length; i++) { // getting the random index int t = (int)Math.random() * a.length; // and swapping values a random index // with the current index int x = a[t]; a[t] = a[i]; a[i] = x; } } public static void main(String[] args) { fr = new FastReader(); int T = 1; T = fr.nextInt(); int t1 = T; while (T-- > 0) { solve(t1-T); } } /* Things to remember * Keep it Simple (Golden Rule) * Think Reverse * Don't get stuck on one approach * Check corner case * On error, check->edge case, implementation and question, * On error, check constraints, check if long needed, * On error, which one to choose when two values are equal for greedy * for two different constraints, check if they share same value. like x=2, y=2; */ // 1 1 1 2 3 6 6 static Integer a[]; static String str; public static void solve(int testcase) { int n = fr.nextInt(); Integer indexArr[] = new Integer[n]; a = new Integer[n]; for(int i=0;i<n;i++) { a[i] = fr.nextInt(); indexArr[i] = i; } str = fr.nextLine(); Arrays.sort(indexArr, (a,b)->getVal(a,0)==getVal(b,0)?getVal(a,1)-getVal(b,1):getVal(a,0)-getVal(b,0)); for(int i=0;i<n;i++) { int lowVal = getVal(indexArr[i], 0); int highVal = getVal(indexArr[i], 1); if(i+1<lowVal || i+1>highVal) { System.out.println("NO"); return; } } System.out.println("YES"); //EOF } public static int getVal(int idx, int type) { char c = str.charAt(idx); if(type == 0) { if(c=='B') { return Math.min(a[idx], 1); } else { return Math.max(1, a[idx]); } } else { if(c=='R') { return a.length; } else { return a[idx]; } } } }
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
0f1603d3394b8bb731c389f48520317e
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.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.security.KeyPair; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; /* Name of the class has to be "Main" only if the class is public. */ public class temp { abstract class sort implements Comparator<ArrayList<Integer>>{ @Override public int compare(ArrayList<Integer> a,ArrayList<Integer> b) { return a.get(0)-b.get(0); } } private static Comparator sort; public static void main (String[] args) throws Exception { FastScanner sc= new FastScanner(); int tt = sc.nextInt(); while(tt-->0){ int n=sc.nextInt(); int nums[]=sc.nextInts(n); 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)=='R')red.add(nums[i]); else blue.add(nums[i]); } Collections.sort(red); Collections.sort(blue); boolean bob=true; for(int i=1;i<=blue.size();i++) { if(blue.get(i-1)<i) { bob=false;break; } } if(bob==false) { System.out.println("NO");continue; } bob=true; int k=blue.size()+1; for(int i=0;i<red.size();i++) { if(red.get(i)>k) { bob=false;break; } k++; } if(bob==false) { System.out.println("NO");continue; } System.out.println("YES"); } } public static int fac(int n) { if(n==0) return 1; return n*fac(n-1); } public static boolean palin(String res) { StringBuilder sb=new StringBuilder(res); String temp=sb.reverse().toString(); return(temp.equals(res)); } public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static int gcd(int a, int b) { if(a > b) a = (a+b)-(b=a); if(a == 0L) return b; return gcd(b%a, a); } public static ArrayList<Integer> findDiv(int N) { //gens all divisors of N ArrayList<Integer> ls1 = new ArrayList<Integer>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++) if(N%i == 0) { ls1.add(i); ls2.add(N/i); } Collections.reverse(ls2); for(int b: ls2) if(b != ls1.get(ls1.size()-1)) ls1.add(b); return ls1; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } static class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
Java
["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
155aedccaa6f0eba4f1bc462e4d9d5be
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.*; public class cp { static PrintWriter w = new PrintWriter(System.out); static FastScanner s = new FastScanner(); static int mod = 1000000007; static class Edge { int src; int wt; int nbr; Edge(int src, int nbr, int et) { this.src = src; this.wt = et; this.nbr = nbr; } } class EdgeComparator implements Comparator<Edge> { @Override //return samllest elemnt on polling public int compare(Edge s1, Edge s2) { if (s1.wt < s2.wt) { return -1; } else if (s1.wt > s2.wt) { return 1; } return 0; } } static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static void prime_till_n(boolean[] prime) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. for (int p = 2; p * p < prime.length; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i < prime.length; i += p) { prime[i] = false; } } } // int l = 1; // for (int i = 2; i <= n; i++) { // if (prime[i]) { // w.print(i+","); // arr[i] = l; // l++; // } // } //Time complexit Nlog(log(N)) } static int noofdivisors(int n) { //it counts no of divisors of every number upto number n int arr[] = new int[n + 1]; for (int i = 1; i <= (n); i++) { for (int j = i; j <= (n); j = j + i) { arr[j]++; } } return arr[0]; } static char[] reverse(char arr[]) { char[] b = new char[arr.length]; int j = arr.length; for (int i = 0; i < arr.length; i++) { b[j - 1] = arr[i]; j = j - 1; } return b; } static long factorial(int n) { if (n == 0) { return 1; } long su = 1; for (int i = 1; i <= n; i++) { su = (((su % mod) * ((long) i % mod)) % mod); } while (su < 0) { su += mod; } return su; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } long[] readArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } static class Vertex { int x; int y; int wt; public Vertex(int x, int y) { this.x = x; this.y = y; } public Vertex(int x, int y, int wt) { this.x = x; this.y = y; this.wt = wt; } } public static long power(long x, long n) { if (n == 0) { return 1l; } long pow = power(x, n / 2) % mod; if ((n & 1) == 1) // if `y` is odd { return ((((x % mod) * (pow % mod)) % mod) * (pow % mod)) % mod; } // otherwise, `y` is even return ((pow % mod) * (pow % mod)) % mod; } public static BigInteger factorial2(int N) { // Initialize result BigInteger f = new BigInteger("1"); // Or BigInteger.ONE // Multiply f with 2, 3, ...N for (int i = 2; i <= N; i++) { f = f.multiply(BigInteger.valueOf(i)); } return f; } public static void main(String[] args) { { int t = s.nextInt(); // int t = 1; while (t-- > 0) { solve(); } w.close(); } } public static void solve() { int n = s.nextInt(); int arr[][]= new int[n][2]; for(int i=0;i<n;i++) arr[i][0]=s.nextInt(); String str = s.next(); for(int i=0;i<n;i++) { if(str.charAt(i)=='B') arr[i][1]=-1; else arr[i][1]=1; } for(int i=0;i<n;i++) { if((arr[i][0]<=0 && arr[i][1]==-1)||(arr[i][0]>n && arr[i][1]==1)) { w.println("NO");return; } if(arr[i][0]<=0) arr[i][0]=1; else if(arr[i][0]>n) arr[i][0]=n; } TreeSet<Integer> set = new TreeSet<>(); // for(int i=0;i<n;i++) // set.add(arr[i][0]); Arrays.sort(arr,new Comparator<int[]>(){ public int compare(int[] a, int[] b) { int res = Integer.compare(a[1],b[1]); if(res ==0 && a[1]==1) res = Integer.compare(b[0],a[0]); else if(res ==0 && a[1]==-1) res = Integer.compare(a[0],b[0]); return res; } }); int l =1; int r = n; for(int i=0;i<n;i++) { if(arr[i][1]==1) { if(arr[i][0]<=r) { arr[i][0]=r; r--; set.add(arr[i][0]); } } else { if(arr[i][0]>=l){ arr[i][0]=l; l++; set.add(arr[i][0]); } } } if(set.size()==n) w.println("YES"); else w.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
d7b7699c8333c475363243ebbdda84f4
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 KaiXin * @version 11 * @date 2022-06-14 16:20 */ public class Main { static final int SIZE = (int)2e5 + 5; static int[] a = new int[SIZE]; static ArrayList<Integer>red = new ArrayList<>(); static ArrayList<Integer>blue = new ArrayList<>(); public static void solve(InputReader in, PrintWriter out){ int n = in.nextInt(); blue.clear(); red.clear(); for(int i = 1; i <= n; ++i) a[i] = in.nextInt(); String s = in.next(); for(int i = 1; i <= n; ++i){ if(s.charAt(i - 1) == 'R') red.add(a[i]); else blue.add(a[i]); } blue.sort(Integer::compareTo); red.sort(Integer::compareTo); int sta = n; boolean judge = true; for(int i = red.size() - 1; i >= 0; i--){ if(red.get(i) > sta) { judge = false; break; } sta--; } for(int i = blue.size() - 1; i >= 0; i--){ if(blue.get(i) < sta){ judge = false; break; } sta--; } if(judge) out.println("YES"); else out.println("NO"); } public static void main(String[] args) throws FileNotFoundException { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int T = 1; T = in.nextInt(); for(int i = 1; i <= T; ++i){ solve(in,out); } out.close(); } private static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } /* 3 1 1 1 -1 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
PASSED
ef3effe3d975edff2d2cfd98ae6c2cd8
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.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.PriorityQueue; import java.util.Random; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; // D -> CodeForcesProblemSet public final class D { static FastReader fr = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static final int gigamod = 1000000007; static int t = 1; static double epsilon = 0.00000001; static boolean[] isPrime; static int[] smallestFactorOf; @SuppressWarnings("unused") public static void main(String[] args) { t = fr.nextInt(); OUTER: for (int tc = 0; tc < t; tc++) { int n = fr.nextInt(); long[] arr = fr.nextLongArray(n); char[] colOf = fr.next().toCharArray(); // tell whether we can make it a permutation // blue -> decrease // red -> increase // if there's a blue element greater than 'n', // there's no way to decrease it to make it // right // -- // if there's a red element less than 1, there's // no way to increase it to make it right boolean mess = false; for (int i = 0; i < n; i++) if (colOf[i] == 'R' && arr[i] > n) { mess = true; } else if (colOf[i] == 'B' && arr[i] < 1) { mess = true; } if (mess) { out.println("NO"); continue OUTER; } // sweep line kinda vibes Segment[] segs = new Segment[n]; for (int i = 0; i < n; i++) { if (colOf[i] == 'R') { // upto 'n' int segLo = (int) Math.max(1, arr[i]); int segHi = n; segs[i] = new Segment(segLo, segHi); } else { // from 1 int segLo = 1; int segHi = (int) Math.min(n, arr[i]); segs[i] = new Segment(segLo, segHi); } } // we must be able to assign to every point, one segment PriorityQueue<Segment> pq = new PriorityQueue<>(new Comparator<Segment>() { public int compare(Segment s1, Segment s2) { if (s1.hi < s2.hi) return -1; if (s1.hi > s2.hi) return 1; return Integer.compare(s1.lo, s2.lo); } }); for (int i = 0; i < n; i++) pq.add(segs[i]); int[] perm = new int[n]; for (int i = 1; i < n + 1; i++) { while (!pq.isEmpty() && pq.peek().hi < i) pq.poll(); if (!pq.isEmpty()) { Segment best = pq.poll(); if (best.lo <= i && best.hi >= i) perm[i - 1] = i; } } int min = Arrays.stream(perm).min().getAsInt(); if (min == 0) { out.println("NO"); continue OUTER; } out.println("YES"); } out.close(); } static class Segment { int lo, hi; Segment(int ll, int hh) { lo = ll; hi = hh; } } static class Tuple { int from, idx; Tuple (int ff, int ii) { from = ff; idx = ii; } } public static long[][] maxSparseTable(long[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); long[][] ret = new long[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; }else { ret[i] = new long[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = Math.max(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } public static long sparseRangeMaxQ(long[][] table, int l, int r) { // [a,b) assert l <= r; if(l >= r)return Integer.MIN_VALUE; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(r-l); return Math.max(table[t][l], table[t][r-(1<<t)]); } public static int[][] minSparseTable(int[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); int[][] ret = new int[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; }else { ret[i] = new int[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = Math.min(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } public static int sparseRangeMinQ(int[][] table, int l, int r) { // [a,b) assert l <= r; if(l >= r)return Integer.MAX_VALUE; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(r-l); return Math.min(table[t][l], table[t][r-(1<<t)]); } static class Pair { int low, high, idx; Pair(int vv, int ii, int iiid) { low = vv; high = ii; idx = iiid; } } static boolean isPalindrome(char[] s) { int n = s.length; boolean isPal = true; for (int i = 0; i < n / 2; i++) if (s[i] != s[n - 1 - i]) isPal = false; return isPal; } static char[] nextTime(char[] s, int x) { String hrStr = s[0] + "" + s[1]; String minStr = s[3] + "" + s[4]; int hr = Integer.parseInt(hrStr); int min = Integer.parseInt(minStr); // we just have to add 'x' minutes int numFittingHrs = x / 60; int remMins = x - numFittingHrs * 60; hr = (hr + numFittingHrs) % 24; if ((min + remMins) >= 60) hr++; hr %= 24; min = (min + remMins) % 60; hrStr = hr + ""; if (hr < 10) hrStr = "0" + hrStr; minStr = min + ""; if (min < 10) minStr = "0" + min; return (hrStr + ":" + minStr).toCharArray(); } static class UnionFind { // Uses weighted quick-union with path compression. private int[] parent; // parent[i] = parent of i private int[] size; // size[i] = number of sites in tree rooted at i // Note: not necessarily correct if i is not a root node private int count; // number of components public UnionFind(int n) { count = n; parent = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } // Number of connected components. public int count() { return count; } // Find the root of p. public int find(int p) { while (p != parent[p]) p = parent[p]; return p; } public boolean connected(int p, int q) { return find(p) == find(q); } public int numConnectedTo(int node) { return size[find(node)]; } // Weighted union. public void union(int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return; // make smaller root point to larger one if (size[rootP] < size[rootQ]) { parent[rootP] = rootQ; size[rootQ] += size[rootP]; } else { parent[rootQ] = rootP; size[rootP] += size[rootQ]; } count--; } public static int[] connectedComponents(UnionFind uf) { // We can do this in nlogn. int n = uf.size.length; int[] compoColors = new int[n]; for (int i = 0; i < n; i++) compoColors[i] = uf.find(i); HashMap<Integer, Integer> oldToNew = new HashMap<>(); int newCtr = 0; for (int i = 0; i < n; i++) { int thisOldColor = compoColors[i]; Integer thisNewColor = oldToNew.get(thisOldColor); if (thisNewColor == null) thisNewColor = newCtr++; oldToNew.put(thisOldColor, thisNewColor); compoColors[i] = thisNewColor; } return compoColors; } } static class UGraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; @SuppressWarnings("unchecked") public UGraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); adj[to].add(from); } public HashSet<Integer> adj(int from) { return adj[from]; } public int degree(int v) { return adj[v].size(); } public int V() { return adj.length; } public int E() { return E; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static void dfsMark(int current, boolean[] marked, UGraph g) { if (marked[current]) return; marked[current] = true; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, marked, g); } public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) { if (marked[current]) return; marked[current] = true; if (from != -1) distTo[current] = distTo[from] + 1; HashSet<Integer> adj = g.adj(current); int alreadyMarkedCtr = 0; for (int adjc : adj) { if (marked[adjc]) alreadyMarkedCtr++; dfsMark(adjc, current, distTo, marked, g, endPoints); } if (alreadyMarkedCtr == adj.size()) endPoints.add(current); } public static void bfsOrder(int current, UGraph g) { } public static void dfsMark(int current, int[] colorIds, int color, UGraph g) { if (colorIds[current] != -1) return; colorIds[current] = color; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, colorIds, color, g); } public static int[] connectedComponents(UGraph g) { int n = g.V(); int[] componentId = new int[n]; Arrays.fill(componentId, -1); int colorCtr = 0; for (int i = 0; i < n; i++) { if (componentId[i] != -1) continue; dfsMark(i, componentId, colorCtr, g); colorCtr++; } return componentId; } public static boolean hasCycle(UGraph ug) { int n = ug.V(); boolean[] marked = new boolean[n]; boolean[] hasCycleFirst = new boolean[1]; for (int i = 0; i < n; i++) { if (marked[i]) continue; hcDfsMark(i, ug, marked, hasCycleFirst, -1); } return hasCycleFirst[0]; } // Helper for hasCycle. private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) { if (marked[current]) return; if (hasCycleFirst[0]) return; marked[current] = true; HashSet<Integer> adjc = ug.adj(current); for (int adj : adjc) { if (marked[adj] && adj != parent && parent != -1) { hasCycleFirst[0] = true; return; } hcDfsMark(adj, ug, marked, hasCycleFirst, current); } } } static class Digraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; @SuppressWarnings("unchecked") public Digraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); } public HashSet<Integer> adj(int from) { return adj[from]; } public int V() { return adj.length; } public int E() { return E; } public Digraph reversed() { Digraph dg = new Digraph(V()); for (int i = 0; i < V(); i++) for (int adjVert : adj(i)) dg.addEdge(adjVert, i); return dg; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static int[] KosarajuSharirSCC(Digraph dg) { int[] id = new int[dg.V()]; Digraph reversed = dg.reversed(); // Gotta perform topological sort on this one to get the stack. Stack<Integer> revStack = Digraph.topologicalSort(reversed); // Initializing id and idCtr. id = new int[dg.V()]; int idCtr = -1; // Creating a 'marked' array. boolean[] marked = new boolean[dg.V()]; while (!revStack.isEmpty()) { int vertex = revStack.pop(); if (!marked[vertex]) sccDFS(dg, vertex, marked, ++idCtr, id); } return id; } private static void sccDFS(Digraph dg, int source, boolean[] marked, int idCtr, int[] id) { marked[source] = true; id[source] = idCtr; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) sccDFS(dg, adjVertex, marked, idCtr, id); } public static Stack<Integer> topologicalSort(Digraph dg) { // dg has to be a directed acyclic graph. // We'll have to run dfs on the digraph and push the deepest nodes on stack first. // We'll need a Stack<Integer> and a int[] marked. Stack<Integer> topologicalStack = new Stack<Integer>(); boolean[] marked = new boolean[dg.V()]; // Calling dfs for (int i = 0; i < dg.V(); i++) if (!marked[i]) runDfs(dg, topologicalStack, marked, i); return topologicalStack; } static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) { marked[source] = true; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) runDfs(dg, topologicalStack, marked, adjVertex); topologicalStack.add(source); } } static class FastReader { private BufferedReader bfr; private StringTokenizer st; public FastReader() { bfr = new BufferedReader(new InputStreamReader(System.in)); } String next() { if (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bfr.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()); } char nextChar() { return next().toCharArray()[0]; } String nextString() { return next(); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } double[] nextDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextDouble(); return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } int[][] nextIntGrid(int n, int m) { int[][] grid = new int[n][m]; for (int i = 0; i < n; i++) { char[] line = fr.next().toCharArray(); for (int j = 0; j < m; j++) grid[i][j] = line[j] - 48; } return grid; } } static class LCA { int[] height, first, segtree; ArrayList<Integer> euler; boolean[] visited; int n; LCA(ArrayList<Point>[] outFrom, int root) { n = outFrom.length; height = new int[n]; first = new int[n]; euler = new ArrayList<>(); visited = new boolean[n]; dfs(outFrom, root, 0); int m = euler.size(); segtree = new int[m * 4]; build(1, 0, m - 1); } void dfs(ArrayList<Point>[] outFrom, int node, int h) { visited[node] = true; height[node] = h; first[node] = euler.size(); euler.add(node); for (Point to : outFrom[node]) { if (!visited[(int) to.x]) { dfs(outFrom, (int) to.x, h + 1); euler.add(node); } } } void build(int node, int b, int e) { if (b == e) { segtree[node] = euler.get(b); } else { int mid = (b + e) / 2; build(node << 1, b, mid); build(node << 1 | 1, mid + 1, e); int l = segtree[node << 1], r = segtree[node << 1 | 1]; segtree[node] = (height[l] < height[r]) ? l : r; } } int query(int node, int b, int e, int L, int R) { if (b > R || e < L) return -1; if (b >= L && e <= R) return segtree[node]; int mid = (b + e) >> 1; int left = query(node << 1, b, mid, L, R); int right = query(node << 1 | 1, mid + 1, e, L, R); if (left == -1) return right; if (right == -1) return left; return height[left] < height[right] ? left : right; } int lca(int u, int v) { int left = first[u], right = first[v]; if (left > right) { int temp = left; left = right; right = temp; } return query(1, 0, euler.size() - 1, left, right); } } static class FenwickTree { long[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates public FenwickTree(int size) { array = new long[size + 1]; } public long rsq(int ind) { assert ind > 0; long sum = 0; while (ind > 0) { sum += array[ind]; //Extracting the portion up to the first significant one of the binary representation of 'ind' and decrementing ind by that number ind -= ind & (-ind); } return sum; } public long rsq(int a, int b) { assert b >= a && a > 0 && b > 0; return rsq(b) - rsq(a - 1); } public void update(int ind, long value) { assert ind > 0; while (ind < array.length) { array[ind] += value; //Extracting the portion up to the first significant one of the binary representation of 'ind' and incrementing ind by that number ind += ind & (-ind); } } public int size() { return array.length - 1; } } static final class RedBlackCountSet { // Manual implementation of RB-BST for C++ PBDS functionality. // Modification required according to requirements. // Colors for Nodes: private static final boolean RED = true; private static final boolean BLACK = false; // Pointer to the root: private Node root; // Public constructor: public RedBlackCountSet() { root = null; } // Node class for storing key value pairs: private class Node { long key; long value; Node left, right; boolean color; long size; // Size of the subtree rooted at that node. public Node(long key, long value, boolean color) { this.key = key; this.value = value; this.left = this.right = null; this.color = color; this.size = value; } } /***** Invariant Maintenance functions: *****/ // When a temporary 4 node is formed: private Node flipColors(Node node) { node.left.color = node.right.color = BLACK; node.color = RED; return node; } // When there's a right leaning red link and it is not a 3 node: private Node rotateLeft(Node node) { Node rn = node.right; node.right = rn.left; rn.left = node; rn.color = node.color; node.color = RED; return rn; } // When there are 2 red links in a row: private Node rotateRight(Node node) { Node ln = node.left; node.left = ln.right; ln.right = node; ln.color = node.color; node.color = RED; return ln; } /***** Invariant Maintenance functions end *****/ // Public functions: public void put(long key) { root = put(root, key); root.color = BLACK; // One of the invariants. } private Node put(Node node, long key) { // Standard insertion procedure: if (node == null) return new Node(key, 1, RED); // Recursing: int cmp = Long.compare(key, node.key); if (cmp < 0) node.left = put(node.left, key); else if (cmp > 0) node.right = put(node.right, key); else node.value++; // Invariant maintenance: if (node.left != null && node.right != null) { if (node.right.color = RED && node.left.color == BLACK) node = rotateLeft(node); if (node.left.color == RED && node.left.left != null && node.left.left.color == RED) node = rotateRight(node); if (node.left.color == RED && node.right.color == RED) node = flipColors(node); } // Property maintenance: node.size = node.value + size(node.left) + size(node.right); return node; } public long get(long key) { return get(root, key).value; } private Node get(Node node, long key) { if (node == null) return new Node(0, 0, RED); int cmp = Long.compare(key, node.key); if (cmp < 0) return get(node.left, key); else if (cmp > 0) return get(node.right, key); else return node; } private long size(Node node) { if (node == null) return 0; else return node.size; } public long smeqRank(long key) { return smeqRank(root, key); } // Modify according to requirements. private long smeqRank(Node node, long key) { if (node == null) return 0; int cmp = Long.compare(key, node.key); if (cmp < 0) return smeqRank(node.left, key); else if (cmp > 0) return node.value + size(node.left) + smeqRank(node.right, key); else return node.value + size(node.left); } public long greqRank(long key) { return greqRank(root, key); } // Modify according to requirements. private long greqRank(Node node, long key) { if (node == null) return 0; int cmp = Long.compare(key, node.key); if (cmp < 0) return node.value + size(node.right) + greqRank(node.left, key); else if (cmp > 0) return node.value + greqRank(node.right, key); else return node.value + size(node.right); } } static class SegmentTree { private Node[] heap; private int[] array; private int size; public SegmentTree(int[] array) { this.array = Arrays.copyOf(array, array.length); //The max size of this array is about 2 * 2 ^ log2(n) + 1 size = (int) (2 * Math.pow(2.0, Math.floor((Math.log((double) array.length) / Math.log(2.0)) + 1))); heap = new Node[size]; build(1, 0, array.length); } public int size() { return array.length; } //Initialize the Nodes of the Segment tree private void build(int v, int from, int size) { heap[v] = new Node(); heap[v].from = from; heap[v].to = from + size - 1; if (size == 1) { heap[v].sum = array[from]; heap[v].max = array[from]; } else { //Build childs build(2 * v, from, size / 2); build(2 * v + 1, from + size / 2, size - size / 2); heap[v].sum = heap[2 * v].sum + heap[2 * v + 1].sum; //min = min of the children heap[v].max = Math.max(heap[2 * v].max, heap[2 * v + 1].max); } } public int rsq(int from, int to) { return rsq(1, from, to); } private int rsq(int v, int from, int to) { Node n = heap[v]; //If you did a range update that contained this node, you can infer the Sum without going down the tree if (n.pendingVal != null && contains(n.from, n.to, from, to)) { return (to - from + 1) * n.pendingVal; } if (contains(from, to, n.from, n.to)) { return heap[v].sum; } if (intersects(from, to, n.from, n.to)) { propagate(v); int leftSum = rsq(2 * v, from, to); int rightSum = rsq(2 * v + 1, from, to); return leftSum + rightSum; } return 0; } public int rMaxQ(int from, int to) { return rMaxQ(1, from, to); } private int rMaxQ(int v, int from, int to) { Node n = heap[v]; //If you did a range update that contained this node, you can infer the Min value without going down the tree if (n.pendingVal != null && contains(n.from, n.to, from, to)) { return n.pendingVal; } if (contains(from, to, n.from, n.to)) { return heap[v].max; } if (intersects(from, to, n.from, n.to)) { propagate(v); int leftMin = rMaxQ(2 * v, from, to); int rightMin = rMaxQ(2 * v + 1, from, to); return Math.max(leftMin, rightMin); } return Integer.MAX_VALUE; } public void update(int from, int to, int value) { update(1, from, to, value); } private void update(int v, int from, int to, int value) { //The Node of the heap tree represents a range of the array with bounds: [n.from, n.to] Node n = heap[v]; if (contains(from, to, n.from, n.to)) { change(n, value); } if (n.size() == 1) return; if (intersects(from, to, n.from, n.to)) { propagate(v); update(2 * v, from, to, value); update(2 * v + 1, from, to, value); n.sum = heap[2 * v].sum + heap[2 * v + 1].sum; n.max = Math.max(heap[2 * v].max, heap[2 * v + 1].max); } } //Propagate temporal values to children private void propagate(int v) { Node n = heap[v]; if (n.pendingVal != null) { change(heap[2 * v], n.pendingVal); change(heap[2 * v + 1], n.pendingVal); n.pendingVal = null; //unset the pending propagation value } } //Save the temporal values that will be propagated lazily private void change(Node n, int value) { n.pendingVal = value; n.sum = n.size() * value; n.max = value; array[n.from] = value; } //Test if the range1 contains range2 private boolean contains(int from1, int to1, int from2, int to2) { return from2 >= from1 && to2 <= to1; } //check inclusive intersection, test if range1[from1, to1] intersects range2[from2, to2] private boolean intersects(int from1, int to1, int from2, int to2) { return from1 <= from2 && to1 >= from2 // (.[..)..] or (.[...]..) || from1 >= from2 && from1 <= to2; // [.(..]..) or [..(..).. } //The Node class represents a partition range of the array. static class Node { int sum; int max; //Here We store the value that will be propagated lazily Integer pendingVal = null; int from; int to; int size() { return to - from + 1; } } } @SuppressWarnings("serial") static class CountMap<T> extends TreeMap<T, Integer>{ CountMap() { } CountMap(T[] arr) { this.putCM(arr); } public Integer putCM(T key) { if (super.containsKey(key)) { return super.put(key, super.get(key) + 1); } else { return super.put(key, 1); } } public Integer removeCM(T key) { Integer count = super.get(key); if (count == null) return -1; if (count == 1) return super.remove(key); else return super.put(key, super.get(key) - 1); } public Integer getCM(T key) { Integer count = super.get(key); if (count == null) return 0; return count; } public void putCM(T[] arr) { for (T l : arr) this.putCM(l); } } static class Point implements Comparable<Point> { long x; long y; long id; Point() { x = y = id = 0; } Point(Point p) { this.x = p.x; this.y = p.y; this.id = p.id; } Point(long a, long b, long id) { this.x = a; this.y = b; this.id = id; } Point(long a, long b) { this.x = a; this.y = b; } @Override public int compareTo(Point o) { if (this.x < o.x) return -1; if (this.x > o.x) return 1; if (this.y < o.y) return -1; if (this.y > o.y) return 1; return 0; } public boolean equals(Point that) { return this.compareTo(that) == 0; } } static int longestPrefixPalindromeLen(char[] s) { int n = s.length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) sb.append(s[i]); StringBuilder sb2 = new StringBuilder(sb); sb.append('^'); sb.append(sb2.reverse()); // out.println(sb.toString()); char[] newS = sb.toString().toCharArray(); int m = newS.length; int[] pi = piCalcKMP(newS); return pi[m - 1]; } static int KMPNumOcc(char[] text, char[] pat) { int n = text.length; int m = pat.length; char[] patPlusText = new char[n + m + 1]; for (int i = 0; i < m; i++) patPlusText[i] = pat[i]; patPlusText[m] = '^'; // Seperator for (int i = 0; i < n; i++) patPlusText[m + i] = text[i]; int[] fullPi = piCalcKMP(patPlusText); int answer = 0; for (int i = 0; i < n + m + 1; i++) if (fullPi[i] == m) answer++; return answer; } static int[] piCalcKMP(char[] s) { int n = s.length; int[] pi = new int[n]; for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]) j++; pi[i] = j; } return pi; } static String toBinaryString(long num, int bits) { StringBuilder sb = new StringBuilder(Long.toBinaryString(num)); sb.reverse(); for (int i = sb.length(); i < bits; i++) sb.append('0'); return sb.reverse().toString(); } static long modDiv(long a, long b) { return mod(a * power(b, 998244353 - 2, 998244353)); } static void coprimeGenerator(int m, int n, ArrayList<Point> coprimes, int limit, int numCoprimes) { if (m > limit) return; if (m <= limit && n <= limit) coprimes.add(new Point(m, n)); if (coprimes.size() > numCoprimes) return; coprimeGenerator(2 * m - n, m, coprimes, limit, numCoprimes); coprimeGenerator(2 * m + n, m, coprimes, limit, numCoprimes); coprimeGenerator(m + 2 * n, n, coprimes, limit, numCoprimes); } static long hash(long i) { return (i * 2654435761L % gigamod); } static long hash2(long key) { long h = Long.hashCode(key); h ^= (h >>> 20) ^ (h >>> 12) ^ (h >>> 7) ^ (h >>> 4); return h & (gigamod-1); } static int mapTo1D(int row, int col, int n, int m) { // Maps elements in a 2D matrix serially to elements in // a 1D array. return row * m + col; } static int[] mapTo2D(int idx, int n, int m) { // Inverse of what the one above does. int[] rnc = new int[2]; rnc[0] = idx / m; rnc[1] = idx % m; return rnc; } static double distance(Point p1, Point p2) { return Math.sqrt(Math.pow(p2.y-p1.y, 2) + Math.pow(p2.x-p1.x, 2)); } /*static HashMap<Integer, Integer> smolNumPrimeFactorization(int num) { if (smallestFactorOf == null) primeGenerator(num + 1); CountMap<Integer> fnps = new CountMap<>(); while (num != 1) { fnps.putCM(smallestFactorOf[num]); num /= smallestFactorOf[num]; } return fnps; }*/ static HashMap<Long, Integer> primeFactorization(long num) { // Returns map of factor and its power in the number. HashMap<Long, Integer> map = new HashMap<>(); while (num % 2 == 0) { num /= 2; Integer pwrCnt = map.get(2L); map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1); } for (long i = 3; i * i <= num; i += 2) { while (num % i == 0) { num /= i; Integer pwrCnt = map.get(i); map.put(i, pwrCnt != null ? pwrCnt + 1 : 1); } } // If the number is prime, we have to add it to the // map. if (num != 1) map.put(num, 1); return map; } static HashSet<Long> divisors(long num) { HashSet<Long> divisors = new HashSet<Long>(); divisors.add(1L); divisors.add(num); for (long i = 2; i * i <= num; i++) { if (num % i == 0) { divisors.add(num/i); divisors.add(i); } } return divisors; } static int bsearch(int[] arr, int val, int lo, int hi) { // Returns the index of the first element // larger than or equal to val. int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { idx = mid; hi = mid - 1; } else lo = mid + 1; } return idx; } static int bsearch(long[] arr, long val, int lo, int hi) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { idx = mid; hi = mid - 1; } else lo = mid + 1; } return idx; } static int bsearch(long[] arr, long val, int lo, int hi, boolean sMode) { // Returns the index of the last element // smaller than or equal to val. int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { hi = mid - 1; } else { idx = mid; lo = mid + 1; } } return idx; } static int bsearch(int[] arr, long val, int lo, int hi, boolean sMode) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] > val) { hi = mid - 1; } else { idx = mid; lo = mid + 1; } } return idx; } static long factorial(long n) { if (n <= 1) return 1; long factorial = 1; for (int i = 1; i <= n; i++) factorial = mod(factorial * i); return factorial; } static long factorialInDivision(long a, long b) { if (a == b) return 1; if (b < a) { long temp = a; a = b; b = temp; } long factorial = 1; for (long i = a + 1; i <= b; i++) factorial = mod(factorial * i); return factorial; } static long nCr(long n, long r, long[] fac) { long p = 998244353; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r /*long fac[] = new long[(int)n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p;*/ return (fac[(int)n] * modInverse(fac[(int)r], p) % p * modInverse(fac[(int)n - (int)r], p) % p) % p; } static long modInverse(long n, long p) { return power(n, p - 2, p); } static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if ((y & 1)==1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long nPr(long n, long r) { return factorialInDivision(n, n - r); } static int logk(long n, long k) { return (int)(Math.log(n) / Math.log(k)); } static boolean[] primeGenerator(int upto) { // Sieve of Eratosthenes: isPrime = new boolean[upto + 1]; smallestFactorOf = new int[upto + 1]; Arrays.fill(smallestFactorOf, 1); Arrays.fill(isPrime, true); isPrime[1] = isPrime[0] = false; for (long i = 2; i < upto + 1; i++) if (isPrime[(int) i]) { smallestFactorOf[(int) i] = (int) i; // Mark all the multiples greater than or equal // to the square of i to be false. for (long j = i; j * i < upto + 1; j++) { if (isPrime[(int) j * (int) i]) { isPrime[(int) j * (int) i] = false; smallestFactorOf[(int) j * (int) i] = (int) i; } } } return isPrime; } static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static long gcd(long[] arr) { int n = arr.length; long gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static int gcd(int[] arr) { int n = arr.length; int gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long lcm(long[] arr) { long lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(long a, long b) { return (a * b)/gcd(a, b); } static boolean less(int a, int b) { return a < b ? true : false; } static boolean isSorted(int[] a) { for (int i = 1; i < a.length; i++) { if (less(a[i], a[i - 1])) return false; } return true; } static boolean isSorted(long[] a) { for (int i = 1; i < a.length; i++) { if (a[i] < a[i - 1]) return false; } return true; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(double[] a, int i, int j) { double temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(char[] a, int i, int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; } static void sort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void reverseSort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverse(char[] arr) { int n = arr.length; for (int i = 0; i < n / 2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void shuffleArray(long[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(int[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(double[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { double tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(char[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { char tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static String toString(int[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(boolean[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(long[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(char[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + ""); return sb.toString(); } static String toString(int[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(long[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(double[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(char[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static long mod(long a, long m) { return (a%m + m) % m; } static long mod(long num) { return (num % gigamod + gigamod) % gigamod; } } // NOTES: // ASCII VALUE OF 'A': 65 // ASCII VALUE OF 'a': 97 // Range of long: 9 * 10^18 // ASCII VALUE OF '0': 48 // Primes upto 'n' can be given by (n / (logn)).
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
b50d8168b3402d088c1e122b01c00c3c
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.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class D1607 { public static void main(String []args) throws Exception { PrintWriter pw = new PrintWriter(System.out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); while(t-->0) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int [] arr = new int[n]; int count = 0,j=0,k=0; for(int i=0;i<n;i++) { arr[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(br.readLine()); String color = st.nextToken().toString(); for(int i=0;i<n;i++) { if(color.charAt(i) == 'B') count++; } int []blue = new int[count]; int []red = new int[n-count]; for(int i=0;i<n;i++) { if(color.charAt(i) == 'B') blue[j++] = arr[i]; else red[k++] = arr[i]; } Arrays.sort(blue); Arrays.sort(red); boolean flag = true; for(int i=0;i<count;i++) { if(blue[i] < i+1) { flag = false; break; } } for(int i=0;i<n-count;i++) { if(!flag) break; if(red[i] > i+1+count) { flag = false; break; } } if(flag) pw.println("Yes"); else pw.println("No"); } pw.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 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
94f74ab9a6c90804a68abb78a09fd078
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.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.Buffer; import java.util.*; public class Main2 { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int testCases = sc.nextInt(); while (testCases -- > 0) { int length = sc.nextInt(); int[] array = new int[length]; for(int i = 0; i < array.length; i++) { array[i] = sc.nextInt(); } String bluesReds = sc.next(); int blueCount = 0; for(int i = 0; i < array.length; i++) { if(bluesReds.charAt(i) == 'B') blueCount++; } int redCount = length - blueCount; Integer[] blues = new Integer[blueCount]; Integer[] reds = new Integer[redCount]; int k = 0; int y = 0; for(int i = 0; i < array.length; i++) { if(bluesReds.charAt(i) == 'B') { blues[k++] = array[i]; } else { reds[y++] = array[i]; } } Arrays.sort(blues); Arrays.sort(reds,Collections.reverseOrder()); TreeSet<Integer> map = new TreeSet<>(); for(int i = 1; i <= length; i++) map.add(i); boolean can = true; for(int i = 0; i < reds.length; i++) { int max = map.last(); if(reds[i] > max) { can = false; break; } else { map.remove(max); } } for(int i = 0; i < blues.length; i++) { int least = map.first(); if(blues[i] < least) { can = false; break; } else { map.remove(least); } } if(can) 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
8ffb58158b3aeeb368df91c3cd363feb
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.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class D1607 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for (int t = 0; t < T; t++) { int N = in.nextInt(); int[] A = new int[N]; for (int n = 0; n < N; n++) { A[n] = in.nextInt(); } char[] color = in.next().toCharArray(); List<Integer> blue = new ArrayList<>(); List<Integer> red = new ArrayList<>(); for (int n = 0; n < N; n++) { (color[n] == 'R' ? red : blue).add(A[n]); } Collections.sort(blue); Collections.sort(red); boolean ok = true; int next = 1; for (int a : blue) { if (a < next) { ok = false; break; } next++; } if (ok) { for (int a : red) { if (a > next) { ok = false; break; } next++; } } 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 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
31de57719e149fc053723ac3ebc8f52e
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.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static class pair { int val; int ch; pair(int val,int ch) { this.val=val; this.ch=ch; } } public static long gcd(long a,long b) { while(b!=0) { long temp=a; a=b; b=temp%b; } return a; } public static boolean isfac(long num) { //System.out.println(num); while(num%2==0)num=num/2; while( num%3==0)num=num/3; if(num!=1)return false; return true; } public static boolean isMultiple(int num) { while(num!=0) { if((num&1)==1 && num!=1)return false; num=num>>2; } return true; } public static int solve(String str,int idx,int n) { //System.out.println(str); if(str.length()==0)return 0; if(str.charAt(0)!='0' && isMultiple(Integer.parseInt(str)))return str.length(); if(idx==n)return 0; int ans=0; for(int i=0;i<str.length();i++) { int ans1=solve((i==0)?"":str.substring(0,i)+str.substring(i+1),idx+1,n); int ans2=solve(str,idx+1,n); ans=Math.max(ans,Math.max(ans1,ans2)); } return ans; } public static long partitionKSubset(int n, int k) { // write your code here int mod=998244353; long dp[][]=new long[k+1][n+1]; for(int i=0;i<=k;i++) { for(int j=0;j<=n;j++) { if(i==0 || j==0)dp[i][j]=0; else if(i==1 || i==j)dp[i][j]=1; else if(i>j)dp[i][j]=0; else { dp[i][j]=((dp[i][j-1]%mod*i%mod)+dp[i-1][j-1]%mod)%mod; } } } return dp[k][n]; } public static boolean check2(int n) { while(n>1) { if(n%3!=0)return false; else n/=3; } if(n==1) return true; else return false; } public static boolean check(int n) { if(n%3==0 ||n%3==1) { int num=(n)/3; if((check2(num)||check(num-1)) && (num!=2))return true; } return false; } public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++)arr[i]=sc.nextInt(); String str=sc.next(); pair parr[]=new pair[n]; for(int i=0;i<n;i++) { parr[i]=new pair(arr[i],str.charAt(i)-'A'); } Arrays.sort(parr,(p1,p2)->{return p1.val-p2.val;}); Arrays.sort(parr,(p1,p2)->{return p1.ch-p2.ch;}); boolean flag=true; for(int i=1;i<=n;i++) { pair p=parr[i-1]; if(p.val>i) { if(p.ch!='B'-'A'){flag=false;break;} }else if(p.val<i) { if(p.ch!='R'-'A'){flag=false;break;} } } 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
573a70ed8c1431683d1b11e052689c38
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.RoadMap.Diff1300; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; /** * Given: * 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 i the array. * * No elements of some color at all are also possible. * * 2 operations: * * 1 either you can select any blue element and decrease its value by 1; * 2 Or you can select any red element and increase its value by 1. * * To find: * * whether it is possible to make 0 or more steps such that the resulting array is a permutation of numbers from 1 * to n? * * * @author Syed Ali. * @createdAt 17/11/2021, Wednesday, 08:02 * */ public class BlueRedPermutaion { private static class Element implements Comparator<Element> { Integer value; Character color; public Element(int value, Character color) { this.value = value; this.color = color; } @Override public int compare(Element o1, Element o2){ return o1.value.compareTo(o2.value); } public Element() { } } private static Scanner sc = new Scanner(System.in); private static void solve(){ int n = sc.nextInt(); int[] arr = new int[n]; List<Element> blues = new ArrayList<>(),reds = new ArrayList<>(); for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); } char[] colors = sc.next().toCharArray(); for(int i=0;i<colors.length;i++){ if(colors[i] == 'R'){ reds.add(new Element(arr[i],'R')); }else{ blues.add(new Element(arr[i],'B')); } } blues.sort(new Element()); reds.sort(new Element()); boolean possible = true; int index = 1; for(int i = 0;i<blues.size();i++,index++){ Element element = blues.get(i); if(index == element.value) continue; if(element.value > index && element.color.equals('B')) continue; possible = false; break; } for(int i=0;i<reds.size() && possible;i++,index++){ Element element = reds.get(i); if(index == element.value) continue; if(element.value < index && element.color.equals('R')) continue; possible = false; break; } System.out.println(possible ? "YES" : "NO"); } public static void main(String[] args) { 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 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
c70ade65334ee13d5eba58ad535189c4
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:Farhan Shaikh */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Pupil { static FastReader sc = new FastReader(); public static void main (String[] args) throws java.lang.Exception { // your code goes here int t=sc.nextInt(); while(t>0){ int n=sc.nextInt(); ArrayList<Integer>red=new ArrayList<>(); ArrayList<Integer>blue=new ArrayList<>(); int arr[]=new int[n]; ai(arr,n); String s=sc.next(); boolean b=true; for(int i=0;i<n;i++) { if(s.charAt(i)=='R') { if(arr[i]>n) { b=false; } red.add(arr[i]); } else { if(arr[i]<=0) { b=false; } blue.add(arr[i]); } } if(!b) { no(); } else { Collections.sort(blue); Collections.sort(red); int counter=blue.size()+1; for(int i=0;i<red.size();i++) { if(counter<red.get(i)) { b=false; break; } counter++; } if(!b) { no(); } else { counter=n-red.size(); for(int i=blue.size()-1;i>=0;i--) { if(counter>blue.get(i)) { b=false; break; } counter--; } if(!b) { no(); } else { yes(); } } } t--; } } // FAST I/O static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static boolean two(int n)//power of two { if((n&(n-1))==0) { return true; } else{ return false; } } public static boolean isPrime(long n){ for (int i = 2; i * i <= n; ++i) { if (n % i == 0) { return false; } } return true; } public static int digit(int n) { int n1=(int)Math.floor((int)Math.log10(n)) + 1; return n1; } public static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } public static long highestPowerof2(long x) { // check for the set bits x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; // Then we remove all but the top bit by xor'ing the // string of 1's with that string of 1's shifted one to // the left, and we end up with just the one top bit // followed by 0's. return x ^ (x >> 1); } public static void yes() { System.out.println("YES"); return; } public static void no() { System.out.println("NO"); return ; } public static void al(long arr[],int n) { for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); } return ; } public static void ai(int arr[],int n) { for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } return ; } } //CAAL THE BELOW FUNCTION IF PARING PRIORITY IS NEEDED // // PriorityQueue<pair> pq = new PriorityQueue<>(); **********declare the syntax in the main function****** // pq.add(1,2)/////// // class pair implements Comparable<pair> { // int value, index; // pair(int v, int i) { index = i; value = v; } // @Override // public int compareTo(pair o) { return o.value - value; } // } // User defined Pair class // class Pair { // int x; // int y; // // Constructor // public Pair(int x, int y) // { // this.x = x; // this.y = y; // } // } // Arrays.sort(arr, new Comparator<Pair>() { // @Override public int compare(Pair p1, Pair p2) // { // return p1.x - p2.x; // } // }); // class Pair { // int height, id; // // public Pair(int i, int s) { // this.height = s; // this.id = i; // } // } //Arrays.sort(trips, (a, b) -> Integer.compare(a[1], b[1])); // ArrayList<ArrayList<Integer>> connections = new ArrayList<ArrayList<Integer>>(); // for(int i = 0; i<n;i++) // connections.add(new ArrayList<Integer>());
Java
["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
524e0b187c2ccd4629319066359df545
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 _1607D { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n=sc.nextInt(); int []arr= new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); char []ch=sc.next().toCharArray(); List<Integer> blue= new ArrayList<>(); List<Integer> red= new ArrayList<>(); for(int i=0;i<n;i++) { if (ch[i] == 'B') blue.add(arr[i]); else red.add(arr[i]); } Collections.sort(blue); Collections.sort(red); int curr=1; boolean possible=true; for(int val : blue){ if(val<curr) possible=false; else curr++; } for(int val : red) { if (val > curr) possible = false; else curr++; } System.out.println(possible?"YES":"NO"); } } 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; } } static class FastIO { InputStream dis; byte[] buffer = new byte[1 << 17]; int pointer = 0; public FastIO(String fileName) throws Exception { dis = new FileInputStream(fileName); } public FastIO(InputStream is) throws Exception { dis = is; } int nextInt() throws Exception { int ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } long nextLong() throws Exception { long ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } byte nextByte() throws Exception { if (pointer == buffer.length) { dis.read(buffer, 0, buffer.length); pointer = 0; } return buffer[pointer++]; } String next() throws Exception { StringBuffer ret = new StringBuffer(); byte b; do { b = nextByte(); } while (b <= ' '); while (b > ' ') { ret.appendCodePoint(b); b = nextByte(); } return ret.toString(); } } }
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
505b64e47d6f40786a5cac35cfdeab00
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 whatever //do not write package name here */ 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]; int i; for(i=0;i<n;i++) a[i]=sc.nextInt(); char ch[]=sc.next().toCharArray(); ArrayList<Integer> x=new ArrayList<>(); ArrayList<Integer> y=new ArrayList<>(); for(i=0;i<n;i++){ if(ch[i]=='B'){ x.add(a[i]); } else{ y.add(a[i]); } } HashSet<Integer> set=new HashSet<>(); Collections.sort(x,Collections.reverseOrder()); Collections.sort(y,Collections.reverseOrder()); int k=n; boolean ans=true; for(int j:y){ if(j>k){ ans=false; break; } set.add(k); k--; } for(int j:x){ if(j<k){ ans=false; break; } set.add(k); k--; } if(ans==true&&set.size()==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
467742a3bc9f779ddcdecc85624cb784
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.lang.Math; public class Main { public class MainSolution extends MainSolutionT { // global vars public void init(int tests_count){} public class TestCase extends TestCaseT { public Object solve() { int n = readInt(); int[] a = readIntArray(n); int[] r = new int[n]; int[] b = new int[n]; int rc = 0; int bc = 0; String s = readLn(); int i; for (i=n-1; i>=0; i--) { if (s.charAt(i)=='R') { r[rc++] = a[i]; } else { b[bc++] = a[i]; } } Arrays.sort(r, 0, rc); Arrays.sort(b, 0, bc); int j=bc; for (i=bc-1; i>=0; i--) { if (b[i]<j) return "NO"; j --; } j = bc+1; for (i=0; i<rc; i++) { if (r[i]>j) return "NO"; j ++; } return "YES"; } } public void run() { int t = multiply_test ? readInt() : 1; this.init(t); for (int i = 0; i < t; i++) { TestCase T = new TestCase(); T.run(i + 1); } } public void loc_params() { this.log_enabled = false; } public void params(){ this.multiply_test = true; } } public class MainSolutionT extends MainSolutionBase { public class TestCaseT extends TestCaseBase { } } public class MainSolutionBase { public boolean is_local = false; public MainSolutionBase() { } public class TestCaseBase { public Object solve() { return null; } public int caseNumber; public TestCaseBase() { } public void run(int cn){ this.caseNumber = cn; Object r = this.solve(); if ((r != null)) { out.println(r); } } } public String impossible(){ return "IMPOSSIBLE"; } public String strf(String format, Object... args) { return String.format(format, args); } public BufferedReader in; public PrintStream out; public boolean log_enabled = false; public boolean multiply_test = true; public void params() { } public void loc_params() { } private StringTokenizer tokenizer = null; public int readInt() { return Integer.parseInt(readToken()); } public long readLong() { return Long.parseLong(readToken()); } public double readDouble() { return Double.parseDouble(readToken()); } public String readLn() { try { String s; while ((s = in.readLine()).length() == 0); return s; } catch (Exception e) { return ""; } } public String readToken() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } catch (Exception e) { return ""; } } public int[] readIntArray(int n) { int[] x = new int[n]; readIntArray(x, n); return x; } public void readIntArray(int[] x, int n) { for (int i = 0; i < n; i++) { x[i] = readInt(); } } public long[] readLongArray(int n) { long[] x = new long[n]; readLongArray(x, n); return x; } public void readLongArray(long[] x, int n) { for (int i = 0; i < n; i++) { x[i] = readLong(); } } public void readLongArrayRev(long[] x, int n) { for (int i = 0; i < n; i++) { x[n-i-1] = readLong(); } } public void logWrite(String format, Object... args) { if (!log_enabled) { return; } out.printf(format, args); } public void readLongArrayBuf(long[] x, int n) { char[]buf = new char[1000000]; long r = -1; int k= 0, l = 0; long d; while (true) { try{ l = in.read(buf, 0, 1000000); } catch(Exception E){}; for (int i=0; i<l; i++) { if (('0'<=buf[i])&&(buf[i]<='9')) { if (r == -1) { r = 0; } d = buf[i] - '0'; r = 10 * r + d; } else { if (r != -1) { x[k++] = r; } r = -1; } } if (l<1000000) return; } } public void readIntArrayBuf(int[] x, int n) { char[]buf = new char[1000000]; int r = -1; int k= 0, l = 0; int d; while (true) { try{ l = in.read(buf, 0, 1000000); } catch(Exception E){}; for (int i=0; i<l; i++) { if (('0'<=buf[i])&&(buf[i]<='9')) { if (r == -1) { r = 0; } d = buf[i] - '0'; r = 10 * r + d; } else { if (r != -1) { x[k++] = r; } r = -1; } } if (l<1000000) return; } } public void printArray(long[] a, int n) { printArray(a, n, ' '); } public void printArray(int[] a, int n) { printArray(a, n, ' '); } public void printArray(long[] a, int n, char dl) { long x; int i, l = 0; for (i=0; i<n; i++) { x = a[i]; if (x<0) { x = -x; l++; } if (x==0) { l++; } else { while (x>0) { x /= 10; l++; } } } l += n-1; char[] s = new char[l]; l--; boolean z; for (i=n-1; i>=0; i--) { x = a[i]; z = false; if (x<0) { x = -x; z = true; } do{ s[l--] = (char)('0' + (x % 10)); x /= 10; } while (x>0); if (z) { s[l--] = '-'; } if (i>0) { s[l--] = dl; } } out.println(new String(s)); } public void printArray(double[] a, int n, char dl) { long x; double y; int i, l = 0; for (i=0; i<n; i++) { x = (long)a[i]; if (x<0) { x = -x; l++; } if (x==0) { l++; } else { while (x>0) { x /= 10; l++; } } } l += n-1 + 10*n; char[] s = new char[l]; l--; boolean z; int j; for (i=n-1; i>=0; i--) { x = (long)a[i]; y = (long)(1000000000*(a[i]-x)); z = false; if (x<0) { x = -x; z = true; } for (j=0; j<9; j++) { s[l--] = (char)('0' + (y % 10)); y /= 10; } s[l--] = '.'; do{ s[l--] = (char)('0' + (x % 10)); x /= 10; } while (x>0); if (z) { s[l--] = '-'; } if (i>0) { s[l--] = dl; } } out.println(new String(s)); } public void printArray(int[] a, int n, char dl) { int x; int i, l = 0; for (i=0; i<n; i++) { x = a[i]; if (x<0) { x = -x; l++; } if (x==0) { l++; } else { while (x>0) { x /= 10; l++; } } } l += n-1; char[] s = new char[l]; l--; boolean z; for (i=n-1; i>=0; i--) { x = a[i]; z = false; if (x<0) { x = -x; z = true; } do{ s[l--] = (char)('0' + (x % 10)); x /= 10; } while (x>0); if (z) { s[l--] = '-'; } if (i>0) { s[l--] = dl; } } out.println(new String(s)); } public void printMatrix(int[][] a, int n, int m) { int x; int i,j, l = 0; for (i=0; i<n; i++) { for (j=0; j<m; j++) { x = a[i][j]; if (x<0) { x = -x; l++; } if (x==0) { l++; } else { while (x>0) { x /= 10; l++; } } } l += m-1; } l += n-1; char[] s = new char[l]; l--; boolean z; for (i=n-1; i>=0; i--) { for (j=m-1; j>=0; j--) { x = a[i][j]; z = false; if (x<0) { x = -x; z = true; } do{ s[l--] = (char)('0' + (x % 10)); x /= 10; } while (x>0); if (z) { s[l--] = '-'; } if (j>0) { s[l--] = ' '; } } if (i>0) { s[l--] = '\n'; } } out.println(new String(s)); } public void printMatrix(long[][] a, int n, int m) { long x; int i,j, l = 0; for (i=0; i<n; i++) { for (j=0; j<m; j++) { x = a[i][j]; if (x<0) { x = -x; l++; } if (x==0) { l++; } else { while (x>0) { x /= 10; l++; } } } l += m-1; } l += n-1; char[] s = new char[l]; l--; boolean z; for (i=n-1; i>=0; i--) { for (j=m-1; j>=0; j--) { x = a[i][j]; z = false; if (x<0) { x = -x; z = true; } do{ s[l--] = (char)('0' + (x % 10)); x /= 10; } while (x>0); if (z) { s[l--] = '-'; } if (j>0) { s[l--] = ' '; } } if (i>0) { s[l--] = '\n'; } } out.println(new String(s)); } } public void run() { MainSolution S; try { S = new MainSolution(); S.in = new BufferedReader(new InputStreamReader(System.in)); //S.out = System.out; S.out = new PrintStream(new BufferedOutputStream( System.out )); } catch (Exception e) { return; } S.params(); S.run(); S.out.flush(); } public static void main(String args[]) { Locale.setDefault(Locale.US); Main M = new Main(); M.run(); } }
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
3ce9991e7696eb5bf65bc1fd5c0166e0
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 { public static class Pair implements Comparable < Pair > { int d; int i; Pair(int d, int i) { this.d = d; this.i = i; } public int compareTo(Pair o) { if (this.d - o.d == 0) return this.i - o.i; return this.d - o.d; } } public static class SegmentTree { long[] st; long[] lazy; int n; SegmentTree(long[] arr, int n) { this.n = n; st = new long[4 * n]; lazy = new long[4 * n]; construct(arr, 0, n - 1, 0); } public long construct(long[] arr, int si, int ei, int node) { if (si == ei) { st[node] = arr[si]; return arr[si]; } int mid = (si + ei) / 2; long left = construct(arr, si, mid, 2 * node + 1); long right = construct(arr, mid + 1, ei, 2 * node + 2); st[node] = left + right; return st[node]; } public long get(int l, int r) { return get(0, n - 1, l, r, 0); } public long get(int si, int ei, int l, int r, int node) { if (r < si || l > ei) return 0; if (lazy[node] != 0) { st[node] += lazy[node] * (ei - si + 1); if (si != ei) { lazy[2 * node + 1] += lazy[node]; lazy[2 * node + 2] += lazy[node]; } lazy[node] = 0; } if (l <= si && r >= ei) return st[node]; int mid = (si + ei) / 2; return get(si, mid, l, r, 2 * node + 1) + get(mid + 1, ei, l, r, 2 * node + 2); } public void update(int index, int value) { update(0, n - 1, index, 0, value); } public void update(int si, int ei, int index, int node, int val) { if (si == ei) { st[node] = val; return; } int mid = (si + ei) / 2; if (index <= mid) { update(si, mid, index, 2 * node + 1, val); } else { update(mid + 1, ei, index, 2 * node + 2, val); } st[node] = st[2 * node + 1] + st[2 * node + 2]; } public void rangeUpdate(int l, int r, int val) { rangeUpdate(0, n - 1, l, r, 0, val); } public void rangeUpdate(int si, int ei, int l, int r, int node, int val) { if (r < si || l > ei) return; if (lazy[node] != 0) { st[node] += lazy[node] * (ei - si + 1); if (si != ei) { lazy[2 * node + 1] += lazy[node]; lazy[2 * node + 2] += lazy[node]; } lazy[node] = 0; } if (l <= si && r >= ei) { st[node] += val * (ei - si + 1); if (si != ei) { lazy[2 * node + 1] += val; lazy[2 * node + 2] += val; } return; } int mid = (si + ei) / 2; rangeUpdate(si, mid, l, r, 2 * node + 1, val); rangeUpdate(mid + 1, ei, l, r, 2 * node + 2, val); st[node] = st[2 * node + 1] + st[2 * node + 2]; } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { try { System.setIn(new FileInputStream(new File("input.txt"))); System.setOut(new PrintStream(new File("output.txt"))); } catch (Exception e) { } } Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); StringBuilder sb = new StringBuilder(); while (tc-- > 0) { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); String s = sc.next(); Pair[] prr = new Pair[n]; for (int i = 0; i < n; i++) { prr[i] = new Pair(s.charAt(i) == 'B' ? 0 : 1, arr[i]); } Arrays.sort(prr); boolean flag = true; for (int i = 0; i < n; i++) { Pair p = prr[i]; if (p.d == 0) { if (p.i < (i + 1)) flag = false; } else if (p.i > (i + 1)) flag = false; } if (flag) sb.append("YES"); else sb.append("NO"); sb.append("\n"); } System.out.println(sb); } static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a - b, b); return gcd(a, b - a); } public static int Xor(int n) { if (n % 4 == 0) return n; // If n%4 gives remainder 1 if (n % 4 == 1) return 1; // If n%4 gives remainder 2 if (n % 4 == 2) return n + 1; // If n%4 gives remainder 3 return 0; } public static long pow(long a, long b, long mod) { long res = 1; while (b > 0) { if ((b & 1) > 0) res = (res * a) % mod; b = b >> 1; a = ((a % mod) * (a % mod)) % mod; } return (res % mod + mod) % mod; } public static boolean isEqual(ArrayList<Integer> a, ArrayList<Integer> b, int n) { HashSet<Integer> hs = new HashSet<>(); if (a.size() < (n / 2) || b.size() < (n / 2)) return false; int s1 = 0; for (int ele : a) hs.add(ele); for (int ele : b) hs.add(ele); if (hs.size() == n) return true; return false; } public static double digit(long num) { return Math.floor(Math.log10(num) + 1); } public static int count(ArrayList<Integer> al, int num) { if (al.get(al.size() - 1) == num) return 0; int k = al.get(al.size() - 1); ArrayList<Integer> temp = new ArrayList<>(); for (int i = 0; i < al.size(); i++) { if (al.get(i) > k) temp.add(al.get(i)); } return 1 + count(temp, num); } public static String Util(String s) { for (int i = s.length() - 1; i >= 1; i--) { int l = s.charAt(i - 1) - '0'; int r = s.charAt(i) - '0'; if (l + r >= 10) { return s.substring(0, i - 1) + (l + r) + s.substring(i + 1); } } int l = s.charAt(0) - '0'; int r = s.charAt(1) - '0'; return (l + r) + s.substring(2); } public static boolean isPos(int idx, long[] arr, long[] diff) { if (idx == 0) { for (int i = 0; i <= Math.min(arr[0], arr[1]); i++) { diff[idx] = i; arr[0] -= i; arr[1] -= i; if (isPos(idx + 1, arr, diff)) { return true; } arr[0] += i; arr[1] += i; } } else if (idx == 1) { if (arr[2] - arr[1] >= 0) { long k = arr[1]; diff[idx] = k; arr[1] = 0; arr[2] -= k; if (isPos(idx + 1, arr, diff)) { return true; } arr[1] = k; arr[2] += k; } else return false; } else { if (arr[2] == arr[0] && arr[1] == 0) { diff[2] = arr[2]; return true; } else { return false; } } return false; } public static boolean isPal(String s) { for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != s.charAt(s.length() - 1 - i)) return false; } return true; } static int upperBound(ArrayList<Long> arr, long key) { int mid, N = arr.size(); // Initialise starting index and // ending index int low = 0; int high = N; // Till low is less than high while (low < high && low != N) { // Find the index of the middle element mid = low + (high - low) / 2; // If key is greater than or equal // to arr[mid], then find in // right subarray if (key >= arr.get(mid)) { low = mid + 1; } // If key is less than arr[mid] // then find in left subarray else { high = mid; } } // If key is greater than last element which is // array[n-1] then upper bound // does not exists in the array return low; } static int lowerBound(ArrayList<Long> array, long key) { // Initialize starting index and // ending index int low = 0, high = array.size(); int mid; // Till high does not crosses low while (low < high) { // Find the index of the middle element mid = low + (high - low) / 2; // If key is less than or equal // to array[mid], then find in // left subarray if (key <= array.get(mid)) { high = mid; } // If key is greater than array[mid], // then find in right subarray else { low = mid + 1; } } // If key is greater than last element which is // array[n-1] then lower bound // does not exists in the array if (low < array.size() && array.get(low) < key) { low++; } // Returning the lower_bound index return low; } }
Java
["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
c209ebf00e5937c704cea8df9752a71c
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.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; public final class NotPassed_Q1607D_BlueRedPermutation { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static int g[][]; static long mod=(long) 998244353,INF=Long.MAX_VALUE; // static boolean set[]; static int max=0; static int lca[][]; static int par[],col[],D[]; static long dp[][][],fact[]; static int seg[],size[],N; static int dp1[][],dp2[][]; static HashSet<Integer> set[]; public static void main(String args[])throws IOException { /* * star,rope,TPST * BS,LST,MS,MQ */ int T=i(); while(T-->0) { int N=i(); int[] A = new int[N]; for(int i=0; i<N; ++i) { A[i] = i(); } String line = in.next(); ArrayList<Integer> l = new ArrayList<>(); ArrayList<Integer> r = new ArrayList<>(); for(int i=0; i<N; ++i) { (line.charAt(i) == 'B' ? l : r).add(A[i]); } boolean flag = true; Collections.sort(l); Collections.sort(r, Collections.reverseOrder()); for(int i=0; i<l.size(); ++i) { if(l.get(i) < i+1) { flag = false; break; } } for(int i=0; i<r.size(); ++i) { if(r.get(i) > N-i) { flag = false; break; } } // for(int i=) ans.append((flag?"YES":"NO")+"\n"); } out.println(ans); out.close(); } static int distance(int a,int b) { int d=D[a]+D[b]; int l=LCA(a,b); l=2*D[l]; return d-l; } static int LCA(int a,int b) { if(D[a]<D[b]) { int t=a; a=b; b=t; } int d=D[a]-D[b]; int p=1; for(int i=0; i>=0 && p<=d; i++) { if((p&d)!=0) { a=lca[a][i]; } p<<=1; } if(a==b)return a; for(int i=max-1; i>=0; i--) { if(lca[a][i]!=-1 && lca[a][i]!=lca[b][i]) { a=lca[a][i]; b=lca[b][i]; } } return lca[a][0]; } static void dfs(int n,int p) { lca[n][0]=p; if(p!=-1)D[n]=D[p]+1; for(int c:g[n]) { if(c!=p) { dfs(c,n); } } } static int[] rev(int A[],int N) { int B[]=new int[N]; int j=N-1; for(int i=0; i<N; i++)B[i]=A[j--]; return B; } static int[] last(int A[],int N) { int last[]=new int[N+5]; Arrays.fill(last, -1); for(int i=N-1; i>=0; i--) { int a=A[i]; if(last[a]==-1)last[a]=i; } return last; } static int f(int A[],int last[]) { int N=A.length; int c=0; int i=0; int pre[]=new int[N+5]; while(i<N && last[A[i]]==i) { i++; } int from=i; int counter=0; while(i<N) { int r=last[A[i]]; pre[i+1]+=1; pre[r]-=1; int max=0,index=0; for(int j=i+1; j<r; j++) { if(last[A[j]]>r) { max=Math.max(max, last[A[j]]); if(max==last[A[j]])index=j; } } //System.out.println(max+" "+index); if(max==0) { i=r+1; while(i<N && last[A[i]]==i)i++; } else { i=index; counter++; } // break; } for( i=1; i<=N; i++)pre[i]+=pre[i-1]; for(int a:pre)if(a>0)c++; c-=counter; return c; } static void dfs2(int n,int p,int k) { if(p!=-1) { //System.out.println(n); for(int i=1; i<=k; i++) { dp2[n][i]+=dp1[n][i]; int from_par=0; if(i-2>=0)from_par=dp2[p][i-1]-dp1[n][i-2]; else from_par=1; //System.out.println(i+" "+from_par); dp2[n][i]+=from_par; } } for(int c:g[n]) { if(c!=p) dfs2(c,n,k); } } static void dfs(int n,int p,int k) { dp1[n][0]=1; for(int c:g[n]) { if(c!=p) { dfs(c,n,k); for(int i=1; i<=k; i++) { dp1[n][i]+=dp1[c][i-1]; } } } } static int[] prefix_function(char X[])//returns pi(i) array { int N=X.length; int pre[]=new int[N]; for(int i=1; i<N; i++) { int j=pre[i-1]; while(j>0 && X[i]!=X[j]) j=pre[j-1]; if(X[i]==X[j])j++; pre[i]=j; } return pre; } static TreeNode start; public static void f(TreeNode root,TreeNode p,int r) { if(root==null)return; if(p!=null) { root.par=p; } if(root.val==r)start=root; f(root.left,root,r); f(root.right,root,r); } static int right(int A[],int Limit,int l,int r) { while(r-l>1) { int m=(l+r)/2; if(A[m]<Limit)l=m; else r=m; } return l; } static int left(int A[],int a,int l,int r) { while(r-l>1) { int m=(l+r)/2; if(A[m]<a)l=m; else r=m; } return l; } static int f(int k,int x,int N) { int l=x-1,r=N; while(r-l>1) { int m=(l+r)/2; int a=ask(1,0,N-1,x,m); if(a<k) { l=m; } else r=m; } //System.out.println("-------"); if(r==N)return -1; return r; } static void build(int v,int tl,int tr,int A[]) { if(tl==tr) { seg[v]=A[tl]; return; } int tm=(tl+tr)/2; build(v*2,tl,tm,A); build(v*2+1,tm+1,tr,A); seg[v]=Math.max(seg[v*2],seg[v*2+1]); } static void update(int v,int tl,int tr,int index,int a) { if(index==tl && tl==tr) seg[v]=a; else { int tm=(tl+tr)/2; if(index<=tm)update(2*v,tl,tm,index,a); else update(2*v+1,tm+1,tr,index,a); seg[v]=Math.max(seg[v*2],seg[v*2+1]); } } static int ask(int v,int tl,int tr,int l,int r) { if(l>r)return -1; if(l==tl && tr==r)return seg[v]; int tm=(tl+tr)/2; return Math.max(ask(v*2,tl,tm,l,Math.min(tm, r)),ask(2*v+1,tm+1,tr,Math.max(tm+1, l),r)); } // static int query(long a,TreeNode root) // { // long p=1L<<30; // TreeNode temp=root; // while(p!=0) // { // System.out.println(a+" "+p); // if((a&p)!=0) // { // temp=temp.right; // // } // else temp=temp.left; // p>>=1; // } // return temp.index; // } // static void delete(long a,TreeNode root) // { // long p=1L<<30; // TreeNode temp=root; // while(p!=0) // { // if((a&p)!=0) // { // temp.right.cnt--; // temp=temp.right; // } // else // { // temp.left.cnt--; // temp=temp.left; // } // p>>=1; // } // } // static void insert(long a,TreeNode root,int i) // { // long p=1L<<30; // TreeNode temp=root; // while(p!=0) // { // if((a&p)!=0) // { // temp.right.cnt++; // temp=temp.right; // } // else // { // temp.left.cnt++; // temp=temp.left; // } // p>>=1; // } // temp.index=i; // } // static TreeNode create(int p) // { // // TreeNode root=new TreeNode(0); // if(p!=0) // { // root.left=create(p-1); // root.right=create(p-1); // } // return root; // } static boolean f(long A[],long m,int N) { long B[]=new long[N]; for(int i=0; i<N; i++) { B[i]=A[i]; } for(int i=N-1; i>=0; i--) { if(B[i]<m)return false; if(i>=2) { long extra=Math.min(B[i]-m, A[i]); long x=extra/3L; B[i-2]+=2L*x; B[i-1]+=x; } } return true; } static int f(int l,int r,long A[],long x) { while(r-l>1) { int m=(l+r)/2; if(A[m]>=x)l=m; else r=m; } return r; } static boolean f(long m,long H,long A[],int N) { long s=m; for(int i=0; i<N-1;i++) { s+=Math.min(m, A[i+1]-A[i]); } return s>=H; } static long ask(long l,long r) { System.out.println("? "+l+" "+r); return l(); } static long f(long N,long M) { long s=0; if(N%3==0) { N/=3; s=N*M; } else { long b=N%3; N/=3; N++; s=N*M; N--; long a=N*M; if(M%3==0) { M/=3; a+=(b*M); } else { M/=3; M++; a+=(b*M); } s=Math.min(s, a); } return s; } static int ask(StringBuilder sb,int a) { System.out.println(sb+""+a); return i(); } static void swap(char X[],int i,int j) { char x=X[i]; X[i]=X[j]; X[j]=x; } static int min(int a,int b,int c) { return Math.min(Math.min(a, b), c); } static long and(int i,int j) { System.out.println("and "+i+" "+j); return l(); } static long or(int i,int j) { System.out.println("or "+i+" "+j); return l(); } static int len=0,number=0; static void f(char X[],int i,int num,int l) { if(i==X.length) { if(num==0)return; //update our num if(isPrime(num))return; if(l<len) { len=l; number=num; } return; } int a=X[i]-'0'; f(X,i+1,num*10+a,l+1); f(X,i+1,num,l); } static boolean is_Sorted(int A[]) { int N=A.length; for(int i=1; i<=N; i++)if(A[i-1]!=i)return false; return true; } static boolean f(StringBuilder sb,String Y,String order) { StringBuilder res=new StringBuilder(sb.toString()); HashSet<Character> set=new HashSet<>(); for(char ch:order.toCharArray()) { set.add(ch); for(int i=0; i<sb.length(); i++) { char x=sb.charAt(i); if(set.contains(x))continue; res.append(x); } } String str=res.toString(); return str.equals(Y); } static boolean all_Zero(int f[]) { for(int a:f)if(a!=0)return false; return true; } static long form(int a,int l) { long x=0; while(l-->0) { x*=10; x+=a; } return x; } static int count(String X) { HashSet<Integer> set=new HashSet<>(); for(char x:X.toCharArray())set.add(x-'0'); return set.size(); } static int f(long K) { long l=0,r=K; while(r-l>1) { long m=(l+r)/2; if(m*m<K)l=m; else r=m; } return (int)l; } // static void build(int v,int tl,int tr,long A[]) // { // if(tl==tr) // { // seg[v]=A[tl]; // } // else // { // int tm=(tl+tr)/2; // build(v*2,tl,tm,A); // build(v*2+1,tm+1,tr,A); // seg[v]=Math.min(seg[v*2], seg[v*2+1]); // } // } static int [] sub(int A[],int B[]) { int N=A.length; int f[]=new int[N]; for(int i=N-1; i>=0; i--) { if(B[i]<A[i]) { B[i]+=26; B[i-1]-=1; } f[i]=B[i]-A[i]; } for(int i=0; i<N; i++) { if(f[i]%2!=0)f[i+1]+=26; f[i]/=2; } return f; } static int[] f(int N) { char X[]=in.next().toCharArray(); int A[]=new int[N]; for(int i=0; i<N; i++)A[i]=X[i]-'a'; return A; } static int max(int a ,int b,int c,int d) { a=Math.max(a, b); c=Math.max(c,d); return Math.max(a, c); } static int min(int a ,int b,int c,int d) { a=Math.min(a, b); c=Math.min(c,d); return Math.min(a, c); } static HashMap<Integer,Integer> Hash(int A[]) { HashMap<Integer,Integer> mp=new HashMap<>(); for(int a:A) { int f=mp.getOrDefault(a,0)+1; mp.put(a, f); } return mp; } static long mul(long a, long b) { return ( a %mod * 1L * b%mod )%mod; } static void swap(int A[],int a,int b) { int t=A[a]; A[a]=A[b]; A[b]=t; } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { a=find(a); b=find(b); if(a!=b) { if(par[a]>par[b]) //this means size of a is less than that of b { int t=b; b=a; a=t; } par[a]+=par[b]; par[b]=a; } } static boolean isSorted(int A[]) { for(int i=1; i<A.length; i++) { if(A[i]<A[i-1])return false; } return true; } static boolean isDivisible(StringBuilder X,int i,long num) { long r=0; for(; i<X.length(); i++) { r=r*10+(X.charAt(i)-'0'); r=r%num; } return r==0; } static int lower_Bound(int A[],int low,int high, int x) { if (low > high) if (x >= A[high]) return A[high]; int mid = (low + high) / 2; if (A[mid] == x) return A[mid]; if (mid > 0 && A[mid - 1] <= x && x < A[mid]) return A[mid - 1]; if (x < A[mid]) return lower_Bound( A, low, mid - 1, x); return lower_Bound(A, mid + 1, high, x); } static String f(String A) { String X=""; for(int i=A.length()-1; i>=0; i--) { int c=A.charAt(i)-'0'; X+=(c+1)%2; } return X; } static void sort(long[] a) //check for long { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static String swap(String X,int i,int j) { char ch[]=X.toCharArray(); char a=ch[i]; ch[i]=ch[j]; ch[j]=a; return new String(ch); } static int sD(long n) { if (n % 2 == 0 ) return 2; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0 ) return i; } return (int)n; } static void setGraph(int N) { par=new int[N+1]; // D=new int[N+1]; // g=new ArrayList[N+1]; // set=new boolean[N+1]; col=new int[N+1]; for(int i=0; i<=N; i++) { col[i]=-1; // g[i]=new ArrayList<>(); // D[i]=Integer.MAX_VALUE; } } static long pow(long a,long b) { //long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(boolean A[][]) { for(boolean a[]:A)print(a); } static void print(long A[][]) { for(long a[]:A)print(a); } static void print(int A[][]) { for(int a[]:A)print(a); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } } class segNode { long pref,suff,sum,max; segNode(long a,long b,long c,long d) { pref=a; suff=b; sum=c; max=d; } } //class TreeNode //{ // int cnt,index; // TreeNode left,right; // TreeNode(int c) // { // cnt=c; // index=-1; // } // TreeNode(int c,int index) // { // cnt=c; // this.index=index; // } //} class post implements Comparable<post> { long x,y,d,t; post(long a,long b,long c) { x=a; y=b; d=c; } public int compareTo(post X) { if(X.t==this.t) { return 0; } else { long xt=this.t-X.t; if(xt>0)return 1; return -1; } } } class TreeNode { int val; TreeNode left, right,par; TreeNode() {} TreeNode(int item) { val = item; left =null; right = null; par=null; } } class edge { int a,wt; edge(int a,int w) { this.a=a; wt=w; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["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
e18d60b4b73a3aa9cdebc02b3bc8d6cc
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.Scanner; import java.util.LinkedList; import java.util.*; import java.util.StringTokenizer; public class C { 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 merge(int A[],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]; int k=l; for(int i=0;i<n1;i++) L[i]=A[k+i]; for(int i=0;i<n2;i++) R[i]=A[m+i+1]; int i=0; int j=0; while(i<n1 && j<n2){ if(L[i]<R[j]) A[k++]=L[i++]; else A[k++]=R[j++]; } while(j<n2) A[k++]=R[j++]; while(i<n1){ A[k++]=L[i]; i++; } } static void sort(int A[],int l,int r){ if(l<r){ int m=l+(r-l)/2; sort(A,l,m); sort(A,m+1,r); merge(A,l,m,r); } } public static void main(String[] args) { FastReader s=new FastReader(); int t=s.nextInt(); while(t-->0){ int n=s.nextInt(); int A[]=new int[n]; for(int i=0;i<n;i++){ A[i]=s.nextInt(); } String RB=s.next(); char c[]=RB.toCharArray(); int r=0; int b=0; for(int i=0;i<n;i++){ if(c[i]=='R') r++; } b=n-r; int RR[]=new int[r]; int BB[]=new int[b]; int ir=0,ib=0; for(int i=0;i<n;i++){ if(c[i]=='R') RR[ir++]=A[i]; else BB[ib++]=A[i]; } if(r>0) sort(RR,0,r-1); if(b>0) sort(BB,0,b-1); if((r>0 && RR[r-1]>n) || (b>0 && BB[0]<1) ) System.out.println("NO"); else{ int i=0; int j=0; int itr=1; boolean ba=true; while(i<r && j<b){ if(possible(BB,j,itr,0)){ j++; itr++; } else if(possible(RR,i,itr,1)){ i++; itr++; } else{ ba=false; break; } } while(i<r){ if(possible(RR,i,itr,1)){ i++; itr++; } else{ ba=false; break; } } while(j<b){ if(possible(BB,j,itr,0)){ j++; itr++; } else{ ba=false; break; } } if(ba==true) System.out.println("YES"); else System.out.println("NO"); } } } static boolean possible(int A[],int dex,int itr,int k){ if(A.length==0 ) return false; if(k==1){ if(dex>=A.length) return false; if(A[dex]>itr) return false; return true; } else{ if(dex>=A.length) return false; if(A[dex]<itr) 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
166f61444bd228e43b0ac521fd61d120
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 brperm { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(f.readLine()); while(t-->0) { int n=Integer.parseInt(f.readLine()); StringTokenizer st = new StringTokenizer(f.readLine()); String s=f.readLine(); 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(Integer.parseInt(st.nextToken())); } else { r.add(Integer.parseInt(st.nextToken())); } } Collections.sort(b); Collections.sort(r); boolean yes=true; int count=1; for(int i=0;i<b.size();i++) { if(b.get(i)<count++) { yes=false; } } for(int i=0;i<r.size();i++) { if(r.get(i)>count++) { yes=false; } } System.out.println(yes?"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
bb9e2c858efe0ac987e789b687da207a
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 static java.lang.Math.sqrt; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc; // static FastWriter out; public static void main(String hi[]){ initializeIO(); sc=new FastReader(); // FastWriter out=new FastWriter(); int t=sc.nextInt(); // boolean[] seave=sieveOfEratosthenes((int)(1e6)); // int t=1; while(t--!=0){ int n=sc.nextInt(); long[] arr=readLongArray(n); String s=sc.next(); List<Long> b=new ArrayList<>(); List<Long> r=new ArrayList<>(); // print(arr); for(int i=0;i<arr.length;i++){ if(s.charAt(i)=='R'){ r.add(arr[i]); }else{ b.add(arr[i]); } } Collections.sort(b); Collections.sort(r); boolean see=true; int i=0,j=0,c=1; // out.println(" r "+r+" b "+b); while(i<r.size()||j<b.size()){ if(j<b.size()&&c<=b.get(j)){ j++; c++; // print("inside -2- j= "+j+" "+c+" "+b); }else if(i<r.size()&&c>=r.get(i)){ i++; c++; // print("inside -1- i= "+i+" "+c+" "+r); }else{ // print("break i j c"); // print("break "+(r.get(i))+" "+(b.get(j))+" "+c); break; } } // out.println(c+" "+i+" "+j); out.println((c>n)?"yes":"no"); // System.out.println(String.format("%.10f", max)); } } /* //pallindrome 1:- agar palllindrome relared hai to dkho equal character k pair koonse konse hain kyunki bo humeasaa laag jaynge 2:- phir usme ek char add kardo agar kaar sakte ho too *****if lost**** try to think of prefix and postfix array */ private static void print(String s){ out.println(s); } private static void print(double s){ out.println(s); } private static void print(float s){ out.println(s); } private static void print(long s){ out.println(s); } private static void print(int s){ out.println(s); } private static boolean isPrime(int n){ // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static String[] readStringArray(int n){ String[] arr=new String[n]; for(int i=0;i<n;i++){ arr[i]=sc.next(); } return arr; } private static Map<Character,Integer> freq(String s){ Map<Character,Integer> map=new HashMap<>(); for(char c:s.toCharArray()){ map.put(c,map.getOrDefault(c,0)+1); } return map; } static boolean[] sieveOfEratosthenes(long n) { boolean prime[] = new boolean[(int)n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true){ // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } public static long maxProfit(List<Long> prices) { long sofar=0; long max_v=0; for(int i=0;i<prices.size();i++){ sofar+=prices.get(i); if (sofar<0) { sofar=0; } max_v=Math.max(max_v,sofar); } return max_v; } static boolean isMemberAC(int a, int d, int x) { // If difference is 0, then x must // be same as a. if (d == 0) return (x == a); // Else difference between x and a // must be divisible by d. return ((x - a) % d == 0 && (x - a) / d >= 0); } private static void sort(int[] arr){ int n=arr.length; List<Integer> li=new ArrayList<>(); for(int x:arr){ li.add(x); } Collections.sort(li); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sort(long[] arr){ int n=arr.length; List<Long> li=new ArrayList<>(); for(long x:arr){ li.add(x); } Collections.sort(li); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static long sum(int[] arr){ long sum=0; for(int x:arr){ sum+=x; } return sum; } private static long[] readLongArray(int n){ long[] arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); } return arr; } private static long evenSumFibo(long n){ long l1=0,l2=2; long sum=0; while (l2<n) { long l3=(4*l2)+l1; sum+=l2; if(l3>n)break; l1=l2; l2=l3; } return sum; } private static void initializeIO(){ try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println(e.getMessage()); } } private static int maxOfArray(int[] arr){ int max=Integer.MIN_VALUE; for(int x:arr){ max=Math.max(max,x); } return max; } private static long maxOfArray(long[] arr){ long max=Long.MIN_VALUE; for(long x:arr){ max=Math.max(max,x); } return max; } private static int[][] getIntervals(int n,int m){ int[][] arr=new int[n][m]; for(int j=0;j<n;j++){ for(int i=0;i<m;i++){ arr[j][m]=sc.nextInt(); } } return arr; } private static long gcd(long a,long b){ if(b==0)return a; return gcd(b,a%b); } private static int[] rintArray(int n){ int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } return arr; } private static double[] readDoubleArray(int n){ double[] arr=new double[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextDouble(); } return arr; } private static long[] rlongArray(int n){ long[] arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); } return arr; } private static String[] rstringArray(int n){ String[] arr=new String[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLine(); } return arr; } private static void print(int[] arr){ out.println(Arrays.toString(arr)); } private static void print(long[] arr){ out.println(Arrays.toString(arr)); } private static void print(String[] arr){ out.println(Arrays.toString(arr)); } static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null || !st.hasMoreTokens()){ try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } static class Dsu { int[] parent, size; Dsu(int n) { parent = new int[n + 1]; size = new int[n + 1]; for (int i = 0; i <= n; i++) { parent[i] = i; size[i] = 1; } } private int findParent(int u) { if (parent[u] == u) return u; return parent[u] = findParent(parent[u]); } private boolean union(int u, int v) { // System.out.println("uf "+u+" "+v); int pu = findParent(u); // System.out.println("uf2 "+pu+" "+v); int pv = findParent(v); // System.out.println("uf3 " + u + " " + pv); if (pu == pv) return false; if (size[pu] <= size[pv]) { parent[pu] = pv; size[pv] += size[pu]; } else { parent[pv] = pu; size[pu] += size[pv]; } return true; } } }
Java
["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
06e7004c20373e7a3460ff7a75ff6b5d
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.rajan.codeforces.leve_1300; import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class BlueRedPermutation { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); int tt = Integer.parseInt(reader.readLine()); while (tt-- > 0) { int n = Integer.parseInt(reader.readLine()); String[] temp = reader.readLine().split("\\s+"); String color = reader.readLine(); List<int[]> nums = new ArrayList<>(); for (int i = 0; i < n; i++) { nums.add(new int[]{Integer.parseInt(temp[i]), color.charAt(i)}); } Collections.sort(nums, (x, y) -> { if (x[1] == y[1]) return Integer.compare(x[0], y[0]); return Integer.compare(x[1], y[1]); }); boolean canPermutationBeFormed = true; for (int i = 0; i < n; i++) { int[] data = nums.get(i); if (data[0] == i + 1) continue; else { if (data[0] < (i + 1) && data[1] == 'R') continue; if (data[0] > (i + 1) && data[1] == 'B') continue; canPermutationBeFormed = false; break; } } writer.write(canPermutationBeFormed ? "Yes\n" : "No\n"); } writer.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 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
f095cb18a04345082daea29e9edd772d
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 void main(String[] args) throws Exception { int tc = io.nextInt(); for (int i = 0; i < tc; i++) { solve(); } io.close(); } private static void solve() throws Exception { int n = io.nextInt(); int[] data = io.nextInts(n); String s = io.next(); Queue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt((int[] o) -> o[0]).thenComparingInt(o -> o[1])); for (int i = 0; i < n; i++) { pq.add(new int[]{s.charAt(i) == 'B' ? 0 : 1, data[i]}); } int assigned = 0; while (pq.size() > 0) { int[] curr = pq.poll(); if (curr[0] == 0) { if (curr[1] >= assigned + 1) { assigned++; } else { io.println("NO"); return; } } else { if (curr[1] <= assigned + 1) { assigned++; } else { io.println("NO"); return; } } } io.println("YES"); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(a.length); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } //-----------PrintWriter for faster output--------------------------------- public static FastIO io = new FastIO(); //-----------MyScanner class for faster input---------- static class FastIO extends PrintWriter { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar, numChars; // standard input public FastIO() { this(System.in, System.out); } public FastIO(InputStream i, OutputStream o) { super(o); stream = i; } // file input public FastIO(String i, String o) throws IOException { super(new FileWriter(o)); stream = new FileInputStream(i); } // throws InputMismatchException() if previously detected end of file private int nextByte() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars == -1) return -1; // end of file } return buf[curChar++]; } // to read in entire lines, replace c <= ' ' // with a function that checks whether c is a line break public String next() { int c; do { c = nextByte(); } while (c <= ' '); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > ' '); return res.toString(); } public String nextLine() { int c; do { c = nextByte(); } while (c < '\n'); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > '\n'); return res.toString(); } public int nextInt() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public long nextLong() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } int[] nextInts(int n) { int[] data = new int[n]; for (int i = 0; i < n; i++) { data[i] = io.nextInt(); } return data; } 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
fa47acf2fc0b46905baf1f6950ab5056
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.lang.*; import java.util.*; import java.io.*; public class D_Blue_Red_Permutation { public static void main(String[] arg){ Scanner s=new Scanner(System.in); try { int t=s.nextInt(); while(t-->0){ int n=s.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++){ arr[i]=s.nextInt(); } char[] str=s.next().toCharArray(); ArrayList<Integer> B=new ArrayList<>(); ArrayList<Integer> R=new ArrayList<>(); for(int i=0;i<n;i++){ if(str[i]=='B'){ B.add(arr[i]); }else{ R.add(arr[i]); } } Collections.sort(B); Collections.sort(R,Collections.reverseOrder()); int flag=1; for(int i=0;i<B.size();i++){ if(B.get(i)<i+1){ flag=0; } } for(int i=0;i<R.size();i++ ){ if(R.get(i)>n-i){ flag=0; } } if(flag==1){ System.out.println("YES"); }else{ System.out.println("NO"); } } } catch (Exception e) { } s.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
e5d2a3b6afe6b875033fdaa7a919c16a
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.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class D_Blue_Red_Permutation{ static Scanner in=new Scanner(); static PrintWriter out=new PrintWriter( new OutputStreamWriter(System.out) ); static int testCases,n; static long a[]; static char color[]; static void solve(){ int redColored=0,blueColored=0; for(char i: color){ if(i=='R'){ redColored++; }else{ blueColored++; } } long red[]=new long[redColored]; long blue[]=new long[blueColored]; //long blueColored[]=new long[blueColored]; int blueIndex=0,redIndex=0; for(int i=0;i<n;i++){ if( color[i]=='R' ){ red[redIndex++]=a[i]; }else{ blue[blueIndex++]=a[i]; } } sort(red,0,redColored-1); sort(blue,0,blueColored-1); int presentNumber=0; for(int i=0;i<blueColored;i++){ if( presentNumber+1<=blue[i] ){ ++presentNumber; }else{ out.println("NO"); out.flush(); return; } } for(int i=0;i<redColored;i++){ if( presentNumber+1>=red[i] ){ ++presentNumber; }else{ out.println("NO"); out.flush(); return; } } out.println("YES"); out.flush(); } public static void main(String [] amit) throws IOException{ testCases=in.nextInt(); for(int t=0;t<testCases;t++){ n=in.nextInt(); a=new long[n]; for(int i=0;i<n;i++){ a[i]=in.nextLong(); } color=in.next().toCharArray(); solve(); } in.close(); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void merge(long a[],int left,int right,int mid){ int n1=mid-left+1,n2=right-mid; long L[]=new long[n1]; long R[]=new long[n2]; for(int i=0;i<n1;i++){ L[i]=a[left+i]; } for(int i=0;i<n2;i++){ R[i]=a[mid+1+i]; } int i=0,j=0,k1=left; while(i<n1 && j<n2){ if( L[i]<=R[j] ){ a[k1]=L[i]; i++; }else{ a[k1]=R[j]; j++; } k1++; } while(i<n1){ a[k1]=L[i]; i++; k1++; } while(j<n2){ a[k1]=R[j]; j++; k1++; } } static void sort(long a[],int left,int right){ if(left>=right){ return; } int mid=(left+right)/2; sort(a,left,mid); sort(a,mid+1,right); merge(a,left,right,mid); } static class Scanner{ BufferedReader in; StringTokenizer st; public Scanner() { in=new BufferedReader( new InputStreamReader(System.in) ); } String next() throws IOException{ while(st==null || !st.hasMoreElements()){ st=new StringTokenizer(in.readLine()); } return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next()); } long nextLong() throws IOException{ return Long.parseLong(next()); } String nextLine() throws IOException{ return in.readLine(); } char nextChar() throws IOException{ return next().charAt(0); } double nextDouble() throws IOException{ return Double.parseDouble(next()); } float nextFloat() throws IOException{ return Float.parseFloat(next()); } boolean nextBoolean() throws IOException{ return Boolean.parseBoolean(next()); } void close() throws IOException{ in.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
d552e9ab832450dc0f8445d7b36d175f
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.stream.Collectors; import java.io.*; import java.math.*; public class R753_D{ public static FastScanner sc; public static PrintWriter pw; public static StringBuilder sb; public static int MOD= 1000000007; public static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static void printList(List<Long> al) { System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" "))); } public static void printList(Deque<Long> al) { System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" "))); } public static void printArr(long[] arr) { System.out.println(Arrays.toString(arr).trim().replace("[", "").replace("]","").replace(","," ")); } public static long gcd(long a, long b) { if(b==0) return a; return gcd(b,a%b); } public static long lcm(long a, long b) { return((a*b)/gcd(a,b)); } public static void decreasingOrder(ArrayList<Long> al) { Comparator<Long> c = Collections.reverseOrder(); Collections.sort(al,c); } public static boolean[] sieveOfEratosthenes(int n) { boolean isPrime[] = new boolean[n+1]; Arrays.fill(isPrime, true); isPrime[0]=false; isPrime[1]=false; for(int i=2;i*i<n;i++) { for(int j=2*i;j<n;j+=i) { isPrime[j]=false; } } return isPrime; } public static long nCr(long N, long R) { double result=1; for(int i=1;i<=R;i++) result=((result*(N-R+i))/i); return (long) result; } public static long fastPow(long a, long b, int n) { long result=1; while(b>0) { if((b&1)==1) result=(result*a %n)%n; a=(a%n * a%n)%n; b>>=1; } return result; } public static int BinarySearch(long[] arr, long key) { int low=0; int high=arr.length-1; while(low<=high) { int mid=(low+high)/2; if(arr[mid]==key) return mid; else if(arr[mid]>key) high=mid-1; else low=mid+1; } return low; //High=low-1 } public static void sortPairByFirst(ArrayList<ds> al) { Collections.sort (al,new Comparator<ds>() { public int compare(ds p1, ds p2) { return p1.x-p2.x; } }); } public static void solve(int t) { int n=sc.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); String s=sc.next(); Stack<Integer> red = new Stack<Integer>(); Stack<Integer> blue = new Stack<Integer>(); ArrayList<ds> al = new ArrayList<>(); for(int i=0;i<n;i++) { ds p = new ds(arr[i],s.charAt(i)); al.add(p); } sortPairByFirst(al); // for(int i=0;i<n;i++) { // System.out.println(al.get(i).x+" "+al.get(i).y); // } for(int i=n-1;i>=0;i--) { ds p=al.get(i); if(p.y=='R') red.push(p.x); else blue.push(p.x); } boolean ans=true; for(int i=1;i<=n;i++) { if(blue.isEmpty()==false && blue.peek()>=i) { blue.pop(); } else if(red.isEmpty()==false && red.peek()<=i) { red.pop(); } else { ans=false; break; } } if(ans) System.out.println("YES"); else System.out.println("NO"); } public static void main(String[] args) { sc = new FastScanner(); pw = new PrintWriter(new OutputStreamWriter(System.out)); sb= new StringBuilder(""); int t=sc.nextInt(); for(int i=1;i<=t;i++) solve(i); System.out.println(sb); } } class ds{ int x; char y; public ds(int x, char 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
91e6d9012f562b686505b429a2ee34d6
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 BlueRedPermutation{ static MyScanner sc = new MyScanner(); static void solve(){ int n = sc.nextInt(); int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = sc.nextInt(); } String str = sc.nextLine(); ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for(int i = 0;i<n;i++){ if(str.charAt(i)=='B'){ blue.add(arr[i]); }else{ red.add(arr[i]); } } Collections.sort(red); Collections.sort(blue); int cur = 1; for(int x:blue){ if(cur>x){ out.println("NO"); return; } cur++; } for(int y:red){ if(cur<y){ out.println("NO"); return; } cur++; } out.println("YES"); } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); 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(); } int[] readIntArray(int n){ int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(next()); } 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; } } } class Pair implements Comparable<Pair>{ char y; int x; Pair(char yy,int xx){ x = xx; y = yy; } public int compareTo(Pair p){ if(this.y=='B' && p.y=='R'){ return 1; }else if(this.y=='R' && p.y=='B'){ return -1; }else{ return this.x-p.x; } } }
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
4b6129a2f43b0a895184f3401f1498b5
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.stream.Collectors; import java.io.*; import java.math.*; public class R753_D{ public static FastScanner sc; public static PrintWriter pw; public static StringBuilder sb; public static int MOD= 1000000007; public static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static void printList(List<Long> al) { System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" "))); } public static void printList(Deque<Long> al) { System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" "))); } public static void printArr(long[] arr) { System.out.println(Arrays.toString(arr).trim().replace("[", "").replace("]","").replace(","," ")); } public static long gcd(long a, long b) { if(b==0) return a; return gcd(b,a%b); } public static long lcm(long a, long b) { return((a*b)/gcd(a,b)); } public static void decreasingOrder(ArrayList<Long> al) { Comparator<Long> c = Collections.reverseOrder(); Collections.sort(al,c); } public static boolean[] sieveOfEratosthenes(int n) { boolean isPrime[] = new boolean[n+1]; Arrays.fill(isPrime, true); isPrime[0]=false; isPrime[1]=false; for(int i=2;i*i<n;i++) { for(int j=2*i;j<n;j+=i) { isPrime[j]=false; } } return isPrime; } public static long nCr(long N, long R) { double result=1; for(int i=1;i<=R;i++) result=((result*(N-R+i))/i); return (long) result; } public static long fastPow(long a, long b, int n) { long result=1; while(b>0) { if((b&1)==1) result=(result*a %n)%n; a=(a%n * a%n)%n; b>>=1; } return result; } public static int BinarySearch(long[] arr, long key) { int low=0; int high=arr.length-1; while(low<=high) { int mid=(low+high)/2; if(arr[mid]==key) return mid; else if(arr[mid]>key) high=mid-1; else low=mid+1; } return low; //High=low-1 } public static void sortPairByFirst(ArrayList<ds> al) { Collections.sort (al,new Comparator<ds>() { public int compare(ds p1, ds p2) { return p1.x-p2.x; } }); } public static void solve(int t) { int n=sc.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); String s=sc.next(); Stack<Integer> red = new Stack<Integer>(); Stack<Integer> blue = new Stack<Integer>(); ArrayList<ds> al = new ArrayList<>(); for(int i=0;i<n;i++) { ds p = new ds(arr[i],s.charAt(i)); al.add(p); } sortPairByFirst(al); // for(int i=0;i<n;i++) { // System.out.println(al.get(i).x+" "+al.get(i).y); // } for(int i=n-1;i>=0;i--) { ds p=al.get(i); if(p.y=='R') red.push(p.x); else blue.push(p.x); } boolean ans=true; for(int i=1;i<=n;i++) { if(blue.isEmpty()==false && blue.peek()>=i) { blue.pop(); } else if(red.isEmpty()==false && red.peek()<=i) { red.pop(); } else { ans=false; break; } } if(ans) System.out.println("YES"); else System.out.println("NO"); } public static void main(String[] args) { sc = new FastScanner(); pw = new PrintWriter(new OutputStreamWriter(System.out)); sb= new StringBuilder(""); int t=sc.nextInt(); for(int i=1;i<=t;i++) solve(i); System.out.println(sb); } } class ds{ int x; char y; public ds(int x, char 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
8264860d40ec8af5e68f932126755016
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
040661439b0ca3281d699d2f0ca044e1
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); int t; t = in.nextInt(); //t = 1; while (t > 0) { solver.call(in,out); t--; } out.close(); } static class TaskA { public void call(InputReader in, PrintWriter out) { int n; n = in.nextInt(); int[] arr = new int[n]; ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } String s = in.next(); for (int i = 0; i < n; i++) { if(s.charAt(i)=='R'){ red.add(arr[i]); } else blue.add(arr[i]); } Collections.sort(blue); Collections.sort(red); int a = 1; for (Integer i: blue) { if(i < a){ out.println("NO"); return; } a++; } for (Integer i: red) { if(i > a){ out.println("NO"); return; } a++; } out.println("YES"); } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class answer implements Comparable<answer>{ int a, b; public answer(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return o.a - this.a; } } static class arrayListClass { ArrayList<Integer> arrayList2 ; public arrayListClass(ArrayList<Integer> arrayList) { this.arrayList2 = arrayList; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static final Random random=new Random(); static void shuffleSort(int[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["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
8d6653a5a2ac109b97a07ed05f94bfe4
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 S{ public static boolean b(ArrayList<Integer> bl, int r, int l){ if (l<r) return true; int c=0; for (int i=r;i<=l;i++){ if (bl.get(c)<i) return false; c++; } return true; } public static boolean r(ArrayList<Integer> rl, int r, int l){ if (l<r) return true; int c=0; for (int i =r;i<=l;i++){ if (rl.get(c)>i) return false; c++; } return true; } public static void main (String [] args)throws IOException{ BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); int t = Integer.parseInt(br.readLine()); while (t-->0){ int n = Integer.parseInt(br.readLine()); int ar[]= new int[n]; StringTokenizer st= new StringTokenizer (br.readLine()); for (int i =0;i<n;i++){ ar[i]= Integer.parseInt(st.nextToken()); } String s = br.readLine(); char cr[]= s.toCharArray(); ArrayList<Integer> bl = new ArrayList<>(); ArrayList<Integer> rl = new ArrayList<>(); for (int i =0;i<n;i++){ if (cr[i]=='B') bl.add(ar[i]); else rl.add(ar[i]); } Collections.sort(bl); Collections.sort (rl); boolean flag1= b(bl,1,bl.size()); boolean flag2= r(rl,bl.size()+1,n); boolean flag3 = r(rl,1,rl.size()); boolean flag4 = b(bl,rl.size()+1,n); if ((flag1&&flag2) || (flag3&&flag4)) 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
969296bdb9f2de163d00b1b07835181f
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.*; import java.util.stream.IntStream; public class Main { static class Pair implements Comparable<Pair> { int index; long value; boolean check=false; Pair(int index, long value,boolean check) { this.index = index; this.value = value; this.check=false; } @Override public int compareTo(Pair o) { return Long.compare(this.value, o.value); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int ncr(int n, int r) { if (r == 0 || n == r) { return 1; } return ncr(n - 1, r - 1) + ncr(n - 1, r); } static class Edge { int v; int l; Edge(int v, int l) { this.v = v; this.l = l; } } static int find(int parent[], int x) { if (parent[x] == x) { return parent[x]; } parent[x] = find(parent, parent[x]); return parent[x]; } static long sum(long n) { return (long) n * (n + 1) / 2; } static long upperbound(long arr[], long key) { int answer = Integer.MAX_VALUE; int start = 0; int end = arr.length - 1; while (start <= end) { int mid = (start + end) >> 1; if (arr[mid] == key) { return arr[mid]; } else if (arr[mid] > key) { answer = mid; end = mid - 1; } else { start = mid + 1; } } if (answer != Integer.MAX_VALUE) return arr[answer]; else { return answer; } } static long lowerbound(long arr[], long x) { int start = 0; int end = arr.length - 1; int answer = -1; while (start <= end) { int mid = (start + end) >> 1; if (arr[mid] == x) { return arr[mid]; } else if (arr[mid] < x) { answer = mid; start = mid + 1; } else { end = mid - 1; } } if (answer != -1) return arr[answer]; else { return answer; } } //This elements pair class static class Pair1 { int first; int second; int third; Pair1(int first, int second, int third) { this.first = first; this.second = second; this.third = third; } } static boolean check(int value, int a[], int b[]) { int value1 = 0; int count = 0; for (int i = 0; i < a.length; ++i) { int rich = a[i]; int poor = b[i]; if (rich >= (value - 1) - value1 && poor >= value1) { ++count; ++value1; } } if (count >= value) { return true; } else { return false; } } static void DFS(List<List<Integer>> L, int parent, List<Integer> first, List<Integer> second, boolean check, int dp[]) { dp[parent] = 1; if (check == false) { first.add(parent); } else { second.add(parent); } for (int connected : L.get(parent)) { if (dp[connected] != 1) { DFS(L, connected, first, second, !check, dp); } } } static void Bfs(long dp[],List<List<Integer>>L,int parent){ Queue<Integer>Q=new LinkedList<>(); Q.add(parent); boolean bl[]=new boolean[dp.length]; while(Q.size()!=0){ int remove=Q.remove(); for(int connected:L.get(remove)){ if(bl[connected]==false){ bl[remove]=true; dp[connected]=dp[remove]+1; Q.add(connected); } } } } static void multisourceBFS(long dp[],int friends[],List<List<Integer>>L){ Queue<Integer>Q=new LinkedList<>(); Arrays.fill(dp,Integer.MAX_VALUE); for(int i=0;i<friends.length;++i){ dp[friends[i]]=0; Q.add(friends[i]); } boolean bl[]=new boolean[dp.length]; while(Q.size()!=0){ int remove=Q.poll(); for(int connected:L.get(remove)){ if(bl[connected]==false){ bl[connected]=true; dp[connected]=Math.min(dp[connected],dp[remove]+1); Q.add(connected); } } } } static boolean prime(int n){ if(n==1){ return false; } if(n==2){ return true; } if(n%2==0){ return false; } if(n%3==0||n%6==0){ return false; } for(int i=6;i*i<=n;i+=6){ if(n%(i-1)==0||n%(i+1)==0){ return false; } } return true; } static boolean find(List<Integer>ans,int key){ int start=0; int end=ans.size()-1; while(start<=end){ int mid=(start+end)>>1; if(ans.get(mid)==key){ return true; } else if(ans.get(mid)>key){ end=mid-1; } else{ start=mid+1; } } return false; } public static void main(String[] args) { FastReader scan=new FastReader(); int t= scan.nextInt(); //For Red we have to increase the count //For Blue we have to decrease the count while(t-->0){ int n=scan.nextInt(); List<Pair>L=new ArrayList<>(); int arr[]=new int[n]; for(int i=0;i<arr.length;++i){ arr[i]=scan.nextInt(); } boolean ans=true; String mm=scan.next(); for(int i=0;i<arr.length;++i){ char ch=mm.charAt(i); L.add(new Pair(arr[i],ch,false)); } Collections.sort(L, new Comparator<Pair>() { @Override public int compare(Pair o1, Pair o2) { if(o1.value!= o2.value){ return (int)(o1.value-o2.value); } else{ return o1.index-o2.index; } } }); boolean answer=true; for(int i=0;i<L.size();++i){ if(L.get(i).value=='B'){ if(L.get(i).index<i+1){ answer=false; break; } } else{ if(L.get(i).index>i+1){ answer=false; break; } } } if(answer==false){ 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 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
510df2b5370afc45db0ea9dcce92b5ac
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 RBPermutation { public static void main(String[] args) throws IOException { FastReader fr = new FastReader(); PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out)); int t = fr.nextInt(); while (t-- > 0) { int n = fr.nextInt(); String[] s = fr.nextLine().split(" "); char[] c = fr.nextLine().toCharArray(); Pair[] arr = new Pair[n]; // i don't like my solution, i still need to think for (int i = 0; i < s.length; i++) { char a = c[i]; if (a == 'R') { arr[i] = new Pair(1, toInt(s[i])); } else { arr[i] = new Pair(0, toInt(s[i])); } } Arrays.sort(arr, new Comp()); boolean possible = true; for (int i = 0; i < n; i++) { if (arr[i].x == 0) { if (arr[i].y < i + 1) { possible = false; break; } } else { if (arr[i].y > i + 1) { possible = false; break; } } } if (possible) { pr.println("YES"); } else { pr.println("NO"); } } pr.close(); } static class Pair { int x, y; public Pair(int x, int y) { this.x = x; this.y = y; } } static class Comp implements Comparator<Pair> { public int compare(Pair a, Pair b) { if (a.x == b.x) { return Integer.compare(a.y, b.y); } else { return Integer.compare(a.x, b.x); } } } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static int toInt(String s) { return Integer.parseInt(s); } // MERGE SORT IMPLEMENTATION void sort(int[] arr, int l, int r) { if (l < r) { int m = l + (r - l) / 2; sort(arr, l, m); sort(arr, m + 1, r); // call merge merge(arr, l, m, r); } } void merge(int[] arr, int l, int m, int r) { // find sizes int len1 = m - l + 1; int len2 = r - m; int[] L = new int[len1]; int[] R = new int[len2]; // push to copies for (int i = 0; i < L.length; i++) L[i] = arr[l + i]; for (int i = 0; i < R.length; i++) { R[i] = arr[m + 1 + i]; } // fill in new array int i = 0, j = 0; int k = l; while (i < len1 && j < len2) { if (L[i] < R[i]) { arr[k] = L[i]; i++; } else { arr[k] = R[i]; j++; } k++; } // add remaining elements while (i < len1) { arr[k] = L[i]; i++; k++; } while (j < len2) { arr[k] = R[j]; j++; k++; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() throws FileNotFoundException { 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
7895a33f41b979bd2be1868d5b63d6bc
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.lang.Math; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; public class Main { static PrintWriter pw; static Scanner sc; static StringBuilder ans; static long mod = 1000000000+7; static int MAX_I = Integer.MAX_VALUE; static int MIN_I = Integer.MIN_VALUE; static long MAX_L = Long.MAX_VALUE; static long MIN_L = Long.MIN_VALUE; static void pn(final Object arg) { pw.print(arg); pw.flush(); } /*-------------- for input in an value ---------------------*/ static int ni() { return sc.nextInt(); } static long nl() { return sc.nextLong(); } static double nd() { return sc.nextDouble(); } static String ns() { return sc.next(); } static String nLine() { return sc.nextLine(); } static void ap(int arg) { ans.append(arg); } static void ap(long arg) { ans.append(arg); } static void ap(String arg) { ans.append(arg); } static void ap(StringBuilder arg) { ans.append(arg); } static void yes() { ap("Yes\n"); } static void no() { ap("No\n"); } /* for Dubuggin */ static void printArr(int ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ " "); ap("\n"); } static void printArr(long ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ " "); ap("\n"); } static void printArr(String ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ " "); ap("\n"); } static void printIntegerList(List<Integer> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); } static void printLongList(List<Long> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); } static void printStringList(List<String> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); } /*-------------- for input in an array ---------------------*/ static void readArray(int arr[]){ for(int i=0; i<arr.length; i++)arr[i] = ni(); } static void readArray(long arr[]){ for(int i=0; i<arr.length; i++)arr[i] = nl(); } static void readArray(String arr[]){ for(int i=0; i<arr.length; i++)arr[i] = ns(); } static void readArray(double arr[]){ for(int i=0; i<arr.length; i++)arr[i] = nd(); } /*-------------- File vs Input ---------------------*/ static void runFile() throws Exception { sc = new Scanner(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); } static void runIo() throws Exception { pw =new PrintWriter(System.out); sc = new Scanner(System.in); } static boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0);} static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long nCr(long n, long r) { // Combinations if (n < r) return 0; if (r > n - r) { // because nCr(n, r) == nCr(n, n - r) r = n - r; } long ans = 1L; for (long i = 0; i < r; i++) { ans *= (n - i); ans /= (i + 1); } return ans; } static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);} static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean sv[] = new boolean[10002]; static void seive() { //true -> not prime // false->prime sv[0] = sv[1] = true; sv[2] = false; for(int i = 0; i< sv.length; i++) { if( !sv[i] && (long)i*(long)i < sv.length ) { for ( int j = i*i; j<sv.length ; j += i ) { sv[j] = true; } } } } static long kadensAlgo(long ar[]) { int n = ar.length; long pre = ar[0]; long ans = ar[0]; for(int i = 1; i<n; i++) { pre = Math.max(pre + ar[i], ar[i]); ans = Math.max(pre, ans); } return ans; } static long binpow( long a, long b) { long res = 1; while (b > 0) { if ( (b & 1) > 0){ res = (res * a); } a = (a * a); b >>= 1; } return res; } static long factorial(long n) { long res = 1, i; for (i = 2; i <= n; i++){ res = ((res%mod) * (i%mod))%mod; } return res; } static int getCountPrime(int n) { int ans = 0; while (n%2==0) { ans ++; n /= 2; } for (int i = 3; i *i<= n; i+= 2) { while (n%i == 0) { ans ++; n /= i; } } if(n > 1 ) ans++; return ans; } static void sort(int[] arr) { /* because Arrays.sort() uses quicksort which is dumb Collections.sort() uses merge sort */ ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } static void sort(long[] arr) { /* because Arrays.sort() uses quicksort which is dumb Collections.sort() uses merge sort */ ArrayList<Long> ls = new ArrayList<Long>(); for(Long x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } static int upperBound(long ar[], long k, int l, int r) { while (l <= r) { int m = (l + r) >> 1 ; if (ar[m] > k) { r = m - 1 ; } else { l = m + 1 ; } } return l ; } static int lowerBound(long ar[], long k, int l, int r) { while (l <= r) { int m = (l + r) >> 1 ; if (ar[m] >= k) { r = m - 1 ; } else { l = m + 1 ; } } return l ; } static void findDivisor(int n, ArrayList<Integer> al) { for(int i = 1; i*i <= n; i++){ if( n % i == 0){ if(n/i==i){ al.add(i); } else{ al.add(n/i); al.add(i); } } } } static class Pair{ Pair( ){ } } public static void main(String[] args) throws Exception { // runFile(); runIo(); int t; t = 1; t = sc.nextInt(); ans = new StringBuilder(); while( t-- > 0 ) { solve(); } pn(ans+""); } public static void solve ( ) { int n = ni(); int ar[] = new int[n]; readArray(ar); String s = ns(); int bLen = 0, rLen=0; for(char ch : s.toCharArray()) if( ch == 'B' ) bLen++; else rLen++; int b[] = new int[bLen]; int r[] = new int[rLen]; int i = 0, j = 0; for(int k = 0; k<n; k++) if( s.charAt(k) == 'B' ) b[i++] = ar[k]; else r[j++] = ar[k]; sort(b); sort(r); for( i = 0; i<bLen; i++) { if( b[i] <= i ) { ap("NO\n"); return; } } for( i = 0; i<rLen; i++) { if( r[i] > bLen+i+1 ) { ap("NO\n"); return; } } ap("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 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
14a5244ae7e556385901b3ddabe3ce3c
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.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; import java.util.Collections; public class C { static FastScanner sc = new FastScanner(); public static void main(String[] args) { int t = sc.nextInt(); while(t-->0) { if(solve()) System.out.println("YES"); else System.out.println("NO"); } } public static boolean solve() { int n = sc.nextInt(); int a[] = new int[n]; inputArray(a); ArrayList<Integer> red = new ArrayList<Integer>(); ArrayList<Integer> blue = new ArrayList<Integer>(); String color = sc.next(); for(int i=0;i<n;i++) { char co = color.charAt(i); if(co == 'R') red.add(a[i]); else blue.add(a[i]); } Collections.sort(red); Collections.sort(blue); int curr = 1; for(int i: blue) { if(curr > i) return false; curr++; } for(int i: red) { if(curr < i) return false; curr++; } return true; } public static void sort(int arr[]) { ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0;i<arr.length;i++) arr[i] = ls.get(i); } public static int max(int a, int b) { return Math.max(a, b); } public static int min(int a, int b) { return Math.min(a, b); } public static void print(int a[]) { for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } public static void inputArray(int a[]) { for(int i=0;i<a.length;i++) a[i] = sc.nextInt(); } static class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
Java
["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
555b65981a338d6e46ac574dd1656510
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.*; public class D { public static void main(String args[]) { Scanner fs = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int T = fs.nextInt(); for(int loop=1; loop<=T; loop++) { int n = fs.nextInt(); int[] nums = new int[n]; for(int i = 0; i < n; i++) nums[i] = fs.nextInt(); List<Integer> r = new ArrayList<>(); List<Integer> b = new ArrayList<>(); String c = fs.next(); for(int i = 0; i < c.length(); i++) { if(c.charAt(i) == 'B') b.add(nums[i]); else r.add(nums[i]); } Collections.sort(b); Collections.sort(r, (x, y) -> y - x); boolean ok = true; for(int i = 0; i < b.size(); i++) { if(b.get(i) < i + 1) ok = false; } for(int i = 0; i < r.size(); i++) { if(r.get(i) > n - i) ok = false; } 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 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
9cc5b761eced025b19121e2139137d6a
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.math.*; import java.util.* ; import java.io.* ; @SuppressWarnings("unused") /* * * I am working for the day i will surpass you * */ //Scanner s = new Scanner(new File("input.txt")); //s.close(); //PrintWriter writer = new PrintWriter("output.txt"); //writer.close(); public class cf { static final int mod = (int)1e9+7 ; static final double pi = 3.1415926536 ; static boolean not_prime[] = new boolean[1000001] ; static void sieve() { for(int i=2 ; i*i<1000001 ; i++) { if(not_prime[i]==false) { for(int j=2*i ; j<1000001 ; j+=i) { not_prime[j]=true ; } } }not_prime[0]=true ; not_prime[1]=true ; } public static long bexp(long base , long power) { long res=1L ; base = base%mod ; while(power>0) { if((power&1)==1) { res=(res*base)%mod ; power-- ; } else { base=(base*base)%mod ; power>>=1 ; } } return res ; } static long modInverse(long n, long p){return power(n, p - 2, p); } static long power(long x, long y, long p) { // Initialize result long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long nCrModPFermat(int n, int r, long p){if(n<r) return 0 ;if (r == 0)return 1; long[] fac = new long[n + 1];fac[0] = 1;for (int i = 1; i <= n; i++)fac[i] = fac[i - 1] * i % p;return (fac[n] * modInverse(fac[r], p) % p* modInverse(fac[n - r], p) % p) % p;} static long modular_add(long a, long b){ return ((a % mod) + (b % mod)) % mod; } static long modular_sub(long a, long b){ return ((a % mod) - (b % mod) + mod) % mod; } static long modular_mult(long a, long b){ return ((a % mod) * (b % mod)) % mod; } static long lcm(int a, int b){ return (a / gcd(a, b)) * b; } static long gcd(long a, long b){if (b == 0) {return a;}return gcd(b, a % b);} public static void main(String[] args)throws Exception { FastReader in = new FastReader() ; // FastIO in = new FastIO(System.in) ; StringBuilder op = new StringBuilder() ; int T = in.nextInt() ; // int T=1 ; for(int tt=0 ; tt<T ; tt++) { PriorityQueue<Integer> blue = new PriorityQueue<>() ;//can be dec PriorityQueue<Integer> red = new PriorityQueue<>() ;//can be inc int n = in.nextInt(); int a[] = in.readArray(n) ; char ch[] = in.nextLine().toCharArray() ; for(int i=0 ; i<n ; i++) { if(ch[i] == 'R') red.add(a[i]) ; else blue.add(a[i]) ; } int changeTo=1 ; while(!red.isEmpty() || !blue.isEmpty()) { if(!blue.isEmpty() && blue.peek()>=changeTo) { blue.poll() ; } else if(!red.isEmpty() && red.peek()<=changeTo) { red.poll() ; } else break ; changeTo++ ; } if(!red.isEmpty() || !blue.isEmpty()) { op.append("NO\n") ; } else { op.append("YES\n") ; } } System.out.println(op.toString()); } static class pair implements Comparator<pair> { int first , second ; pair(int first , int second){ this.first=first ; this.second=second; } public pair() {} public int compare(pair arg0, pair arg1) { return arg0.first-arg1.first; } } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastIO { BufferedReader br= new BufferedReader( new InputStreamReader(System.in)); InputStream dis; byte[] buffer = new byte[1 << 17]; int pointer = 0; public FastIO(String fileName) throws Exception { dis = new FileInputStream(fileName); } public FastIO(InputStream is) throws Exception { dis = is; } int nextInt() throws Exception { int ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } long nextLong() throws Exception { long ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } byte nextByte() throws Exception { if (pointer == buffer.length) { dis.read(buffer, 0, buffer.length); pointer = 0; } return buffer[pointer++]; } String next() throws Exception { StringBuffer ret = new StringBuffer(); byte b; do { b = nextByte(); } while (b <= ' '); while (b > ' ') { ret.appendCodePoint(b); b = nextByte(); } return ret.toString(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) throws Exception { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return 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; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=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
747889d437e2a6f3a2d15ae8ef11f696
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.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; import java.util.function.ToIntFunction; public class BlueRedPerm { public static void main(String[] args){ Scanner s = new Scanner(System.in); int N = s.nextInt(); for(int i=0; i<N; i++){ int n = s.nextInt(); s.nextLine(); String[] arr = s.nextLine().split(" "); char[] rb = s.nextLine().toCharArray(); ArrayList<Integer> redVals = new ArrayList<>(); ArrayList<Integer> blueVals = new ArrayList<>(); for(int j=0; j<n; j++){ if(rb[j] == 'R'){ redVals.add(Integer.parseInt(arr[j])); } else { blueVals.add(Integer.parseInt(arr[j])); } } Collections.sort(redVals); Collections.sort(blueVals); boolean shouldBreak = false; for(int j=0; j<blueVals.size(); j++){ if(blueVals.get(j) < j+1){ System.out.println("NO"); shouldBreak = true; break; } } if(shouldBreak) continue; for(int j=0; j<redVals.size(); j++){ if(redVals.get(j) > j+blueVals.size()+1){ System.out.println("NO"); shouldBreak = true; break; } } if(shouldBreak) continue; 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 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
6ddf61d6ab1c572442bd19cfc4d437ab
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.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { FastScanner input = new FastScanner(); int tc = input.nextInt(); work: while (tc-- > 0) { int n = input.nextInt(); int lenred = 0; int lenblue =0; int a[] = new int[n]; for (int i = 0; i <n; i++) { a[i] = input.nextInt()-1; } char color[] = input.next().toCharArray(); PriorityQueue<Integer> red= new PriorityQueue<>(),blue= new PriorityQueue<>(); for (int i = 0; i < n; i++) { if(color[i]=='B') { blue.add(a[i]); } else { red.add(a[i]); } } for (int i = 0; i <n; i++) { if(!blue.isEmpty()) { if(blue.peek()>=i) { blue.poll(); continue; } } if(!red.isEmpty()) { if(red.peek()<=i) { red.poll(); continue; } } System.out.println("NO"); continue work; } System.out.println("YES"); } } 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()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
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
ed362e6f94330f79061dc9e303f867d8
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.RoadMap.Diff1300; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; /** * Given: * 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 i the array. * * No elements of some color at all are also possible. * * 2 operations: * * 1 either you can select any blue element and decrease its value by 1; * 2 Or you can select any red element and increase its value by 1. * * To find: * * whether it is possible to make 0 or more steps such that the resulting array is a permutation of numbers from 1 * to n? * * * @author Syed Ali. * @createdAt 17/11/2021, Wednesday, 08:02 * */ public class BlueRedPermutaion { private static class Element implements Comparator<Element> { Integer value; Character color; public Element(int value, Character color) { this.value = value; this.color = color; } @Override public int compare(Element o1, Element o2){ return o1.value.compareTo(o2.value); } public Element() { } } private static Scanner sc = new Scanner(System.in); private static void solve(){ int n = sc.nextInt(); int[] arr = new int[n]; List<Element> blues = new ArrayList<>(),reds = new ArrayList<>(); for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); } char[] colors = sc.next().toCharArray(); for(int i=0;i<colors.length;i++){ if(colors[i] == 'R'){ reds.add(new Element(arr[i],'R')); }else{ blues.add(new Element(arr[i],'B')); } } blues.sort(new Element()); reds.sort(new Element()); boolean possible = true; int index = 1; for(int i = 0;i<blues.size();i++,index++){ Element element = blues.get(i); if(index == element.value) continue; if(element.value > index && element.color.equals('B')) continue; possible = false; break; } for(int i=0;i<reds.size() && possible;i++,index++){ Element element = reds.get(i); if(index == element.value) continue; if(element.value < index && element.color.equals('R')) continue; possible = false; break; } System.out.println(possible ? "YES" : "NO"); } public static void main(String[] args) { 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 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
fe472f95fff827fad71185ccdcf58e2e
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
c5a1577a1999dc059fdefbafd63178cf
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 s= new Scanner(System.in); int t = s.nextInt(); for(int i=0;i<t;i++) { int n = s.nextInt(); int[] arr = new int[n]; char[] arr1 = new char[n]; List<Integer> blue = new ArrayList<Integer>(); List<Integer> red = new ArrayList<Integer>(); for(int j=0;j<n;j++) { arr[j] = s.nextInt(); } String s1 = s.next(); for(int j=0;j<n;j++) { arr1[j] = s1.charAt(j); if(s1.charAt(j)=='B') { blue.add(arr[j]); } else { red.add(arr[j]); } } Collections.sort(blue); Collections.sort(red); boolean b = true; for(int k1=0;b && (k1<blue.size());k1++) { if(blue.get(k1)<k1+1) { b = false; } } for(int k2=0;b && (k2<red.size());k2++) { if(red.get(k2)>k2+1+blue.size()) { b = false; } } System.out.println((b) ? "YES" : "NO"); System.out.flush(); } } } /* public static void sort(Scanner s, char[] l, char[] l1, int a1, int b1, int a2, int b2){ int a=a1, b=a2, c=a1; String s1; while(a<b1 && b<b2){ System.out.println("? "+l[a]+" "+l[b]); System.out.flush(); s1 = s.next(); l1[c++] = (s1.equals("<")) ? l[a++] : l[b++]; } while(a<b1){ l1[c++]=l[a++]; } while(b<b2){ l1[c++]=l[b++]; } } public static void main(String[] args) { Scanner s= new Scanner(System.in); int n = s.nextInt(); int q= s.nextInt(); char[] l = new char[n]; char[] l1 = new char[n]; String s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for(int i=0;i<n;i++){ l[i]=s1.charAt(i); } // for(int i=0;i<n;i++){ // System.out.print(l[i]+" "); // } int i=1, j; boolean b=true; while(i<n){ for(j=0; j<n-2*i; j+=2*i){ sort(s,((b) ? l : l1),((b) ? l1 : l),j,j+i,j+i,j+2*i); } if(j+i<n){ sort(s,((b) ? l : l1),((b) ? l1 : l),j,j+i,j+i,n); } else{ for(j=j; j<n; j++){ if(b){ l1[j]=l[j]; } else{ l[j]=l1[j]; } } } i*=2; b=!b; } System.out.print("! "); for(i=0; i<n; i++){ System.out.print(l[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
f0f08a16f8d291bd51cb3a441acfeeec
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 blueredperm{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = Integer.parseInt(sc.nextLine()); while(t-- > 0){ int n = Integer.parseInt(sc.nextLine()); int[] arr = new int[n]; for(int i = 0;i<n;i++){ arr[i] = sc.nextInt(); } sc.nextLine(); String s = sc.nextLine(); String[] colour = s.split(""); ArrayList<Integer> b = new ArrayList<>(); ArrayList<Integer> r = new ArrayList<>(); for(int i=0;i<n;i++){ if(colour[i].equals("B")){ b.add(arr[i]); }else{ r.add(arr[i]); } } Collections.sort(b); Collections.sort(r); boolean ans = true; int curr = 1; for(Integer i : b){ if(i>=curr){ curr++; } else{ ans = false; break; } } for(Integer i : r){ if(i<=curr){ curr++; } else{ ans = false; break; } } if(ans == true){ 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
edbd7886fc9ff225634452e423f1ceca
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.*; public class Task { 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 reader = new FastReader(); int tests = reader.nextInt(); for (int T = 0; T < tests; T++) { int n = reader.nextInt(); int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = reader.nextInt(); ArrayList<Integer> blues = new ArrayList<>(); ArrayList<Integer> reds = new ArrayList<>(); String line = reader.nextLine(); for (int i = 0; i < n; i++) (line.charAt(i) == 'R' ? reds : blues).add(array[i]); blues.sort(Integer::compare); reds.sort(Integer::compare); boolean valid = true; for (int i = 0; i < blues.size() && valid; i++) valid = (blues.get(i) >= i + 1); for (int i = 0; i < reds.size() && valid; i++) valid = (reds.get(i) <= n - reds.size() + i + 1); System.out.println(valid ? "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
3c0b048b55ec2561617090e7dc1ae68a
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 problemD { 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 S=sc.next(); int x=0,y=0; boolean fl=false; for(int i=0;i<n&&!fl;i++){ if(S.charAt(i)=='B'){ x++; if(A[i]<=0) fl=true; } else { y++; if(A[i]>n) fl=true; } } if(fl){ System.out.println("NO"); continue; } int[] B=new int[x]; int[] R=new int[y]; x=y=0; for(int i=0;i<n;i++){ if(S.charAt(i)=='B')B[x++]=A[i]; else R[y++]=A[i]; } Arrays.sort(B); for(int i=0;i<x&&!fl;i++) if(B[i]<=i)fl=true; Arrays.sort(R); for(int i=0;i<y&&!fl;i++) if(R[i]>x+i+1)fl=true; if(fl){ System.out.println("NO"); continue; } 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 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
c319d60c15396ccdc74d546e1ac0c3d2
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 Question4 { public static void main(String[] args) { int t,n,i; Scanner sc = new Scanner(System.in); t = sc.nextInt(); while(t--> 0) { n = sc.nextInt(); int arr[] = new int[n]; for(i=0;i<n;i++) arr[i] = sc.nextInt(); String str = sc.next(); List<PriorityQueue<Integer>> list = new ArrayList<PriorityQueue<Integer>>(); // First element denotes single element // Second element denotes downward arrow // Third element denotes upward arrow list.add(new PriorityQueue<Integer>()); list.add(new PriorityQueue<Integer>()); list.add(new PriorityQueue<Integer>()); for(i=0;i<str.length();i++) { char c = str.charAt(i); int num = arr[i]; if((num == n && c == 'R') || (num == 1 && c == 'B')) list.get(0).add(num); else if(c == 'B') list.get(1).add(num); else list.get(2).add(num); } if(list.get(0).size() <= 2) { int checkNum = 1; while(checkNum <= n) { if(!list.get(0).isEmpty() && list.get(0).peek() == checkNum) { list.get(0).remove(); checkNum++; } else if(!list.get(1).isEmpty() && list.get(1).peek() >= checkNum) { list.get(1).remove(); checkNum++; } else if(!list.get(2).isEmpty() && list.get(2).peek() <= checkNum) { list.get(2).remove(); checkNum++; } else break; } if(checkNum > n) System.out.println("YES"); else System.out.println("NO"); } 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
c379c81aac82ab26bc8bb1dcde880f63
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.ArrayList; import java.util.Collections; import java.util.Scanner; /** * * @author Acer */ public class RedBluePermutation_D { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); k: 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(); ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for (int i = 0; i < n; i++) { if(s.charAt(i) == 'B') blue.add(arr[i]); else if(s.charAt(i) == 'R') red.add(arr[i]); } Collections.sort(blue); int x = 1; boolean flag = true; for (int i = 0; i < blue.size(); i++) { if(blue.get(i) < x+i){ flag = false; break; } } if(!flag){ System.out.println("NO"); continue; } x = blue.size()+1; Collections.sort(red); for (int i = 0; i < red.size(); i++) { if(red.get(i) > x+i){ System.out.println("NO"); continue k; } } 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 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
1ea4ff9a48ec756c85e692f32bd21606
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
/* * akshaygupta26 */ import java.io.*; import java.util.*; public class D { static long mod =(long)(1e9+7); public static void main(String[] args) { FastReader sc=new FastReader(); StringBuilder ans=new StringBuilder(); int test=sc.nextInt(); outer:while(test-->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++) { int temp=sc.nextInt(); arr[i]=temp; } char colors[]=sc.next().trim().toCharArray(); for(int i=0;i<n;i++) { if(colors[i] == 'B') blue.add(arr[i]); else red.add(arr[i]); } Collections.sort(red); Collections.sort(blue); int cur= 1; int bp=0; int rp=0; int bz=blue.size(); int rz=red.size(); while(cur<=n) { if(bp<bz) { if(blue.get(bp)<cur) { ans.append("NO\n");continue outer; } cur++; bp++; } else { if(red.get(rp)<=cur) { cur++; rp++; } else { ans.append("NO\n");continue outer; } } } ans.append("YES\n"); } System.out.print(ans); } static long ceil(double a,double b) { return (long)Math.ceil(a/b); } static long add(long a,long b) { return (a+b)%mod; } static long mult(long a,long b) { return (a*b)%mod; } static long _gcd(long a,long b) { if(b == 0) return a; return _gcd(b,a%b); } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } 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
6d64412695754a7e0eb2fe9e89c813ee
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.TreeMap; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DBlueRedPermutation solver = new DBlueRedPermutation(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class DBlueRedPermutation { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } char c[] = in.next().toCharArray(); TreeMap<Integer, Integer> up = new TreeMap<>(); TreeMap<Integer, Integer> down = new TreeMap<>(); for (int i = 0; i < n; i++) { if (c[i] == 'B') { add(down, arr[i]); } else { add(up, arr[i]); } } for (int i = 0; i < n; i++) { int currentNumber = i + 1; Integer upper = down.ceilingKey(currentNumber); if (upper == null) { Integer lower = up.floorKey(currentNumber); if (lower == null) { out.println("NO"); return; } remove(up, lower); } else { remove(down, upper); } } out.println("YES"); } private void remove(TreeMap<Integer, Integer> map, Integer in) { int freq = map.get(in); if (freq == 1) { map.remove(in); } else { map.put(in, freq - 1); } } private void add(TreeMap<Integer, Integer> map, int in) { if (!map.containsKey(in)) { map.put(in, 0); } map.put(in, map.get(in) + 1); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
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
e7c5a68bbabb6cde2d2da6bcbb7205b7
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 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
22acfe4bb0b4e127ef6f033d0263bc97
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.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Blue_Red_Permutation { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int tc = scanner.nextInt(); while(tc-- > 0) { int n = scanner.nextInt(); int[] numbers = new int[n]; for(int i = 0 ; i < n ; i++) { numbers[i] = scanner.nextInt(); } String s = scanner.next(); char[] colors = s.toCharArray(); List<Integer> list1 = new ArrayList<>(); List<Integer> list2 = new ArrayList<>(); for(int i = 0 ; i < n ; i++) { if(colors[i]=='B')list1.add(numbers[i]); else list2.add(numbers[i]); } boolean possible = true; int j = 1; Collections.sort(list1);Collections.sort(list2); for(int i = 0 ; i < list1.size() ; i++) { // blue list if(list1.get(i)>=j)j++; else {possible=false;break;} } //check #1 if(!possible) {System.out.println("NO");continue;} for(int i = 0 ; i < list2.size() ; i++) { // red list if(list2.get(i)<=j)j++; else {possible=false;break;} } if(!possible){System.out.println("NO");continue;} else System.out.println("YES"); } scanner.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
1417592c70762a64a936b2c349d9c2db
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 Templ { static StreamTokenizer in; static int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } public static void main(String[] args) throws IOException { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int[] arr = new int[n]; for (int j = 0; j < n; j++) { arr[j] = sc.nextInt(); } String s = sc.next(); int blues = 0; for (int j = 0; j < s.length(); j++) { if (s.charAt(j) == 'B') blues++; } PriorityQueue<Integer> pq = new PriorityQueue<>( (x,y)-> s.charAt(x) == s.charAt(y) ? Integer.compare(arr[x], arr[y]) : Integer.compare(s.charAt(x), s.charAt(y))); for (int j = 0; j < n; j++) { pq.add(j); } int tt = 1; boolean fail = false; while (!pq.isEmpty()) { int p = pq.poll(); if (blues > 0) { if (tt > arr[p]) { fail = true; break; } blues--; } else { if (tt < arr[p]) { fail = true; break; } } tt++; } if (fail) { 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 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
47023f63a5a1d7cf05b4b357be7753b5
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
79cf195386a6055b3c1aae4c7027f226
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 D { public static void main(String[] args) { new D().solve(System.in, System.out); } public void solve(InputStream in, OutputStream out) { InputReader inputReader = new InputReader(in); PrintWriter writer = new PrintWriter(new BufferedOutputStream(out)); int t = inputReader.nextInt(); for (int t1 = 0; t1 < t; t1++) { int n = inputReader.nextInt(); List<Integer> a = new ArrayList<>(n); for (int i = 0; i < n; i++) { a.add(inputReader.nextInt()); } String s = inputReader.next(); boolean result = solve(a, s); writer.println(result ? "YES" : "NO"); } writer.close(); } public boolean solve(List<Integer> a, String s) { List<Integer> reds = new ArrayList<>(); List<Integer> blues = new ArrayList<>(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'B') { blues.add(a.get(i)); } else { reds.add(a.get(i)); } } Collections.sort(blues); reds.sort((o1, o2) -> -Integer.compare(o1, o2)); for (int i = 0; i < blues.size(); i++) { if (blues.get(i) < (i + 1)) { return false; } } int n = a.size(); for (int i = 0; i < reds.size(); i++) { if (reds.get(i) > n - i) { return false; } } return true; } private 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 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
add244207454e50032abd3a290232929
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.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.AbstractMap; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.*; import java.io.*; // Java Template Pramod Hosahalli... public class Solution { //static FastReader in = new FastReader(); static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static int mod = 1000000007; private static void go() throws Exception { int n = pi(in.readLine()); String s[] = in.readLine().split(" "); Long[] fx = new Long[n]; for(int i = 0; i < n; i++) fx[i] = pl(s[i]); char[] ch = in.readLine().toCharArray(); TreeMap<Long,Long> rGroup = new TreeMap<>(); TreeMap<Long,Long> bGroup = new TreeMap<>(); for(int i = 0; i < n; i++){ if(ch[i] == 'B') bGroup.put(fx[i], bGroup.getOrDefault(fx[i], 0L) + 1); else rGroup.put(fx[i], rGroup.getOrDefault(fx[i], 0L) + 1); } for(int i = 1; i <= n; i++){ if(bGroup.size() > 0){ Long key = bGroup.firstKey(); if(key >= i){ bGroup.put(key, bGroup.get(key)-1); if(bGroup.get(key) == 0L) bGroup.remove(key); continue; } } if(rGroup.size() > 0){ Long key = rGroup.firstKey(); if(key <= i){ rGroup.put(key, rGroup.get(key)-1); if(rGroup.get(key) == 0L) rGroup.remove(key); continue; } } print(1, "NO"); return; } print(1, "YES"); } public static void main(String[] args) throws Exception { int t = pi(in.readLine()); while(t-- > 0){ go(); } in.close(); } private static double solve(long n, double[][] f) { double[][] res = new double[2][2]; res[0][0] = 1; res[0][1] = 0; res[1][0] = 0; res[1][1] = 1; while(n > 0){ if(n % 2 == 1) res = matrixmul(res,f); n = n >> 1; f = matrixmul(f, f); } return res[0][0]; } private static long[][] matrixmul(long[][] a, long[][] b) { long[][] c = new long[2][2]; for(int i = 0; i < 2; i++) for(int j = 0 ; j < 2; j++) for(int k = 0; k < 2; k++) c[i][k] = (c[i][k] + mul(a[i][j], b[j][k])) % mod; return c; } private static double[][] matrixmul(double[][] a, double[][] b) { double[][] c = new double[2][2]; for(int i = 0; i < 2; i++) for(int j = 0 ; j < 2; j++) for(int k = 0; k < 2; k++) c[i][k] = c[i][k] + a[i][j] * b[j][k]; return c; } static long mul(long a, long b) { return ((a % mod) * (b % mod)) % mod; } static class FastReader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public int[] readIntArray(int n) throws IOException{ int[] fx = new int[n]; for(int i = 0; i < n; i++) fx[i] = ni(); return fx; } public long[] readLongArray(int n) throws IOException { long[] fx = new long[n]; for(int i = 0; i < n; i++) fx[i] = nl(); return fx; } 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 ni() 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 nl() 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 nd() 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(); } } static String rl() throws Exception{ return in.readLine(); } static int pi(String s) { return Integer.parseInt(s); } static long pl(String s) { return Long.parseLong(s); } static void print(int newline, String s) { System.out.print(s); if (newline != 0) System.out.println(); // System.out.flush(); } static void print(int newline, int[] arr) { StringBuilder sb = new StringBuilder(""); for(int x : arr) sb.append(x+" "); System.out.print(sb.toString().trim()); if (newline != 0) System.out.println(); // System.out.flush(); } static void cprint(int x) { print(0, "Case #" + x + ": "); } //utility function for gcd private static long gcd(long a, long b) { if(a == 0 || b == 0)return a + b; if(a > b)return gcd(a % b, b); return gcd(a, b % a); } // construct sparse table : for static range queries static class SparseTable{ static long[][] precompute(long[] arr) { int n = arr.length; int p = (int)((double)Math.log(n) / Math.log(2)); long[][] f = new long[p+1][n]; for(int i = 0; i < n; i++) f[0][i] = arr[i]; for(int j = 1; j <= p; j++){ for(int i = 0; i + (1 << j) - 1 < n; i++){ f[j][i] = gcd(f[j-1][i], f[j-1][i + (1 << (j-1))]); } } return f; } } //Union Rank Algorithm static class UnionFind { int[] parent = null; int[] rank = null; UnionFind(int n){ parent = new int[n]; rank = new int[n]; for(int i = 0; i < n; i++) parent[i] = i; } int find(int v){ if(parent[v] != v) parent[v] = find(parent[v]); return parent[v]; } boolean union(int x, int y) { int u = find(x); int v = find(y); //both belong to same set... if(u == v)return false; if(rank[u] > rank[v]) parent[v] = u; else if(rank[u] < rank[v]) parent[u] = v; else{ parent[u] = v; rank[v]++; } return true; } } //Fenwick Tree Implementation static class BIT{ int bit[] = null; BIT(int n){ bit = new int[n+1]; } private void update(int x){ int index = x; while(index < bit.length){ bit[index]+=1; index += (index & -index); } } private int sum(int l){ int index = l-1; int c = 0; while(index > 0){ c+=bit[index]; index -= (index & -index); } return c; } } //Suffix Array Impl static class SuffixArray{ // suffix array in O(n*log^2(n)) public static Integer[] suffixArray(CharSequence s) { int n = s.length(); Integer[] sa = new Integer[n]; int[] rank = new int[n]; for (int i = 0; i < n; i++) { sa[i] = i; rank[i] = s.charAt(i); } for (int len = 1; len < n; len *= 2) { long[] rank2 = new long[n]; for (int i = 0; i < n; i++) rank2[i] = ((long) rank[i] << 32) + (i + len < n ? rank[i + len] + 1 : 0); Arrays.sort(sa, (a, b) -> Long.compare(rank2[a], rank2[b])); for (int i = 0; i < n; i++) rank[sa[i]] = i > 0 && rank2[sa[i - 1]] == rank2[sa[i]] ? rank[sa[i - 1]] : i; } return sa; } } }
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
fd7161373e997d58729a52a7eb185321
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
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl, C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog. Code, Compile, Run and Debug online from anywhere in world. *******************************************************************************/ import java.util.*; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-- > 0){ int n = s.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i] = s.nextInt(); String str = s.next(); List<Integer> left = new ArrayList<>(); List<Integer> right = new ArrayList<>(); for(int i=0;i<arr.length;i++){ if(str.charAt(i) == 'B') left.add(arr[i]); else right.add(arr[i]); } Collections.sort(left); Collections.sort(right); boolean flag = false; for(int i=0;i<left.size();i++) if(left.get(i) < i+1){ flag = true; break; } if(!flag){ for(int i=0;i<right.size();i++) if(right.get(i) > left.size()+i+1){ 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 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
d77efca652b497881907f024e8f257c4
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 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(); } scn.nextLine(); String str = scn.nextLine(); ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); for(int i = 0; i < n; i++){ if(str.charAt(i) == 'B'){ a.add(arr[i]); } else{ b.add(arr[i]); } } Collections.sort(a); Collections.sort(b); boolean flag = true; int i = 1; while(flag && i <= a.size()){ if(i > a.get(i-1)){ flag = false; break; } i++; } while(flag && i <= a.size() + b.size()){ if(i < b.get(i-1-a.size())){ flag = false; break; } i++; } 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
69b84d9e4aa6a49f916a8dd90f25a6f0
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.*; //CODE FORCES public class anshulvmc { public 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); } public static int gcd(int a, int b) { if (b==0) return a; return gcd(b, a%b); } public static void google(int t) { System.out.println("Case #"+t+": "); } // public static void gt(int[][] arr,int k) { // int n = arr.length+1; // k = Math.min(k,n+1); // // Node[] nodes = new Node[n]; // for(int i=0;i<n;i++) nodes[i] = new Node(); // for(int i=0;i<n-1;i++) { // int a = arr[i][0]; // int b = arr[i][1]; // System.out.println(a+" "+b); // nodes[a].adj.add(nodes[b]); // nodes[b].adj.add(nodes[a]); // } // // ArrayDeque<Node> bfs = new ArrayDeque<>(); // for(Node nn:nodes) { // if(nn.adj.size()<2) { // bfs.addLast(nn); // nn.dist=0; // } // } // // while(bfs.size()>0) { // Node nn = bfs.removeFirst(); // for(Node a : nn.adj) { // if(a.dist!=-1) continue; // a.usedDegree++; // if(a.adj.size() - a.usedDegree <= 1) { // a.dist = nn.dist+1; // bfs.addLast(a); // } // } // } // // int[] cs = new int[n+1]; // for(Node nn:nodes) { // cs[nn.dist]++; // } // for(int i=1;i<cs.length;i++) cs[i]+=cs[i-1]; // System.out.println(n-cs[k-1]); // } public static class Node{ ArrayList<Node> adj = new ArrayList<>(); int dist = -1; int usedDegree = 0; } public static void cat_mice(int dest,int[] arr) { sort(arr); int time = dest; int timeleft = dest-1; int counter=0; for(int i=arr.length-1;i>=0;i--) { int val = arr[i]; int takes = time - val; if(takes <= timeleft) { timeleft -= takes; counter++; } } System.out.println(counter); } public static void minex(int n,int[] arr) { sort(arr); int ans=arr[0]; for(int i=0;i<arr.length-1;i++) { ans = Math.max(ans,arr[i+1] - arr[i]); } System.out.println(ans); } public static void func(long start,long n) { long x = start; if(n==0){ System.out.println(x); return; } long k=n-1; long c=k/4; long rem=k%4; long ans=x; if(x%2 == 0) { ans -= 1; ans -= (c * 4); if(rem == 1) { ans += n; } else if(rem == 2) { ans += n + n-1; } else if(rem == 3){ ans += (n-2) + (n-1) - n; } } else { ans += 1; ans += (c * 4); if(rem == 1) { ans -= n; } else if(rem == 2) { ans -= n + n-1; } else if(rem == 3) { ans -= n-2 + n-1 - n; } } System.out.println(ans); } public static boolean redblue(int[] num, String chnum) { ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for(int i=0;i<chnum.length();i++) { char ch = chnum.charAt(i); if(ch == 'B') { blue.add(num[i]); } else { red.add(num[i]); } } Collections.sort(blue); Collections.sort(red); // System.out.println(blue); // System.out.println(red); for(int i=0;i<blue.size();i++) { if(blue.get(i) >= i+1) { } else { return false; } } for(int i=0;i<red.size();i++) { if(red.get(i) > i+1 + blue.size()) { // System.out.println(red.get(i)+" "+(i+1 + blue.size())); return false; } } return true; } public static void main(String args[]) { Scanner scn = new Scanner(System.in); int test = scn.nextInt(); for(int i=0;i<test;i++) { int size = scn.nextInt(); int[] arr = new int[size]; for(int j=0;j<size;j++) { arr[j] = scn.nextInt(); } String str = scn.next(); boolean f = redblue(arr,str); if(f) { System.out.println("YES"); } else { System.out.println("NO"); } } // int n = 1; // int[] dp = new int[2]; // System.out.println(fact(n,dp)); } }
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
ef98a11b7cf8229198d73809923aa1ec
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 static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.File; import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; public class Main { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair ob) { return (int)(first - ob.first); } } static class Tuple implements Comparable<Tuple> { int first, second,third; public Tuple(int first, int second, int third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int[] parent; int[] rank; //Size of the trees is used as the rank public DSU(int n) { parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) //finding through path compression { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public boolean union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return false; //if they are already connected we exit by returning false. // if a's parent is less than b's parent if(rank[a] < rank[b]) { //then move a under b parent[a] = b; } //else if rank of j's parent is less than i's parent else if(rank[a] > rank[b]) { //then move b under a parent[b] = a; } //if both have the same rank. else { //move a under b (it doesnt matter if its the other way around. parent[b] = a; rank[a] = 1 + rank[a]; } return true; } } static class Reader { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) throws IOException { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] longReadArray(int n) throws IOException { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static long lcm(long a, long b) { return (a / LongGCD(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long modPow(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return modPow(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)(n)] * modInverse(fac[(int)(r)], p) % p * modInverse(fac[(int)(n - r)], p) % p) % p; } public static long fact(long n) { long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (long i = 1; i <= n; i++) fac[(int)(i)] = fac[(int)(i - 1)] * i; return fac[(int)(n)]; } public static long nCr(long n, long k) { long ans = 1; for(long i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } //Modular Operations for Addition and Multiplication. public static long perfomMod(long x) { return ((x%M + M)%M); } public static long addMod(long a, long b) { return perfomMod(perfomMod(a)+perfomMod(b)); } public static long subMod(long a, long b) { return perfomMod(perfomMod(a)-perfomMod(b)); } public static long mulMod(long a, long b) { return perfomMod(perfomMod(a)*perfomMod(b)); } public static boolean isPrime(long n) { if(n == 1) { return false; } //check only for sqrt of the number as the divisors //keep repeating so only half of them are required. So,sqrt. for(int i = 2;i*i<=n;i++) { if(n%i == 0) { return false; } } return true; } public static List<Long> SieveList(int n) { boolean prime[] = new boolean[(int)(n+1)]; Arrays.fill(prime, true); List<Long> l = new ArrayList<>(); for (long p = 2; p*p<=n; p++) { if (prime[(int)(p)] == true) { for(long i = p*p; i<=n; i += p) { prime[(int)(i)] = false; } } } for (long p = 2; p<=n; p++) { if (prime[(int)(p)] == true) { l.add(p); } } return l; } public static long log2(long n) { long ans = (long)(log(n)/log(2)); return ans; } public static boolean isPow2(long n) { return (n != 0 && ((n & (n-1))) == 0); } public static boolean isSq(int x) { long s = (long)Math.round(Math.sqrt(x)); return s*s==x; } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(long a[], long x) { int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(long a[], long x) { int l=-1, r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void Sort(long[] a) { List<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); // Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void ssort(char[] a) { List<Character> l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); long[] a = sc.longReadArray(n); char[] s = sc.next().toCharArray(); ArrayList<Long> blue = new ArrayList<>(), red=new ArrayList<>(); for(int i = 0;i<n;i++) { if(s[i] == 'B') { blue.add(a[i]); } else { red.add(a[i]); } } Collections.sort(blue); Collections.sort(red); Collections.reverse(red); boolean flag = true; for(int i = 0;i<blue.size();i++) { if(blue.get(i) < (i+1)) flag = false; } for(int i = 0;i<red.size();i++) { if(red.get(i) > n-i) flag = false; } fout.println((flag == true) ? "YES" : "NO"); } fout.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
04cc9488a26a90cfb60ae07f9f8cfbf7
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 D { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder bw = new StringBuilder(); int TC = Integer.parseInt(br.readLine()); while (TC-- > 0) { int N = Integer.parseInt(br.readLine()); int[] S = new int[N]; StringTokenizer st = new StringTokenizer(br.readLine(), " "); for (int i = 0; i < N; i++) S[i] = Integer.parseInt(st.nextToken()); char[] C = br.readLine().toCharArray(); ArrayList<Integer> R = new ArrayList<>(); ArrayList<Integer> B = new ArrayList<>(); for (int i = 0; i < N; i++) { if (C[i] == 'R') R.add(S[i]); else B.add(S[i]); } Collections.sort(R); Collections.sort(B); int q = N + 1; for (int i = R.size() - 1; i >= 0; i--) if (R.get(i) <= q - 1) { q--; } int p = 0; for (int i = 0; i < B.size(); i++) if (B.get(i) >= p + 1) { p++; } bw.append(p + 1 >= q ? "YES" : "NO"); bw.append("\n"); } System.out.print(bw); } }
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
b80ea924d8b02187bd0adf3bd390c245
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
/* codeforces (Linear keyboard) * You are given a keyboard that consists of 26 keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter. You have to type the word s on this keyboard. It also consists only of lowercase Latin letters. To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it. Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word. For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions 8, 5, 12 and 15, respectively. Therefore, it will take |5−8|+|12−5|+|12−12|+|15−12|=13 units of time to type the word "hello". Determine how long it will take to print the word s. Input The first line contains an integer t (1≤t≤1000) — the number of test cases. The next 2t lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length 26, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard.\ The second line of the description contains the word s. The word has a length from 1 to 50 letters inclusive and consists of lowercase Latin letters. Output Print t lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word s on the given keyboard. */ import java.util.Scanner; public class linearKeyboard { public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { int t = sc.nextInt(); while (t != 0) { String keyboard = sc.next(); String word = sc.next(); int ans = 0; int[] arr = new int[26]; // here we are creating an array which map the character with postion, index as // chracter ASCII and element of that index is its positon for (int i = 0; i < keyboard.length(); i++) { arr[keyboard.charAt(i) - 97] = i + 1; } for (int i = 0; i < word.length() - 1; i++) { ans = ans + Math.abs(arr[word.charAt(i + 1) - 97] - arr[word.charAt(i) - 97]); } System.out.println(ans); t--; } } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
8db1f23a79924a48e1d126a7ea62757d
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class keyboard2 {public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String s[]=new String[1]; String s1=""; int sum=0; for(int i=0;i<n;i++) { { s[0]=sc.next(); { s1=sc.next(); { for(int j=0;j<s1.length()-1;j++) { char ch=s1.charAt(j); char ch1=s1.charAt(j+1); int a=s[0].indexOf(ch); int b=s[0].indexOf(ch1); sum+=Math.abs(b-a); } System.out.println(sum); sum=0; } } }} }}
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
eeb26e649f4c80f4a07fa12db3c1dd3e
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
//package com.tdorosz._1607; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class LinearKeyboard { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int testCases = scanner.nextInt(); scanner.nextLine(); for (int i = 0; i < testCases; i++) { handleTestCase(scanner); } } private static void handleTestCase(Scanner scanner) { String alphabet = scanner.nextLine(); String word = scanner.nextLine(); Map<Character, Integer> positions = new HashMap<>(); for (int i = 0; i < alphabet.length(); i++) { positions.put(alphabet.charAt(i), i); } int sum = 0; char prev = word.charAt(0); for (int i = 1; i < word.length(); i++) { int diff = Math.abs(positions.get(word.charAt(i)) - positions.get(prev)); sum += diff; prev = word.charAt(i); } System.out.println(sum); } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
dcab698d6f5c46da20e25860d3849478
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; // Berhasil public class LinearKeyboard { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int banyak = scn.nextInt(); ArrayList<String> arrKalimat = new ArrayList<String>(); ArrayList<String> arrKata = new ArrayList<String>(); for (int i = 1; i <= banyak; i++) { String abjad = scn.next(); String katadihitung = scn.next(); arrKalimat.add(abjad); arrKata.add(katadihitung); } ArrayList<Integer> hasilAkhir = new ArrayList<Integer>(); for (int m = 0; m < arrKalimat.size(); m++) { String word = arrKalimat.get(m); String kataOperasi = arrKata.get(m); ArrayList<Character> tempKata = new ArrayList<Character>(); ArrayList<Integer> tempAngka = new ArrayList<Integer>(); for (int i = 0; i < kataOperasi.length(); i++) { char a = kataOperasi.charAt(i); for (int j = 0; j < word.length(); j++) { if (a == word.charAt(j)) { tempKata.add(a); tempAngka.add(j + 1); } } } int simpanAngka = 0; int finalAngka = 0; for (int z = 0; z < tempKata.size(); z++) { if(z != 0) { finalAngka += Math.abs(simpanAngka - tempAngka.get(z)); } simpanAngka = tempAngka.get(z); } hasilAkhir.add(finalAngka); } for (Integer ang : hasilAkhir) { System.out.println(ang); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
618f013e5dd92ef5dd9c9ffcd2eca129
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; public class LinearKeyboard { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int banyak = scn.nextInt(); ArrayList<String> arrKalimat = new ArrayList<String>(); ArrayList<String> arrKata = new ArrayList<String>(); for (int i = 1; i <= banyak; i++) { String abjad = scn.next(); String katadihitung = scn.next(); arrKalimat.add(abjad); arrKata.add(katadihitung); } ArrayList<Integer> hasilAkhir = new ArrayList<Integer>(); for (int m = 0; m < arrKalimat.size(); m++) { String word = arrKalimat.get(m); String kataOperasi = arrKata.get(m); ArrayList<Character> tempKata = new ArrayList<Character>(); ArrayList<Integer> tempAngka = new ArrayList<Integer>(); for (int i = 0; i < kataOperasi.length(); i++) { char a = kataOperasi.charAt(i); for (int j = 0; j < word.length(); j++) { if (a == word.charAt(j)) { tempKata.add(a); tempAngka.add(j + 1); } } } int counter = 0; int simpanAngka = 0; int finalAngka = 0; for (int z = 0; z < tempKata.size(); z++) { counter++; if(counter != 1) { finalAngka += Math.abs(simpanAngka - tempAngka.get(z)); // System.out.println("Final Angka: " + z + " " + finalAngka); } simpanAngka = tempAngka.get(z); } hasilAkhir.add(finalAngka); // System.out.println("Hasil Akhir: " + hasilAkhir); } for (Integer ang : hasilAkhir) { System.out.println(ang); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
d458b52ec0a4833e86d5aa40f8d73586
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.HashMap; import java.util.Scanner; public class A1607 { public static void main(String[] args) throws Exception { final Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t ; i++) { fn(sc); } } private static void fn(Scanner sc) { var hm = new HashMap<Character, Integer>(); var al = sc.next(); for (int i = 0; i < al.length(); i++) { hm.put(al.charAt(i), i); } var word = sc.next(); int ans = 0; char cur = word.charAt(0); for (int i = 1; i < word.length() ; i++) { char p = word.charAt(i); ans += Math.abs(hm.get(cur) - hm.get(p)); cur = p; } System.out.println(ans); } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
9448bd6c902cc9dd67e19651058e2362
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
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) { String s=sc.next(); String c=sc.next(); ArrayList<Character> arr=new ArrayList<>(); for(int i=0;i<26;i++){ arr.add(s.charAt(i)); } int ans=0; for(int i=0;i<c.length()-1;i++){ ans+=Math.abs(arr.indexOf(c.charAt(i))-arr.indexOf(c.charAt(i+1))); } System.out.println(ans); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
aae9932ddb39eaacb998870866865ee6
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.*; public class Training1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int t=input.nextInt(); for (int k = 0; k < t; k++) { String line=input.next(); char[] s=input.next().toCharArray(); int sum=0; for (int i = 1; i < s.length; i++) { int temp1 = line.indexOf(s[i-1]); int temp2 = line.indexOf(s[i]); sum+=Math.abs(temp2-temp1); } System.out.println(sum); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
9f4bd6bdc0583333dd2f9597e7548038
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.Scanner; public class main{ public static void main(String [] args){ Scanner in = new Scanner(System.in); int nt, n, ans, xi, yi; char x, y; String alp, s; nt = in.nextInt(); while(nt > 0){ alp = in.next(); s = in.next(); n = s.length(); ans = 0; for(int i = 0; i < n - 1; i ++){ x = s.charAt(i); y = s.charAt(i + 1); xi = 0; yi = 0; for(int j = 0; j < 26; j ++){ if(alp.charAt(j) == x){ xi = j; } if(alp.charAt(j) == y){ yi = j; } } ans += Math.abs(xi - yi); } System.out.println(ans); nt --; } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
b981467f528fe0dbcefd1442ee88d43e
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.*; public class HelloWorld { public static void main(String[] args) { Scanner sc = new Scanner (System.in); int t = sc.nextInt(); while(t>0){ t--; String str = sc.next(); String s = sc.next(); int a = s.length(); int []arr = new int[a]; if(a==1) System.out.println(0); else{ for(int i=0;i<a;i++){ for(int j=0;j<26;j++){ if(s.charAt(i)==str.charAt(j)) arr[i] = j; } } int sum=0; for(int i=1;i<a;i++){ sum+=Math.abs(arr[i]-arr[i-1]); } System.out.println(sum); } } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
260b6e2c877689e2e57acca2696946cd
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.*; import java.util.function.*; import java.io.*; // you can compare with output.txt and expected out public class Round753A { 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(); Round753A sol = new Round753A(); 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, ... String keyboard, s; void getInput() { keyboard = in.next(); s = in.next(); } void printOutput() { out.printlnAns(ans); } int ans; void solve(){ int[] pos = new int['z'+1]; for(int i=0; i<keyboard.length(); i++) pos[keyboard.charAt(i)] = i; ans = 0; char curr = s.charAt(0); for(int i=1; i<s.length(); i++) { char next = s.charAt(i); ans += Math.abs(pos[next]-pos[curr]); curr = next; } } // 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
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
ac3fbe759ec20a689d0ccac091c6dbd1
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class LinearKeyboard { public static void main(String[]args) throws NumberFormatException, IOException { InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); int t = Integer.parseInt(br.readLine()); for(int i =0; i<t; i++) { String keyboard = br.readLine(); int[]loc_val =new int[26]; for(int j =0; j<keyboard.length(); j++) { char ch = keyboard.charAt(j); loc_val[ch-'a'] = j; } int sum = 0; String word = br.readLine(); char prev_char = word.charAt(0); for(int j=1; j<word.length(); j++) { char ch = word.charAt(j); sum += Math.abs(loc_val[ch-'a']-loc_val[prev_char-'a']); prev_char = ch; } System.out.println(sum); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
5e168dd48a12a9b4d67c088d83bd3645
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.Scanner; public class codeforce_keyboardproblem { public static void main(String[] args) { Scanner scn = new Scanner(System.in); // System.out.println("Enter the number of test cases."); int t = scn.nextInt(); while (t-->0) { String keyboard, word; // System.out.println("Enter the keyboard of test cases."); keyboard = scn.next(); // System.out.println("Enter the word of test cases."); word = scn.next(); // System.out.println(keyboard.charAt(1)); int[] mappingArray = new int[256]; for (int i=0; i<keyboard.length(); i++) { mappingArray[keyboard.charAt(i)] = i+1; } // display(mappingArray); // System.out.println(); // System.out.println("good upto this point "+ word.length()); int ans = 0; for (int i=1; i<word.length(); i++) { ans = ans + Math.abs(mappingArray[word.charAt(i)] - mappingArray[word.charAt(i-1)]); } // System.out.println("answer is " + ans); System.out.println(ans); } } public static void display(int[] array) { for (int i=0; i<array.length; i++) { System.out.print(array[i]+ " "); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
5439c3dc21747b697686bd2cf994378e
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.Scanner; import java.lang.Math; import java.lang.String; public class LinearKeyboard { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t > 0) { String keyboard = sc.next(); String word = sc.next(); char[] k = keyboard.toCharArray(); char[] w = word.toCharArray(); int[] mapping = new int[256]; for(int i = 0; i < 26; i++) { mapping[k[i]] = i+1; } int answer = 0; for (int i = 0; i < w.length-1; i++) { answer = answer + Math.abs(mapping[w[i]] - mapping[w[i+1]]); } t--; System.out.println(answer); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
ba701244dc24fc3f6b6cdf1ff7ec0f87
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.nio.charset.StandardCharsets; import java.util.Scanner; public class CF1607A { public static void main(String[] args) { Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8); int t = scanner.nextInt(); for (int i = 0; i < t; i++) { String keyboard = scanner.next(); String s = scanner.next(); System.out.println(solve(keyboard, s)); } } private static String solve(String keyboard, String s) { // 长度 26 数组模拟 HashMap int[] dict = new int[26]; for (int i = 0; i < keyboard.length(); i++) { dict[keyboard.charAt(i) - 'a'] = i; } int res = 0; int preIdx = dict[s.charAt(0) - 'a']; for (int i = 1; i < s.length(); i++) { int idx = s.charAt(i) - 'a'; res += Math.abs(dict[idx] - preIdx); preIdx = dict[idx]; } return String.valueOf(res); } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
0a5e07fc088b14c491879b9a0178cf87
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.Scanner; public class LinearKeyboard { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { String k = sc.next(); String s = sc.next(); int ans = 0; int currIdx = 0; int previdx = 0; for (int j = 1; j < s.length(); j++) { currIdx = k.indexOf(s.charAt(j)) + 1; previdx = k.indexOf(s.charAt(j - 1)) + 1; ans += Math.abs(currIdx - previdx); System.out.print(""); } System.out.println(ans); } sc.close(); } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
0381e64b38a303401378c2f2383351a7
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.Scanner; public class linearKeyboard { public static void p (Object o) {System.out.println(o);} public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testCases = sc.nextInt(); while (testCases --> 0) { String keyboard = sc.next() , word = sc.next(); int count = 0; for (int i = 1; i < word.length(); i++) { count += Math.abs(keyboard.indexOf(word.charAt(i-1))-keyboard.indexOf(word.charAt(i))); } p(count); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
dc1b49a3492817ecd6768d39075425f8
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.*; public class Testing { public static void main(String[] args) { Scanner s = new Scanner (System.in); int t=s.nextInt(); for(int i=0;i<t;i++){ int firstindex=0,secondindex=0,sum=0; String input=s.next(),word=s.next(); for(int j=0;j<word.length();j++){ if(j==0) firstindex=input.indexOf(word.charAt(j))+1; else{ secondindex=input.indexOf(word.charAt(j))+1; sum+=Math.abs(firstindex-secondindex); firstindex=secondindex; } } System.out.println(sum); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
31d4514f20a64a8721fc41ae291b4337
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for(int i=0; i<t; i++) { String alphabet = scanner.next(); String word = scanner.next(); int sum = 0; for(int j=1; j< word.length(); j++) { sum += Math.abs((alphabet.lastIndexOf(word.charAt(j)) - alphabet.lastIndexOf(word.charAt(j-1)))); } System.out.println(sum); } scanner.close(); } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
9cc679827492b565e84c88ed6b257261
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.Scanner; public class test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { String s=sc.next(); String s1 = sc.next(); int ans=0; int l=s.indexOf(s1.charAt(0)); for (int i = 1; i <s1.length(); i++) { int z = s.indexOf(s1.charAt(i)); ans= ans + Math.abs(l-z); l =z; } System.out.println(ans); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 17
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
bb64b3a556412efdd2ea85da9cb5966b
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
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){ String str =sc.next(); String s = sc.next(); int count =0; for (int i=1;i<s.length();i++){ count+= Math.abs(str.indexOf(s.charAt(i)) - str.indexOf(s.charAt(i-1))); } System.out.println(count); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 11
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
bc269481c530ad2b0c5371e226141b1b
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.*; public class codeforces { public static void main(String[] args) { Scanner s= new Scanner(System.in); int t=s.nextInt(); while(--t>=0) { String str=s.next(); String word=s.next(); int sum=0,first=str.indexOf(word.charAt(0)); for(int i=0;i<word.length();i++) { int second= str.indexOf(word.charAt(i)); sum=sum+Math.abs(second-first); first=second; } System.out.println(sum); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 11
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
3e53fddce026a11e57b8db503526c43e
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int j=0;j<n;j++){ String k = sc.next(); String s = sc.next(); int sum = 0; for(int i=1;i<s.length();i++){ sum += Math.abs(k.indexOf(s.charAt(i-1)) - k.indexOf(s.charAt(i))); } System.out.println(sum); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 11
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
e01c3eeecb97b23513a62f21b63d94df
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
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){ String alpha=sc.next(); String str=sc.next(); int arr[]=new int[str.length()]; int sum=0; String s=""; for(int i=0;i<str.length();i++){ for(int j=0;j<alpha.length();j++ ){ if(str.charAt(i)==alpha.charAt(j)){ arr[i]=j+1; break; } } } for(int i=0,j=1;j<arr.length;i++,j++){ sum+=Math.abs(arr[j]-arr[i]); } System.out.println(sum); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 11
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
7eff63350bcf0ea912011cdeec22f4dc
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args){ Scanner s=new Scanner(System.in) ; int n=s.nextInt(); s.nextLine(); while(2*n-->0){ String c=s.nextLine(); char[] v=c.toCharArray(); int sum=0; String a=s.nextLine(); char[] b=a.toCharArray(); int k=b.length; for(int j=0;j<k-1;j++){ int h=find(v,b[j]); int g=find(v,b[j+1]); sum+=(int)Math.abs(h-g); } System.out.println(sum); } } public static int find(char[] a,char s){ for(int i=0;i<a.length;i++){ if(a[i]==s){ return i; } } return -1; } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 11
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
85de817960d5153dcaa0d464f90228bd
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { static Scanner sc = new Scanner(System.in); public static void main(String[] args){ int t = sc.nextInt(); while(t-->0){ solve(); } } public static void solve() { char[] arr = sc.next().toCharArray(); char[] word = sc.next().toCharArray(); long sum = 0; int[] val = new int[26]; for(int i = 0;i < 26;i++){ val[(int)(arr[i] - 'a')] = i + 1; } for(int i = 1;i < word.length;i++) { sum += Math.abs(val[(int)(word[i] - 'a')] - val[(int)(word[i - 1] - 'a')]); } System.out.println(sum); } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 11
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
09ae5b45ce2e41a300824cf7fc2399c0
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); scan.nextLine(); for(int i = 0;i<t;i++){ String alphabet = scan.nextLine(); String s = scan.nextLine(); int sum = 0; int[]value = new int[alphabet.length()]; for(int j = 0;j<alphabet.length();j++){ value[alphabet.charAt(j)-'a'] = j; } for(int k = 0;k<s.length()-1;k++){ sum += Math.abs(value[s.charAt(k+1) - 'a'] - value[s.charAt(k) - 'a']); } System.out.println(sum); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 11
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
fc09f822b288e5c2f3301621d9cb885a
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.*; public class Solution { private static Scanner scan = new Scanner(System.in); public static void main(String[] args) { int t = scan.nextInt(); scan.nextLine(); for(int i = 0;i<t;i++){ String alphabet = scan.nextLine(); String s = scan.nextLine(); int sum = 0; for(int j = 0;j<s.length()-1;j++){ sum += Math.abs((alphabet.indexOf(s.charAt(j+1) ) + 1) - (alphabet.indexOf(s.charAt(j)) +1)); } System.out.println(sum); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 11
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
3742e6960a331d1451920b6cbff9ea09
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.*; public class ln { public static void main(String[] args){ Scanner sc= new Scanner(System.in); int t=sc.nextInt(); sc.nextLine(); int[] a= new int[t]; for(int j=0;j<t;j++){ String keyb = sc.nextLine(); String str=sc.nextLine(); int[] map= new int[26]; for(int i=0;i<26;i++){ map[keyb.charAt(i)-'a']=i+1; } int ans=0; for(int i=0;i<str.length()-1;i++){ ans+=Math.abs(map[str.charAt(i)-'a'] - map[str.charAt(i+1)-'a']); } a[j]=ans; // } for(int i=0;i<a.length;i++) System.out.println(a[i]); } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 11
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
a4a8bc0ec5728e61bee939f65db9ff88
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.*; public class my { public static void main(String[] args){ int t; Scanner sc= new Scanner(System.in); t=sc.nextInt(); sc.nextLine(); int[] a =new int[t]; for(int j=0;j<t;j++){ String keyboard; String str; keyboard = sc.nextLine(); str=sc.nextLine(); int ans=0; for(int i=0;i<str.length()-1;i++){ ans+=Math.abs(keyboard.indexOf(str.charAt(i))-keyboard.indexOf(str.charAt(i+1))); } a[j]=ans;//System.out.println(ans); // t--; } for(int i=0;i<a.length;i++) System.out.println(a[i]); } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 11
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
1aea109e478fb667524388e2978fb10a
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class LinearKeyboard { public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { int t = sc.nextInt(); for (int i = 0; i < t; i++) { String keyboard = sc.next(); String word = sc.next(); Map<Character, Integer> map = new HashMap<>(); for (int j = 0; j < keyboard.toCharArray().length; j++) { map.put(keyboard.toCharArray()[j], j); } int count = 0; for (int j = 0; j < word.toCharArray().length - 1; j++) { int a = map.get(word.toCharArray()[j]); int b = map.get(word.toCharArray()[j + 1]); count += Math.abs(a - b); } System.out.println(count); } } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 11
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
c45a06465d80ef1baa85895e836bfd9a
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.stream.IntStream; public class MockCompetition { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String caseNumber = scanner.nextLine(); while (scanner.hasNext()) { String alphabetList = scanner.nextLine(); int seconds = 0; int[] ints = Arrays.stream(scanner.nextLine().split("")) .flatMapToInt(string -> IntStream.of(alphabetList.indexOf(string) + 1)) .toArray(); for (int letter = 1; letter < ints.length; letter++) { seconds += Math.abs(ints[letter] - ints[letter - 1]); } System.out.println(seconds); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 11
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output
PASSED
32782b22ebdae8ad732f56776f387245
train_108.jsonl
1635863700
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.
256 megabytes
import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.stream.IntStream; public class MockCompetition { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String caseNumber = scanner.nextLine(); while (scanner.hasNext()) { String alphabetList = scanner.nextLine(); int seconds = 0; int[] ints = Arrays.stream(scanner.nextLine().split("")) .flatMapToInt(string -> IntStream.of(alphabetList.indexOf(string) + 1)) .toArray(); for (int letter = 1; letter < ints.length; letter++) { seconds += Math.abs(ints[letter] - ints[letter - 1]); } System.out.println(seconds); } } }
Java
["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"]
1 second
["13\n0\n68\n0\n74"]
null
Java 11
standard input
[ "implementation", "strings" ]
7f9853be7ac857bb3c4eb17e554ad3f1
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.
800
Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.
standard output