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
sequencelengths
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
04151685f414fc1476aa42b17e71edd9
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; 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 k = input.nextInt(); int hold = k; char a[] =input.next().toCharArray(); int ans[] =new int[n]; char res[] = new char[n]; if(k%2==0) { for (int i = 0; i <n&&k>0; i++) { if(a[i]=='0') { ans[i]++; k--; } } ans[n-1]+=k; // System.out.println(Arrays.toString(ans)); } else { for (int i = 0; i <n&&k>0; i++) { if(a[i]=='1') { ans[i]++; k--; } } ans[n-1]+=k; // System.out.println(Arrays.toString(ans)); } for (int i = 0; i <n; i++) { if(a[i]=='1'&&(hold-ans[i])%2==0) { res[i] = '1'; } else if(a[i]=='1'&&(hold-ans[i])%2==1) { res[i] = '0'; } else if(a[i]=='0'&&(hold-ans[i])%2==1) { res[i]='1'; } else if(a[i]=='0'&&(hold-ans[i])%2==0) { res[i]='0'; } } StringBuilder result =new StringBuilder(); result.append(res); result.append("\n"); for (int an : ans) { result.append(an+" "); } System.out.println(result); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
38e2dd69e47909cab3fd907c3f256b8f
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.*; import java.util.*; public class B { public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out = new PrintWriter(System.out); int T = fs.nextInt(); for (int tt=0; tt<T; tt++) { int n = fs.nextInt(); int k = fs.nextInt(); int K = k; String s = fs.next(); int[] arr = new int[n]; if(k%2==0) { int i=0; while(k>0 && i<n) { if(s.charAt(i)=='0') { arr[i] = 1; k--; } i++; } } else { int i=0; while(k>0 && i<n) { if(s.charAt(i)=='1') { arr[i] = 1; k--; } i++; } } if(k>0) arr[n-1]+=k; StringBuilder sb = new StringBuilder(); for(int i =0; i<n; i++) { if((K-arr[i])%2==0) { sb.append(s.charAt(i)); } else { sb.append(s.charAt(i)=='1'?'0':'1'); } } out.println(sb); for(int x: arr) { out.print(x+" "); } out.println(); } out.close(); } 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 class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
864c337d3a445272dfe53d3def713dee
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.util.Scanner; //B. Bit Flipping public class B { public static void main(String[] args) { new B().solve(); } public void solve() { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); char[] res = null; int[] cnt = null; while (t-- > 0) { int n = scanner.nextInt(); int k = scanner.nextInt(); scanner.nextLine(); res = scanner.nextLine().toCharArray(); cnt = new int[n]; int ones = 0; int zeros = 0; for (int i = 0; i < n; i++) { if (res[i] == '1') ones++; else zeros++; } int j = 0; if (k % 2 == 0) { for (int i = 0; i < n && j < k; i++) { if (res[i] == '0') { res[i] = '1'; j++; cnt[i]++; } } if (zeros % 2 != 0 && zeros < k) res[n - 1] = '0'; } else { for (int i = 0; i < n; i++) { if (res[i] == '0') res[i] = '1'; else res[i] = '0'; } for (int i = 0; i < n && j < k; i++) { if (res[i] == '0') { res[i] = '1'; j++; cnt[i]++; } } if (ones % 2 == 0 && ones < k) res[n - 1] = '0'; } cnt[n - 1] += k - j; System.out.println(String.valueOf(res, 0, n)); for (int i = 0; i < n; i++) { System.out.print(cnt[i] + " "); } System.out.println(); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
f76b9e0b0abe1cbda0d256b974f29a39
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef{ public static void main (String[] args) throws java.lang.Exception{ Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-- > 0){ int n = scn.nextInt(), k = scn.nextInt(); String str = scn.next(); char[]ch = str.toCharArray(); int freq[] = new int[n], temp = k; StringBuilder sb = new StringBuilder(); for(int i = 0; i < n && temp > 0; i++){ if(k % 2 == ch[i] - '0'){ freq[i] = 1; temp--; } } freq[n - 1] += temp; for(int i = 0; i < n; i++){ if((k - freq[i]) % 2 == 1){ ch[i] = (char)('1' - (ch[i] - '0')); } } System.out.println(new String(ch)); for(int ele: freq){ System.out.print(ele + " "); } System.out.println(); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
46fffeabeabdae3108a72ef5c097d5f5
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; import java.util.concurrent.ThreadLocalRandom; public class c731{ public static void main(String[] args) throws IOException{ BufferedWriter out = new BufferedWriter( new OutputStreamWriter(System.out)); BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); PrintWriter pt = new PrintWriter(System.out); FastReader sc = new FastReader(); int t = sc.nextInt(); for(int o = 0; o<t;o++) { int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); int c1 = 0; int c2 = 0; for(int i = 0 ; i<n;i++){ if(s.charAt(i) == 0) { c2 ++; }else { c1++; } } int[] arr = new int[n]; if(k%2 == 1) { int x =0; for(int i = 0 ; i<n;i++) { if(x == k) { break; } if(s.charAt(i) == '0') { continue; } arr[i] = 1; x++; } int r = k - x; arr[n-1] += r; }else { int x =0; for(int i = 0 ; i<n;i++) { if(x == k) { break; } if(s.charAt(i) == '1') { continue; } arr[i] = 1; x++; } int r = k - x; arr[n-1] += r; } StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); for(int i = 0; i<n; i++) { if(k%2 == 1) { if(s.charAt(i) == '1') { if(arr[i]%2 == 1) { sb1.append("1" ); }else { sb1.append("0"); } }else { if(arr[i]%2 == 0) { sb1.append("1" ); }else { sb1.append("0"); } } }else { if(s.charAt(i) == '1') { if(arr[i]%2 == 0) { sb1.append("1" ); }else { sb1.append("0"); } }else { if(arr[i]%2 == 1) { sb1.append("1" ); }else { sb1.append("0"); } } } } for(int i = 0 ; i<n;i++) { sb2.append(arr[i] + " "); } System.out.println(sb1); System.out.println(sb2); } } //------------------------------------------------------------------------------------------------------------------------------------------------ public static boolean check(int[] arr , int n , int v , int l ) { int x = v/2; int y = v/2; // System.out.println(x + " " + y); if(v%2 == 1 ) { x++; } for(int i = 0 ; i<n;i++) { int d = l - arr[i]; int c = Math.min(d/2, y); y -= c; arr[i] -= c*2; if(arr[i] > x) { return false; } x -= arr[i]; } return true; } public static int cnt_set(long x) { long v = 1l; int c =0; int f = 0; while(v<=x) { if((v&x)!=0) { c++; } v = v<<1; } return c; } public static int lis(int[] arr,int[] dp) { int n = arr.length; ArrayList<Integer> al = new ArrayList<Integer>(); al.add(arr[0]); dp[0]= 1; for(int i = 1 ; i<n;i++) { int x = al.get(al.size()-1); if(arr[i]>x) { al.add(arr[i]); }else { int v = lower_bound(al, 0, al.size(), arr[i]); // System.out.println(v); al.set(v, arr[i]); } dp[i] = al.size(); } //return al.size(); return al.size(); } public static int lis2(int[] arr,int[] dp) { int n = arr.length; ArrayList<Integer> al = new ArrayList<Integer>(); al.add(-arr[n-1]); dp[n-1] = 1; // System.out.println(al); for(int i = n-2 ; i>=0;i--) { int x = al.get(al.size()-1); // System.out.println(-arr[i] + " " + i + " " + x); if((-arr[i])>x) { al.add(-arr[i]); }else { int v = lower_bound(al, 0, al.size(), -arr[i]); // System.out.println(v); al.set(v, -arr[i]); } dp[i] = al.size(); } //return al.size(); return al.size(); } static int cntDivisors(int n){ int cnt = 0; for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { if (n/i == i) cnt++; else cnt+=2; } } return cnt; } public static long power(long x, long y, long p){ long res = 1; x = x % p; if (x == 0) return 0; while (y > 0){ if ((y & 1) != 0) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } public static long ncr(long[] fac, int n , int r , long m) { if(r>n) { return 0; } return fac[n]*(modInverse(fac[r], m))%m *(modInverse(fac[n-r], m))%m; } public static int lower_bound(ArrayList<Integer> arr,int lo , int hi, int k) { int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (arr.get(mid) <k) { s=mid+1; } else { e=mid; } } if(s==arr.size()) { return -1; } return s; } public static int upper_bound(ArrayList<Integer> arr,int lo , int hi, int k) { int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (arr.get(mid) <=k) { s=mid+1; } else { e=mid; } } if(s==arr.size()) { return -1; } return s; } // ----------------------------------------------------------------------------------------------------------------------------------------------- public static long gcd(long a, long b){ if (a == 0) return b; return gcd(b % a, a); } //-------------------------------------------------------------------------------------------------------------------------------------------------------- public static long modInverse(long a, long m){ long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient long q = a / m; long t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } //_________________________________________________________________________________________________________________________________________________________________ // private static int[] parent; // private static int[] size; public static int find(int[] parent, int u) { while(u != parent[u]) { parent[u] = parent[parent[u]]; u = parent[u]; } return u; } private static void union(int[] parent,int[] size,int u, int v) { int rootU = find(parent,u); int rootV = find(parent,v); if(rootU == rootV) { return; } if(size[rootU] < size[rootV]) { parent[rootU] = rootV; size[rootV] += size[rootU]; } else { parent[rootV] = rootU; size[rootU] += size[rootV]; } } //----------------------------------------------------------------------------------------------------------------------------------- //segment tree //for finding minimum in range public static void build(int [] seg,int []arr,int idx, int lo , int hi) { if(lo == hi) { seg[idx] = arr[lo]; return; } int mid = (lo + hi)/2; build(seg,arr,2*idx+1, lo, mid); build(seg,arr,idx*2+2, mid +1, hi); // seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]); // seg[idx] = seg[idx*2+1]+ seg[idx*2+2]; // seg[idx] = Math.min(seg[idx*2+1], seg[idx*2+2]); seg[idx] = seg[idx*2+1] * seg[idx*2+2]; } //for finding minimum in range public static int query(int[]seg,int idx , int lo , int hi , int l , int r) { if(lo>=l && hi<=r) { return seg[idx]; } if(hi<l || lo>r) { return 1; } int mid = (lo + hi)/2; int left = query(seg,idx*2 +1, lo, mid, l, r); int right = query(seg,idx*2 + 2, mid + 1, hi, l, r); // return Math.min(left, right); //return gcd(left, right); // return Math.min(left, right); return left * right; } // // for sum // //public static void update(int[]seg,int idx, int lo , int hi , int node , int val) { // if(lo == hi) { // seg[idx] += val; // }else { //int mid = (lo + hi )/2; //if(node<=mid && node>=lo) { // update(seg, idx * 2 +1, lo, mid, node, val); //}else { // update(seg, idx*2 + 2, mid + 1, hi, node, val); //} //seg[idx] = seg[idx*2 + 1] + seg[idx*2 + 2]; // //} //} //--------------------------------------------------------------------------------------------------------------------------------------- // //static void shuffleArray(long[] ar) //{ // // If running on Java 6 or older, use `new Random()` on RHS here // Random rnd = ThreadLocalRandom.current(); // for (int i = ar.length - 1; i > 0; i--) // { // int index = rnd.nextInt(i + 1); // // Simple swap // int a = ar[index]; // ar[index] = ar[i]; // ar[i] = a; // } //} // static void shuffleArray(coup[] ar) // { // // If running on Java 6 or older, use `new Random()` on RHS here // Random rnd = ThreadLocalRandom.current(); // for (int i = ar.length - 1; i > 0; i--) // { // int index = rnd.nextInt(i + 1); // // Simple swap // coup a = ar[index]; // ar[index] = ar[i]; // ar[i] = a; // } // } //----------------------------------------------------------------------------------------------------------------------------------------------------------- } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //------------------------------------------------------------------------------------------------------------------------------------------------------------ //----------------------------------------------------------------------------------------------------------------------------------------------------------- class coup{ int a; int b; public coup(int a , int b) { this.a = a; this.b = b; } } class tripp{ int a; int b; double c; public tripp(int a , int b, double c) { this.a = a; this.b = b; this.c = c; } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
c9f606e365d67c6b8fc4c28dbdf2a148
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
/* Author _trevorphillips_ */ import java.io.*; import java.util.*; public class B_Bit_Flipping { 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 f = new FastReader(); StringBuilder sb = new StringBuilder(); int t = f.nextInt(); while (t-- > 0) { int n = f.nextInt(); int k = f.nextInt(); char[] s = f.nextLine().toCharArray(); if (k % 2 == 1) { for (int g = 0; g < s.length; g++) { if (s[g] == '1') { s[g] = '0'; } else { s[g] = '1'; } } } int[] arr = new int[n]; for (int i = 0; i < s.length; i++) { if (s[i] == '0' && k > 0) { k--; s[i] = '1'; arr[i]++; } } arr[n - 1] += k; if (k % 2 == 1) { s[n - 1] = '0'; } for (int i = 0; i < s.length; i++) { sb.append(s[i]); } sb.append("\n"); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } sb.append("\n"); } System.out.println(sb); } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
05180992f369ea1e1eff74f4e816e35d
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.util.*; import java.io.*; public class B { 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(), k = in.nextInt(); char[] s = in.next().toCharArray(); int[] res = new int[n]; int tempK = k; for (int i = 0; i < n && tempK > 0; i++) { if (k % 2 == s[i] - '0') { res[i] = 1; tempK--; } } res[n - 1] += tempK; for (int i = 0; i < n; i++) { if ((k - res[i]) % 2 == 1){ // debug(s[i]); s[i] = (char)((int)'1' - (int)(s[i] - '0')); } } for (char c: s) pw.print(c); pw.println(); for (int v: res) pw.print(v + " "); pw.println(); } pw.close(); } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
34e8666cee877103f46c61ea7b570513
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.util.*; import java.lang.Math.*; public class Inheritance{ public static void main(String[] args){ Scanner s=new Scanner(System.in); long t=s.nextLong(); while(t-->0) { int n=s.nextInt(); long k=s.nextLong(); String s1=s.next(); StringBuffer ans=new StringBuffer(""); long arr[]=new long[(int)n]; int i1=0; int i=0; if(k%2==0) { for(;i<n;i++) { if(s1.charAt(i)=='0' && k!=0) { arr[i1]=1; i1++; k=k-1; ans.append("1"); } else if(s1.charAt(i)=='1' && k!=0) { arr[i1]=0; i1++; ans.append("1");; } else if (k==0) { if(s1.charAt(i)=='0') ans.append("0"); else ans.append("1"); arr[i1]=0; i1++; } } if(k!=0) { k=k+arr[n-1]; char x=s1.charAt(n-1); ans.setCharAt(n-1, x);; if(k%2==0) { arr[(int)n-1]=k; } else { arr[(int)n-1]=k; if(ans.charAt(n-1)=='0') ans.setCharAt((int)n-1, '1'); else ans.setCharAt((int)n-1, '0'); } } } else { for(;i<n;i++) { if(s1.charAt(i)=='0' && k!=0) { arr[i1]=0; i1++; ans.append("1"); } else if(s1.charAt(i)=='1' && k!=0) { arr[i1]=1; k=k-1; i1++; ans.append("1");; } else if (k==0) { if(s1.charAt(i)=='0') ans.append("1"); else ans.append("0"); arr[i1]=0; i1++; } } if(k!=0) { k=k+arr[n-1]; char x=s1.charAt(n-1); ans.setCharAt(n-1, x);; if(k%2!=0) { arr[(int)n-1]=k; } else { arr[(int)n-1]=k; if(ans.charAt(n-1)=='0') ans.setCharAt((int)n-1, '1'); else ans.setCharAt((int)n-1, '0'); } } } System.out.println(ans); for(int i2=0;i2<n;i2++) { System.out.print(arr[i2]+" "); } System.out.println(); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
e0814a5a0338397dc78ed8b4926343df
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces782{ static long mod = 1000000007L; static MyScanner sc = new MyScanner(); static void solve() { int n = sc.nextInt(); int r = sc.nextInt(); int b = sc.nextInt(); int arr[] = new int[2 * b + 1]; Arrays.fill(arr, 1); r = r - b; r--; while (r > 0) for (int i = 0; i < arr.length&& r > 0; i += 2) { arr[i]++; r--; } for(int i = 0;i<arr.length;i++){ if(i==0 || i%2==0){ for(int j = 0;j<arr[i];j++){ out.print('R'); } }else{ out.print('B'); } } out.println(); } static void solve2(){ int n = sc.nextInt(); int k = sc.nextInt(); char arr[] = sc.nextLine().toCharArray(); if(k%2!=0){ for(int i = 0;i<n;i++){ if(arr[i]=='1') arr[i] = '0'; else arr[i]='1'; } } int ans[] = new int[n]; for(int i = 0;i<n && k>0;i++){ if(arr[i]=='0'){ ans[i]++; k--; arr[i] = '1'; } } if(k>0){ if(k%2!=0){ if(arr[n-1]=='1') arr[n-1] = '0'; else arr[n-1]='1'; } ans[n-1]+=k; } for(int i = 0;i<n;i++){ out.print(arr[i]); } out.println(); for(int i = 0;i<n;i++){ out.print(ans[i]+" "); } out.println(); } static void solve3(){ int n= sc.nextInt(); long a = sc.nextLong(); long b = sc.nextLong(); long arr[] = new long[n+1]; for(int i =1 ;i<=n;i++) arr[i] = sc.nextLong(); long dp[][] = new long[arr.length+1][arr.length+1]; for(int i = 0;i<dp.length;i++){ Arrays.fill(dp[i],-1); } out.println(fun(dp,arr,0,1,a,b)); } static long fun(long dp[][],long arr[],int i,int j,long a,long b){ if(j==arr.length) return 0; if(dp[i][j]!=-1) return dp[i][j]; if(i+1==j){ dp[i][j] = b*(arr[j]-arr[i])+fun(dp,arr,i,j+1,a,b); }else dp[i][j] = Math.min(b*(arr[j]-arr[i])+fun(dp,arr,i,j+1,a,b),(a*(arr[i+1]-arr[i]))+fun(dp,arr,i+1,j,a,b)); return dp[i][j]; } static void solve4(){ } static void reverse(int arr[]){ int i = 0;int j = arr.length-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static class Pair{ int odd; int even; Pair(int o,int e){ odd = o; even = e; } } static long pow(long a, long b) { if (b == 0) return 1; long res = pow(a, b / 2); res = (res * res) % 1_000_000_007; if (b % 2 == 1) { res = (res * a) % 1_000_000_007; } return res; } static int lis(int arr[],int n){ int lis[] = new int[n]; lis[0] = 1; for(int i = 1;i<n;i++){ lis[i] = 1; for(int j = 0;j<i;j++){ if(arr[i]>arr[j]){ lis[i] = Math.max(lis[i],lis[j]+1); } } } int max = Integer.MIN_VALUE; for(int i = 0;i<n;i++){ max = Math.max(lis[i],max); } return max; } static boolean isPali(String str){ int i = 0; int j = str.length()-1; while(i<j){ if(str.charAt(i)!=str.charAt(j)){ return false; } i++; j--; } return true; } static long gcd(long a,long b){ if(b==0) return a; return gcd(b,a%b); } static String reverse(String str){ char arr[] = str.toCharArray(); int i = 0; int j = arr.length-1; while(i<j){ char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } String st = new String(arr); return st; } static boolean isprime(int n){ if(n==1) return false; if(n==3 || n==2) return true; if(n%2==0 || n%3==0) return false; for(int i = 5;i*i<=n;i+= 6){ if(n%i== 0 || n%(i+2)==0){ return false; } } return true; } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); // int t= 1; while(t-- >0){ // solve(); solve2(); // solve3(); } // Stop writing your solution here. ------------------------------------- out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int[] readIntArray(int n){ int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(next()); } return arr; } int[] reverse(int arr[]){ int n= arr.length; int i = 0; int j = n-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; j--;i++; } return arr; } long[] readLongArray(int n){ long arr[] = new long[n]; for(int i = 0;i<n;i++){ arr[i] = Long.parseLong(next()); } return arr; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } private static void sort(long[] arr) { List<Long> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
7fdf8e9614bd219a90bb77a6a312baa5
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.*; import java.util.*; public class test { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.nextLine(); int tm = k; int[] vis = new int[s.length()]; for(int i = 0;i<vis.length&&k>0;i++) { if(s.charAt(i)=='1') { if(tm%2==1) { vis[i]++; k--; } } else { if(tm%2==0) { vis[i]++; k--; } } } vis[n-1]+=k; StringBuilder tmp = new StringBuilder(); for (int j = 0; j < n; j++) { if (s.charAt(j) == '0' && (tm - vis[j]) % 2 == 0) { tmp.append("0"); } if (s.charAt(j) == '1' && (tm - vis[j]) % 2 == 1) { tmp.append("0"); } if (s.charAt(j) == '0' && (tm - vis[j]) % 2 == 1) { tmp.append("1"); } if (s.charAt(j) == '1' && (tm - vis[j]) % 2 == 0) { tmp.append("1"); } } pw.println(tmp); StringBuilder res = new StringBuilder(); for (int i = 0; i < vis.length; i++) { res.append(vis[i]); if (i != vis.length - 1) res.append(" "); } pw.println(res); } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
65d14e9b0a4617cfa3b1fd9159a0b058
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.*; import java.util.*; public class B_1659 { public static void main(String[] args) { FastReader sc = new FastReader(); PrintWriter out=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt(); char[] ch=sc.next().toCharArray(); if(k%2==1) { for(int i=0;i<n;i++) { if(ch[i]=='1') ch[i]='0'; else ch[i]='1'; } } int[] arr=new int[n]; for(int i=0;i<n;i++) { if(ch[i]=='0' && k>0) { ch[i]='1'; k--; arr[i]++; } } arr[n-1]+=k; if(k%2==1) ch[n-1]='0'; out.println(ch); for(int i=0;i<n;i++) out.print(arr[i]+" "); out.println(); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return (str); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
ced2048f62e0dcdbcfbabcd51d6f5958
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
//import java.io.IOException; import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.InputMismatchException; import java.util.List; public class BitFlipping { static InputReader inputReader=new InputReader(System.in); static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static void solve() throws IOException { int n=inputReader.nextInt(); int k=inputReader.nextInt(); int temp=k; String str=inputReader.readString(); int answer[]=new int[n]; for (int i=0;i<n-1;i++) { if (str.charAt(i)=='0') { if (temp%2!=0) { answer[i]=0; } else { if (k > 0) { answer[i] = 1; k--; } } } else { if (temp%2==0) { answer[i]=0; } else { if (k > 0) { answer[i] = 1; k--; } } } } answer[n-1]=k; StringBuffer fans=new StringBuffer(); for (int i=0;i<n;i++) { int oper=(temp-answer[i]); if (str.charAt(i)=='0') { if (oper%2==0) { fans.append('0'); } else { fans.append('1'); } } else { if (oper%2==0) { fans.append('1'); } else { fans.append('0'); } } } out.println(new String(fans)); for (int ele:answer) { out.print(ele+" "); } out.println(); } static PrintWriter out=new PrintWriter((System.out)); static void SortDec(long arr[]) { List<Long>list=new ArrayList<>(); for(long ele:arr) { list.add(ele); } Collections.sort(list,Collections.reverseOrder()); for (int i=0;i<list.size();i++) { arr[i]=list.get(i); } } static void Sort(long arr[]) { List<Long>list=new ArrayList<>(); for(long ele:arr) { list.add(ele); } Collections.sort(list); for (int i=0;i<list.size();i++) { arr[i]=list.get(i); } } public static void main(String args[])throws IOException { int t=inputReader.nextInt(); while (t-->0) { solve(); } long s = System.currentTimeMillis(); // out.println(System.currentTimeMillis()-s+"ms"); out.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
7bc977f4ae590e3ed1dd286c211030c6
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class B_Bit_Flipping { static Scanner in = new Scanner(); static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans = new StringBuilder(); static int testCases, n; static long k; static char a[]; static void solve(int t) { int s[] = new int[n]; for(int i = 0; i < n; ++i) { s[i] = a[i] - '0'; } long copyK = k; long dp[] = new long[n]; int num[] = {0, 1}; int j = 0; for(int i = 0; i < n && k > 0; ++i) { a[i] ^= num[j]; if(k % 2 == a[i] - '0' && i != n - 1) { dp[i]++; --k; j = (j + 1) % 2; } } dp[n - 1] += k; for(int i = 0; i < n; ++i) { if((copyK - dp[i]) % 2 == 1 ) { s[i] ^= 1; } } for(int i : s) { ans.append(i); } ans.append("\n"); for(long i : dp) { ans.append(i).append(" "); } if (t != testCases) { ans.append("\n"); } } public static void main(String[] priya) throws IOException { testCases = in.nextInt(); for (int t = 0; t < testCases; ++t) { n = in.nextInt(); k = in.nextLong(); a = in.next().toCharArray(); solve(t + 1); } out.print(ans.toString()); out.flush(); in.close(); } static String mul(String num1, String num2) { int len1 = num1.length(); int len2 = num2.length(); if (len1 == 0 || len2 == 0) { return "0"; } int result[] = new int[len1 + len2]; int i_n1 = 0; int i_n2 = 0; for (int i = len1 - 1; i >= 0; i--) { int carry = 0; int n1 = num1.charAt(i) - '0'; i_n2 = 0; for (int j = len2 - 1; j >= 0; j--) { int n2 = num2.charAt(j) - '0'; int sum = n1 * n2 + result[i_n1 + i_n2] + carry; carry = sum / 10; result[i_n1 + i_n2] = sum % 10; i_n2++; } if (carry > 0) { result[i_n1 + i_n2] += carry; } i_n1++; } int i = result.length - 1; while (i >= 0 && result[i] == 0) { i--; } if (i == -1) { return "0"; } String s = ""; while (i >= 0) { s += (result[i--]); } return s; } static class Node<T> { T data; Node<T> next; public Node() { this.next = null; } public Node(T data) { this.data = data; this.next = null; } public T getData() { return data; } public void setData(T data) { this.data = data; } public Node<T> getNext() { return next; } public void setNext(Node<T> next) { this.next = next; } @Override public String toString() { return this.getData().toString() + " "; } } static class ArrayList<T> { Node<T> head, tail; int len; public ArrayList() { this.head = null; this.tail = null; this.len = 0; } int size() { return len; } boolean isEmpty() { return len == 0; } int indexOf(T data) { if (isEmpty()) { throw new ArrayIndexOutOfBoundsException(); } Node<T> temp = head; int index = -1, i = 0; while (temp != null) { if (temp.getData() == data) { index = i; } i++; temp = temp.getNext(); } return index; } void add(T data) { Node<T> newNode = new Node<>(data); if (isEmpty()) { head = newNode; tail = newNode; len++; } else { tail.setNext(newNode); tail = newNode; len++; } } void see() { if (isEmpty()) { throw new ArrayIndexOutOfBoundsException(); } Node<T> temp = head; while (temp != null) { out.print(temp.getData().toString() + " "); out.flush(); temp = temp.getNext(); } out.println(); out.flush(); } void inserFirst(T data) { Node<T> newNode = new Node<>(data); Node<T> temp = head; if (isEmpty()) { head = newNode; tail = newNode; len++; } else { newNode.setNext(temp); head = newNode; len++; } } T get(int index) { if (isEmpty() || index >= len) { throw new ArrayIndexOutOfBoundsException(); } Node<T> temp = head; int i = 0; T data = null; while (temp != null) { if (i == index) { data = temp.getData(); } i++; temp = temp.getNext(); } return data; } void addAt(T data, int index) { if (index >= len) { throw new ArrayIndexOutOfBoundsException(); } Node<T> newNode = new Node<>(data); int i = 0; Node<T> temp = head; while (temp.next != null) { if (i == index) { newNode.setNext(temp.next); temp.next = newNode; } i++; temp = temp.getNext(); } // temp.setNext(temp); len++; } void popFront() { if (isEmpty()) { throw new ArrayIndexOutOfBoundsException(); } if (head == tail) { head = null; tail = null; } else { head = head.getNext(); } len--; } void removeAt(int index) { if (index >= len) { throw new ArrayIndexOutOfBoundsException(); } if (index == 0) { this.popFront(); return; } Node<T> temp = head; int i = 0; Node<T> n = new Node<>(); while (temp != null) { if (i == index) { n.next = temp.next; temp.next = n; break; } i++; n = temp; temp = temp.getNext(); } tail = n; --len; } void clearAll() { this.head = null; this.tail = null; } } static void merge(long a[], int left, int right, int mid) { int n1 = mid - left + 1, n2 = right - mid; long L[] = new long[n1]; long R[] = new long[n2]; for (int i = 0; i < n1; i++) { L[i] = a[left + i]; } for (int i = 0; i < n2; i++) { R[i] = a[mid + 1 + i]; } int i = 0, j = 0, k1 = left; while (i < n1 && j < n2) { if (L[i] <= R[j]) { a[k1] = L[i]; i++; } else { a[k1] = R[j]; j++; } k1++; } while (i < n1) { a[k1] = L[i]; i++; k1++; } while (j < n2) { a[k1] = R[j]; j++; k1++; } } static void sort(long a[], int left, int right) { if (left >= right) { return; } int mid = (left + right) / 2; sort(a, left, mid); sort(a, mid + 1, right); merge(a, left, right, mid); } static class Scanner { BufferedReader in; StringTokenizer st; public Scanner() { in = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } String nextLine() throws IOException { return in.readLine(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } void close() throws IOException { in.close(); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
5a91b9b91c2dc9057d52b1698662dfe5
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.util.*; public class Main { public static void main(String arggs[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); while(t-- > 0) { int n = sc.nextInt(), k = sc.nextInt(), cnt = 0, arr[] = new int[n]; String s = sc.next(); for(int i=0; i<n-1; i++) { if(s.charAt(i)-'0'== k%2) { if(cnt < k) { cnt++; arr[i]++; sb.append(1); } else sb.append(0); }else sb.append(1); } arr[n-1] += k-cnt; sb.append((s.charAt(n-1)-'0' == cnt%2 ? 0 : 1) + "\n"); for(int a : arr) sb.append(a + " "); sb.append("\n"); } System.out.println(sb); } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
7bbe8426301961b9345cd8dd7062d7f7
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.PriorityQueue; import java.util.StringTokenizer; public class B { public static void main(String[]args) throws IOException { Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(),k=sc.nextInt(); String s=sc.next(); int[]ans=new int[n]; int i=0; boolean flipped=false; int kk=k; boolean allO=false,oEx=false; while(kk-->0) { int st=kk+1; if(st%2==0) { char trgt=flipped?'1':'0'; while(i<n&&s.charAt(i)!=trgt)i++; if(i==n) { allO=true; break; } ans[i]++; }else { char trgt=flipped?'0':'1'; while(i<n&&s.charAt(i)!=trgt)i++; if(i==n) { oEx=true; break; } ans[i]++; } i++; flipped=!flipped; } ans[n-1]+=kk+1; if(allO||oEx) { StringBuilder sb=new StringBuilder(); for(int j=0;j<n-1;j++)sb.append("1"); sb.append(allO?"1":"0"); out.println(sb.toString()); }else { int mx1=-1; for(int j=n-1;j>=0;j--) { if(ans[j]==1) {mx1=j; break; } } for(int j=0;j<=mx1;j++) { out.print("1"); } for(int j=mx1+1;j<n;j++) { int c='1'-s.charAt(j); out.print(k%2==0?1-c:c); } out.println(); } for(int j=0;j<ans.length;j++)out.print(ans[j]+" "); out.println(); } out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public boolean hasNext() {return st.hasMoreTokens();} public int nextInt() throws IOException {return Integer.parseInt(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready(); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
f7895505f8b85dacfd7fd641d5adc99d
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Random; import java.util.StringTokenizer; /* 1 6 3 100001 */ public class B { public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { int n=fs.nextInt(); int k=fs.nextInt(); char[] a=fs.next().toCharArray(); if (k%2==1) { for (int i=0; i<n; i++) { if (a[i]=='0') a[i]='1'; else a[i]='0'; } } int[] choice=new int[n]; for (int i=0; i<a.length; i++) { if (a[i]=='0' && k>0) { a[i]='1'; k--; choice[i]++; } } choice[n-1]+=k; if (k%2==1) { a[n-1]='0'; } out.println(a); for (int i=0; i<n; i++) { out.print(choice[i]+" "); } out.println(); } out.close(); } static final Random random=new Random(); static final int mod=1_000_000_007; 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 long add(long a, long b) { return (a+b)%mod; } static long sub(long a, long b) { return ((a-b)%mod+mod)%mod; } static long mul(long a, long b) { return (a*b)%mod; } static long exp(long base, long exp) { if (exp==0) return 1; long half=exp(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials=new long[2_000_001]; static long[] invFactorials=new long[2_000_001]; static void precompFacts() { factorials[0]=invFactorials[0]=1; for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i); invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2); for (int i=invFactorials.length-2; i>=0; i--) invFactorials[i]=mul(invFactorials[i+1], i+1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k])); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
10884e0a1d1464375e20f9d20b0264db
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.util.*; import javax.management.Query; import java.io.*; public class practice { // static int n,k; //// static String t,s; // static long[]memo; // static int n;// int k=sc.nextInt(); static String s; static HashMap<Long,Integer>hm; public static void main(String[] args) throws Exception { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int k=sc.nextInt(); String s=sc.next(); int[] a=new int[n]; int[] b=new int[n]; int y=0; for (int i = 0; i < n-1; i++) { if(k>0) { if (k % 2 == ((s.charAt(i) - '0')^y)) { a[i]=1; b[i]=1; k--; y^=1; }else{ a[i]=1; } } else{ a[i]=((s.charAt(i) - '0')^y); } } a[n-1]=((s.charAt(n-1) - '0')^y); b[n-1]=k; for (int i = 0; i <n ; i++) { pw.print(a[i]); } pw.println(); for (int i = 0; i <n ; i++) { pw.print(b[i]+" "); } pw.println(); } pw.close(); } public static void sort(int[] in) { shuffle(in); Arrays.sort(in); } public static void shuffle(int[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); int tmp = in[i]; in[i] = in[idx]; in[idx] = tmp; } } static LinkedList getfact(int n){ LinkedList<Integer>ll=new LinkedList<>(); LinkedList<Integer>ll2=new LinkedList<>(); for (int i = 1; i <= Math.sqrt(n); i++) { if(n%i==0) { ll.add(i); if(i!=n/i) ll2.addLast(n/i); } } while (!ll2.isEmpty()){ ll.add(ll2.removeLast()); } return ll; } static void rev(int n){ String s1=s.substring(0,n); s=s.substring(n); for (int i = 0; i <n ; i++) { s=s1.charAt(i)+s; } } static class SegmentTree { // 1-based DS, OOP int N; //the number of elements in the array as a power of 2 (i.e. after padding) long[] array, sTree; Long[]lazy; SegmentTree(long[] in) { array = in; N = in.length - 1; sTree = new long[N<<1]; //no. of nodes = 2*N - 1, we add one to cross out index zero lazy = new Long[N<<1]; build(1,1,N); } void build(int node, int b, int e) // O(n) { if(b == e) sTree[node] = array[b]; else { int mid = b + e >> 1; build(node<<1,b,mid); build(node<<1|1,mid+1,e); sTree[node] = sTree[node<<1]+sTree[node<<1|1]; } } void update_point(int index, int val) // O(log n) { index += N - 1; sTree[index] += val; while(index>1) { index >>= 1; sTree[index] = sTree[index<<1] + sTree[index<<1|1]; } } void update_range(int i, int j, int val) // O(log n) { update_range(1,1,N,i,j,val); } void update_range(int node, int b, int e, int i, int j, int val) { if(i > e || j < b) return; if(b >= i && e <= j) { sTree[node] = (e-b+1)*val; lazy[node] = val*1l; } else { int mid = b + e >> 1; propagate(node, b, mid, e); update_range(node<<1,b,mid,i,j,val); update_range(node<<1|1,mid+1,e,i,j,val); sTree[node] = sTree[node<<1] + sTree[node<<1|1]; } } void propagate(int node, int b, int mid, int e) { if(lazy[node]!=null) { lazy[node << 1] = lazy[node]; lazy[node << 1 | 1] = lazy[node]; sTree[node << 1] = (mid - b + 1) * lazy[node]; sTree[node << 1 | 1] = (e - mid) * lazy[node]; } lazy[node] = null; } long query(int i, int j) { return query(1,1,N,i,j); } long query(int node, int b, int e, int i, int j) // O(log n) { if(i>e || j <b) return 0; if(b>= i && e <= j) return sTree[node]; int mid = b + e >> 1; propagate(node, b, mid, e); long q1 = query(node<<1,b,mid,i,j); long q2 = query(node<<1|1,mid+1,e,i,j); return q1 + q2; } } // public static long dp(int idx) { // if (idx >= n) // return Long.MAX_VALUE/2; // return Math.min(dp(idx+1),memo[idx]+dp(idx+k)); // } // if(num==k) // return dp(0,idx+1); // if(memo[num][idx]!=-1) // return memo[num][idx]; // long ret=0; // if(num==0) { // if(s.charAt(idx)=='a') // ret= dp(1,idx+1); // else if(s.charAt(idx)=='?') { // ret=Math.max(1+dp(1,idx+1),dp(0,idx+1) ); // } // } // else { // if(num%2==0) { // if(s.charAt(idx)=='a') // ret=dp(num+1,idx+1); // else if(s.charAt(idx)=='?') // ret=Math.max(1+dp(num+1,idx+1),dp(0,idx+1)); // } // else { // if(s.charAt(idx)=='b') // ret=dp(num+1,idx+1); // else if(s.charAt(idx)=='?') // ret=Math.max(1+dp(num+1,idx+1),dp(0,idx+1)); // } // } // } static void putorrem(long x){ if(hm.getOrDefault(x,0)==1){ hm.remove(x); } else hm.put(x,hm.getOrDefault(x,0)-1); } public static int max4(int a,int b, int c,int d) { int [] s= {a,b,c,d}; Arrays.sort(s); return s[3]; } public static double logbase2(int k) { return( (Math.log(k)+0.0)/Math.log(2)); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } public tuble add(tuble t){ return new tuble(this.x+t.x,this.y+t.y,this.z+t.z); } } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
514bc5102ef74d01e035c5f5757496d6
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.text.DecimalFormat; import java.util.*; /* 101001 */ public class Codeforces { static int mod= 1000000007 ; public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); DecimalFormat formatter= new DecimalFormat("#0.000000"); // int t=1; int t=fs.nextInt(); outer:for(int time=1;time<=t;time++) { int n=fs.nextInt(), k=fs.nextInt(); int f=k; char arr[]=fs.next().toCharArray(); int ans[]=new int[n]; if(k%2==0) { for(int i=0;i<n-1;i++) { if(arr[i]=='0'&&k>0) { k--; ans[i]=1; } } ans[n-1]=k; } else { for(int i=0;i<n-1;i++) { if(arr[i]=='1'&&k>0) { k--; ans[i]=1; } } ans[n-1]=k; } for(int i=0;i<n;i++) { int a= (f-ans[i]); a%=2; int b; if(arr[i]=='1') b=1; else b=0; out.print((a^b)); } out.println(); for(int ele:ans) out.print(ele+" "); out.println(); } out.close(); } static long pow(long a,long b) { if(b<0) return 1; long res=1; while(b!=0) { if((b&1)!=0) { res*=a; res%=mod; } a*=a; a%=mod; b=b>>1; } return res; } static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } static long nck(int n,int k) { if(k>n) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { // return fact[(int)n]; long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n); int temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort Arrays.sort(a); } static void sort(long[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n); long temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort Arrays.sort(a); } // Use this to input code since it is faster than a Scanner 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(); } String nextLine() { String str=""; try { str= (br.readLine()); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long[] readArrayL(int n) { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
5f2da4ccae2531f8bee2537b2d1c258d
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); // int a = 1; int t; t = in.nextInt(); //t = 1; while (t > 0) { // out.print("Case #"+(a++)+": "); solver.call(in,out); t--; } out.close(); } static class TaskA { public void call(InputReader in, PrintWriter out) { int n, k, a = 0; n = in.nextInt(); k = in.nextInt(); char[] arr = in.next().toCharArray(); int[] ans = new int[n]; for (int i = 0; i < n; i++) { if(arr[i] == '1'){ if(k%2!=0) { ans[i]++; a++; } } if(a==k){ break; } if(arr[i] == '0'){ if(k%2==0){ ans[i]++; a++; } } if(a==k){ break; } } if(a!=k){ ans[n - 1] += (k - a); } StringBuilder s = new StringBuilder(""); for (int i = 0; i < n; i++) { if(arr[i]=='1' && (k - ans[i]) %2==0){ s.append("1"); } else if(arr[i]=='0' && (k - ans[i]) %2!=0){ s.append("1"); } else{ s.append("0"); } } out.println(s); for(Integer i : ans){ out.print(i+" "); } out.println(); } } static int gcd(int a, int b ) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class answer implements Comparable<answer>{ int a, b; long c; public answer(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public answer(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return Integer.compare(this.a, o.a); } } static class answer1 implements Comparable<answer1>{ int a, b, c; public answer1(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public answer1(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(answer1 o) { if(this.b == o.b){ return Integer.compare(this.a, o.a); } return Integer.compare(this.b, o.b); } } 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
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
ffc3639ac16715c0154b2b1855d69647
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class B1659 { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); solver.solve(in, out); out.close(); } private static class Solver { void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int k = in.nextInt(); String s = in.next(); char[] chars = s.toCharArray(); int[] ans = new int[chars.length]; if (k % 2 == 0) { for (int i = 0; i < n; i++) { if (chars[i] == '0' && k > 0) { k--; ans[i]++; } } ans[n - 1] += k; for (int i = 0; i < n; i++) { if (ans[i] % 2 != 0) { chars[i] = (char)('0' + ('1' - chars[i])); } } } else { for (int i = 0; i < n; i++) { if (chars[i] == '1' && k > 0) { k--; ans[i]++; } } ans[n - 1] += k; for (int i = 0; i < n; i++) { if (ans[i] % 2 == 0) { chars[i] = (char)('0' + ('1' - chars[i])); } } } out.println(chars); for (int an : ans) { out.print(an + " "); } out.println(); } } } private static class InputReader { BufferedReader reader; StringTokenizer tokenizer; InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
fefdeab69d7e668a7c62a1ab5b501f76
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class B_Bit_Flipping { static Scanner in = new Scanner(); static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans = new StringBuilder(); static int testCases, n; static long k; static char a[]; static void solve(int t) { int num[] = {0, 1}; long dp[] = new long[n]; int s[] = new int[n]; for (int i = 0; i < n; ++i) { s[i] = a[i] - '0'; } int j = 0; long copyK = k; for (int i = 0; i < n && k > 0; ++i) { a[i] ^= num[j]; if (k % 2 == a[i] - '0' && i != n - 1) { ++dp[i]; j = (j + 1) % 2; --k; } } dp[n - 1] += k; for (int i = 0; i < n; ++i) { if ((copyK - dp[i]) % 2 == 1) { s[i] ^= 1; } } for (int i : s) { ans.append(i); } ans.append("\n"); for (long i : dp) { ans.append(i).append(" "); } if (t != testCases) { ans.append("\n"); } } public static void main(String[] priya) throws IOException { testCases = in.nextInt(); for (int t = 0; t < testCases; ++t) { n = in.nextInt(); k = in.nextLong(); a = in.next().toCharArray(); solve(t + 1); } out.print(ans.toString()); out.flush(); in.close(); } static String mul(String num1, String num2) { int len1 = num1.length(); int len2 = num2.length(); if (len1 == 0 || len2 == 0) { return "0"; } int result[] = new int[len1 + len2]; int i_n1 = 0; int i_n2 = 0; for (int i = len1 - 1; i >= 0; i--) { int carry = 0; int n1 = num1.charAt(i) - '0'; i_n2 = 0; for (int j = len2 - 1; j >= 0; j--) { int n2 = num2.charAt(j) - '0'; int sum = n1 * n2 + result[i_n1 + i_n2] + carry; carry = sum / 10; result[i_n1 + i_n2] = sum % 10; i_n2++; } if (carry > 0) { result[i_n1 + i_n2] += carry; } i_n1++; } int i = result.length - 1; while (i >= 0 && result[i] == 0) { i--; } if (i == -1) { return "0"; } String s = ""; while (i >= 0) { s += (result[i--]); } return s; } static class Node<T> { T data; Node<T> next; public Node() { this.next = null; } public Node(T data) { this.data = data; this.next = null; } public T getData() { return data; } public void setData(T data) { this.data = data; } public Node<T> getNext() { return next; } public void setNext(Node<T> next) { this.next = next; } @Override public String toString() { return this.getData().toString() + " "; } } static class ArrayList<T> { Node<T> head, tail; int len; public ArrayList() { this.head = null; this.tail = null; this.len = 0; } int size() { return len; } boolean isEmpty() { return len == 0; } int indexOf(T data) { if (isEmpty()) { throw new ArrayIndexOutOfBoundsException(); } Node<T> temp = head; int index = -1, i = 0; while (temp != null) { if (temp.getData() == data) { index = i; } i++; temp = temp.getNext(); } return index; } void add(T data) { Node<T> newNode = new Node<>(data); if (isEmpty()) { head = newNode; tail = newNode; len++; } else { tail.setNext(newNode); tail = newNode; len++; } } void see() { if (isEmpty()) { throw new ArrayIndexOutOfBoundsException(); } Node<T> temp = head; while (temp != null) { out.print(temp.getData().toString() + " "); out.flush(); temp = temp.getNext(); } out.println(); out.flush(); } void inserFirst(T data) { Node<T> newNode = new Node<>(data); Node<T> temp = head; if (isEmpty()) { head = newNode; tail = newNode; len++; } else { newNode.setNext(temp); head = newNode; len++; } } T get(int index) { if (isEmpty() || index >= len) { throw new ArrayIndexOutOfBoundsException(); } Node<T> temp = head; int i = 0; T data = null; while (temp != null) { if (i == index) { data = temp.getData(); } i++; temp = temp.getNext(); } return data; } void addAt(T data, int index) { if (index >= len) { throw new ArrayIndexOutOfBoundsException(); } Node<T> newNode = new Node<>(data); int i = 0; Node<T> temp = head; while (temp.next != null) { if (i == index) { newNode.setNext(temp.next); temp.next = newNode; } i++; temp = temp.getNext(); } // temp.setNext(temp); len++; } void popFront() { if (isEmpty()) { throw new ArrayIndexOutOfBoundsException(); } if (head == tail) { head = null; tail = null; } else { head = head.getNext(); } len--; } void removeAt(int index) { if (index >= len) { throw new ArrayIndexOutOfBoundsException(); } if (index == 0) { this.popFront(); return; } Node<T> temp = head; int i = 0; Node<T> n = new Node<>(); while (temp != null) { if (i == index) { n.next = temp.next; temp.next = n; break; } i++; n = temp; temp = temp.getNext(); } tail = n; --len; } void clearAll() { this.head = null; this.tail = null; } } static void merge(long a[], int left, int right, int mid) { int n1 = mid - left + 1, n2 = right - mid; long L[] = new long[n1]; long R[] = new long[n2]; for (int i = 0; i < n1; i++) { L[i] = a[left + i]; } for (int i = 0; i < n2; i++) { R[i] = a[mid + 1 + i]; } int i = 0, j = 0, k1 = left; while (i < n1 && j < n2) { if (L[i] <= R[j]) { a[k1] = L[i]; i++; } else { a[k1] = R[j]; j++; } k1++; } while (i < n1) { a[k1] = L[i]; i++; k1++; } while (j < n2) { a[k1] = R[j]; j++; k1++; } } static void sort(long a[], int left, int right) { if (left >= right) { return; } int mid = (left + right) / 2; sort(a, left, mid); sort(a, mid + 1, right); merge(a, left, right, mid); } static class Scanner { BufferedReader in; StringTokenizer st; public Scanner() { in = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } String nextLine() throws IOException { return in.readLine(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } void close() throws IOException { in.close(); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
5d8240f61e841edf03ec83ffb0e8240d
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
/* I am dead inside Do you like NCT, sKz, BTS? 5 4 3 2 1 Moonwalk Imma knock it down like domino Is this what you want? Is this what you want? Let's ttalkbocky about that */ import static java.lang.Math.*; import java.util.*; import java.io.*; import java.math.*; public class NewTimeB { 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 K = Integer.parseInt(st.nextToken()); String line = infile.readLine().trim(); char[] input = line.toCharArray(); int[] arr = new int[N]; for(int i=0; i < N; i++) arr[i] = input[i]-'0'; int[] res = new int[N]; if(K == 0) { sb.append(line).append("\n"); for(int x: res) sb.append(x+" "); sb.append("\n"); continue; } if((K&1) == 1) for(int i=0; i < N; i++) arr[i] ^= 1; for(int i=0; i < N; i++) if(K > 0 && arr[i] == 0) { res[i] = 1; arr[i] = 1; K--; } res[N-1] += K; arr[N-1] ^= (K&1); for(int x: arr) sb.append(x); sb.append('\n'); for(int x: res) sb.append(x+" "); sb.append('\n'); } System.out.print(sb); } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } } /* K=2 011 000 101 [1,1,0] 011 000 110 [1,0,1] */
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
ab08662a740facdf93fe65903c397daa
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.*; import java.util.*; public class CodeForces { /*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/ public static void solve(int tCase) throws IOException { int n = sc.nextInt(); int k = sc.nextInt(); char[] arr = sc.next().toCharArray(); Pair<String,int[]> ans; if(k%2==0)ans = find(arr,'0',n,k); else ans = find(arr,'1',n,k); out.println(ans.ff); for(int x : ans.ss)out.print(x+" "); out.println(); } private static Pair<String,int[]> find(char[] arr, char a,int n,int k){ int[] ans = new int[n]; int used = 0; for(int i=0;i<n && k>0;i++) if(arr[i] == a){ ans[i] = 1; k--; used++; } ans[0] += 2*(k/2); used+= 2*(k/2); k = k%2; char[] bbr = new char[n]; for(int i=0;i<n;i++){ int rem = used - ans[i]; if(rem % 2 ==1)bbr[i] = arr[i]=='1'?'0':'1'; else bbr[i] = arr[i]; } if(k==1){ // out.println(Arrays.toString(bbr)+" "+Arrays.toString(ans)); Pair<String ,int[] > pp = null; for(int i=0;i<n;i++){ if(bbr[i]=='1'){ char[] x = new char[n]; int[] a1 = new int[n]; x[i] = bbr[i]; a1[i]++; for(int j=0;j<n;j++){ a1[j] += ans[j]; if(i==j)continue; x[j] = bbr[j]=='1'?'0':'1'; } pp = new Pair<>(String.valueOf(x),a1); break; } } for(int i=0;i<n;i++) { if (bbr[i] == '0') { char[] x = new char[n]; int[] a1 = new int[n]; x[i] = bbr[i]; a1[i]++; for (int j = 0; j < n; j++) { a1[j] += ans[j]; if (i == j) continue; x[j] = bbr[j] == '1' ? '0' : '1'; } String y = String.valueOf(x); if (pp==null || y.compareTo(pp.ff) > 0) pp = new Pair<>(y, a1); break; } } for(int i=n-1;i>=0;i--){ if(bbr[i]=='1'){ char[] x = new char[n]; int[] a1 = new int[n]; x[i] = bbr[i]; a1[i]++; for(int j=0;j<n;j++){ a1[j] += ans[j]; if(i==j)continue; x[j] = bbr[j]=='1'?'0':'1'; } String y = String.valueOf(x); if (pp==null || y.compareTo(pp.ff) > 0) pp = new Pair<>(y, a1); break; } } for(int i=n-1;i>=0;i--) { if (bbr[i] == '0') { char[] x = new char[n]; int[] a1 = new int[n]; x[i] = bbr[i]; a1[i]++; for (int j = 0; j < n; j++) { a1[j] += ans[j]; if (i == j) continue; x[j] = bbr[j] == '1' ? '0' : '1'; } String y = String.valueOf(x); if (pp==null || y.compareTo(pp.ff) > 0) pp = new Pair<>(y, a1); break; } } return pp; } return new Pair<>(String.valueOf(bbr),ans); } public static void main(String[] args) throws IOException { openIO(); int testCase = 1; testCase = sc.nextInt(); for (int i = 1; i <= testCase; i++) solve(i); closeIO(); } /*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/ /*--------------------------------------HELPER FUNCTIONS STARTS HERE-----------------------------------------*/ // public static int mod = (int) 1e9 + 7; public static int mod = 998244353; public static int inf_int = (int) 2e9+10; public static long inf_long = (long) 2e18; public static void _sort(int[] arr, boolean isAscending) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } public static void _sort(long[] arr, boolean isAscending) { int n = arr.length; List<Long> list = new ArrayList<>(); for (long ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // time : O(1), space : O(1) public static int _digitCount(long num,int base){ // this will give the # of digits needed for a number num in format : base return (int)(1 + Math.log(num)/Math.log(base)); } // time : O(n), space: O(n) public static long _fact(int n){ // simple factorial calculator long ans = 1; for(int i=2;i<=n;i++) ans = ans * i % mod; return ans; } // time for pre-computation of factorial and inverse-factorial table : O(nlog(mod)) public static long[] factorial , inverseFact; public static void _ncr_precompute(int n){ factorial = new long[n+1]; inverseFact = new long[n+1]; factorial[0] = inverseFact[0] = 1; for (int i = 1; i <=n; i++) { factorial[i] = (factorial[i - 1] * i) % mod; inverseFact[i] = _modExpo(factorial[i], mod - 2); } } // time of factorial calculation after pre-computation is O(1) public static int _ncr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod); } public static int _npr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[n - r] % mod); } // euclidean algorithm time O(max (loga ,logb)) public static long _gcd(long a, long b) { while (a>0){ long x = a; a = b % a; b = x; } return b; // if (a == 0) // return b; // return _gcd(b % a, a); } // lcm(a,b) * gcd(a,b) = a * b public static long _lcm(long a, long b) { return (a / _gcd(a, b)) * b; } // binary exponentiation time O(logn) public static long _modExpo(long x, long n) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans *= x; ans %= mod; n--; } else { x *= x; x %= mod; n >>= 1; } } return ans; } // function to find a/b under modulo mod. time : O(logn) public static long _modInv(long a,long b){ return (a * _modExpo(b,mod-2)) % mod; } //sieve or first divisor time : O(mx * log ( log (mx) ) ) public static int[] _seive(int mx){ int[] firstDivisor = new int[mx+1]; for(int i=0;i<=mx;i++)firstDivisor[i] = i; for(int i=2;i*i<=mx;i++) if(firstDivisor[i] == i) for(int j = i*i;j<=mx;j+=i) if(firstDivisor[j]==j)firstDivisor[j] = i; return firstDivisor; } // check if x is a prime # of not. time : O( n ^ 1/2 ) private static boolean _isPrime(long x){ for(long i=2;i*i<=x;i++) if(x%i==0)return false; return true; } static class Pair<K, V>{ K ff; V ss; public Pair(K ff, V ss) { this.ff = ff; this.ss = ss; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || this.getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return ff.equals(pair.ff) && ss.equals(pair.ss); } @Override public int hashCode() { return Objects.hash(ff, ss); } @Override public String toString(){ return ff.toString()+" "+ss.toString(); } } /*--------------------------------------HELPER FUNCTIONS ENDS HERE-----------------------------------------*/ /*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/ static FastestReader sc; static PrintWriter out; private static void openIO() throws IOException { sc = new FastestReader(); out = new PrintWriter(System.out); } public static void closeIO() throws IOException { out.flush(); out.close(); sc.close(); } private static final class FastestReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public FastestReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastestReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() throws IOException { int b; //noinspection StatementWithEmptyBody while ((b = read()) != -1 && isSpaceChar(b)) {} return b; } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); final boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return neg?-ret:ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); final boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return neg?-ret:ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); final boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10); return neg?-ret:ret; } public String nextLine() throws IOException { final byte[] buf = new byte[(1<<10)]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } /*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/ } /** Some points to keep in mind : * 1. don't use Arrays.sort(primitive data type array) * 2. try to make the parameters of a recursive function as less as possible, * more use static variables. * **/
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
e44d7b89673acb5c87b3942fd5348da5
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static long factorial(int number) { long f = 1; int j = 1; while (j <= number) { f = (f * j) % 998244353; j++; } return f; } public static long combination(int n, int r) { return factorial(n) / (factorial(n - r) * factorial(r)); } public static void main(String[] args) throws Exception { int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int k = sc.nextInt(); int tmp=k; char[] s = sc.next().toCharArray(); int o=0; int[]res = new int[n]; char[] helper = new char[n]; for(int i=0;i<s.length;i++) { helper[i]=s[i]; } for(int i=0;i<n&&k>0;i++) { s[i]^=o; if(k%2!=s[i]-'0' || i==n-1) continue; res[i]++; k--; o^=1; } res[n-1]+=k; for(int i=0;i<n;i++) { if((tmp-res[i])%2==1) { helper[i]^=1; } } String ans = new String(helper); pw.println(helper); for(int i=0;i<n;i++) { pw.print(res[i]+" "); } pw.println(); } pw.flush(); } static class SegmentTree { int[] arr, sTree, lazy; int N; public SegmentTree(int[] in) { arr = in; N = in.length - 1; sTree = new int[2 * N]; lazy = new int[2 * N]; build(1, 1, N); } public void build(int Node, int left, int right) { // O(n) if (left == right) sTree[Node] = arr[left]; else { int r = 2 * Node + 1; int l = 2 * Node; int mid = (left + right) / 2; build(l, left, mid); build(r, mid + 1, right); sTree[Node] = sTree[l] + sTree[r]; } } public int query(int i, int j) { return query(1, 1, N, i, j); } public int query(int Node, int left, int right, int i, int j) { if (right < i || left > j) return 0; if (right <= j && i <= left) { return sTree[Node]; } int mid = (left + right) / 2; int l = 2 * Node; int r = 2 * Node + 1; return query(l, left, mid, i, j) + query(r, mid + 1, right, i, j); } public void updatePoint(int idx, int value) { int node = idx + N - 1; arr[idx] = value; sTree[node] = value; while (node > 1) { node /= 2; int l = 2 * node; int r = 2 * node + 1; sTree[node] = sTree[l] + sTree[r]; } } public void updateRange(int i, int j, int val) { updateRange(1, 1, N, i, j, val); } public void updateRange(int Node, int left, int right, int i, int j, int val) { if (i > right || left > j) return; if (right <= j && i <= left) { sTree[Node] += val * (right - left + 1); lazy[Node] += val; } else { int l = 2 * Node; int r = 2 * Node + 1; int mid = (left + right) / 2; propagate(Node, left, right); updateRange(l, left, mid, i, j, val); updateRange(r, mid + 1, right, mid, j, val); sTree[Node] = sTree[l] + sTree[r]; } } public void propagate(int node, int left, int right) { int l = 2 * node; int r = 2 * node + 1; int mid = (left + right) / 2; lazy[l] = lazy[node]; lazy[r] = lazy[node]; sTree[l] += lazy[node] * (mid - left + 1); sTree[r] += lazy[node] * (right - mid); lazy[node] = 0; } } public static void sort(int[] in) { shuffle(in); Arrays.sort(in); } public static void shuffle(int[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); int tmp = in[i]; in[i] = in[idx]; in[idx] = tmp; } } static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return x - o.x; } public String toString() { return x + " " + y; } } static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } public static void display(char[][] a) { for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j]); } System.out.println(); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
bb41a22096757b27e12a341ebfa23c49
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.util.*; public class Main { public static void main(String arggs[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); while(t-- > 0) { int n = sc.nextInt(), k = sc.nextInt(), cnt = 0, arr[] = new int[n]; String s = sc.next(); for(int i=0; i<n-1; i++) { if(s.charAt(i)-'0'== k%2) { if(cnt < k) { cnt++; arr[i]++; sb.append(1); } else sb.append(0); }else sb.append(1); } arr[n-1] += k-cnt; sb.append((s.charAt(n-1)-'0' == cnt%2 ? 0 : 1) + "\n"); for(int a : arr) sb.append(a + " "); sb.append("\n"); } System.out.println(sb); } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
e9ba14e19d71cf7bdfd2a86eb47e4f16
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
// "static void main" must be defined in a public class. 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 k = s.nextInt(); String str = s.next(); int value = k; int[] arr = new int[str.length()]; char find = '0'; int idx = -1; if(k%2!=0) { for(int j=0;j<str.length();j++) { if(str.charAt(j)=='1') { idx = j; break; } } if(idx==-1) idx = str.length()-1; arr[idx]=1; k--; find = '1'; } // System.out.println("????????"+" "+idx+" "+find+" "+k); for(int j=idx+1;j<str.length();j++) { if(k==0) break; if(str.charAt(j)==find) { arr[j]+=1; k--; } } arr[arr.length-1]+=(k); // System.out.println("----------->"); // for(int j=0;j<arr.length;j++) // System.out.println(arr[j]+" (***)"); StringBuilder sb = new StringBuilder(""); for(int j=0;j<arr.length;j++) { int val = value - arr[j]; if(val%2==0) sb.append(str.charAt(j)); else { char c = '0'; if(str.charAt(j)=='0') c='1'; sb.append(c); } } System.out.println(sb); for(int j=0;j<arr.length;j++) System.out.print(arr[j]+" "); System.out.println(); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
7f32783a5a0604e52b50240b22cc7e6b
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=1000000007; // static long mod=998244353; static int MAX=Integer.MAX_VALUE; static int MIN=Integer.MIN_VALUE; static long MAXL=Long.MAX_VALUE; static long MINL=Long.MIN_VALUE; static ArrayList<Integer> graph[]; // static ArrayList<ArrayList<Integer>> graph; static long fact[]; static int seg[]; static int dp[][]; // static long dp[][]; public static void main (String[] args) throws java.lang.Exception { // code goes here int t=I(); outer:while(t-->0) { int n=I(),k=I(); char c[]=S().toCharArray(); int o=0,z=0; for(int i=0;i<c.length;i++){ if(c[i]=='0')z++; else o++; } int ans[]=new int[c.length]; if(k%2==0){ if(k<=z){ for(int i=0;i<c.length;i++){ if(c[i]=='0' && k-->0){ c[i]='1'; ans[i]=1; } } }else{ if(z%2==0){ for(int i=0;i<c.length;i++){ if(c[i]=='0'){ c[i]='1'; ans[i]=1; } } ans[0]+=k-z; }else{ for(int i=0;i<c.length;i++){ if(c[i]=='0'){ c[i]='1'; ans[i]=1; } } c[c.length-1]='0'; // ans[0]+=k-z-1; ans[c.length-1]+=k-z; } } }else{ if(k<=o){ for(int i=0;i<c.length;i++){ if(c[i]=='1' && k-->0){ c[i]='1'; ans[i]=1; }else if(c[i]=='0'){ c[i]='1'; }else{ c[i]='0'; } } }else{ if(o%2==0){ for(int i=0;i<c.length;i++){ if(c[i]=='1'){ ans[i]=1; } c[i]='1'; } c[c.length-1]='0'; ans[c.length-1]+=k-o; }else{ for(int i=0;i<c.length;i++){ if(c[i]=='1'){ ans[i]=1; } c[i]='1'; } ans[c.length-1]+=k-o; } } } for(int i=0;i<c.length;i++){ out.print(c[i]); }out.println(); for(int i=0;i<c.length;i++){ out.print(ans[i]+" "); }out.println(); } out.close(); } public static class pair { long a; long b; public pair(long aa,long bb) { a=aa; b=bb; } } public static class myComp implements Comparator<pair> { //sort in ascending order. public int compare(pair p1,pair p2) { if(p1.a==p2.a) return 0; else if(p1.a<p2.a) return -1; else return 1; } // sort in descending order. // public int compare(pair p1,pair p2) // { // if(p1.a==p2.a) // return 0; // else if(p1.a<p2.a) // return 1; // else // return -1; // } } public static void DFS(int s,boolean visited[]) { visited[s]=true; for(int i:graph[s]){ if(!visited[i]){ DFS(i,visited); } } } public static void setGraph(int n,int m) { graph=new ArrayList[n+1]; for(int i=0;i<=n;i++){ graph[i]=new ArrayList<>(); } for(int i=0;i<m;i++){ int u=I(),v=I(); graph[u].add(v); graph[v].add(u); } } //LOWER_BOUND and UPPER_BOUND functions //It returns answer according to zero based indexing. public static int lower_bound(long arr[],long X,int start, int end) //start=0,end=n-1 { if(start>end)return -1; if(arr[arr.length-1]<X)return end; if(arr[0]>X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; // if(arr[mid]==X){ //Returns last index of lower bound value. // if(mid<end && arr[mid+1]==X){ // left=mid+1; // }else{ // return mid; // } // } if(arr[mid]==X){ //Returns first index of lower bound value. if(mid>start && arr[mid-1]==X){ right=mid-1; }else{ return mid; } } else if(arr[mid]>X){ if(mid>start && arr[mid-1]<X){ return mid-1; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1]>X){ return mid; }else{ left=mid+1; } } } return left; } //It returns answer according to zero based indexing. public static int upper_bound(long arr[],long X,int start,int end) //start=0,end=n-1 { if(arr[0]>=X)return start; if(arr[arr.length-1]<X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; if(arr[mid]==X){ //returns first index of upper bound value. if(mid>start && arr[mid-1]==X){ right=mid-1; }else{ return mid; } } // if(arr[mid]==X){ //returns last index of upper bound value. // if(mid<end && arr[mid+1]==X){ // left=mid+1; // }else{ // return mid; // } // } else if(arr[mid]>X){ if(mid>start && arr[mid-1]<X){ return mid; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1]>X){ return mid+1; }else{ left=mid+1; } } } return left; } //END //Segment Tree Code public static void buildTree(int a[],int si,int ss,int se) { if(ss==se){ // seg[si]=ss; if(a[ss]%2==0){ seg[si]=1; }else{ seg[si]=0; } return; } int mid=(ss+se)/2; buildTree(a,2*si+1,ss,mid); buildTree(a,2*si+2,mid+1,se); // seg[si]=max(seg[2*si+1],seg[2*si+2]); seg[si]=(seg[2*si+1]&seg[2*si+2]); } public static void merge(ArrayList<Integer> f,ArrayList<Integer> a,ArrayList<Integer> b) { int i=0,j=0; while(i<a.size() && j<b.size()){ if(a.get(i)<=b.get(j)){ f.add(a.get(i)); i++; }else{ f.add(b.get(j)); j++; } } while(i<a.size()){ f.add(a.get(i)); i++; } while(j<b.size()){ f.add(b.get(j)); j++; } } public static void update(int si,int ss,int se,int pos,int val) { if(ss==se){ seg[si]=(int)add(seg[si],val); return; } int mid=(ss+se)/2; if(pos<=mid){ update(2*si+1,ss,mid,pos,val); }else{ update(2*si+2,mid+1,se,pos,val); } seg[si]=(int)add(seg[2*si+1],seg[2*si+2]); } public static int query(int a[],int si,int ss,int se,int qs,int qe) { if(qs>se || qe<ss)return -1; if(ss>=qs && se<=qe)return seg[si]; int mid=(ss+se)/2; // return max(query(2*si+1,ss,mid,qs,qe),query(2*si+2,mid+1,se,qs,qe)); int p1=query(a,2*si+1,ss,mid,qs,qe); int p2=query(a,2*si+2,mid+1,se,qs,qe); if(p1==-1 && p2==-1)return -1; else if(p1==-1)return p2; else if(p2==-1)return p1; else return (p1&p2); } //Segment Tree Code end //Prefix Function of KMP Algorithm public static int[] KMP(char c[],int n) { int pi[]=new int[n]; for(int i=1;i<n;i++){ int j=pi[i-1]; while(j>0 && c[i]!=c[j]){ j=pi[j-1]; } if(c[i]==c[j])j++; pi[i]=j; } return pi; } public static long kadane(long a[],int n) //largest sum subarray { long max_sum=Long.MIN_VALUE,max_end=0; for(int i=0;i<n;i++){ max_end+=a[i]; if(max_sum<max_end){max_sum=max_end;} if(max_end<0){max_end=0;} } return max_sum; } public static long nPr(int n,int r) { long ans=divide(fact(n),fact(n-r),mod); return ans; } public static long nCr(int n,int r) { long ans=divide(fact[n],mul(fact[n-r],fact[r]),mod); return ans; } public static boolean isSorted(int a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static boolean isSorted(long a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static int computeXOR(int n) //compute XOR of all numbers between 1 to n. { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } public static int np2(int x) { x--; x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x++; return x; } public static int hp2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static long hp2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static ArrayList<Integer> primeSieve(int n) { ArrayList<Integer> arr=new ArrayList<>(); boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } // Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n); public static class FenwickTree { int farr[]; int n; public FenwickTree(int c) { n=c+1; farr=new int[n]; } // public void update_range(int l,int r,long p) // { // update(l,p); // update(r+1,(-1)*p); // } public void update(int x,int p) { for(;x<n;x+=x&(-x)) { farr[x]+=p; } } public int get(int x) { int ans=0; for(;x>0;x-=x&(-x)) { ans=ans+farr[x]; } return ans; } } //Disjoint Set Union public static class DSU { int par[],rank[]; public DSU(int c) { par=new int[c+1]; rank=new int[c+1]; for(int i=0;i<=c;i++) { par[i]=i; rank[i]=0; } } public int find(int a) { if(a==par[a]) return a; return par[a]=find(par[a]); } public void union(int a,int b) { int a_rep=find(a),b_rep=find(b); if(a_rep==b_rep) return; if(rank[a_rep]<rank[b_rep]) par[a_rep]=b_rep; else if(rank[a_rep]>rank[b_rep]) par[b_rep]=a_rep; else { par[b_rep]=a_rep; rank[a_rep]++; } } } public static HashMap<Integer,Integer> primeFact(int a) { // HashSet<Long> arr=new HashSet<>(); HashMap<Integer,Integer> hm=new HashMap<>(); int p=0; while(a%2==0){ // arr.add(2L); p++; a=a/2; } hm.put(2,hm.getOrDefault(2,0)+p); for(int i=3;i*i<=a;i+=2){ p=0; while(a%i==0){ // arr.add(i); p++; a=a/i; } hm.put(i,hm.getOrDefault(i,0)+p); } if(a>2){ // arr.add(a); hm.put(a,hm.getOrDefault(a,0)+1); } // return arr; return hm; } public static boolean isInteger(double N) { int X = (int)N; double temp2 = N - X; if (temp2 > 0) { return false; } return true; } public static boolean isPalindrome(String s) { int n=s.length(); for(int i=0;i<=n/2;i++){ if(s.charAt(i)!=s.charAt(n-i-1)){ return false; } } return true; } public static int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static long fact(long n) { long fact=1; for(long i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static long fact(int n) { long fact=1; for(int i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static void printArray(long a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(int a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(char a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]); } out.println(); } public static void printArray(String a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(boolean a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(pair a[]) { for(pair p:a){ out.println(p.a+"->"+p.b); } } public static void printArray(int a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(long a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(char a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(ArrayList<?> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printMapI(HashMap<?,?> hm){ for(Map.Entry<?,?> e:hm.entrySet()){ out.println(e.getKey()+"->"+e.getValue()); }out.println(); } public static void printMap(HashMap<Long,ArrayList<Integer>> hm){ for(Map.Entry<Long,ArrayList<Integer>> e:hm.entrySet()){ out.print(e.getKey()+"->"); ArrayList<Integer> arr=e.getValue(); for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); }out.println(); } } //Modular Arithmetic public static long add(long a,long b) { a+=b; if(a>=mod)a-=mod; return a; } public static long sub(long a,long b) { a-=b; if(a<0)a+=mod; return a; } public static long mul(long a,long b) { return ((a%mod)*(b%mod))%mod; } public static long divide(long a,long b,long m) { a=mul(a,modInverse(b,m)); return a; } public static long modInverse(long a,long m) { int x=0,y=0; own p=new own(x,y); long g=gcdExt(a,m,p); if(g!=1){ out.println("inverse does not exists"); return -1; }else{ long res=((p.a%m)+m)%m; return res; } } public static long gcdExt(long a,long b,own p) { if(b==0){ p.a=1; p.b=0; return a; } int x1=0,y1=0; own p1=new own(x1,y1); long gcd=gcdExt(b,a%b,p1); p.b=p1.a - (a/b) * p1.b; p.a=p1.b; return gcd; } public static long pwr(long m,long n) { long res=1; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m); } n=n>>1; m=(m*m); } return res; } public static long modpwr(long m,long n) { long res=1; m=m%mod; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m)%mod; } n=n>>1; m=(m*m)%mod; } return res; } public static class own { long a; long b; public own(long val,long index) { a=val; b=index; } } //Modular Airthmetic public static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(char[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { char tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } //max & min public static int max(int a,int b){return Math.max(a,b);} public static int min(int a,int b){return Math.min(a,b);} public static int max(int a,int b,int c){return Math.max(a,Math.max(b,c));} public static int min(int a,int b,int c){return Math.min(a,Math.min(b,c));} public static long max(long a,long b){return Math.max(a,b);} public static long min(long a,long b){return Math.min(a,b);} public static long max(long a,long b,long c){return Math.max(a,Math.max(b,c));} public static long min(long a,long b,long c){return Math.min(a,Math.min(b,c));} public static int maxinA(int a[]){int n=a.length;int mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static long maxinA(long a[]){int n=a.length;long mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static int mininA(int a[]){int n=a.length;int mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long mininA(long a[]){int n=a.length;long mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long suminA(int a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} public static long suminA(long a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} //end public static int[] I(int n){int a[]=new int[n];for(int i=0;i<n;i++){a[i]=I();}return a;} public static long[] IL(int n){long a[]=new long[n];for(int i=0;i<n;i++){a[i]=L();}return a;} public static long[] prefix(int a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;} public static long[] prefix(long a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;} public static int I(){return sc.I();} public static long L(){return sc.L();} public static String S(){return sc.S();} public static double D(){return sc.D();} } class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try { st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int I(){ return Integer.parseInt(next());} long L(){ return Long.parseLong(next());} double D(){return Double.parseDouble(next());} String S(){ String str = ""; try { str = br.readLine(); } catch (IOException e){ e.printStackTrace(); } return str; } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
d2195f28eb08751701fce67ddd1698e0
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class Main { static final int INF = 0x3f3f3f3f; static final long LNF = 0x3f3f3f3f3f3f3f3fL; public static void main(String[] args) throws IOException { initReader(); int t=nextInt(); while (t--!=0){ int n=nextInt(); int k=nextInt(); String s=next(); int[]arr=new int[n]; int p=0; if(k%2!=0){ p=1; } int m=k; for(int i=0;i<s.length();i++){ if(m==0)break; if(s.charAt(i)-'0'==p){ arr[i]=1; m--; } } if(m!=0)arr[n-1]+=m; for(int i=0;i<n;i++){ if(s.charAt(i)=='1'&&(k-arr[i])%2!=0)pw.print("0"); else if(s.charAt(i)=='1'&&(k-arr[i])%2==0)pw.print("1"); else if(s.charAt(i)=='0'&&(k-arr[i])%2==0)pw.print("0"); else if(s.charAt(i)=='0'&&(k-arr[i])%2!=0)pw.print("1"); } pw.println(); for(int i=0;i<n;i++){ pw.print(arr[i]+" "); } pw.println(); } pw.close(); } /***************************************************************************************/ static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter pw; public static void initReader() throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = new StringTokenizer(""); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); // 从文件读写 // reader = new BufferedReader(new FileReader("test.in")); // tokenizer = new StringTokenizer(""); // pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out"))); } public static boolean hasNext() { try { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (Exception e) { return false; } return true; } public static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static String nextLine() { try { return reader.readLine(); } catch (Exception e) { return null; } } public static int nextInt() throws IOException { return Integer.parseInt(next()); } public static long nextLong() throws IOException { return Long.parseLong(next()); } public static double nextDouble() throws IOException { return Double.parseDouble(next()); } public static char nextChar() throws IOException { return next().charAt(0); } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
f8e3d308f67943c26120617022aa65c8
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.lang.*; import java.util.*; import java.io.*; 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(); long k=sc.nextLong(); sc.nextLine(); String s=sc.nextLine(); StringBuilder str=new StringBuilder(); long arr[]=new long[n]; long count=k; for(int i=0;i<n-1;i++){ if(s.charAt(i)=='1'){ if(count==0){ char c=s.charAt(i); if(k%2==1){ c=(c=='1')?'0':'1'; } str.append(c); } else if(k%2==1){ arr[i]=1; str.append('1'); count--; } else{ str.append('1'); } } else{ if(count==0){ char c=s.charAt(i); if(k%2==1){ c=(c=='1')?'0':'1'; } str.append(c); } else if(k%2==0){ count--; arr[i]=1; str.append('1'); } else{ str.append('1'); } } } char c=s.charAt(n-1); if((k-count)%2==1){ c=(c=='1')?'0':'1'; } str.append(c); arr[n-1]=count; System.out.println(str.toString()); for(int i=0;i<n;i++){ System.out.print(arr[i]+" "); } System.out.println(); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
0e57719a1bfa547433b43d3d5000a5fa
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.*; import java.util.*; public class BitFlipping{ private static BufferedReader br; private static BufferedWriter bw; private static String[] buffer; private static int index; private static void before() throws Exception { try{ new BufferedReader(new FileReader("local.txt")); local(); }catch(Exception e){ online(); } buffer = new String[0]; index = 0; } private static void after() throws Exception { br.close(); bw.close(); } private static void online() throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(System.out)); } private static void local() throws Exception { br = new BufferedReader(new FileReader("input.txt")); bw = new BufferedWriter(new FileWriter("output.txt")); } private static String next() throws Exception { if(buffer.length == index) { buffer = br.readLine().split(" "); index = 0; } return buffer[index++]; } private static int nextInt() throws Exception { return Integer.parseInt(next()); } private static long nextLong() throws Exception { return Long.parseLong(next()); } private static double nextDouble() throws Exception { return Double.parseDouble(next()); } private static float nextFloat() throws Exception { return Float.parseFloat(next()); } private static boolean nextBoolean() throws Exception { return Boolean.parseBoolean(next()); } private static void print(int x) throws Exception { bw.write(x + ""); } private static void println(int x) throws Exception { bw.write(x + "\n"); } private static void print(long x) throws Exception { bw.write(x + ""); } private static void println(long x) throws Exception { bw.write(x + "\n"); } private static void print(boolean x) throws Exception { bw.write(x + ""); } private static void println(boolean x) throws Exception { bw.write(x + "\n"); } private static void print(char x) throws Exception { bw.write(x); } private static void println(char x) throws Exception { bw.write(x + "\n"); } private static void print(float x) throws Exception { bw.write(x + ""); } private static void println(float x) throws Exception { bw.write(x + "\n"); } private static void print(double x) throws Exception { bw.write(x + ""); } private static void println(double x) throws Exception { bw.write(x + "\n"); } private static void print(String x) throws Exception { bw.write(x); } private static void println(String x) throws Exception { bw.write(x + "\n"); } private static void println() throws Exception { bw.write("\n"); } public static void main(String[] args) throws Exception { before(); solve(); after(); } private static void solve() throws Exception { int t = nextInt(); while(t-- > 0) { int n = nextInt(); int k = nextInt(); String s = next(); char[] ch = s.toCharArray(); int[] arr = new int[n]; if(k % 2 == 0) { for(int i = 0; i < n && k > 0; i++) { if(ch[i] == '0') { ch[i] = '1'; arr[i]++; k--; } } arr[n - 1] += k; if(k > 0 && k % 2 == 1) ch[n-1] = '0'; } else { for(int i = 0; i < n; i++) { if(ch[i] == '0') ch[i] = '1'; else { if(k > 0) { arr[i]++; k--; } else ch[i] = '0'; } } arr[n - 1] += k; if(k > 0 && k % 2 == 1) ch[n - 1] = '0'; } println(new String(ch)); for(int x : arr) print(x + " "); println(); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
42cd5e9cb39005e8f7563bd1f610b22d
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.StringTokenizer; import java.util.TreeMap; public class Practice2 { // // static class Pair{ // int val; // int data; // Pair(int val,int data){ // this.val=val; // this.data=data; // } // } // // public static void printarr(int[] arr) { int n=arr.length; for(int i=0;i<n;i++) { System.out.print(arr[i]+" "); } System.out.println(); } // /** Code for Dijkstra's algorithm **/ public static class ListNode { int vertex, weight; ListNode(int v,int w) { vertex = v; weight = w; } int getVertex() { return vertex; } int getWeight() { return weight; } } public static int[] dijkstra( int V, HashMap<Integer,ArrayList<ListNode> > graph, int source) { int[] distance = new int[V]; for (int i = 0; i < V; i++) distance[i] = Integer.MAX_VALUE; distance[0] = 0; PriorityQueue<ListNode> pq = new PriorityQueue<>( (v1, v2) -> v1.getWeight() - v2.getWeight()); pq.add(new ListNode(source, 0)); while (pq.size() > 0) { ListNode current = pq.poll(); for (ListNode n : graph.get(current.getVertex())) { if (distance[current.getVertex()] + n.getWeight() < distance[n.getVertex()]) { distance[n.getVertex()] = n.getWeight() + distance[current.getVertex()]; pq.add(new ListNode( n.getVertex(), distance[n.getVertex()])); } } } // If you want to calculate distance from source to // a particular target, you can return // distance[target] return distance; } //Methos to return all divisor of a number static ArrayList<Long> allDivisors(long n) { ArrayList<Long> al=new ArrayList<>(); long i=2; while(i*i<=n) { if(n%i==0) al.add(i); if(n%i==0&&i*i!=n) al.add(n/i); i++; } return al; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static int find(boolean[] vis,int x,int y,int n,int m) { if(x<0||y<0||x>=n||y>=m) return 0; // if(vis[i][j]==true) return return 0; } static long power(long N,long R) { long x=998244353; if(R==0) return 1; if(R==1) return N; long temp= power(N,R/2)%x; // (a*b)%p = (a%p*b%p)*p temp=(temp*temp)%x; if(R%2==0){ return temp%x; }else{ return (N*temp)%x; } } static int[] sort(int[] arr) { int n=arr.length; ArrayList<Integer> al=new ArrayList<>(); for(int i=0;i<n;i++) { al.add(arr[i]); } Collections.sort(al); for(int i=0;i<n;i++) { arr[i]=al.get(i); } return arr; } static long[] sort(long[] arr) { long n=arr.length; ArrayList<Long> al=new ArrayList<>(); for(int i=0;i<n;i++) { al.add(arr[i]); } Collections.sort(al); for(int i=0;i<n;i++) { arr[i]=al.get(i); } return arr; } static class Pair{ int val; int res; Pair(int val,int res){ this.val=val; this.res=res; } } public static void main (String[] args) { PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); // out.print(); //out.println(); FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt(); int a=k; String str=sc.nextLine(); int[] arr=new int[n]; StringBuffer ans=new StringBuffer(); if(k%2==0) { for(int i=0;i<n;i++) { if(i==n-1) { arr[i]=k; a -=k; if(a%2==0) { if(str.charAt(i)=='0') { ans.append('0'); }else { ans.append('1'); } }else { if(str.charAt(i)=='0') { ans.append('1'); }else { ans.append('0'); } } k=0; continue; } if(str.charAt(i)=='0'&&k>0) { ans.append('1'); arr[i]++; k--; }else { ans.append(str.charAt(i)); } } }else { for(int i=0;i<n;i++) { if(i==n-1) { arr[i]=k; a -=k; if(a%2==0) { if(str.charAt(i)=='0') { ans.append('0'); }else { ans.append('1'); } }else { if(str.charAt(i)=='0') { ans.append('1'); }else { ans.append('0'); } } k=0; continue; } if(str.charAt(i)=='1'&&k>0) { ans.append('1'); arr[i]++; k--; }else { if(str.charAt(i)=='0') { ans.append('1'); }else { ans.append('0'); } } } } ans.append("\n"); for(int i=0;i<n;i++) { ans.append(arr[i]+" "); } out.println(ans); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
844f34dde313c49255550b16d8295c50
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.util.*; import java.io.*; // BEFORE 31ST MARCH 2022 !! //MAX RATING EVER ACHIEVED-1622(LETS SEE WHEN WILL I GET TO CHANGE THIS) ////*************************************************************************** /* public class E_Gardener_and_Tree implements Runnable{ public static void main(String[] args) throws Exception { new Thread(null, new E_Gardener_and_Tree(), "E_Gardener_and_Tree", 1<<28).start(); } public void run(){ WRITE YOUR CODE HERE!!!! JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!! } } */ /////************************************************************************** public class B_Bit_Flipping{ public static void main(String[] args) { FastScanner s= new FastScanner(); //PrintWriter out=new PrintWriter(System.out); //end of program //out.println(answer); //out.close(); StringBuilder res = new StringBuilder(); int t=s.nextInt(); int p=0; while(p<t){ int n=s.nextInt(); long k=s.nextLong(); String str=s.nextToken(); if(k==0){ res.append(str+" \n"); for(int i=0;i<n;i++){ res.append("0 "); } res.append(" \n"); p++; continue; } long array[]= new long[n]; char array2[]= new char[n]; if(k%2!=0){ //give it to 1 long count=0; for(int i=0;i<n;i++){ if(count>=k){ break; } char ch=str.charAt(i); if(ch=='1'){ array[i]=1; count++; } if(count>=k){ break; } } long rem=k-count; array[n-1]+=rem; for(int i=0;i<n;i++){ long yo=k-array[i]; char ch=str.charAt(i); if(ch=='1'){ if(yo%2==0){ array2[i]='1'; } else{ array2[i]='0'; } } else{ if(yo%2==0){ array2[i]='0'; } else{ array2[i]='1'; } } } } else{ // give it to 0 long count=0; for(int i=0;i<n;i++){ if(count>=k){ break; } char ch=str.charAt(i); if(ch=='0'){ array[i]=1; count++; } if(count>=k){ break; } } long rem=k-count; array[n-1]+=rem; for(int i=0;i<n;i++){ long yo=k-array[i]; char ch=str.charAt(i); if(ch=='1'){ if(yo%2==0){ array2[i]='1'; } else{ array2[i]='0'; } } else{ if(yo%2==0){ array2[i]='0'; } else{ array2[i]='1'; } } } } for(int i=0;i<n;i++){ res.append(array2[i]); } res.append(" \n"); for(int i=0;i<n;i++){ res.append(array[i]+" "); } res.append(" \n"); p++; } System.out.println(res); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } static long modpower(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // SIMPLE POWER FUNCTION=> static long power(long x, long y) { long res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = res * x; // y must be even now y = y >> 1; // y = y/2 x = x * x; // Change x to x^2 } return res; } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
be66a3397b2849417580e6e1703d1a97
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(); int k = sc.ni(); char[] strr = sc.ns().toCharArray(); if(k == 0) { for(int i = 0; i < n; i++) { w.pr(strr[i]+""); } w.pl(); for(int i = 0; i < n; i++) { w.pr(0+" "); } w.pl(); return; } if(k%2==0 && strr[0]=='0' || k%2==1 && strr[0]=='1') { k--; int done = 0; int[] steps = new int[n]; steps[0] = 1; for(int i = 1; i < n; i++) { if(strr[i] == strr[0]) { if(done < k) { done++; steps[i] = 1; strr[i] = '1'; } else { strr[i] = '0'; } } else { strr[i] = '1'; } } strr[0] = '1'; int rem = k-done; steps[n-1] += rem; if(rem%2==1) { strr[n-1] = (strr[n-1]=='1')?'0':'1'; } for(int i = 0; i < n; i++) { w.pr(strr[i]+""); } w.pl(); for(int i = 0; i < n; i++) { w.pr(steps[i]+" "); } w.pl(); } else { int done = 0; int[] steps = new int[n]; for(int i = 1; i < n; i++) { if(strr[i] != strr[0]) { if(done < k) { done++; steps[i] = 1; strr[i] = '1'; } else { strr[i] = '0'; } } else { strr[i] = '1'; } } strr[0] = '1'; int rem = k-done; steps[n-1] += rem; if(rem%2==1) { strr[n-1] = (strr[n-1]=='1')?'0':'1'; } for(int i = 0; i < n; i++) { w.pr(strr[i]+""); } w.pl(); for(int i = 0; i < n; i++) { w.pr(steps[i]+" "); } w.pl(); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
cb973a4e1071d70ebe59a1bf9eeb302a
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=input.nextInt(); while(T-->0) { int n=input.nextInt(); int k=input.nextInt(); int k1=k; String s=input.next(); int arr[]=new int[n]; if(k%2==0) { for(int i=0;i<n;i++) { if(s.charAt(i)=='0' && k>0) { arr[i]=1; k--; } } arr[n-1]+=k; } else { for(int i=0;i<n;i++) { if(s.charAt(i)=='1' && k>0) { arr[i]=1; k--; } } arr[n-1]+=k; } for(int i=0;i<n;i++) { int r=k1-arr[i]; if(r%2==0) { out.print(s.charAt(i)); } else { if(s.charAt(i)=='1') { out.print('0'); } else { out.print('1'); } } } out.println(); for(int i=0;i<n;i++) { out.print(arr[i]+" "); } out.println(); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
568f94f0bbaca534df2340c45838f1e3
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class B { FastScanner in; PrintWriter out; boolean systemIO = true; int mod = 1000000007; public int mult(int x, int y) { return (int) (x * 1L * y % mod); } public int sum(int x, int y) { if (x + y >= mod) { return x + y - mod; } return x + y; } public int diff(int x, int y) { if (x >= y) { return x - y; } return x - y + mod; } public int div(int x, int y) { return mult(x, modInv(y)); } public int[][] mult(int[][] a, int[][] b) { int[][] c = new int[a.length][b[0].length]; for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[0].length; j++) { for (int k = 0; k < b.length; k++) { c[i][j] = sum(c[i][j], mult(a[i][k], b[k][j])); } } } return c; } public int pow(int x, long p) { if (p == 0) { return 1; } int ans = pow(x, p / 2); ans = mult(ans, ans); if ((p & 1) > 0) { ans = mult(ans, x); } return ans; } public int[][] pow(int[][] x, long p) { if (p == 0) { int[][] ans = { { 1, 0 }, { 0, 1 } }; return ans; } int[][] ans = pow(x, p / 2); ans = mult(ans, ans); if ((p & 1) > 0) { ans = mult(ans, x); } return ans; } public int modInv(int x) { return pow(x, mod - 2); } int[] dx = { -1, 0, 1, 0 }; int[] dy = { 0, -1, 0, 1 }; int sz = 0; int[] ans; boolean[][] a; boolean[][] b; int n; boolean[][] used = new boolean[n][n]; public boolean check(int x, int y, int dir) { if (x + dx[dir] < 0 || x + dx[dir] >= n) { return false; } if (y + dy[dir] < 0 || y + dy[dir] >= n) { return false; } boolean flag = true; if (dir == 0) { flag = b[y][x - 1]; b[y][x - 1] = false; } if (dir == 2) { flag = b[y][x]; b[y][x] = false; } if (dir == 1) { flag = a[y - 1][x]; a[y - 1][x] = false; } if (dir == 3) { flag = a[y][x]; a[y][x] = false; } return flag; } public void dfsRec() { Stack<Integer> xst = new Stack<>(); Stack<Integer> yst = new Stack<>(); Stack<Integer> prevst = new Stack<>(); Stack<Integer> dirst = new Stack<>(); Stack<Boolean> toAns = new Stack<>(); xst.add(0); yst.add(0); prevst.add(-1); dirst.add(-1); toAns.add(false); while (!xst.isEmpty()) { int x = xst.pop(); int y = yst.pop(); int prev = prevst.pop(); int dir = dirst.pop(); // System.out.println(x + " " + y + " " + prev + " " + dir + " " + toAns.peek()); if (toAns.pop()) { ans[sz++] = dir ^ 2; } used[x][y] = true; for (dir = dir + 1; dir < 4; dir++) { if (dir == (prev ^ 2)) { continue; } if (check(x, y, dir)) { ans[sz++] = dir; xst.add(x); yst.add(y); prevst.add(prev); dirst.add(dir); toAns.add(true); if (!used[x + dx[dir]][y + dy[dir]]) { xst.add(x + dx[dir]); yst.add(y + dy[dir]); prevst.add(dir); dirst.add(-1); toAns.add(false); } break; } } } } public void dfs(int x, int y, int prev) { System.out.println(x + " " + y); if (used[x][y]) { return; } used[x][y] = true; for (int dir = 0; dir < 4; dir++) { if (dir == (prev ^ 2)) { continue; } if (check(x, y, dir)) { ans[sz++] = dir; dfs(x + dx[dir], y + dy[dir], dir); ans[sz++] = dir ^ 2; } } } public void solve() { for (int qwerty = in.nextInt(); qwerty > 0; --qwerty) { int n = in.nextInt(); int k = in.nextInt(); boolean[] b = new boolean[n]; int[] ans = new int[n]; int left = k; String s = in.next(); for (int i = 0; i < s.length(); i++) { b[i] = s.charAt(i) == '1' ^ (k % 2 == 1); if (!b[i] && left > 0) { left--; ++ans[i]; b[i] = !b[i]; } } if (left > 0) { ans[n - 1] += left; if (left % 2 == 1) { b[n - 1] ^= true; } } for (int i = 0; i < b.length; i++) { if (b[i]) { out.print(1); } else { out.print(0); } } out.println(); for (int i = 0; i < ans.length; i++) { out.print(ans[i] + " "); } out.println(); } } public void add(HashMap<Integer, Integer> map, int x) { if (map.containsKey(x)) { map.put(x, map.get(x) + 1); } else { map.put(x, 1); } } public void run() { try { if (systemIO) { in = new FastScanner(System.in); out = new PrintWriter(System.out); } else { in = new FastScanner(new File("input.txt")); out = new PrintWriter(new File("output.txt")); } solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } 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()); } } // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA public static void main(String[] arg) { new B().run(); } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
899896f5c27ff4f26aa3d860fa8b7b5b
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.lang.*; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.File; import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; public class Main { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair ob) { return (int)(first - ob.first); } } static class Tuple implements Comparable<Tuple> { long first, second,third; public Tuple(long first, long second, long third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int count = 0; int[] parent; int[] rank; public DSU(int n) { count = n; parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public void union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return; if(rank[a] < rank[b]) { parent[a] = b; } else if(rank[a] > rank[b]) { parent[b] = a; } else { parent[b] = a; rank[a] = 1 + rank[a]; } count--; } public int countConnected() { return count; } } static class Reader { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) throws IOException { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] longReadArray(int n) throws IOException { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static long lcm(long a, long b) { return (a / LongGCD(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long powMod(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return powMod(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)(n)] * modInverse(fac[(int)(r)], p) % p * modInverse(fac[(int)(n - r)], p) % p) % p; } public static long fact(long n) { long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (long i = 1; i <= n; i++) fac[(int)(i)] = fac[(int)(i - 1)] * i; return fac[(int)(n)]; } public static long nCr(long n, long k) { long ans = 1; for(long i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } //Modular Operations for Addition and Multiplication. public static long perfomMod(long x) { return ((x%M + M)%M); } public static long addMod(long a, long b) { return perfomMod(perfomMod(a)+perfomMod(b)); } public static long subMod(long a, long b) { return perfomMod(perfomMod(a)-perfomMod(b)); } public static long mulMod(long a, long b) { return perfomMod(perfomMod(a)*perfomMod(b)); } public static boolean isPrime(long n) { if(n == 1) { return false; } //check only for sqrt of the number as the divisors //keep repeating so only half of them are required. So,sqrt. for(int i = 2;i*i<=n;i++) { if(n%i == 0) { return false; } } return true; } public static List<Long> SieveList(int n) { boolean prime[] = new boolean[(int)(n+1)]; Arrays.fill(prime, true); List<Long> l = new ArrayList<>(); for (long p = 2; p*p<=n; p++) { if (prime[(int)(p)] == true) { for(long i = p*p; i<=n; i += p) { prime[(int)(i)] = false; } } } for (long p = 2; p<=n; p++) { if (prime[(int)(p)] == true) { l.add(p); } } return l; } public static int countDivisors(int x) { int c = 0; for(int i = 1;i*i<=x;i++) { if(x%i == 0) { if(x/i != i) { c+=2; } else { c++; } } } return c; } public static long log2(long n) { long ans = (long)(log(n)/log(2)); return ans; } public static boolean isPow2(long n) { return (n != 0 && ((n & (n-1))) == 0); } public static boolean isSq(int x) { long s = (long)Math.round(Math.sqrt(x)); return s*s==x; } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(int a[], int x) { int l=-1, r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void Sort(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 tt = sc.nextInt(); fr: while(tt-- > 0) { int n = sc.nextInt(), k = sc.nextInt(); char[] s = sc.next().toCharArray(); int x = (int)(2e5); long[] no = new long[x], color = new long[x]; for(int i = 0;i<n;i++) no[i] = (s[i]-'0'); int currentBit = k & 1; for(int i = 0;i<n;i++) { if(no[i] == currentBit && k > 0) { no[i] ^= 1; k--; color[i] = 1; } else { color[i] = 0; } } if((k & 1) != 0) no[n-1] ^= 1; color[n-1] += k; for(int i = 0;i<n;i++) fout.print((no[i] ^ currentBit)); fout.println(); for(int i = 0;i<n;i++) fout.print(color[i] + " "); fout.println(); } fout.close(); } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
8313dcf667b15d316bb0975c55f15389
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.math.*; import java.util.*; public class Main { private final BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); private final BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); private StringTokenizer st; private final Long MOD = 1000000007L; private final String endl = "\n"; private final String space = " "; boolean isConsonantUpperCase(char c) { return !isVowelUpperCase(c); } // only for upper case boolean isVowelUpperCase(char c) { c = (c + "").toUpperCase().charAt(0); return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'; } // IO UTILS void log(Object... args) { if (!TESTING) return; for (Object s : args) System.out.print(s.toString() + " "); System.out.println(); } String line() throws IOException { return in.readLine().trim(); } StringTokenizer tokens() throws IOException { return new StringTokenizer(line()); } String nextToken() throws IOException { if (st == null || !st.hasMoreTokens()) st = new StringTokenizer(line()); return st.nextToken(); } long l(String s) { return Long.parseLong(s); } long rl() throws IOException { return l(nextToken()); } long[] rla(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = rl(); return arr; } int i(String s) { return Integer.parseInt(s); } int ri() throws IOException { return i(nextToken()); } int[] ria(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = ri(); return arr; } String rs() throws IOException { return nextToken(); } String[] rsa(int n) throws IOException { String[] s = new String[n]; for (int i = 0; i < n; i++) s[i] = nextToken(); return s; } double d(String s) { return Double.parseDouble(s); } double rd() throws IOException { return d(nextToken()); } double[] rda(int n) throws IOException { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = rd(); } return arr; } void ol(Object... arg) throws IOException { int n = arg.length; for (int i = 0; i < n - 1; i += 1) { os(arg[i].toString()); } if (n > 0) ol(arg[n - 1].toString()); } void ol() throws IOException { out.write(endl); } void os() throws IOException { out.write(space); } void ol(String s) throws IOException { out.write(s + endl); } void os(String s) throws IOException { out.write(s + space); } void ol(int n) throws IOException { out.write(n + endl); } void os(int s) throws IOException { out.write(s + space); } void ol(long s) throws IOException { out.write(s + endl); } void os(long s) throws IOException { out.write(s + space); } void o(String s) throws IOException { out.write(s); } void o(int n) throws IOException { out.write(n + ""); } void o(long s) throws IOException { out.write(s + ""); } // IO UTILS END // MATH UTILS long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } long lcm(long a, long b) { long g = gcd(a, b); return (a / g) * b; // directly multiply a and b can overflow } public long modInv(long a, long MOD) { return new BigInteger(a + "").modPow(new BigInteger((MOD - 2) + ""), new BigInteger(MOD + "")).longValue(); } public long modPro(long a, long b, long MOD) { return ((a % MOD) * (b % MOD)) % MOD; } private long factorialMod(int n, long MOD) { long pro = 1; for (long i = 1; i <= n; i++) { pro = pro * i; pro %= MOD; } return pro; } static void ruffleSort(int[] a) { // ruffle int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n), temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { // ruffle int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n); long temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } boolean[] sieveOfEratosthenes(int n) { boolean[] prime = new boolean[n + 1]; for (int i = 0; i < n; i++) prime[i] = true; prime[0] = false; prime[1] = false; for (int i = 4; i <= n; i += 2) { prime[i] = false; } for (int p = 3; p * p <= n; p += 2) { // If prime[p] is not changed, then it is a prime if (prime[p]) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } // MATH UTILS END private final boolean TESTING = false; public static void main(String[] args) throws Exception { new Main().solve(); } private void solve() throws Exception { int testCases = 1; testCases = Integer.parseInt(in.readLine().trim()); preProcess(); for (int i = 1; i <= testCases; i++) { solveTestCase(i); } out.flush(); out.close(); } void preProcess() { } // make debugging flag based private void solveTestCase(int testCaseNumber) throws Exception { int n = ri(), k = ri(); String s = line(); boolean same = k % 2 == 0; int change[] = new int[n]; for (int i = 0; i < n; i++) { int t = s.charAt(i) - '0'; if (same) { change[i] = t; } else { change[i] = 1-t; } } int ans[] = new int[n]; // ol(Arrays.toString(change)); int ii = 0; while (ii < n && k > 0) { if (change[ii] == 0) { ans[ii] += 1; change[ii] = 1; k -= 1; } ii += 1; } // ol(Arrays.toString(change)); // ol(Arrays.toString(ans)); // ol(k); if (k > 0) { if (k % 2 == 0) { ans[n-1] += k; } else { ans[n-1] += k; change[n-1] = 0; } } for (int i : change) { o(i); } ol(); for (int an : ans) { os(an); } ol(); } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
62372858bf34c65fa86a3833f2231c66
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { static long startTime = System.currentTimeMillis(); // for global initializations and methods starts here // global initialisations and methods end here static void run() { boolean tc = true; //AdityaFastIO r = new AdityaFastIO(); FastReader r = new FastReader(); try (OutputStream out = new BufferedOutputStream(System.out)) { //long startTime = System.currentTimeMillis(); int testcases = tc ? r.ni() : 1; int tcCounter = 1; // Hold Here Sparky------------------->>> // Solution Starts Here start: while (testcases-- > 0) { int n = r.ni(); int k = r.ni(); char[] s = r.word().toCharArray(); int[] arr = new int[n + 1]; int[] dp = new int[n + 1]; for (int i = 1; i < n + 1; i++) { arr[i] = s[i - 1] - '0'; } int check = k & 1; int shift = 1; { int i = 1; while (i <= n) { if (arr[i] == check && k > 0) { arr[i] ^= shift; k--; dp[i] = shift; } i++; } } if ((k & 1) == shift) arr[n] ^= shift; dp[n] += k; for (int i = 1; i < n + 1; i++) { int ele = (int) (arr[i] ^ check); out.write((ele + "").getBytes()); } out.write(("\n").getBytes()); for (int i = 1; i < n + 1; i++) { int ele = dp[i]; out.write((ele + " ").getBytes()); } out.write(("\n").getBytes()); } // Solution Ends Here } catch (IOException e) { e.printStackTrace(); } } static class AdityaFastIO { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public BufferedReader br; public StringTokenizer st; public AdityaFastIO() { br = new BufferedReader(new InputStreamReader(System.in)); din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public AdityaFastIO(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String word() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String line() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public String readLine() throws IOException { byte[] buf = new byte[100000001]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int 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(); } } public static void main(String[] args) throws Exception { run(); } static int[] readIntArr(int n, AdityaFastIO r) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = r.ni(); return arr; } static long[] readLongArr(int n, AdityaFastIO r) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = r.nl(); return arr; } static List<Integer> readIntList(int n, AdityaFastIO r) throws IOException { List<Integer> al = new ArrayList<>(); for (int i = 0; i < n; i++) al.add(r.ni()); return al; } static List<Long> readLongList(int n, AdityaFastIO r) throws IOException { List<Long> al = new ArrayList<>(); for (int i = 0; i < n; i++) al.add(r.nl()); return al; } static long mod = 998244353; static long modInv(long base, long e) { long result = 1; base %= mod; while (e > 0) { if ((e & 1) > 0) result = result * base % mod; base = base * base % mod; e >>= 1; } return result; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String word() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String line() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int ni() { return Integer.parseInt(word()); } long nl() { return Long.parseLong(word()); } double nd() { return Double.parseDouble(word()); } } static int MOD = (int) (1e9 + 7); static long powerLL(long x, long n) { long result = 1; while (n > 0) { if (n % 2 == 1) result = result * x % MOD; n = n / 2; x = x * x % MOD; } return result; } static long powerStrings(int i1, int i2) { String sa = String.valueOf(i1); String sb = String.valueOf(i2); long a = 0, b = 0; for (int i = 0; i < sa.length(); i++) a = (a * 10 + (sa.charAt(i) - '0')) % MOD; for (int i = 0; i < sb.length(); i++) b = (b * 10 + (sb.charAt(i) - '0')) % (MOD - 1); return powerLL(a, b); } static long gcd(long a, long b) { if (a == 0) return b; else return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } static long lower_bound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] >= x) r = m; else l = m; } return r; } static int upper_bound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] <= x) l = m; else r = m; } return l + 1; } static void addEdge(ArrayList<ArrayList<Integer>> graph, int edge1, int edge2) { graph.get(edge1).add(edge2); graph.get(edge2).add(edge1); } public static class Pair implements Comparable<Pair> { int first; int second; public Pair(int first, int second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(Pair o) { // TODO Auto-generated method stub if (this.first != o.first) return (int) (this.first - o.first); else return (int) (this.second - o.second); } } public static class PairC<X, Y> implements Comparable<PairC> { X first; Y second; public PairC(X first, Y second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(PairC o) { // TODO Auto-generated method stub return o.compareTo((PairC) first); } } static boolean isCollectionsSorted(List<Long> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) <= list.get(i - 1)) return false; return true; } static boolean isCollectionsSortedReverseOrder(List<Long> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) >= list.get(i - 1)) return false; return true; } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
46df3ebbf57df75abd5e1933528e710c
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; public class CF1{ public static void main(String[] args) { FastScanner sc=new FastScanner(); int T=sc.nextInt(); // int T=1; for (int tt=1; tt<=T; tt++){ int gl = sc.nextInt(); int k = sc.nextInt(); int temp=k; String s = sc.next(); StringBuilder sb= new StringBuilder(); int ans []= new int [gl]; for (int i=0; i<gl; i++){ int n = gl-i-1; if (s.charAt(i)=='1'){ if (k%2==0){ if (temp>n){ ans[i]=((temp-n)/2)*2; // System.out.println(ans[i]); } sb.append('1'); } else { if (temp>0){ sb.append('1'); if (temp>n){ int y=temp-n; if (y%2==0){ ans[i]=y-1; } else ans[i]=y; } else ans[i]=1; } else sb.append('0'); } } else { if (k%2==1){ if (temp>n){ ans[i]=((temp-n)/2)*2; // System.out.println(ans[i]); } sb.append('1'); } else { if (temp>0){ sb.append('1'); if (temp>n){ int y=temp-n; if (y%2==0){ ans[i]=y-1; } else ans[i]=y; } else ans[i]=1; } else sb.append('0'); } } temp-=ans[i]; if (i==gl-1 && temp>0){ ans[i]++; sb.deleteCharAt(i); sb.append('0'); } } StringBuilder x= new StringBuilder(); for (int i=0; i<gl; i++){ x.append(ans[i]+" "); } System.out.println(sb.toString()); System.out.println(x.toString()); } } static class LPair{ long x,y; LPair(long x , long y){ this.x=x; this.y=y; } } static long prime(long n){ for (long i=3; i*i<=n; i+=2){ if (n%i==0) return i; } return -1; } static long factorial (int x){ if (x==0) return 1; long ans =x; for (int i=x-1; i>=1; i--){ ans*=i; ans%=mod; } return ans; } static long mod =1000000007L; static long power2 (long a, long b){ long res=1; while (b>0){ if ((b&1)== 1){ res= (res * a % mod)%mod; } a=(a%mod * a%mod)%mod; b=b>>1; } return res; } static boolean []sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n+1]; for(int i=0;i<=n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } return prime; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sortLong(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 long gcd (long n, long m){ if (m==0) return n; else return gcd(m, n%m); } static class Pair implements Comparable<Pair>{ int x,y; private static final int hashMultiplier = BigInteger.valueOf(new Random().nextInt(1000) + 100).nextProbablePrime().intValue(); public Pair(int x, int y){ this.x = x; this.y = y; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pii = (Pair) o; if (x != pii.x) return false; return y == pii.y; } public int hashCode() { return hashMultiplier * x + y; } public int compareTo(Pair o){ if (this.x==o.x) return Integer.compare(this.y,o.y); else return Integer.compare(this.x,o.x); } // this.x-o.x is ascending } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
4186d48e3de68219e6dbece060d5061a
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Codechef { public static void main (String[] args) throws java.lang.Exception { try{ FastReader read=new FastReader(); StringBuilder sb = new StringBuilder(); int t=read.nextInt(); while(t>0) { int n = read.nextInt(); int k = read.nextInt(); String s = read.nextLine(); int p = 0; int ans[] = new int[n]; for(int i=0;i<n-1;i++) { if(p%2==0) { if(s.charAt(i)=='1') { if(k<1) { sb.append('1'); ans[i]=0; } else if(k%2!=0) { sb.append('1'); ans[i]=1; p++; k--; } else { sb.append('1'); ans[i]=0; } } else { if(k<1) { sb.append('0'); ans[i]=0; } else if(k%2==0) { sb.append('1'); ans[i]=1; p++; k--; } else { sb.append('1'); ans[i]=0; } } } else { if(s.charAt(i)=='0') { if(k<1) { sb.append('1'); ans[i]=0; } else if(k%2!=0) { sb.append('1'); ans[i]=1; p++; k--; } else { sb.append('1'); ans[i]=0; } } else { if(k<1) { sb.append('0'); ans[i]=0; } else if(k%2==0) { sb.append('1'); ans[i]=1; p++; k--; } else { sb.append('1'); ans[i]=0; } } } } if(p%2==0) { if(s.charAt(n-1)=='1') { if(k<1) { sb.append('1'); ans[n-1]=0; } else { sb.append('1'); ans[n-1]=k; p++; k--; } } else { if(k<1) { sb.append('0'); } else { sb.append('0'); ans[n-1]=k; p++; k--; } } } else { if(s.charAt(n-1)=='0') { if(k<1) { sb.append('1'); ans[n-1]=0; } else { sb.append('1'); ans[n-1]=k; p++; k--; } } else { if(k<1) { sb.append('0'); ans[n-1]=0; } else { sb.append('0'); ans[n-1]=k; p++; k--; } } } sb.append('\n'); for(int i=0;i<n;i++) { sb.append(ans[i]+" "); } sb.append('\n'); t--; } System.out.println(sb); } catch(Exception e) {return; } } 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 sort(int[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(ArrayList<Long> A,char ch) { int n = A.size(); Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = A.get(i); int randomPos = i + rnd.nextInt(n - i); A.set(i,A.get(randomPos)); A.set(randomPos,tmp); } Collections.sort(A); } static void sort(ArrayList<Integer> A) { int n = A.size(); Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = A.get(i); int randomPos = i + rnd.nextInt(n - i); A.set(i,A.get(randomPos)); A.set(randomPos,tmp); } Collections.sort(A); } static String sort(String s) { Character ch[] = new Character[s.length()]; for (int i = 0; i < s.length(); i++) { ch[i] = s.charAt(i); } Arrays.sort(ch); StringBuffer st = new StringBuffer(""); for (int i = 0; i < s.length(); i++) { st.append(ch[i]); } return st.toString(); } } class pair implements Comparable<pair> { int X,Y,C; pair(int s,int e,int c) { X=s; Y=e; C=c; } public int compareTo(pair p ) { return this.C-p.C; } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
cd2b6f04ed7ca0385c8b1429c4f05923
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; /** * @author Naitik * */ public class Main { static FastReader sc=new FastReader(); static long dp[][]; // static boolean v[][][]; static int mod=998244353;; // static int mod=1000000007; static int max; static int bit[]; //static long fact[]; static HashMap<Long,Integer> map; //static StringBuffer sb=new StringBuffer(""); //static HashMap<Integer,Integer> map; static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) { // StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); outer :while (ttt-- > 0) { int n=i(); int k=i(); char A[]=s().toCharArray(); ArrayList<Integer> l=new ArrayList<Integer>(); int f=0; int j=0; for(int i=0;i<n && k>0;i++) { if(f==1) { if(A[i]=='1') A[i]='0'; else A[i]='1'; } if(i==n-1) { j++; continue; } if(A[i]=='1') { if(k%2!=0) { l.add(1); k--; f^=1; } else { l.add(0); } } else { if(k%2==0) { l.add(1); k--; f^=1; A[i]='1'; } else { l.add(0); A[i]='1'; } } j++; } if(f==1) { for(int i=j;i<n;i++) { if(A[i]=='1') A[i]='0'; else A[i]='1'; } } while(l.size()<n) { l.add(0); } int y=l.get(l.size()-1); y+=k; l.remove(l.size()-1); l.add(y); for(char ch : A) { out.print(ch); } out.println(); for(int i : l) { out.print(i+" "); } out.println(); } //System.out.println(sb.toString()); out.close(); //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 } static class Pair implements Comparable<Pair> { int x; int y; // int z; Pair(int x,int y){ this.x=x; this.y=y; // this.z=z; } @Override public int compareTo(Pair o) { if(this.x>o.x) return 1; else if(this.x<o.x) return -1; else { if(this.y>o.y) return 1; else if(this.y<o.y) return -1; else return 0; } } public int hashCode() { final int temp = 14; int ans = 1; ans =x*31+y*13; return ans; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null) { return false; } if (this.getClass() != o.getClass()) { return false; } Pair other = (Pair)o; if (this.x != other.x || this.y!=other.y) { return false; } return true; } // /* FOR TREE MAP PAIR USE */ // public int compareTo(Pair o) { // if (x > o.x) { // return 1; // } // if (x < o.x) { // return -1; // } // if (y > o.y) { // return 1; // } // if (y < o.y) { // return -1; // } // return 0; // } } static int find(int A[],int a) { if(A[a]==a) return a; return A[a]=find(A, A[a]); } //static int find(int A[],int a) { // if(A[a]==a) // return a; // return find(A, A[a]); //} //FENWICK TREE static void update(int i, int x){ for(; i < bit.length; i += (i&-i)) bit[i] += x; } static int sum(int i){ int ans = 0; for(; i > 0; i -= (i&-i)) ans += bit[i]; return ans; } //END static void add(long v) { if(!map.containsKey(v)) { map.put(v, 1); } else { map.put(v, map.get(v)+1); } } static void remove(long v) { if(map.containsKey(v)) { map.put(v, map.get(v)-1); if(map.get(v)==0) map.remove(v); } } public static int upper(int A[],int k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { ans=mid; l=mid+1; } else { u=mid-1; } } return ans; } public static int lower(int A[],int k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { l=mid+1; } else { ans=mid; u=mid-1; } } return ans; } static int[] copy(int A[]) { int B[]=new int[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long[] copy(long A[]) { long B[]=new long[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void reverse(long A[]) { int n=A.length; long B[]=new long[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void reverse(int A[]) { int n=A.length; int B[]=new int[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static int[][] input(int n,int m){ int A[][]=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=i(); } } return A; } static char[][] charinput(int n,int m){ char A[][]=new char[n][m]; for(int i=0;i<n;i++) { String s=s(); for(int j=0;j<m;j++) { A[i][j]=s.charAt(j); } } return A; } static int nextPowerOf2(int n) { if(n==0) return 1; n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } static int highestPowerof2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static long highestPowerof2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long [] prefix(long A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] prefix(int A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] suffix(long A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static long [] suffix(int A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static void fill(int dp[]) { Arrays.fill(dp, -1); } static void fill(int dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(int dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(int dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static void fill(long dp[]) { Arrays.fill(dp, -1); } static void fill(long dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(long dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(long dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long power(long x, long y, long p) { if(y==0) return 1; if(x==0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } static void print(int A[]) { for(int i : A) { out.print(i+" "); } out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static HashMap<Integer,Integer> hash(int A[]){ HashMap<Integer,Integer> map=new HashMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static HashMap<Long,Integer> hash(long A[]){ HashMap<Long,Integer> map=new HashMap<Long, Integer>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Integer,Integer> tree(int A[]){ TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Long,Integer> tree(long A[]){ TreeMap<Long,Integer> map=new TreeMap<Long, Integer>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
1e01b7495da723afa5ed678ffd6ca6c1
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.*; import java.util.*; public class B { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader s = new FastReader(); static PrintWriter out = new PrintWriter(System.out); private static int[] rai(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); } return arr; } private static int[][] rai(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextInt(); } } return arr; } private static long[] ral(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } return arr; } private static long[][] ral(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextLong(); } } return arr; } private static int ri() { return s.nextInt(); } private static long rl() { return s.nextLong(); } private static String rs() { return s.next(); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static long gcd(long a,long b) { if(b==0) { return a; } return gcd(b,a%b); } static boolean isPrime(int n) { //check if n is a multiple of 2 if(n==1) { return false; } if(n==2) { return true; } 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 boolean[] sieveOfEratosthenes(int n) { // 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. boolean prime[] = new boolean[n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { // If prime[p] is not changed, then it is a prime if(prime[p] == true) { // Update all multiples of p for(int i = p*p; i <= n; i += p) prime[i] = false; } } return prime; } static int MOD = 998244353; static boolean calc(long a, long b, long c) { long diff = c-b; long req = b-diff; if(req >= a && req%a==0) { return true; } diff = b-a; req = b+diff; if(req >= c && req%c == 0) { return true; } diff = (c-a); if(Math.abs(diff)%2 ==1) { return false; } diff /=2; req = c-diff; return req >= b && req % b == 0; } public static void main(String[] args) { StringBuilder ans = new StringBuilder(); int t = ri(); // int t=1; while (t-- > 0) { int n=ri(); int k=ri(); char[] arr=rs().toCharArray(); int[] count = new int[n]; int used = 0; for(int i=0;i<n && used<k;i++) { if(k%2==1) { if(arr[i]=='1') { count[i] = 1; used++; } } else { if(arr[i]=='0') { count[i] = 1; used++; } } } int val =k - used; if(val%2==0) { count[0]+=val; } else { count[0]+=(val-1); count[n-1]+=1; } for(int i=0;i<n;i++) { int changed = k-count[i]; if(changed%2==1) { if(arr[i]=='0') { arr[i]='1'; } else { arr[i]='0'; } } ans.append(arr[i]); } ans.append("\n"); for(int i:count) { ans.append(i).append(" "); } ans.append("\n"); } out.print(ans.toString()); out.flush(); } /* abdeba abedba */ }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
257464eb157f5dc1e08984ab8cb43d8e
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; public final class B { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); // static int g[][]; static ArrayList<Integer> 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 fact[]; static int size[],N; static long dp[][],sum[][],f[]; static int seg[]; public static void main(String args[])throws IOException { /* * star,rope,TPST * BS,LST,MS,MQ */ int T=i(); outer:while(T-->0) { int N=i(),K=i(); char X[]=in.next().toCharArray(); int f[]=new int[N]; if(K%2==0) { int c=0; for(int i=0; i<N; i++) { if(K==c) { break; } if(X[i]=='0') { c++; f[i]=1; } } f[N-1]+=(K-c); for(int i=0; i<N; i++) { if(f[i]==0)ans.append(X[i]); else { int x=X[i]-'0'; x+=(f[i]%2); x%=2; ans.append(x); } } ans.append("\n"); } else { int c=0; for(int i=0; i<N; i++) { if(K==c) { break; } if(X[i]=='1') { c++; f[i]=1; } } f[N-1]+=(K-c); for(int i=0; i<N; i++) { int x=X[i]-'0'; x+=1; x%=2; if(f[i]%2==0)ans.append(x); //flip it else { x+=1; x%=2; ans.append(x); } } ans.append("\n"); } for(int a:f)ans.append(a+" ");ans.append("\n"); } out.print(ans); out.close(); } static void dfs(int n,int p,long A[],HashMap<String,Integer> mp) { for(int c:g[n]) { if(c!=p) { long x=mp.get(n+" "+c); long y=mp.get(c+" "+n); if(A[n]*y!=A[c]*x) { } dfs(c,n,A,mp); } } } static int count(long N) { int cnt=0; long p=1L; while(p<=N) { if((p&N)!=0)cnt++; p<<=1; } return cnt; } static long kadane(long A[]) { long lsum=A[0],gsum=0; gsum=Math.max(gsum, lsum); for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } public static boolean pal(int i) { StringBuilder sb=new StringBuilder(); StringBuilder rev=new StringBuilder(); int p=1; while(p<=i) { if((i&p)!=0) { sb.append("1"); } else sb.append("0"); p<<=1; } rev=new StringBuilder(sb.toString()); rev.reverse(); if(i==8)System.out.println(sb+" "+rev); return (sb.toString()).equals(rev.toString()); } public static void reverse(int i,int j,int A[]) { while(i<j) { int t=A[i]; A[i]=A[j]; A[j]=t; i++; j--; } } public static int ask(int a,int b,int c) { System.out.println("? "+a+" "+b+" "+c); return i(); } static int[] reverse(int A[],int N) { int B[]=new int[N]; for(int i=N-1; i>=0; i--) { B[N-i-1]=A[i]; } return B; } static boolean isPalin(char X[]) { int i=0,j=X.length-1; while(i<=j) { if(X[i]!=X[j])return false; i++; j--; } return true; } 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[] 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 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]=seg[v*2]+seg[v*2+1]; } static void update(int v,int tl,int tr,int index) { if(index==tl && index==tr) { seg[v]--; } else { int tm=(tl+tr)/2; if(index<=tm)update(v*2,tl,tm,index); else update(v*2+1,tm+1,tr,index); seg[v]=seg[v*2]+seg[v*2+1]; } } static int ask(int v,int tl,int tr,int l,int r) { if(l>r)return 0; if(tl==l && r==tr) { return seg[v]; } int tm=(tl+tr)/2; return ask(v*2,tl,tm,l,Math.min(tm, r))+ask(v*2+1,tm+1,tr,Math.max(tm+1, l),r); } 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,int nodes) // { //// size=new int[N+1]; // par=new int[N+1]; // col=new int[N+1]; //// g=new int[N+1][]; // D=new int[N+1]; // int deg[]=new int[N+1]; // int A[][]=new int[nodes][2]; // for(int i=0; i<nodes; i++) // { // int a=i(),b=i(); // A[i][0]=a; // A[i][1]=b; // deg[a]++; // deg[b]++; // } // for(int i=0; i<=N; i++) // { // g[i]=new int[deg[i]]; // deg[i]=0; // } // for(int a[]:A) // { // int x=a[0],y=a[1]; // g[x][deg[x]++]=y; // g[y][deg[y]++]=x; // } // } 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 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%2L == 0 || N%3L == 0) return false; for (long i=5; i*i<=N; i+=2) if (N%i==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 ); } } //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
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
c7cbee4c339cd40e651aa9fc767808c1
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
//make sure to make new file! import java.io.*; import java.util.*; public class B782{ public static void main(String[] args)throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(f.readLine()); for(int q = 1; q <= t; q++){ StringTokenizer st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); char[] input = f.readLine().toCharArray(); int[] array = new int[n]; for(int k = 0; k < n; k++){ array[k] = Character.getNumericValue(input[k]); } int[] answer = new int[n]; int[] result = new int[n]; int added = 0; if(m%2 == 0){ //maximize for(int k = 0; k < n; k++){ result[k] = array[k]; if(added < m){ if(array[k] == 0){ answer[k]++; added++; result[k] = 1; } } } } else { //minimize but add 1-array to result for(int k = 0; k < n; k++){ result[k] = 1-array[k]; if(added < m){ if(array[k] == 1){ answer[k]++; added++; result[k] = 1; } } } } if((m-added)%2 == 1){ result[n-1] = 1-result[n-1]; } answer[n-1] += (m-added); StringJoiner sj = new StringJoiner(""); StringJoiner sj2 = new StringJoiner(" "); for(int k = 0; k < n; k++){ sj.add("" + result[k]); sj2.add("" + answer[k]); } out.println(sj.toString()); out.println(sj2.toString()); } out.close(); } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
22f7d81ea4116d0a10ac735b8f9e7c8b
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.util.*; import java.io.*; public class BitFlipping { public static void main(String[] args) throws IOException { Reader in = new Reader(); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for (int t = 0; t < T; t++) { int N = in.nextInt(), K = in.nextInt(); String s = in.next(); boolean[] arr = new boolean[N]; for (int i = 0; i < N; i++) { arr[i] = s.charAt(i) == '1'; } if (K % 2 == 1) { for (int i = 0; i < N; i++) { arr[i] = !arr[i]; } } int[] res = new int[N]; for (int i = 0; i < N; i++) { if (!arr[i] && K > 0) { arr[i] = true; res[i]++; K--; } } res[N - 1] += K; if (K > 0 && K % 2 == 1) { arr[N - 1] = !arr[N - 1]; } StringBuilder sb = new StringBuilder(""); for (boolean b : arr) { sb.append(b ? "1" : "0"); } out.println(sb.toString()); for (int i = 0; i < N; i++) { out.print(res[i] + (i == N - 1 ? "\n" : " ")); } } out.close(); } static class Reader { BufferedReader in; StringTokenizer st; public Reader() { in = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } public String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } public String next() throws IOException { while (!st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } } public static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i : arr) { list.add(i); } Collections.sort(list); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
6345aefe1cc1d38f069468ec8bc8f457
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Random; import java.util.StringTokenizer; /* 1 6 3 100001 */ public class B { public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { int n=fs.nextInt(); int k=fs.nextInt(); char[] a=fs.next().toCharArray(); if (k%2==1) { for (int i=0; i<n; i++) { if (a[i]=='0') a[i]='1'; else a[i]='0'; } } int[] choice=new int[n]; for (int i=0; i<a.length; i++) { if (a[i]=='0' && k>0) { a[i]='1'; k--; choice[i]++; } } choice[n-1]+=k; if (k%2==1) { a[n-1]='0'; } out.println(a); for (int i=0; i<n; i++) { out.print(choice[i]+" "); } out.println(); } out.close(); } static final Random random=new Random(); static final int mod=1_000_000_007; 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 long add(long a, long b) { return (a+b)%mod; } static long sub(long a, long b) { return ((a-b)%mod+mod)%mod; } static long mul(long a, long b) { return (a*b)%mod; } static long exp(long base, long exp) { if (exp==0) return 1; long half=exp(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials=new long[2_000_001]; static long[] invFactorials=new long[2_000_001]; static void precompFacts() { factorials[0]=invFactorials[0]=1; for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i); invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2); for (int i=invFactorials.length-2; i>=0; i--) invFactorials[i]=mul(invFactorials[i+1], i+1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k])); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
37cdb416b011c14c0a7543180cecbf7f
train_110.jsonl
1650206100
You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class B { public static void main(String[] args) throws NumberFormatException, IOException { MyReader reader = new MyReader(); int t = reader.nextInt(); for(int testcase=0; testcase<t; testcase++) { int n = reader.nextInt(); int k = reader.nextInt(); String s = reader.nextLine(); int[] arr = new int[n]; for(int i=0; i<n; i++) { if(s.charAt(i) == '1') arr[i] = 1; else arr[i] = 0; } if(k % 2 == 1) { for(int i=0; i<n; i++) { arr[i]++; arr[i] %= 2; } } int count0 = 0; int[] f = new int[n]; for(int i=0; i<n-1; i++) { if(arr[i] == 0) count0++; } if(k>count0) { //make everything other than last 1 for(int i=0; i<n-1; i++) { if(arr[i] == 0) { arr[i] = 1; f[i]++; } } k -= count0; f[n-1] = k; if(k%2 == 1) { arr[n-1]++; arr[n-1] %= 2; } } else { for(int i=0; i<n; i++) { if(k == 0) break; if(arr[i] == 0) { arr[i] = 1; f[i]++; k--; } } } StringBuilder sb1 = new StringBuilder(); for(int i=0; i<n; i++) { sb1.append(arr[i]); } StringBuilder sb2 = new StringBuilder(); for(int i=0; i<n; i++) { sb2.append(f[i] + " "); } System.out.println(sb1.toString()); System.out.println(sb2.toString()); } } static class MyReader { BufferedReader br; StringTokenizer st; public MyReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["6\n\n6 3\n\n100001\n\n6 4\n\n100011\n\n6 0\n\n000000\n\n6 1\n\n111001\n\n6 11\n\n101100\n\n6 12\n\n001110"]
1 second
["111110\n1 0 0 2 0 0 \n111110\n0 1 1 1 0 1 \n000000\n0 0 0 0 0 0 \n100110\n1 0 0 0 0 0 \n111111\n1 2 1 3 0 4 \n111110\n1 1 4 2 0 4"]
NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$. Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$. Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$. The final string is $$$111110$$$ and this is the lexicographically largest string we can get.
Java 8
standard input
[ "bitmasks", "constructive algorithms", "greedy", "strings" ]
38375efafa4861f3443126f20cacf3ae
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,300
For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.
standard output
PASSED
006fd57fb26d328ea9e05f9fd002ffa2
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.Scanner; public class codeforces { public static void main(String[] args) { Scanner scan = new Scanner(System.in); long t = scan.nextLong(); for (long i = 0; i < t; i++) { long games = scan.nextLong(); long r = scan.nextLong(); long b = scan.nextLong(); System.out.println(compute(games, r, b)); } } public static String compute(long games, long r, long b) { long rest = r%(b+1); long ratio = r/(b+1); String sol = ""; long j = 0; while(j <= b) { for (int i = 0; i < ratio; i++) { sol += "R"; } if (j < rest) sol += "R"; if (j < b) sol = sol + "B"; j++; } return sol; } public static long findMax(long games, long r, long b) { long i = 1; while(true) { if ((b+1)*i + b >= games) return i; i++; } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
f83f9996276c444491388bdcc073d707
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.lang.*; import java.util.*; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner a=new Scanner(System.in); int y=a.nextInt(); while (y>0){ int xx=a.nextInt(); int m=a.nextInt(); int n=a.nextInt(); if (n==1){ for (int i=0;i<m/2;i++){ System.out.print("R"); } System.out.print("B"); for (int i=m/2;i<m;i++){ System.out.print("R"); } }else{ int x=m%n; int d=m/n; int u=d; int hh=-1; if (d<x){ u=d+1; int zz=m-(n*d); int zz1=n-zz; for (int i=0;i<zz;i++){ for (int j=0;j<u;j++){ System.out.print("R"); } System.out.print("B"); } for (int i=0;i<zz1;i++){ for (int j=0;j<d;j++){ System.out.print("R"); } System.out.print("B"); } } else{ while (m-u*n<u){ u--; if (m-u*n<=u){ }else{ u++; break; } } for (int i=0;i<n;i++){ for (int j=0;j<u;j++){ System.out.print("R"); } System.out.print("B"); } for (int i=0;i<(m-n*u);i++){ System.out.print("R"); } } } System.out.println(); y--; } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
a2c5ec3e2ef04f3e41bca968049a32b7
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.*; import java.util.*; public class ProblemB { static int MOD = (int)1e9+7; static int MAX = (int)1e6+1; static LinkedHashSet<Integer>temp; public static void main(String[] args) throws Exception { Scanner in = new Scanner(); StringBuilder sb = new StringBuilder(); int test = in.readInt(); while(test-->0){ int n = in.readInt(); int r = in.readInt(); int b = in.readInt(); char res[] = new char[n+1]; for(int i = 2;i<=100;i++){ int mx = i,tb= b; Arrays.fill(res, 'R'); for(int j =i;j<=n;j+=i){ if(tb>0){res[j] = 'B';tb--;mx =n-j;} } if(mx<=i){ for(int j =1;j<=n;j++){ if(tb>0&& j+1<n && res[j] == 'R' && res[j+1] == res[j]){ res[j] = 'B';tb--; } } break; } } for(int i = 1;i<=n;i++)sb.append(res[i]); sb.append("\n"); } System.out.println(sb); } public static ArrayList<Integer> subtract(int a[],int b[]){ int n = a.length-1; ArrayList<Integer>l = new ArrayList<>(); boolean carry = false; for(int i =n;i>0;i--){ if(carry)a[i]--; if(a[i]>=b[i-1]){ l.add(a[i]-b[i-1]);carry = false; } else { a[i]+=10; l.add(a[i]-b[i-1]); carry = true; } } Collections.reverse(l); return l; } public static int[] fill(String s){ int a[] = new int[s.length()]; for(int i = 0;i<s.length();i++)a[i] = s.charAt(i)-'0'; return a; } public static long factorial(int n){ long fac = 1; for(int i = 1;i<=n;i++)fac*=i; return fac; } public static ArrayList<Integer> factors(int n){ ArrayList<Integer>l = new ArrayList<>(); for(int i = 1;i*i<=n;i++){ if(n%i == 0){ l.add(i); if(n/i != i)l.add(n/i); } } return l; } public static void build(int seg[],int ind,int l,int r,int a[]){ if(l == r){ seg[ind] = a[l]; return; } int mid = (l+r)/2; build(seg,(2*ind)+1,l,mid,a); build(seg,(2*ind)+2,mid+1,r,a); seg[ind] = Math.min(seg[(2*ind)+1],seg[(2*ind)+2]); } public static long gcd(long a, long b){ return b ==0 ?a:gcd(b,a%b); } public static long nearest(TreeSet<Long> list,long target){ long nearest = -1,sofar = Integer.MAX_VALUE; for(long it:list){ if(Math.abs(it - target)<sofar){ sofar = Math.abs(it - target); nearest = it; } } return nearest; } public static ArrayList<Long> factors(long n){ ArrayList<Long>l = new ArrayList<>(); for(long i = 1;i*i<=n;i++){ if(n%i == 0){ l.add(i); if(n/i != i)l.add(n/i); } } Collections.sort(l); return l; } public static void swap(char a[],int i, int j){ char t = a[i]; a[i] = a[j]; a[j] = t; } public static boolean isSorted(long a[]){ for(int i =0;i<a.length;i++){ if(i+1<a.length && a[i]>a[i+1])return false; } return true; } public static int first(ArrayList<Long>list,long target){ int index = -1; int l = 0,r = list.size()-1; while(l<=r){ int mid = (l+r)/2; if(list.get(mid) == target){ index = mid; r = mid-1; }else if(list.get(mid)>target){ r = mid-1; }else l = mid+1; } return index; } public static int last(ArrayList<Long>list,long target){ int index = -1; int l = 0,r = list.size()-1; while(l<=r){ int mid = (l+r)/2; if(list.get(mid) == target){ index= mid; l = mid+1; }else if(list.get(mid)<target){ l = mid+1; }else r = mid-1; } return index; } public static void sort(int arr[]){ ArrayList<Integer>list = new ArrayList<>(); for(int it:arr)list.add(it); Collections.sort(list); for(int i = 0;i<arr.length;i++)arr[i] = list.get(i); } static class Pair{ int x,y,z; public Pair(int x,int y){ this.x = x; this.y = y; } } static class Scanner{ BufferedReader br; StringTokenizer st; public Scanner(){ br = new BufferedReader(new InputStreamReader(System.in)); } public String read() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int readInt() { return Integer.parseInt(read()); } public long readLong() { return Long.parseLong(read()); } public double readDouble(){return Double.parseDouble(read());} public String readLine() throws Exception { return br.readLine(); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
9ca1da8e428d273c48d364ad49d30af4
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.*; import java.util.*; public class ProblemB { static int MOD = (int)1e9+7; static int MAX = (int)1e6+1; static LinkedHashSet<Integer>temp; public static void main(String[] args) throws Exception { Scanner in = new Scanner(); StringBuilder sb = new StringBuilder(); int test = in.readInt(); while(test-->0){ int n = in.readInt(); int r = in.readInt(); int b = in.readInt(); char res[] = new char[n+1]; for(int i = 2;i<=100;i++){ int mx = i,tb= b; Arrays.fill(res, 'R'); for(int j =i;j<=n;j+=i){ if(tb>0){res[j] = 'B';tb--;mx =Math.max(i-1, n-j);} } if(mx<=i){ for(int j =1;j<=n;j++){ if(tb>0&& j+1<n && res[j] == 'R' && res[j+1] == res[j]){ res[j] = 'B';tb--; } } break; } } for(int i = 1;i<=n;i++)sb.append(res[i]); sb.append("\n"); } System.out.println(sb); } public static ArrayList<Integer> subtract(int a[],int b[]){ int n = a.length-1; ArrayList<Integer>l = new ArrayList<>(); boolean carry = false; for(int i =n;i>0;i--){ if(carry)a[i]--; if(a[i]>=b[i-1]){ l.add(a[i]-b[i-1]);carry = false; } else { a[i]+=10; l.add(a[i]-b[i-1]); carry = true; } } Collections.reverse(l); return l; } public static int[] fill(String s){ int a[] = new int[s.length()]; for(int i = 0;i<s.length();i++)a[i] = s.charAt(i)-'0'; return a; } public static long factorial(int n){ long fac = 1; for(int i = 1;i<=n;i++)fac*=i; return fac; } public static ArrayList<Integer> factors(int n){ ArrayList<Integer>l = new ArrayList<>(); for(int i = 1;i*i<=n;i++){ if(n%i == 0){ l.add(i); if(n/i != i)l.add(n/i); } } return l; } public static void build(int seg[],int ind,int l,int r,int a[]){ if(l == r){ seg[ind] = a[l]; return; } int mid = (l+r)/2; build(seg,(2*ind)+1,l,mid,a); build(seg,(2*ind)+2,mid+1,r,a); seg[ind] = Math.min(seg[(2*ind)+1],seg[(2*ind)+2]); } public static long gcd(long a, long b){ return b ==0 ?a:gcd(b,a%b); } public static long nearest(TreeSet<Long> list,long target){ long nearest = -1,sofar = Integer.MAX_VALUE; for(long it:list){ if(Math.abs(it - target)<sofar){ sofar = Math.abs(it - target); nearest = it; } } return nearest; } public static ArrayList<Long> factors(long n){ ArrayList<Long>l = new ArrayList<>(); for(long i = 1;i*i<=n;i++){ if(n%i == 0){ l.add(i); if(n/i != i)l.add(n/i); } } Collections.sort(l); return l; } public static void swap(char a[],int i, int j){ char t = a[i]; a[i] = a[j]; a[j] = t; } public static boolean isSorted(long a[]){ for(int i =0;i<a.length;i++){ if(i+1<a.length && a[i]>a[i+1])return false; } return true; } public static int first(ArrayList<Long>list,long target){ int index = -1; int l = 0,r = list.size()-1; while(l<=r){ int mid = (l+r)/2; if(list.get(mid) == target){ index = mid; r = mid-1; }else if(list.get(mid)>target){ r = mid-1; }else l = mid+1; } return index; } public static int last(ArrayList<Long>list,long target){ int index = -1; int l = 0,r = list.size()-1; while(l<=r){ int mid = (l+r)/2; if(list.get(mid) == target){ index= mid; l = mid+1; }else if(list.get(mid)<target){ l = mid+1; }else r = mid-1; } return index; } public static void sort(int arr[]){ ArrayList<Integer>list = new ArrayList<>(); for(int it:arr)list.add(it); Collections.sort(list); for(int i = 0;i<arr.length;i++)arr[i] = list.get(i); } static class Pair{ int x,y,z; public Pair(int x,int y){ this.x = x; this.y = y; } } static class Scanner{ BufferedReader br; StringTokenizer st; public Scanner(){ br = new BufferedReader(new InputStreamReader(System.in)); } public String read() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int readInt() { return Integer.parseInt(read()); } public long readLong() { return Long.parseLong(read()); } public double readDouble(){return Double.parseDouble(read());} public String readLine() throws Exception { return br.readLine(); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
9a88e981b80ca655aaa9175a4351f08c
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import javax.sound.midi.Soundbank; import java.awt.*; import java.io.*; import java.util.*; import java.util.zip.InflaterInputStream; public class Main { static int N = 1001; // Array to store inverse of 1 to N static long[] factorialNumInverse = new long[N + 1]; // Array to precompute inverse of 1! to N! static long[] naturalNumInverse = new long[N + 1]; // Array to store factorial of first N numbers static long[] fact = new long[N + 1]; // Function to precompute inverse of numbers public static void InverseofNumber(int p) { naturalNumInverse[0] = naturalNumInverse[1] = 1; for(int i = 2; i <= N; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p; } public static void InverseofFactorial(int p) { factorialNumInverse[0] = factorialNumInverse[1] = 1; // Precompute inverse of natural numbers for(int i = 2; i <= N; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to N public static void factorial(int p) { fact[0] = 1; // Precompute factorials for(int i = 1; i <= N; i++) { fact[i] = (fact[i - 1] * (long)i) % p; } } public static long ncp(int N, int R, int p) { // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p; return ans; } public static void main(String[] args) throws IOException { Reader.init(System.in); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Reader.nextInt(); while (t > 0){ int n = Reader.nextInt(); int r = Reader.nextInt(); int b = Reader.nextInt(); int l = (r)%(b+1); int v = r/(b+1); while ( r > 0){ int i = 0; while ( i < v ){ output.write("R"); i++; r--; } if ( l > 0){ output.write("R"); l--; r--; } if ( b > 0){ output.write("B"); b--; } } output.write("\n"); t--; } output.flush(); } public static boolean help(int m,int r,int b){ boolean ans = true; while ( r > 0 ){ if ( b == 0 && r > m){ ans =false; break; } else{ r-=(int)min(m,r); b--; } } return ans; } public static void primeFactorisation(long n,HashMap<Long,Integer> map){ long i = 2; while ( n%i == 0){ if ( map.containsKey(i)){ map.put(i,map.get(i)+1); } else{ map.put(i,1); } n/=i; } i = 3; long last = (long)Math.pow(n,0.5); while ( i <= last){ while ( n%i == 0){ if ( map.containsKey(i)){ map.put(i,map.get(i)+1); } else{ map.put(i,1); } n/=i; } i++; } if ( n > 2){ map.put(n,1); } } public static boolean prime(int n){ int i = 2; int r = (int)Math.pow(n,0.5); boolean ans = true; while ( i<=r){ if ( n%i == 0){ ans = false; break; } i++; } return ans; } public static long log2(long N) { long result = (long)(Math.log(N) / Math.log(2)); return result; } private static int bs(int low,int high,int [] array,long find){ if ( low <= high ){ int mid = low + (high-low)/2; if ( array[mid] > find){ high = mid -1; return bs(low,high,array,find); } else if ( array[mid] < find){ low = mid+1; return bs(low,high,array,find); } return mid; } return -1; } private static long max(long a, long b) { return Math.max(a,b); } private static long min(long a,long b){ return Math.min(a,b); } public static long modularExponentiation(long a,long b,long mod){ if ( b == 1){ return a; } else{ long ans = modularExponentiation(a,b/2,mod)%mod; if ( b%2 == 1){ return (a*((ans*ans)%mod))%mod; } return ((ans*ans)%mod); } } public static long sum(long n){ return (n*(n+1))/2; } public static long abs(long a){ return a < 0 ? (-1*a) : a; } public static long gcd(long a,long b){ if ( a == 0){ return b; } else{ return gcd(b%a,a); } } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } static long nextLong() throws IOException{ return Long.parseLong(next()); } } class NComparator implements Comparator<Node>{ @Override public int compare(Node o1, Node o2) { if ( o2.infected == 1 && o1.infected == 0){ return -1; } else if ( o2.infected == 0 && o1.infected == 1){ return 1; } else{ if ( o2.value > o1.value){ return 1; } else if ( o2.value < o1.value){ return -1; } else{ return 0; } } } } class DComparator implements Comparator<Integer>{ @Override public int compare(Integer o1, Integer o2) { if ( o2 > o1){ return 1; } else if ( o2 < o1){ return -1; } else{ return 0; } } } class Node{ int value; int infected; Node(int A,int B){ value= A; infected = B; } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
ca01fad21d0a0d2778ff4c133f74a0d1
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class A { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(reader.readLine()); while (t > 0) { t--; StringTokenizer st = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); int segments = b + 1; for (int i = 0; i < segments; i++) { int segmentSize = r / (b + 1); sb.append("R".repeat(segmentSize)); r -= segmentSize; if (b > 0) { sb.append("B"); b--; } } sb.append("R".repeat(r)); out.println(sb); } out.close(); } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
a4970b687f0445dfa11b97128af52a02
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.*; import java.util.Objects; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { FastReader in = new FastReader(); //FastestNumberReader in = new FastestNumberReader(); OutputWriter out = new OutputWriter(); Solver solver = new Solver(); int testCount = 1; testCount = in.nextInt(); for (int i = 1; i <= testCount; i++) { try { solver.solve(i, in, out); } catch (IOException e) { e.printStackTrace(); } } out.close(); } } static class Solver { private void solve(int testNumber, FastReader reader, OutputWriter writer) throws IOException { int n = reader.nextInt(); int r = reader.nextInt(); int parts = reader.nextInt() + 1; StringBuilder ans = new StringBuilder(); int minLength = r / parts; int longerPartsAmount = r % parts; int i = 0; int sequenceLength = 0; int sequenceCnt = 0; for (; sequenceCnt < longerPartsAmount; i++) { if (sequenceLength == minLength + 1) { sequenceLength = 0; ans.append("B"); sequenceCnt++; } else { ans.append("R"); sequenceLength++; } } for (; i < n; i++) { if (sequenceLength == minLength) { sequenceLength = 0; ans.append("B"); } else { ans.append("R"); sequenceLength++; } } writer.println(ans); } } static class Pair<F extends Comparable<? super F>, S extends Comparable<? super S>> implements Comparable<Pair<F, S>> { F first; S second; public Pair(F first, S second) { this.first = first; this.second = second; } @Override public int compareTo(Pair<F, S> p) { int result = this.first.compareTo(p.first); if (result == 0) { return this.second.compareTo(p.second); } return result; } @Override public int hashCode() { return Objects.hash(first, second); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return Objects.equals(first, pair.first) && Objects.equals(second, pair.second); } @Override public String toString() { return "Pair{" + "first=" + first + ", second=" + second + '}'; } } static class FastestNumberReader { private final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer; private int bytesRead; public FastestNumberReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastestNumberReader(String fileName) throws IOException { din = new DataInputStream( new FileInputStream(fileName)); 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); } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } public String readLine(int length) throws IOException { byte[] buf = new byte[length + 1]; // 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; } public void close() throws IOException { din.close(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } int nextInt() { return Integer.parseInt(next()); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } 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 OutputWriter { private final PrintWriter writer; public OutputWriter() { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void flush() { writer.flush(); } public void print(Object obj) { writer.print(obj); } public void println(Object obj) { writer.println(obj); } public void printf(String format, Object... objects) { writer.printf(format, objects); } public void printfln(String format, Object... objects) { writer.printf(format, objects); writer.println(); } public void close() { writer.close(); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
b361c51b690b9c1f2f4d619789c7f7fa
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.*; import java.util.*; public class Solution { public static void main (String[] args) throws java.lang.Exception { int testCase = sc.nextInt(); while(testCase-->0){ int n = sc.nextInt(), red = sc.nextInt(), blue = sc.nextInt(); int remain = red%(blue+1); for(int i=0; i<blue+1; i++){ int breakPoint = red/(blue+1); if(remain>0){ breakPoint++; remain--; } while(breakPoint-->0){ System.out.print("R"); } if(i!=blue) System.out.print("B"); } System.out.println(); } } // Fast Reader Class public static FastReader sc = new FastReader(); public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in));} String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } }return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }return str; } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
10411264d3b81b510641d9b64fd87572
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.*; import java.util.*; public class Solution { public static void main (String[] args) throws java.lang.Exception { int testCase = sc.nextInt(); while(testCase-->0){ int n = sc.nextInt(); int red = sc.nextInt(); int blue = sc.nextInt(); int partitions = blue+1; int [] arr = new int[partitions]; int remain = red%partitions; for(int i=0; i<partitions; i++){ arr[i] = red/partitions; if(remain>0){ arr[i]++; remain--; } } for(int i=0; i<partitions; i++){ while(arr[i]-->0){ System.out.print("R"); } if(i!=partitions-1) System.out.print("B"); } System.out.println(); } } // Fast Reader Class public static FastReader sc = new FastReader(); public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in));} String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } }return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }return str; } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
b2abb36277626df1cd1a83fdbcf9c6c1
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.*; import java.util.*; public class Solution { public class TreeNode{ int val; TreeNode left, right; } public PriorityQueue<Integer> pq = new PriorityQueue<>(); public int kthSmallest(TreeNode root, int k) { traverseTree(root, k); return pq.poll(); } public void traverseTree(TreeNode node, int k){ if(node == null) return; if(pq.size()>=k){ pq.offer(node.val); pq.poll(); }else{ pq.offer(node.val); } traverseTree(node.left,k); traverseTree(node.right,k); } public static void main (String[] args) throws java.lang.Exception { int testCase = sc.nextInt(); while(testCase-->0){ int n = sc.nextInt(); int red = sc.nextInt(); int blue = sc.nextInt(); int partitions = blue+1; if(red%partitions==0){ int breakPoint = red/partitions; int redCount = 0; while(red>0 || blue>0){ if(redCount == breakPoint){ redCount = 0; System.out.print("B"); blue--; }else{ System.out.print("R"); redCount++; red--; } } System.out.println(); }else{ int [] arr = new int[partitions]; int remain = red%partitions; for(int i=0; i<partitions; i++){ arr[i] = red/partitions; if(remain>0){ arr[i]++; remain--; } } for(int i=0; i<partitions; i++){ while(arr[i]-->0){ System.out.print("R"); } if(i!=partitions-1) System.out.print("B"); } System.out.println(); } } } // Fast Reader Class public static FastReader sc = new FastReader(); public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in));} String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } }return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }return str; } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
9dd87ba9ff7f4acbb9c2f9b97c5a6915
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; import java.io.*; public class Solution { private static final String SPACE = "\\s+"; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int tt = Integer.parseInt(in.readLine()); for (int t = 1; t <= tt; t++) { String[] tokens = in.readLine().split(SPACE); int n = Integer.parseInt(tokens[0]); int r = Integer.parseInt(tokens[1]); int b = Integer.parseInt(tokens[2]); int size = r / (b + 1); int extras = r % (b + 1); StringBuilder sb = new StringBuilder(); while (true) { for (int i = 0; i < size && r > 0; i++) { sb.append('R'); r--; } if (extras > 0) { sb.append('R'); r--; extras--; } if (b > 0) { sb.append('B'); b--; } if (r == 0 && b == 0) break; } System.out.println(sb); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
b658bbab256806748070950e6591c7de
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class Main{ public static void main(String[] args) throws Exception{ Scanner cin = new Scanner(System.in); int t=cin.nextInt(); for(int i=0;i<t;i++) { int n=cin.nextInt(); int r=cin.nextInt(); int b=cin.nextInt(); int count=0; int yu=r%(b+1);//有这么多个需要多插一个的 int index=r/(b+1); for(int j=0;j<yu;j++) { for(int k=0;k<index+1;k++) { System.out.print("R"); count++; } System.out.print("B"); } for(int j=yu;j<b;j++) { for(int k=0;k<index;k++) { System.out.print("R"); count++; } System.out.print("B"); } for(;count<r;count++) { System.out.print("R"); } System.out.println(); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
a81140e913988f3c7f73a338426c3005
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; import java.io.*; public class Main { // Graph // prefix sums //inputs public static void main(String args[])throws Exception{ Input sc=new Input(); precalculates p=new precalculates(); StringBuilder sb=new StringBuilder(); int t=sc.readInt(); for(int f=0;f<t;f++){ int d[]=sc.readArray(); int n=d[0],r=d[1],b=d[2]; int rep=r/(b+1); int a[]=new int[b+1]; for(int i=0;i<a.length;i++){ a[i]=rep; r-=rep; } for(int i=0;i<a.length;i++){ if(r>0){ a[i]++; r--; } } for(int i=0;i<b;i++){ for(int j=0;j<a[i];j++){ sb.append("R"); } sb.append("B"); } for(int j=0;j<a[b];j++){ sb.append("R"); } sb.append("\n"); } System.out.print(sb); } } class Input{ BufferedReader br; StringTokenizer st; Input(){ br=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); } public int[] readArray() throws Exception{ st=new StringTokenizer(br.readLine()); int a[]=new int[st.countTokens()]; for(int i=0;i<a.length;i++){ a[i]=Integer.parseInt(st.nextToken()); } return a; } public long[] readArrayLong() throws Exception{ st=new StringTokenizer(br.readLine()); long a[]=new long[st.countTokens()]; for(int i=0;i<a.length;i++){ a[i]=Long.parseLong(st.nextToken()); } return a; } public int readInt() throws Exception{ st=new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } public long readLong() throws Exception{ st=new StringTokenizer(br.readLine()); return Long.parseLong(st.nextToken()); } public String readString() throws Exception{ return br.readLine(); } public int[][] read2dArray(int n,int m)throws Exception{ int a[][]=new int[n][m]; for(int i=0;i<n;i++){ st=new StringTokenizer(br.readLine()); for(int j=0;j<m;j++){ a[i][j]=Integer.parseInt(st.nextToken()); } } return a; } } class precalculates{ public long gcd(long p, long q) { if (q == 0) return p; else return gcd(q, p % q); } public int[] prefixSumOneDimentional(int a[]){ int n=a.length; int dp[]=new int[n]; for(int i=0;i<n;i++){ if(i==0) dp[i]=a[i]; else dp[i]=dp[i-1]+a[i]; } return dp; } public int[] postSumOneDimentional(int a[]) { int n = a.length; int dp[] = new int[n]; for (int i = n - 1; i >= 0; i--) { if (i == n - 1) dp[i] = a[i]; else dp[i] = dp[i + 1] + a[i]; } return dp; } public int[][] prefixSum2d(int a[][]){ int n=a.length;int m=a[0].length; int dp[][]=new int[n+1][m+1]; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ dp[i][j]=a[i-1][j-1]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]; } } return dp; } public long pow(long a,long b){ long mod=998244353; long ans=1; if(b<=0) return 1; if(b%2==0){ ans=pow(a,b/2)%mod; return ((ans%mod)*(ans%mod))%mod; }else{ ans=pow(a,b-1)%mod; return ((a%mod)*(ans%mod))%mod; } } } class GraphInteger{ HashMap<Integer,vertex> vtces; class vertex{ HashMap<Integer,Integer> children; public vertex(){ children=new HashMap<>(); } } public GraphInteger(){ vtces=new HashMap<>(); } public void addVertex(int a){ vtces.put(a,new vertex()); } public void addEdge(int a,int b,int cost){ if(!vtces.containsKey(a)){ vtces.put(a,new vertex()); } if(!vtces.containsKey(b)){ vtces.put(b,new vertex()); } vtces.get(a).children.put(b,cost); // vtces.get(b).children.put(a,cost); } public boolean isCyclicDirected(){ boolean isdone[]=new boolean[vtces.size()+1]; boolean check[]=new boolean[vtces.size()+1]; for(int i=1;i<=vtces.size();i++) { if (!isdone[i] && isCyclicDirected(i,isdone, check)) { return true; } } return false; } private boolean isCyclicDirected(int i,boolean isdone[],boolean check[]){ if(check[i]) return true; if(isdone[i]) return false; check[i]=true; isdone[i]=true; Set<Integer> set=vtces.get(i).children.keySet(); for(Integer ii:set){ if(isCyclicDirected(ii,isdone,check)) return true; } check[i]=false; return false; } } class union_find { int n; int[] sz; int[] par; union_find(int nval) { n = nval; sz = new int[n + 1]; par = new int[n + 1]; for (int i = 0; i <= n; i++) { par[i] = i; sz[i] = 1; } } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } boolean find(int a, int b) { return root(a) == root(b); } int union(int a, int b) { int ra = root(a); int rb = root(b); if (ra == rb) return 0; if(a==b) return 0; if (sz[a] > sz[b]) { int temp = ra; ra = rb; rb = temp; } par[ra] = rb; sz[rb] += sz[ra]; return 1; } } /* static int mod=998244353; private static int add(int x, int y) { x += y; return x % MOD; } private static int mul(int x, int y) { int res = (int) (((long) x * y) % MOD); return res; } private static int binpow(int x, int y) { int z = 1; while (y > 0) { if (y % 2 != 0) z = mul(z, x); x = mul(x, x); y >>= 1; } return z; } private static int inv(int x) { return binpow(x, MOD - 2); } private static int devide(int x, int y) { return mul(x, inv(y)); } private static int C(int n, int k, int[] fact) { return devide(fact[n], mul(fact[k], fact[n-k])); } */
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
7c8050e2a543ed44e656d5a60d321353
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import com.sun.source.tree.Tree; import java.io.*; import java.sql.Array; import java.util.*; public class Main { static Kattio io; static long mod = 998244353, inv2 = 499122177; static { io = new Kattio(); } public static void main(String[] args) { int t = io.nextInt(); for (int i = 0; i < t; i++) { solve(); } io.close(); } private static void solve() { int n = io.nextInt(),r=io.nextInt(), b = io.nextInt(); int [] buck = new int[b+1]; int ind = 0 ; while (r!=0){ buck[ind%buck.length]++; ind++; r --; } // io.println(Arrays.toString(buck)); for (int i = 0; i < buck.length; i++) { for (int j = 0; j < buck[i]; j++) { io.print("R"); } if(i!=buck.length-1) io.print("B"); } io.println(); } static class pair implements Comparable<pair> { private final int x; private final int y; public pair(final int x, final int y) { this.x = x; this.y = y; } @Override public String toString() { return "pair{" + "x=" + x + ", y=" + y + '}'; } @Override public boolean equals(final Object o) { if (this == o) { return true; } if (!(o instanceof pair)) { return false; } final pair pair = (pair) o; if (x != pair.x) { return false; } return y == pair.y; } @Override public int hashCode() { int result = x; result = (int) (31 * result + y); return result; } @Override public int compareTo(pair pair) { if(this.y==pair.y) return Integer.compare(this.x, pair.x); return Integer.compare(this.y, pair.y); } } 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(new FileWriter(problemName + ".out")); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) {} return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
5a1ffee04bacee0e30b858391103ce77
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.*; import java.util.*; public class Main { static int[][] size; static int[][][] parent; static char[][] a; public static void main(String[] args) { MyScanner in = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = in.nextInt(); while(t -- > 0) { int n = in.nextInt(); int r = in.nextInt(); int b = in.nextInt(); int bucket = r / (b + 1); int rem = r - bucket * (b+1); // at most < bucket for(int i = 1; i <= b + 1; i++) { for(int j = 1; j <= bucket; j++) { out.print('R'); } if(rem >0) { out.print('R'); rem--; } if(i != b + 1) { out.print('B'); } } out.println(""); } out.close(); return; } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //-------------------------------------------------------- }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
748e7efbd9bcd2f51aada7f22591db3f
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
//Utilities import java.io.*; import java.util.*; public class a { static int t; static int n, a, b; public static void main(String[] args) throws IOException { t = in.iscan(); while (t-- > 0) { n = in.iscan(); a = in.iscan(); b = in.iscan(); char[] res = new char[n]; if (a >= b) { for (int i = 0; i <= n; i++) { if (a - b * i <= i) { for (int j = 0, cnt = 0; j < n; j++) { if (cnt == i) { res[j] = 'B'; b--; cnt = 0; continue; } res[j] = 'R'; a--; cnt++; } int idx = 0; while (b > 0) { if (res[idx] == 'R' && (idx == n-1 || res[idx+1] == 'R')) { res[idx] = 'B'; idx++; b--; a++; } idx++; } break; } } } else { for (int i = 0; i <= n; i++) { if (b - a * i <= i) { for (int j = 0, cnt = 0; j < n; j++) { if (cnt == i) { res[j] = 'R'; a--; cnt = 0; continue; } res[j] = 'B'; b--; cnt++; } int idx = 0; while (a > 0) { if (res[idx] == 'B' && (idx == n-1 || res[idx+1] == 'B')) { res[idx] = 'R'; idx++; a--; b++; } idx++; } break; } } } out.println(res); } out.close(); } static INPUT in = new INPUT(System.in); static PrintWriter out = new PrintWriter(System.out); private static class INPUT { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public INPUT (InputStream stream) { this.stream = stream; } public INPUT (String file) throws IOException { this.stream = new FileInputStream (file); } public int cscan () throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read (buf); } if (numChars == -1) return numChars; return buf[curChar++]; } public int iscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } int res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public String sscan () throws IOException { int c = cscan (); while (space (c)) c = cscan (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = cscan (); } while (!space (c)); return res.toString (); } public double dscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } double res = 0; while (!space (c) && c != '.') { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); res *= 10; res += c - '0'; c = cscan (); } if (c == '.') { c = cscan (); double m = 1; while (!space (c)) { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); m /= 10; res += (c - '0') * m; c = cscan (); } } return res * sgn; } public long lscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } long res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public boolean space (int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } public static class UTILITIES { static final double EPS = 10e-6; public static void sort(int[] a, boolean increasing) { ArrayList<Integer> arr = new ArrayList<Integer>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(long[] a, boolean increasing) { ArrayList<Long> arr = new ArrayList<Long>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(double[] a, boolean increasing) { ArrayList<Double> arr = new ArrayList<Double>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static int lower_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } public static void updateMap(HashMap<Integer, Integer> map, int key, int v) { if (!map.containsKey(key)) { map.put(key, v); } else { map.put(key, map.get(key) + v); } if (map.get(key) == 0) { map.remove(key); } } public static long gcd (long a, long b) { return b == 0 ? a : gcd (b, a % b); } public static long lcm (long a, long b) { return a * b / gcd (a, b); } public static long fast_pow_mod (long b, long x, int mod) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod; return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod; } public static long fast_pow (long b, long x) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow (b * b, x / 2); return b * fast_pow (b * b, x / 2); } public static long choose (long n, long k) { k = Math.min (k, n - k); long val = 1; for (int i = 0; i < k; ++i) val = val * (n - i) / (i + 1); return val; } public static long permute (int n, int k) { if (n < k) return 0; long val = 1; for (int i = 0; i < k; ++i) val = (val * (n - i)); return val; } // start of permutation and lower/upper bound template public static void nextPermutation(int[] nums) { //find first decreasing digit int mark = -1; for (int i = nums.length - 1; i > 0; i--) { if (nums[i] > nums[i - 1]) { mark = i - 1; break; } } if (mark == -1) { reverse(nums, 0, nums.length - 1); return; } int idx = nums.length-1; for (int i = nums.length-1; i >= mark+1; i--) { if (nums[i] > nums[mark]) { idx = i; break; } } swap(nums, mark, idx); reverse(nums, mark + 1, nums.length - 1); } public static void swap(int[] nums, int i, int j) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } public static void reverse(int[] nums, int i, int j) { while (i < j) { swap(nums, i, j); i++; j--; } } static int lower_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= cmp) high = mid; else low = mid + 1; } return low; } static int upper_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > cmp) high = mid; else low = mid + 1; } return low; } // end of permutation and lower/upper bound template } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
d3a03d7df5990dee7e3ba3bef0683e4a
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class cf1659A { // https://codeforces.com/problemset/problem/1659/A public static void main(String[] args) { Kattio io = new Kattio(); int t = io.nextInt(), chunk, r, b, rem; char divider, fill; for (int i = 0; i < t; i++) { io.nextInt(); r = io.nextInt(); b = io.nextInt(); chunk = r / (b + 1); rem = r % (b + 1); while (r > 0) { for (int k = 0; k < chunk; k++) { if (r > 0) io.print('R'); r--; } if (rem > 0) { io.print('R'); rem--; r--; } if (b > 0) io.print('B'); b--; } io.println(); } io.close(); } // Kattio static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in,System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(problemName+".out"); r = new BufferedReader(new FileReader(problemName+".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) {} return null; } public String nextLine() { try { st = null; return r.readLine(); } catch (Exception e) {} return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
0884e5bd9a67ab5b5c00fd3e096fad9b
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; public class RedVersusBlue { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int r=sc.nextInt(); int b=sc.nextInt(); int div=r/(b+1); int rem=r%(b+1); String v=""; StringBuilder sb=new StringBuilder(); StringBuilder nsb=new StringBuilder(); for(int i=0;i<div+1;i++) { sb.append('R'); } for(int i=0;i<rem;i++) { nsb.append(sb); nsb.append('B'); } for(int i=rem;i<b;i++) { for(int j=0;j<div;j++) nsb.append('R'); nsb.append('B'); } for(int i=0;i<div;i++) nsb.append('R'); String s=nsb.toString(); System.out.println(s); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
8b2593cd4f4495f98dfc51a513a7853e
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int r = sc.nextInt(); int b = sc.nextInt(); int p = r/(b+1); int q = r % (b+1); for (int i = 0; i < (b+1); i++) { for (int j = 0; j < r/(b+1); j++) { System.out.print("R"); } if(q > 0) {System.out.print("R");q--;} if(i < b) System.out.print("B"); } System.out.println(); } } } 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()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } int[] sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i : arr) list.add(i); Collections.sort(list); int[] res = new int[arr.length]; for (int i = 0; i < arr.length; i++) res[i] = list.get(i); return res; } void debug(int a) { System.out.println("This is var " + a); } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
a40184bb478bdec60b436d6b19763998
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; import java.io.*; public class Solution { static boolean cases = true; // Solution static void solve(int t) { int n = sc.nextInt(), r = sc.nextInt(), b = sc.nextInt(); int k = r / (b + 1); int add = r % (b + 1); while (r > 0) { int need = k + ((add > 0) ? 1 : 0); add = Math.max(0, add - 1); r -= need; while (need-- > 0) System.out.print("R"); if (b > 0) { System.out.print("B"); --b; } } System.out.println(); } public static void main(String[] args) { int c = 1; for (int t = (cases ? sc.nextInt() : 0); t > 1; t--, c++) solve(c); solve(c); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } private static final FastScanner sc = new FastScanner(); private static PrintWriter out = new PrintWriter(System.out); }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
efce98ae6a01c6481cd290aaccd60ccb
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; import java.io.*; public class Solution { static boolean cases = true; // Solution static void solve(int t) { int n = sc.nextInt(), r = sc.nextInt(), b = sc.nextInt(); int k = r / (b + 1); int add = r % (b + 1); while (r > 0) { int need = k + ((add > 0) ? 1 : 0); add = Math.max(0, add - 1); r -= need; while (need-- > 0) System.out.print("R"); if (b > 0) { System.out.print("B"); --b; } } System.out.println(); } public static void main(String[] args) { int c = 1; for (int t = (cases ? sc.nextInt() : 0); t > 1; t--, c++) solve(c); solve(c); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } private static final FastScanner sc = new FastScanner(); private static PrintWriter out = new PrintWriter(System.out); }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
f4bf42fe0160163bedd35cef2debe58f
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.DataInputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.*; import java.util.function.Consumer; import java.util.stream.Collectors; import static java.lang.Math.*; public class Round782_A { @SuppressWarnings("FieldCanBeLocal") private static Reader in; private static PrintWriter out; private static String YES = "YES"; private static String NO = "NO"; private static void solve() throws IOException{ //Your Code Goes Here; int n = in.nextInt(), r = in.nextInt(), b = in.nextInt(); int k = r / (b + 1); int bg = k + 1; int nBg = r % (b + 1); int nSm = b + 1 - nBg; for(int i = 0; i < nSm; i++){ if(i != 0) System.out.print('B'); for(int j = 0; j < k; j++){ System.out.print('R'); } } for(int i = 0; i < nBg; i++){ System.out.print('B'); for(int j = 0; j < bg; j++){ System.out.print('R'); } } System.out.println(); } public static void main(String[] args) throws IOException, InterruptedException { in = new Reader(); out = new PrintWriter(new OutputStreamWriter(System.out)); int T = in.nextInt(); while(T --> 0){ solve(); } out.flush(); in.close(); out.close(); } static final Random random = new Random(); static final int mod = 1_000_000_007; private static boolean check(long[] arr, long n){ long ch = arr[0]; for(int i = 0; i < n; i++){ if(ch != arr[i]){ return false; } } return true; } //This function gives the max occuring of any element in an array;(HashMap version) private static long maxFreqHashMap(long[] arr, long n){ Map<Long, Long> hp = new HashMap<Long, Long>(); for(int i = 0; i < n; i++) { long key = arr[i]; if(hp.containsKey(key)) { long freq = hp.get(key); freq++; hp.put(key, freq); } else { hp.put(key, 1L); } } long max_count = 0, res = 1, count = 0; for(Map.Entry<Long, Long> val : hp.entrySet()) { if (max_count < val.getValue()) { res = Math.toIntExact(val.getKey()); max_count = Math.toIntExact(val.getValue()); count = max_count; } } return count; } //This function gives the max occuring of any element in an array; this also known as the "MOORE's Algorithm" private static long maxFreq(long []arr, long n) { // using moore's voting algorithm long res = 0; long count = -1; for(int i = 1; i < n; i++) { if(arr[i] == arr[(int) res]) { count++; } else { count--; } if(count == 0) { res = i; count = 1; } } return arr[(int) res]; /* you've to add below code in the solve() long freq = maxFreq(arr, n); int count = 0; for(int i = 0; i < n; i++){ if(arr[i] == freq){ count++; } } */ } private static int LCSubStr(char[] X, char[] Y, int m, int n) { int[][] LCStuff = new int[m + 1][n + 1]; int result = 0; for (int i = 0; i <= m; i++){ for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) { LCStuff[i][j] = 0; } else if (X[i - 1] == Y[j - 1]) { LCStuff[i][j] = LCStuff[i - 1][j - 1] + 1; result = Integer.max(result, LCStuff[i][j]); } else { LCStuff[i][j] = 0; } } } return result; } private static long longCusBsearch(long[] arr, long n, long h){ long ans = h; long l = 1; long r = h; while(l <= r){ long mid = (l + r) / 2; long ok = 0; for(long i = 0; i < n; i++){ if(i == n - 1) { ok += mid; } else{ long x = arr[(int) (i + 1)] - arr[(int) i]; if(x >= mid) { ok += mid; } else{ ok += x; } } } if(ok >= h){ ans = mid; r = mid - 1; } else{ l = mid + 1; } } return ans; } public static int intCusBsearch(int[] arr, int n, int h){ int ans = h, l = 1, r = h; while(l <= r){ int mid = (l + r) / 2; int ok = 0; for(int i = 0; i < n; i++){ if(i == n - 1){ ok += mid; } else{ int x = arr[i + 1] - arr[i]; if(x >= mid){ ok += mid; } else{ ok += x; } } } if(ok >= h){ ans = mid; r = mid - 1; } else{ l = mid + 1; } } return ans; } /* Method to check if x is power of 2*/ private static boolean isPowerOfTwo (int x) { /* First x in the below expression is for the case when x is 0 */ return x!=0 && ((x&(x-1)) == 0); } //Taken From "Second Thread" static void ruffleSort(int[] a){ int n = a.length;//shuffles, then sort; for(int i=0; i<n; i++){ int oi = random.nextInt(n), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } sort(a); } private static void longRuffleSort(long[] a){ long n = a.length; for(long i = 0;i<n;i++){ long oi = random.nextInt((int) n), temp = a[(int) oi]; a[(int) oi] = a[(int) i]; a[(int) i] = temp; } longSort(a); } private static int gcd(int a, int b){ if(a == 0 || b == 0){ return 0; } while(b != 0){ int temp; temp = a % b; a = b; b = temp; } return a; } private static long gcd(long a, long b){ if(a == 0 || b == 0){ return 0; } while(b != 0){ long temp; temp = a % b; a = b; b = temp; } return a; } private static int lowestCommonMultiple(int a, int b){ return (a / gcd(a, b) * b); } /* The Below func: the for loop runs in sqrt times. The second if is used to if the divisors are equal to print only one of them otherwise we're printing both; */ private static void pDivisors(int n){ for(int i=1;i<Math.sqrt(n);i++){ if(n % i == 0){ if(n / i == i){ System.out.println(i + " "); } else{ System.out.println(i + " " + (n / i) + " "); } } } } private static void returnNothing(){return;} private static int numOfdigitsinN(int a){return (int) (Math.floor(Math.log10(a)) + 1);} //prime Num till N: it takes any number and prints all the prime till that //num; private static boolean isPrime(int n){ if(n <= 1) return false; if(n <= 3) return true; //This is checked so that we can skip middle five number 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; } //below func is to print the isPrime func(); private static void printTheIsPrimeFunc(int n){ for(int i=2;i<=n;i++){ if(isPrime(i)) System.out.println(i + " "); } } public static boolean primeFinder(int n){ int m = 0; int flag = 0; m = n / 2; if(n == 0 ||n == 1){ return false; } else{ for(int i=2;i<=m;i++){ if(n % i == 0){ return false; } } return true; } } private static boolean evenOdd(int num){ //System.out.println((num & 1) == 0 ? "EVEN" : "ODD"); return (num & 1) == 0 ? true : false; } private static boolean[] sieveOfEratosthenes(long n) { boolean prime[] = new boolean[(int)n + 1]; for (int i = 0; i <= n; i++) { prime[i] = true; } 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; } } return prime; } private static int log2(int n){ int result = (int) (Math.log(n) / Math.log(2)); return result; } private static long add(long a, long b){ return (a + b) % mod; } private static long lcm(long a, long b){ return (a / gcd((int) a, (int) b) * b); } private static void sort(int[] a){ ArrayList<Integer> l = new ArrayList<>(); for(int i:a) l.add(i); Collections.sort(l); for(int i=0;i<a.length;i++)a[i] = l.get(i); } private static void longSort(long[] a){ ArrayList<Long> l = new ArrayList<Long>(); for(long i:a) l.add(i); Collections.sort(l); for(int i=0;i<a.length;i++)a[i] = l.get(i); } public static int[][] prefixsum( int n , int m , int arr[][] ){ int prefixsum[][] = new int[n+1][m+1]; for( int i = 1 ;i <= n ;i++) { for( int j = 1 ; j<= m ; j++) { int toadd = 0; if( arr[i-1][j-1] == 1) { toadd = 1; } prefixsum[i][j] = toadd + prefixsum[i][j-1] + prefixsum[i-1][j] - prefixsum[i-1][j-1]; } } return prefixsum; } //call this method when you want to read an integer array; private static int[] readArray(int len) throws IOException{ int[] a = new int[len]; for(int i=0;i<len;i++)a[i] = in.nextInt(); return a; } //call this method when you want to read an Long array; private static long[] readLongArray(int len) throws IOException{ long[] a = new long[len]; for(int i=0;i<len;i++) a[i] = in.nextLong(); return a; } //call this method to print the integer array; private static void printArray(int[] array){ for(int now : array) out.print(now);out.print(' ');out.close(); } //call this method to print the long array; private static void printLongArray(long[] array){ for(long now:array) out.print(now); out.print(' '); out.close(); } /*A way of Reading input...collected from a online blog from here => https://codeforces.com/contest/1625/submission/144334744;*/ private static class Reader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; Reader() {//Constructor; din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException {//To take user input for String values; final byte[] buf = new byte[1024]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextSign() throws IOException {//For taking the signs like plus or minus ... byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() throws IOException { int b; // noinspection ALL while ((b = read()) != -1 && isSpaceChar(b)) { ; } return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
e3753ab2adbdd338b4311049acb25032
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; import java.lang.*; public class second { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String str = ""; int t = sc.nextInt(); for(int i=0;i<t;i++){ int n = sc.nextInt(); int r = sc.nextInt(); int b = sc.nextInt(); int d = r/(b+1); int rem = r-(b+1)*d; for(int z=0;z<d;z++){ str=str+"R"+""; } for(int f=0;f<b;f++){ if(rem>0){ str=str+"R"; rem--; } str=str+"B"; for(int z=0;z<d;z++){ str=str+"R"+""; } } System.out.println(str); str=""; } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
f4072ec4272ad2975cc7356b9121a0f4
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class P03 { static class FastReader{ BufferedReader br ; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()){try {st = new StringTokenizer(br.readLine());}catch(IOException e) {e.printStackTrace();}} return st.nextToken(); } int nextInt(){return Integer.parseInt(next());} long nextLong(){return Long.parseLong(next());} double nextDouble(){return Double.parseDouble(next());} float nextFloat(){return Float.parseFloat(next());} String nextLine(){ String str = ""; try { str = br.readLine(); }catch(IOException e) { e.printStackTrace(); } return str ; } } static class BPair { Integer index;boolean value; public BPair(Integer index, boolean value) {this.value =value;this.index =index;} int getIndex() {return index;} boolean getValue() {return value;} } static class Pair implements Comparable<Pair> { Integer index,value; public Pair(Integer index, Integer value) {this.value = value;this.index = index;} @Override public int compareTo(Pair o){return value - o.value;} int getValue(){ return value;} int getindex(){return index;} } private static long gcd(long l, long m){ if(m==0) {return l;} return gcd(m,l%m); } static void swap(long[] arr, int i, int j){long temp = arr[i];arr[i] = arr[j];arr[j] = temp;} static int partition(long[] arr, int low, int high){ long pivot = arr[high]; int i = (low - 1); for(int j = low; j <= high - 1; j++){ if (arr[j] < pivot){ i++; swap(arr, i, j); } } swap(arr, i + 1, high); return (i + 1); } static void quickSort(long[] arr, int low, int high){ if (low < high){ int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } static long M = 1000000007; static long powe(long a,long b) { long res =1; while(b>0){ if((b&1)!=0) { res=(res*a)&M; } a=(a*a)%M; b>>=1; } return res; } static long power(long x, long y, long p) { long res = 1; x = x % p; if (x == 0) return 0; while (y > 0){ if ((y & 1) != 0) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static boolean [] seive(int n) { boolean a[] = new boolean[n+1]; Arrays.fill(a, true); a[0]=false; a[1]=false; for(int i =2;i<=Math.sqrt(n);i++) { for(int j =2*i;j<=n;j+=i) { a[j]=false; } } return a; } private static void pa(char[] cs){for(int i =0;i<cs.length;i++){System.out.print(cs[i]+" ");}System.out.println();} private static void pa(long[] b){for(int i =0;i<b.length;i++) {System.out.print(b[i]+" ");}System.out.println();} private static void pm(Character[][] a){for(int i =0;i<a.length;i++){for(int j=0;j<a[i].length;j++){System.out.print(a[i][j]);}System.out.println();}} private static void pm(int [][] a) {for(int i =0;i<a.length;i++) {for(int j=0;j<a[0].length;j++) {System.out.print(a[i][j]+" ");}System.out.println();}} private static void pa(int[] a){for(int i =0;i<a.length;i++){System.out.print(a[i]+" ");}System.out.println();} private static boolean isprime(int n){if (n <= 1) return false;else if (n == 2) return true;else if (n % 2 == 0) return false;for (int i = 3; i <= Math.sqrt(n); i += 2){if (n % i == 0) return false;}return true;} static int lb(long[] b, long a){ int l=-1,r=b.length; while(l+1<r) { int m=(l+r)>>>1; if(b[m]>=a) r=m; else l=m; } return r; } static int ub(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; } static void rec(String digits,ArrayList<String> ans,String temp,int in ,HashMap<Integer,String>hm,int l){ if(temp.length()==l){ ans.add(temp); return ; } String s = hm.get(digits.charAt(in)-'0'); int n = s.length(); for(int i =0;i<n;i++){ rec(digits,ans,temp+s.charAt(i),in+1,hm,l); } } public static int longestCommonSubsequence(String text1, String text2) { int n = text1.length(); int m = text2.length(); int dp [][]= new int [n+1][m+1]; for(int i =0;i<=n;i++){ for(int j =0;j<=m;j++){ if(i==0||j==0)dp[i][j]=0; else if(text1.charAt(i-1)==text2.charAt(j-1)){ dp[i][j]=1+dp[i-1][j-1]; } else dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]); } } return dp[n][m]; } static //MAIN FUNCTION -------> ArrayList<String> alpos = new ArrayList<>(); public static void main(String[] args) throws IOException { long start2 = System.currentTimeMillis(); // FastReader fs = new FastReader(); Scanner fs = new Scanner (System.in); int t = fs.nextInt(); // int t =1; while(t-->0) { int n = fs.nextInt(); int r = fs.nextInt(); int b = fs.nextInt(); int x = (int)Math.floor((double)r/(double)(b+1)); int k = r%(b+1); // System.out.println(x+" "+k); String ans =""; for(int i =0;i<b+1;i++) { for(int j=0;j<x;j++) { ans+='R'; } if(k>0) { ans+='R'; k--; } if(i!=b)ans+='B'; } System.out.println(ans); } long end2 = System.currentTimeMillis(); // System.out.println("time :"+(end2-start2)); // sc.close(); } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
26436ca2e3e509def9206f8d7b86a7a6
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; import java.util.Map.Entry; import java.math.*; import java.sql.Array; public class Simple{ public static class Pair implements Comparable<Pair>{ int x; int y; public Pair(int x,int y){ this.x = x; this.y = y; } public int compareTo(Pair other){ return (int) (this.y - other.y); } public boolean equals(Pair other){ if(this.x == other.x && this.y == other.y)return true; return false; } public int hashCode(){ return 31*x + y; } // @Override // public int compareTo(Simple.Pair o) { // // TODO Auto-generated method stub // return 0; // } } static int power(int x, int y, int p) { // Initialize result int res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if (y % 2 == 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n^(-1) mod p static int modInverse(int n, int p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static int nCrModPFermat(int n, int r, int p) { if (n<r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r int[] fac = new int[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 int nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; // The array C is going to store last // row of pascal triangle at the end. // And last entry of last row is nCr int C[] = new int[r + 1]; C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (int i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (int j = Math.min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } 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 class Node{ int root; ArrayList<Node> al; Node par; public Node(int root,ArrayList<Node> al,Node par){ this.root = root; this.al = al; this.par = par; } } // public static int helper(int arr[],int n ,int i,int j,int dp[][]){ // if(i==0 || j==0)return 0; // if(dp[i][j]!=-1){ // return dp[i][j]; // } // if(helper(arr, n, i-1, j-1, dp)>=0){ // return dp[i][j]=Math.max(helper(arr, n, i-1, j-1,dp)+arr[i-1], helper(arr, n, i-1, j,dp)); // } // return dp[i][j] = helper(arr, n, i-1, j,dp); // } // public static void dfs(ArrayList<ArrayList<Integer>> al,int levelcount[],int node,int count){ // levelcount[count]++; // for(Integer x : al.get(node)){ // dfs(al, levelcount, x, count+1); // } // } public static long __gcd(long a, long b) { if (b == 0) return a; return __gcd(b, a % b); } public static long helper(int arr1[][],int m){ Arrays.sort(arr1, (int a[],int b[])-> a[0]-b[0]); long ans =0; for(int i=1;i<m;i++){ long count =0; for(int j=0;j<i;j++){ if(arr1[i][1] > arr1[j][1])count++; } ans+=count; } return ans; } public static class DSU{ int n; int par[]; int rank[]; public DSU(int n){ this.n = n; par = new int[n+1]; rank = new int[n+1]; for(int i=1;i<=n;i++){ par[i] = i ; rank[i] = 0; } } public int findPar(int node){ if(node==par[node]){ return node; } return par[node] = findPar(par[node]);//path compression } public void union(int u,int v){ u = findPar(u); v = findPar(v); if(rank[u]<rank[v]){ par[u] = v; } else if(rank[u]>rank[v]){ par[v] = u; } else{ par[v] = u; rank[u]++; } } } public static void dfs(ArrayList<ArrayList<Integer>> adj, int l[],int r[],boolean vis[],int node,long dp[][]){ long ansl = 0; long ansr = 0; vis[node] = true; for(Integer x : adj.get(node)){ if(!vis[x]){ vis[x] = true; dfs(adj, l, r, vis, x, dp); ansl+=Math.max(Math.abs(l[node]-l[x])+dp[x][0], Math.abs(l[node]-r[x])+dp[x][1]); ansr+=Math.max(Math.abs(r[node]-l[x])+dp[x][0], Math.abs(r[node]-r[x])+dp[x][1]); } } dp[node][0] = ansl; dp[node][1] = ansr; } public static void main(String args[]){ Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int t1 = 1;t1<=t;t1++){ int n = s.nextInt(); int r = s.nextInt(); int l = s.nextInt(); String str =""; // int count = 0; // int part1 = r/(l+1); // if(r%(l+1)!=0)part1++; ArrayList<Integer> al = new ArrayList<>(); int l1 =l; for(int i=1;i<=l1;i++){ int num = r/(l+1); al.add(num); r-=num; l--; } int k = 0; // System.out.println(part); // System.out.println(al.toString()); int count = 0; for(int i=1;i<=n;i++){ if(k<l1&&al.get(k)==count){ str+='B'; k++; count =0; } else{ str+='R'; count++; } } System.out.println(str); } } } /* 4 2 2 7 0 2 5 -2 3 5 0*x1 + 1*x2 + 2*x3 + 3*x4 */
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
cd3a01bacc0553d293ea087876521871
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.Scanner; public class Firstclass { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-->0){ int n =s.nextInt(),r=s.nextInt(),b=s.nextInt(); int res = r/(b+1); int num = r%(b+1); int cont=0; for (int i = 0; i < n; i++) { if (cont!=res){ System.out.print("R"); cont++; } else { if(num>0){ System.out.print("R"); num--; i++; } System.out.print("B"); cont=0; } } System.out.println(); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
667ff91ead0356eb7e6fa8cd9b7496d4
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.*; import java.util.*; public class A_Red_Versus_Blue { public static void main(String args[]) throws IOException { Scanner sc = new Scanner(System.in); long t = sc.nextLong(); while (t-- > 0) { int n=sc.nextInt(); long r=sc.nextLong(); long b=sc.nextLong(); int a[]=new int[n]; Arrays.fill(a, 1); int i=0; while(i<n){ int gap= (int) Math.ceil((float)r/(b+1)); while(gap>0) { a[i]=1; gap--; r--; i++; } if (i<n) { a[i]=0; b--; i++; } } for(i=0;i<n;i++) { if(a[i]==1) { System.out.print("R"); } else{ System.out.print("B"); } } System.out.println(); } sc.close(); } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
0f19f36a08fe9e5fe8018593510217ec
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.*; import java.util.*; public class A_Red_Versus_Blue { public static void main(String args[]) throws IOException { Scanner sc = new Scanner(System.in); long t = sc.nextLong(); while (t-- > 0) { int n=sc.nextInt(); long r=sc.nextLong(); long b=sc.nextLong(); int a[]=new int[n]; Arrays.fill(a, 1); int m=(int) Math.ceil((double)n/(b+1)); int p=m-1; int i=0; while(i<n){ int gap= (int) Math.ceil((float)r/(b+1)); while(gap>0){ a[i]=1; gap--; r--; i++; } if (i<n){ a[i]=0; b--; i++; } } // System.out.println("m="+m); // while(b>0) // { // if(p>=n-m && b!=0) // { // a[p]=0; // p++; // b--; // } // else // {a[p]=0; // b--; // p=p+m;} // } for(i=0;i<n;i++) { if(a[i]==1) { System.out.print("R"); } else{ System.out.print("B"); } } System.out.println(); } sc.close(); } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
738bc9f1b1b3638957a38f5207dc1609
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class redVsBlue { public static void main(String[] args) throws IOException { Reader x = new Reader(); int t = x.nextInt(); String[] answers = new String[t]; int n, r, b, p; StringBuilder s, y; for (int i = 0; i < t; i++) { n = x.nextInt(); r = x.nextInt(); b = x.nextInt(); p = r % (b + 1); s = new StringBuilder(); y = new StringBuilder(); for (int j = 0; j < r / (b + 1); j++) { y.append("R"); } for (int j = 0; j < b + 1; j++) { if (j > 0) { s.append("B"); } s.append(y); if (p > 0) { s.append("R"); p--; } } answers[i] = s.toString(); } for (int i = 0; i < t; i++) { System.out.println(answers[i]); } } public String addChar(String str, char ch, int position) { return str.substring(0, position) + ch + str.substring(position); } static class Reader { String filename; BufferedReader br; StringTokenizer st; PrintWriter out; boolean fileOut = false; public Reader() { this.br = new BufferedReader(new InputStreamReader(System.in)); } public Reader(String name) throws IOException { this.filename = name; this.br = new BufferedReader(new FileReader(this.filename + ".in")); this.out = new PrintWriter(new BufferedWriter(new FileWriter(this.filename + ".out"))); this.fileOut = true; } public String next() throws IOException { while(this.st == null || !this.st.hasMoreTokens()) { try { this.st = new StringTokenizer(this.br.readLine()); } catch (Exception var2) { } } return this.st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(this.next()); } public void println(Object text) { if (this.fileOut) { this.out.println(text); } else { System.out.println(text); } } public void close() { if (this.fileOut) { this.out.close(); } } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
2a476c97eb2d84360e212c9ab7bfecf3
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; public class main { public static void main(String[] strgs) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-- >0) { int n=sc.nextInt(); int r=sc.nextInt(); int b=sc.nextInt(); String s=""; int limit=r/(b+1); for(int i=0;i<limit;i++) { s+="R"; } int rem=r%(b+1); int equal=b+1-rem; String ans=""; for(int i=1;i<=equal;i++) { ans+=s+"B"; } for(int i=1;i<=rem;i++) { ans+=s+"RB"; } System.out.println(ans.substring(0,n)); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
afa6b8b50c2ac404e67cf15c9c97d536
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; public class red_vs_blue { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuffer str = new StringBuffer(""); while(t-- > 0) { int N = sc.nextInt(); int R = sc.nextInt(); int B = sc.nextInt(); int rem = 0, que = 0; que = R/B; rem = R%B; int rint = 0, bint = 0; if(B == 1) { que = R/2; int temp = que; while(temp-- > 0) { str.append('R'); R--; rint++; } if(B-- > 0) { str.append('B'); bint++; } while(R-- > 0) { str.append('R'); rint++; } } else { que = (R/(B+1)); rem = (R%(B+1)); int temp = 0; while(rem-- > 0) { temp = que+1; while(temp-- > 0) { str.append('R'); R--; rint++; } if(B-- > 0) { str.append('B'); bint++; } } while(str.length() < N) { temp = que; while(temp-- > 0 && R > 0) { str.append('R'); R--; rint++; } if(B-- > 0) { str.append('B'); bint++; } } } System.out.println(str); str.delete(0, str.length()); str.append(""); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
8e019cb4cd0dcaa8a63d0bfa07cae436
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class meomeo{ public static void main(String[] args) { try(final Scanner scanner = new Scanner(System.in)){ int noTests = scanner.nextInt(); while(noTests-- >0){ final Integer n = scanner.nextInt(); final Integer r = scanner.nextInt(); Integer b = scanner.nextInt(); Integer noR = 0; Integer diffNoR = 0; StringBuilder sb = new StringBuilder("B"); if(b== 1 && r%2 == 0){ noR = r/2; String startString = "R".repeat(noR); sb.insert(0, startString); sb.append(startString); System.out.println(sb.toString()); continue; } if(b==1&& r%2!=0){ noR = r/2; diffNoR = r/2 + 1; String startString = "R".repeat(diffNoR); String endString = "R".repeat(noR); sb.insert(0, startString); sb.append(endString); System.out.println(sb.toString()); continue; } String res = "B".repeat(b); //res = res + ""; StringBuilder result = new StringBuilder(res); final Integer oldB = b; b = b+1; Integer divStringR = r/b; Integer modStringR = r%b; if(modStringR != 0 && modStringR <= oldB && modStringR !=1){ divStringR +=1; } if(modStringR ==0 && divStringR <=b){ modStringR +=1; } String stringR = "R".repeat(divStringR); String endString = "R".repeat(modStringR); Integer count = 0; Integer countB = -1; for(int i = 0; i<n;i++){ if(result.toString().charAt(i) == 'B'){ countB++; if(i==0){ result.insert(i, stringR); count += stringR.length(); } if(i != 0 && i!= result.toString().length() -1 ){ if((r-count)/(b-countB) < divStringR -1){ divStringR -=1; stringR = "R".repeat(divStringR); } result.insert(i+1, stringR); count += stringR.length(); } if(i == result.length() - 1){ Integer getIndex = n - result.length(); if(getIndex < endString.length()){ String subString = endString.substring(0, getIndex); result.append(subString); } else { endString = "R".repeat(getIndex); result.append(endString); //break; } } } } System.out.println(result.toString()); } } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
fa489e56f770c946f6f1c7be17eaa790
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st; while (t-- > 0) { // int n = Integer.parseInt(br.readLine()); st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); int div = r / (b+1); int mod = r % (b+1); for(int i = 0; i < (b+1); i++) { int temp = div; if(mod-->0) temp++; while(temp-->0) output.write("R"); if(i!=b) output.write("B"); } output.write("\n"); // int arr[] = new int[n]; // for(int i = 0; i < n; i++) // { // arr[i] = e; // } // int k = Integer.parseInt(st.nextToken()); // char arr[] = br.readLine().toCharArray(); // output.write(); // int n = Integer.parseInt(st.nextToken()); // HashMap<Character, Integer> map = new HashMap<Character, Integer>(); // if // output.write("YES\n"); // else // output.write("NO\n"); // long a = Long.parseLong(st.nextToken()); // long b = Long.parseLong(st.nextToken()); // if(flag == 1) // output.write("NO\n"); // else // output.write("YES\n" + x + " " + y + " " + z + "\n"); // output.write(n+ "\n"); } output.flush(); } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
f850386e0fcb7808425589807f189a87
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st; while (t-- > 0) { // int n = Integer.parseInt(br.readLine()); st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); int r2 = r; int nchunks = r2/(b+1); int addr = r % (b+1); while(r2 > 0) { int mkl = nchunks; if(addr>0) mkl++; addr = Math.max(0, addr - 1); r2-=mkl; while(mkl-->0) { output.write("R"); } if(b > 0) { output.write("B"); b--; } } output.write("\n"); // int arr[] = new int[n]; // for(int i = 0; i < n; i++) // { // arr[i] = e; // } // int k = Integer.parseInt(st.nextToken()); // char arr[] = br.readLine().toCharArray(); // output.write(); // int n = Integer.parseInt(st.nextToken()); // HashMap<Character, Integer> map = new HashMap<Character, Integer>(); // if // output.write("YES\n"); // else // output.write("NO\n"); // long a = Long.parseLong(st.nextToken()); // long b = Long.parseLong(st.nextToken()); // if(flag == 1) // output.write("NO\n"); // else // output.write("YES\n" + x + " " + y + " " + z + "\n"); // output.write(n+ "\n"); } output.flush(); } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
2e111341f21e0172d4a7e2cbd2fc6e50
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static Scanner obj = new Scanner(System.in); public static PrintWriter out = new PrintWriter(System.out); public static int i() { return obj.nextInt(); } public static void main(String[] args) { int len = i(); while (len-- != 0) { int n=obj.nextInt(); int r=obj.nextInt(); int b=obj.nextInt(); int val=r/(b+1); int in=0; StringBuffer sb=new StringBuffer(""); while(b>0) { int c=1; while(r>0 && c<=val) { sb.append("R"); r--; c++; } sb.append("B"); in=sb.length(); b--; } for(int i=0;i<val && r>0;i++,r--) sb.append("R"); StringBuffer ans=new StringBuffer(""); for(int i=0;i<sb.length();i++) { if(sb.charAt(i)=='B' && r-1>=0) { ans.append("R"); r--; } ans.append(sb.charAt(i)); } out.println(ans.toString()); } out.flush(); } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
7f2bb9d12515eb029d042919a6da1996
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
//package codeforces; import java.util.*; public class Test2 { static Map<Integer,Integer> mp=new HashMap(); private static int fun(int n) { if(n==0) return 0; if(mp.containsKey(n)) return mp.get(n); else { mp.put(n, 1+fun(n^fun2(n))); return mp.get(n); } } private static int fun2(int n) { StringBuilder s=new StringBuilder(Integer.toBinaryString(n)); s.reverse(); int decimal=Integer.parseInt(s.toString(),2); return decimal; } static int findGcd(int x, int y) { if (x == 0) //returns y is x==0 return y; //calling function that returns GCD return findGcd(y % x, x); } //function finds the LCM static int findLcm(int x, int y) { //returns the LCM return (x / findGcd(x, y)) * y; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t--!=0) { int n=sc.nextInt(); int r=sc.nextInt(); int b=sc.nextInt(); String s=""; int rt=r/(b+1); for(int i=b+1;i>=1;i--) { for(int j=0;j<r/i;j++) System.out.print('R'); r-=r/i; if(i>1) System.out.print('B'); } System.out.println(); } } } /*9 3 5 1 4 WBWWW BBBWB WWBBB 4 3 2 1 BWW BBW WBB WWB 2 3 2 2 WWW WWW 2 2 1 1 WW WB 5 9 5 9 WWWWWWWWW WBWBWBBBW WBBBWWBWW WBWBWBBBW WWWWWWWWW 1 1 1 1 B 1 1 1 1 W 1 2 1 1 WB 2 1 1 1 W B*/
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
f636b0649d9f0ea63c23778513bd19f5
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
//package spoj; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { //FileReader fr = new FileReader(new File("input.txt")); InputStreamReader sr = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(sr); StringBuilder sb = new StringBuilder(); int t = Integer.parseInt(reader.readLine()); while (t-- > 0) { String[] s = reader.readLine().split(" "); int n = Integer.parseInt(s[0]); int r = Integer.parseInt(s[1]); int b = Integer.parseInt(s[2]); StringBuilder res = new StringBuilder(); int x = r / (b + 1); int rem = r % (b + 1); for (int i = 0; i < n; i += x) { for (int j = 0; j < x; j++) { res.append('R'); } if (rem > 0) { res.append('R'); rem--; } if (b > 0) { res.append('B'); b--; } } sb.append(res.subSequence(0, n) + "\n"); } System.out.println(sb); } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
7a8487a5dd1056d0a26fde92738626b8
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
/*----------- ---------------* Author : Ryan Ranaut __Hope is a big word, never lose it__ ------------- --------------*/ import java.io.*; import java.util.*; public class Codeforces1 { static PrintWriter out = new PrintWriter(System.out); static final int mod = 1_000_000_007; static long min = (long) (-1e16); 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()); } int[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } 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; } } /*--------------------------------------------------------------------------*/ //Try seeing general case //Minimization Maximization - BS..... Connections - Graphs..... //Greedy not worthy - Try DP public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); int r = s.nextInt(); int b = s.nextInt(); out.println(find(n, r, b)); } out.close(); } public static String find(int n, int r, int b) { StringBuilder sb = new StringBuilder(); int gap = r/(b+1); int rem = r%(b+1); for(int i=0;i<b+1;i++) { for(int j=0;j<gap;j++) sb.append("R"); if(rem>0) { rem--; sb.append("R"); } if(i == b) break; sb.append("B"); } return sb.toString(); } /*----------------------------------End of the road--------------------------------------*/ static class DSU { int[] parent; int[] ranks; int[] groupSize; int size; public DSU(int n) { size = n; parent = new int[n];//0 based ranks = new int[n]; groupSize = new int[n];//Size of each component for (int i = 0; i < n; i++) { parent[i] = i; ranks[i] = 1; groupSize[i] = 1; } } public int find(int x)//Path Compression { if (parent[x] == x) return x; else return parent[x] = find(parent[x]); } public void union(int x, int y)//Union by rank { int x_rep = find(x); int y_rep = find(y); if (x_rep == y_rep) return; if (ranks[x_rep] < ranks[y_rep]) { parent[x_rep] = y_rep; groupSize[y_rep] += groupSize[x_rep]; } else if (ranks[x_rep] > ranks[y_rep]) { parent[y_rep] = x_rep; groupSize[x_rep] += groupSize[y_rep]; } else { parent[y_rep] = x_rep; ranks[x_rep]++; groupSize[x_rep] += groupSize[y_rep]; } size--;//Total connected components } } public static int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); } public static long gcd(long x, long y) { return y == 0L ? x : gcd(y, x % y); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static long pow(long a, long b) { if (b == 0L) return 1L; long tmp = 1; while (b > 1L) { if ((b & 1L) == 1) tmp *= a; a *= a; b >>= 1; } return (tmp * a); } public static long modPow(long a, long b, long mod) { if (b == 0L) return 1L; long tmp = 1; while (b > 1L) { if ((b & 1L) == 1L) tmp *= a; a *= a; a %= mod; tmp %= mod; b >>= 1; } return (tmp * a) % mod; } static long mul(long a, long b) { return a * b; } static long fact(int n) { long ans = 1; for (int i = 2; i <= n; i++) ans = mul(ans, i); return ans; } static long fastPow(long base, long exp) { if (exp == 0) return 1; long half = fastPow(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static void debug(int... a) { for (int x : a) out.print(x + " "); out.println(); } static void debug(long... a) { for (long x : a) out.print(x + " "); out.println(); } static void debugMatrix(int[][] a) { for (int[] x : a) out.println(Arrays.toString(x)); } static void debugMatrix(long[][] a) { for (long[] x : a) out.println(Arrays.toString(x)); } static void reverseArray(int[] a) { int n = a.length; int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void reverseArray(long[] a) { int n = a.length; long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void sort(int[] a) { ArrayList<Integer> ls = new ArrayList<>(); for (int x : a) ls.add(x); Collections.sort(ls); for (int i = 0; i < a.length; i++) a[i] = ls.get(i); } static void sort(long[] a) { ArrayList<Long> ls = new ArrayList<>(); for (long x : a) ls.add(x); Collections.sort(ls); for (int i = 0; i < a.length; i++) a[i] = ls.get(i); } static class Pair { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
d0c56a1a12a07f9b0bb132e5c5c91106
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.*; import java.util.*; /** * A simple template for competitive programming problems. */ public class Solution { //InputReader in = new InputReader("input.txt"); final InputReader in = new InputReader(System.in); final PrintWriter out = new PrintWriter(System.out); static final int mod = 1000000007; void solve() { int t = in.nextInt(); while(t-->0) { int n = in.nextInt(); int r = in.nextInt(); int b = in.nextInt(); int div = r/(b+1); int rem = r%(b+1); for(int i=0; i<b; i++) { for(int j=0; j<div; j++) { out.print("R"); } if(rem>0) { out.print("R"); rem--; } out.print("B"); } for(int i=0; i<div; i++) { out.print("R"); } out.println(); } } public static void main(final String[] args) throws FileNotFoundException { final Solution s = new Solution(); final Long t1 = System.currentTimeMillis(); s.solve(); System.err.println(System.currentTimeMillis() - t1 + " ms"); s.out.close(); } public Solution() throws FileNotFoundException { } private static class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; Random r = new Random(); InputReader(final InputStream stream) { this.stream = stream; } InputReader(final String fileName) { InputStream stream = null; try { stream = new FileInputStream(fileName); } catch (final FileNotFoundException e) { e.printStackTrace(); } this.stream = stream; } int[] nextArray(final int n) { final int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } int[][] nextMatrix(final int n, final int m) { final int[][] matrix = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) matrix[i][j] = nextInt(); return matrix; } String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); final StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); final StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } 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; } int[] randomArray(int n, int low, int up) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = low + r.nextInt(up - low + 1); } return arr; } double nextDouble() { return Double.parseDouble(nextString()); } private int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (final IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } private boolean isSpaceChar(final int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(final int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
f5c8af0bc9d33e472d095e9ceb2ca657
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class test { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { StringTokenizer tokenizer = new StringTokenizer(br.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); int r = Integer.parseInt(tokenizer.nextToken()); int b = Integer.parseInt(tokenizer.nextToken()); pw.println(solve(n, r, b)); } pw.flush(); pw.close(); br.close(); } public static String solve(int n, int r, int b) { StringBuilder result = new StringBuilder(); int parts = r / (b + 1); int rem = r % (b + 1); for (int i = 0; i < n; i++) { int count = 0; while (count < parts && r > 0) { result.append("R"); count++; r--; } if (rem > 0) { result.append("R"); r--; rem--; } if (b > 0) { result.append("B"); b--; } if (r == 0 && b == 0) break; } return result.toString(); } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
2fea392e4d12fef5e1dba7256b0482ae
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- > 0) { int n = scanner.nextInt(), r = scanner.nextInt(), b = scanner.nextInt(), cr = 0, cb = 0; double max = Math.ceil((double)r/(b + 1)); for (int i = 0, j = 0; i < n; i++) { if (j >= max && b > 0) { System.out.print('B'); j = 0; b--; max = Math.ceil((double)r/(b + 1)); } else { System.out.print('R'); j++; r--; } // System.out.print('R'); // cr++; // r--; // if (cr > (double)r/(b + 1) && b > 0 && i < n - 1) { // System.out.print('B'); // cb++; // b--; // cr = 0; // i++; // } } System.out.println(); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
fb059c13377b7e2ab2e4dad94c0072b6
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; import java.io.*; public class Home { public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); for (int i = 0; i < t; ++i) { int n = sc.nextInt(), r = sc.nextInt(), b = sc.nextInt(); int div = r/(b+1); int[] points = new int[b+1]; Arrays.fill(points, div); int rem = r % (div*(b+1)); for (int j = 0; j < rem; ++j) { points[j] += 1; } for (int j = 0; j < points.length-1; ++j) { System.out.print("R".repeat(points[j])); System.out.print("B"); } System.out.println("R".repeat(points[points.length-1])); } } } class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
de16a9c7ba3f542c1dbc11ddf30725fa
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; public class spidername { static Scanner sc = new Scanner(System.in); static int nxt() { int x = sc.nextInt(); return x; } static void solve() { int n = nxt(); int r = nxt(); int b = nxt(); String ans = ""; int m = (r) / (b + 1); int cnt = 0; int extra = r % (b + 1); for (int i = 0; i < r; i++) { ans += 'R'; cnt++; if (extra > 0) { if (cnt == m + 1 && b > 0) { cnt = 0; extra--; b--; ans += 'B'; } } else { if (cnt == m && b > 0) { cnt = 0; extra--; b--; ans += 'B'; } } } while (b > 0) { ans += 'B'; b--; } System.out.println(ans); } public static void main(String args[]) { int t = nxt(); while (t-- > 0) { solve(); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
08fe4a135338279ef0228df9d2b52035
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.util.*; import static java.util.Arrays.*; public class CodeforcesTemp { public static void main(String[] args) throws IOException { Reader scan = new Reader(); FastPrinter out = new FastPrinter(); int t =scan.nextInt(); for (int tc = 1; tc <= t; tc++) { int n=scan.nextInt(); int r=scan.nextInt(); int b= scan.nextInt(); int groups=r/(b+1); int remain=r%(b+1); StringBuilder sb=new StringBuilder(); for (int i = 0; i < n; i++) { for (int j = 0; j < groups; j++) { sb.append("R"); i++; } if(remain>0){ remain--; sb.append("R"); i++; } if(b>0){ b--; sb.append("B"); i++; } i--; } out.println(sb.toString()); } 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(int length) { long[] array = new long[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 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]); } } } 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 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;}} 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
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
5d559aba4e2b89ef5d3215f13f896370
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package CodeForces782Div2; import java.util.Scanner; /** * * @author jordy */ public class redvblue { static String ans; static int n, r, b; public static void main(String[] args) { Scanner reader = new Scanner(System.in); int t = reader.nextInt(); for (int i = 0; i < t; i++) { //System.out.println("--------------"); n = reader.nextInt(); r = reader.nextInt(); b = reader.nextInt(); // r/(b+1) per region // r%(b+1) of them will have 1 extra StringBuilder build = new StringBuilder(); int perregion = (r/(b+1)); int extra = r%(b+1); for (int j = 0; j < b+1; j++) { for (int k = 0; k < perregion; k++) { build.append("R"); } if(extra>0) { build.append("R"); extra--; } if(j!=b)build.append("B"); } System.out.println(build.toString()); } } } /* 1 35 30 5 1 50 40 10 1 11 7 4 3 7 4 3 6 5 1 19 13 6 5 98 74 24 83 59 24 89 88 1 100 76 24 82 58 24 1 83 59 24 */
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
270dd2406feb8e00bb1f7ee18edbbcc6
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; public class Group{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int r=sc.nextInt(); int b=sc.nextInt(); int idx=r/(b+1); int ext=r%(b+1); while(b>0){ for(int i=0; i<idx; i++){ System.out.print('R'); r--; } if(ext>0){ System.out.print('R'); ext--; r--; } System.out.print('B'); b--; } while(r-->0){ System.out.print('R'); } System.out.println(); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
96bd017ad974a0f52768150e26dea929
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
/***** ---> :) Vijender Srivastava (: <--- *****/ import java.util.*; import java.lang.*; // import java.lang.reflect.Array; import java.io.*; public class Main { static FastReader sc =new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=(long)32768; static StringBuilder sb = new StringBuilder(); /* start */ public static void main(String [] args) { // int testcases = 1; int testcases = i(); // calc(); while(testcases-->0) { solve(); } out.flush(); out.close(); } static void solve() { int n = i(); int a = i(); int b = i(); int cnt = a/(b+1),x=0,y=0; if(a%(b+1)!=0) cnt++; int i = 0; while(i<n) { if(x<cnt) { p("R"); x++; } else { p("B"); x = 0; a-=cnt; b--; if(b+1>0) cnt = a/(b+1); if(a%(b+1)!=0) cnt++; } i++; } pl(""); } /* end */ 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 p(Object o) { out.print(o); } static void pl(Object o) { out.println(o); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static long[] putL(long a[]) { long A[]=new long[a.length]; for(int i=0;i<a.length;i++) { A[i]=a[i]; } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x) ; y = y >> 1; x = (x * x); } return res; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long[] sort(long a[]) { ArrayList<Long> arr = new ArrayList<>(); for(long i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } static int[] sort(int a[]) { ArrayList<Integer> arr = new ArrayList<>(); for(Integer i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } //pair class private static class Pair implements Comparable<Pair> { long first, second; public Pair(long f, long s) { first = f; second = s; } @Override public int compareTo(Pair p) { if (first < p.first) return 1; else if (first > p.first) return -1; else { if (second > p.second) return 1; else if (second < p.second) return -1; else return 0; } } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
ced66d78456d255fd375c44137791b28
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class solution { static final Random random = new Random(); static void sort(int arr[]) { int n = arr.length; for(int i = 0; i < n; i++) { int j = random.nextInt(n),temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long[] readArrayL(int n) { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } static long factorial( long n ) { long res = 1 ; while(n>1) { res = (res*n)%998244353; n-- ; } return res ; } static long gcd(long a, long b) { if (a == 0) return b; if (b == 0) return a; if (a == b) return a; if (a > b) return gcd(a-b, b); return gcd(a, b-a); } static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));//PRINTLN METHOD static FastScanner sc = new FastScanner();//FASTSCANNER OBJECT public static void main(String[] args){ int t = sc.nextInt() ; while(t-- != 0 ) { long n = sc.nextLong(); long r = sc.nextInt(); long b = sc.nextInt(); long p = (r)/(b+1) ; long q = r%(b+1) ; if(b== 1 ) { for(int i = 1 ; i<= n/2 ; i++) { System.out.print("R"); }System.out.print("B"); for(int i = (int)n/2 +1 ; i < n ; i++) { System.out.print("R") ; }System.out.println(); } else { while(n>0) { for(int i = 0 ; i < p ; i++) { if(n>0) { System.out.print("R"); n-- ; } } if(q>0) { System.out.print("R") ; q-- ; n--; } if(n>0) { System.out.print("B");n-- ; } }System.out.println(); } } /////////////////////////////////////////////// out.flush();out.close(); }//END OF MAIN METHOD }//END OF MAIN CLASS
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
3681eb9bc2c72e792af3a7a36a10d81f
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; public class A { static final Reader in = new Reader(); static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int t = in.nextInt(); for (int tc = 1; tc <= t; tc++) { solve(); out.println(); } out.close(); } static void solve() { int n = in.nextInt(), r = in.nextInt(), b = in.nextInt(); String ans = ""; boolean poss = true; int repeats = r; while (poss && repeats > 0) { int curR = r, curB = b; StringBuilder s = new StringBuilder(); s.append("R".repeat(repeats)); curR -= repeats; while (true) { s.append("B"); curB -= 1; int subtract = Math.min(curR, repeats); s.append("R".repeat(subtract)); curR -= subtract; if (curR <= 0 || curB <= 0) { break; } } s.append("B".repeat(Math.min(1, curB))); curB -= Math.min(1, curB); s.append("R".repeat(curR)); if (curB > 0) { for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'B') { int subtract = Math.min(repeats - 1, curB); s.insert(i, "B".repeat(subtract)); curB -= subtract; i += subtract; } if (curB <= 0) { break; } } } int lastB = s.lastIndexOf("B"); int trailingR = Math.max(n - lastB - 1, 0); if (trailingR > repeats) { poss = false; } else { ans = s.toString(); } repeats--; } out.print(ans); } static class Reader { private final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } Reader(InputStream in) { try { din = new DataInputStream(in); } catch (Exception e) { throw new RuntimeException(); } buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } String next() { int c; while ((c = read()) != -1 && (c == ' ' || c == '\n' || c == '\r')) ; StringBuilder s = new StringBuilder(); while (c != -1) { if (c == ' ' || c == '\n' || c == '\r') break; s.append((char) c); c = read(); } return s.toString(); } String nextLine() { int c; while ((c = read()) != -1 && (c == ' ' || c == '\n' || c == '\r')) ; StringBuilder s = new StringBuilder(); while (c != -1) { if (c == '\n' || c == '\r') break; s.append((char) c); c = read(); } return s.toString(); } int nextInt() { 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; } int[] nextInts(int n) { int[] ar = new int[n]; for (int i = 0; i < n; ++i) ar[i] = nextInt(); return ar; } long nextLong() { 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; } long[] nextLongs(int n) { long[] ar = new long[n]; for (int i = 0; i < n; ++i) ar[i] = nextLong(); return ar; } double nextDouble() { 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; } void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } byte read() { try { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } catch (IOException e) { throw new RuntimeException(); } } void close() throws IOException { din.close(); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
a4f6c07817eab39e9f2418ecf203b073
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; public class forces { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int r = sc.nextInt(); int b = sc.nextInt(); int x = r / (b + 1); int y = r % (b + 1); String s = ""; int c = b + 1; while (c-- > 0) { for (int i = 0; i < x; i++) { s = s + "R"; } if (y > 0) { s = s + 'R'; y--; } if (b > 0) { b--; s = s + 'B'; } } System.out.println(s); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
b49ea045b2b4580f5f3510494063af85
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class RedVBlue { public static void main(String[] args) { IO io = new IO(); int numtimes = io.nextInt(); for (int i = 0; i < numtimes; i++) { int n = io.nextInt(); int r = io.nextInt(); int b = io.nextInt(); int x = 0; double max = (double) (r) / (b + 1); String s = ""; double count = max; while (r > 0) { while ((int) count > 0 && r > 0) { s += "R"; count -= 1; r--; } if (b > 0) { s += "B"; b--; } count += max; } System.out.println(s); } } static class IO extends PrintWriter { public BufferedReader reader; public StringTokenizer tokens; public IO(InputStream in, OutputStream out) { super(out); reader = new BufferedReader(new InputStreamReader(in)); } public IO() { super(System.out); reader = new BufferedReader(new InputStreamReader(System.in)); } public IO(String inputFileName, String outputFileName) throws FileNotFoundException { super(outputFileName); reader = new BufferedReader(new FileReader(inputFileName)); } public String next() { while (tokens == null || !tokens.hasMoreTokens()) { try { tokens = new StringTokenizer(reader.readLine()); } catch (IOException e) { System.out.println("BIGPROBLEM"); throw new RuntimeException(e); } } return tokens.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
c3f20edade2401aaf24c3f38b8c51c80
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while (t-->0){ int n=sc.nextInt(),r=sc.nextInt(),b=sc.nextInt(); StringBuilder sb=new StringBuilder(); int s_r=r; int y=b; int s_b=b+1; int x; if(r%s_b==0){ x=r/s_b; }else{ x=(r/s_b)+1; } StringBuilder st=new StringBuilder(); for (int i = 1; i <=x ; i++) { st.append('R'); } String tt=st.toString(); sb.append(tt); s_r-=x; s_b--; while (y-->0){ sb.append('B'); int c; if(s_r%s_b==0){ c=s_r/s_b; }else{ c=(s_r/s_b)+1; } StringBuilder temp=new StringBuilder(); for (int i = 0; i <c ; i++) { temp.append('R'); } sb.append(temp); s_r-=c; s_b--; } System.out.println(sb); } } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output
PASSED
f4fa0435316037bb33fbf449a0f8e81c
train_110.jsonl
1650206100
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
256 megabytes
import java.util.*; public class Main { static Scanner in = new Scanner(System.in); public static void main(String[] args) { int T = in.nextInt(); while (T-- > 0) { solve(); } } private static void solve() { int n, r, b; n = in.nextInt(); r = in.nextInt(); b = in.nextInt(); if(r > b){ int little = r % (b+1); int time_cap = r / (b+1); int cap = 0; int b_cnt = 0; for (int i = 0; i < r; i++){ System.out.print("R"); cap++; int cab_next = time_cap; if(little > 0){ cab_next++; } if(cap >= cab_next && b_cnt < b){ b_cnt++; System.out.print("B"); cap=0; little--; } } while (b_cnt++ < b){ System.out.print("B"); } }else { int little = b % (r+1); int time_cap = b / (r+1); int cap = 0; int r_cnt = 0; for (int i = 0; i < b; i++){ System.out.print("B"); cap++; int cab_next = time_cap; if(little > 0){ cab_next++; } if(cap >= cab_next && r_cnt < r){ r_cnt++; System.out.print("R"); cap=0; little--; } } while (r_cnt++ < r){ System.out.print("R"); } } System.out.println(); } }
Java
["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"]
1 second
["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"]
NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
Java 11
standard input
[ "constructive algorithms", "greedy", "implementation", "math" ]
7cec27879b443ada552a6474c4e45c30
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b &lt; r \leq n$$$, $$$r+b=n$$$).
1,000
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
standard output