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
fbad7a88109f29452ee68514a055b184
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
//some updates in import stuff import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; //key points learned //max space ever that could be alloted in a program to pass in cf //int[][] prefixSum = new int[201][200_005]; -> not a single array more!!! //never allocate memory again again to such bigg array, it will give memory exceeded for sure //believe in your fucking solution and keep improving it!!! (sometimes) public class Main{ static int mod = (int) (Math.pow(10, 9)+7); static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 }; static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 }; static final int[] dx9 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, dy9 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 }; static final double eps = 1e-10; static List<Integer> primeNumbers = new ArrayList<>(); public static void main(String[] args) { MyScanner sc = new MyScanner(); //pretty important for sure - out = new PrintWriter(new BufferedOutputStream(System.out)); //dope shit output for sure //code here int test = sc.nextInt(); while(test--> 0){ int n= sc.nextInt(); int k = sc.nextInt(); char[] inp = sc.nextLine().toCharArray(); int[] ans = new int[n]; int total = k; for(int i= 0; i < n; i++){ if(k == 0) break; if(inp[i] == '1'){ if(total % 2== 0){ }else{ ans[i] = 1; k--; } }else{ if(total % 2== 0){ ans[i] = 1; k--; }else{ } } if(k == 0) break; } ans[n-1] += k; //after this make the proper array too for sure for once, boi //this would help you a lot for sure in future int[] finalans = new int[n]; for(int i = 0; i < n; i++){ int flips = total - ans[i]; int given = inp[i] - '0'; finalans[i] = given; if(flips %2 == 1){ finalans[i] = finalans[i] == 1 ? 0 : 1; } } for(int i : finalans){ out.print(i); } out.println(); for(int i : ans){ out.print(i + " "); } out.println(); } out.close(); } //new stuff to learn (whenever this is need for them, then only) //Lazy Segment Trees //Persistent Segment Trees //Square Root Decomposition //Geometry & Convex Hull //High Level DP -- yk yk //String Matching Algorithms //Heavy light Decomposition //Updation Required //Fenwick Tree - both are done (sum) //Segment Tree - both are done (min, max, sum) //-----CURRENTLY PRESENT-------// //Graph //DSU //powerMODe //power //Segment Tree (work on this one) //Prime Sieve //Count Divisors //Next Permutation //Get NCR //isVowel //Sort (int) //Sort (long) //Binomial Coefficient //Pair //Triplet //lcm (int & long) //gcd (int & long) //gcd (for binomial coefficient) //swap (int & char) //reverse //primeExponentCounts //Fast input and output //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //GRAPH (basic structure) public static class Graph{ public int V; public ArrayList<ArrayList<Integer>> edges; //2 -> [0,1,2] (current) Graph(int V){ this.V = V; edges = new ArrayList<>(V+1); for(int i= 0; i <= V; i++){ edges.add(new ArrayList<>()); } } public void addEdge(int from , int to){ edges.get(from).add(to); edges.get(to).add(from); } } //DSU (path and rank optimised) public static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; Arrays.fill(rank, 1); Arrays.fill(parent,-1); this.n = n; } public int find(int curr){ if(parent[curr] == -1) return curr; //path compression optimisation return parent[curr] = find(parent[curr]); } public void union(int a, int b){ int s1 = find(a); int s2 = find(b); if(s1 != s2){ //union by size if(rank[s1] < rank[s2]){ parent[s1] = s2; rank[s2] += rank[s1]; }else{ parent[s2] = s1; rank[s1] += rank[s2]; } } } } //with mod public static long powerMOD(long x, long y) { long res = 1L; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0){ x %= mod; res %= mod; res = (res * x)%mod; } // y must be even now y = y >> 1; // y = y/2 x%= mod; x = (x * x)%mod; // Change x to x^2 } return res%mod; } //without mod public static long power(long x, long y) { long res = 1L; 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/ x = (x * x); } return res; } public static class segmentTree{ //so let's make a constructor function for this bad boi for sure!!! public long[] arr; public long[] tree; //COMPLEXITY (normal segment tree, stuff) //build -> O(n) //query -> O(logn) //update -> O(logn) //update-range -> O(n) (worst case) //simple iteration and stuff for sure public segmentTree(long[] arr){ int n = arr.length; this.arr = new long[n]; for(int i= 0; i < n; i++){ this.arr[i] = arr[i]; } tree = new long[4*n + 1]; } //pretty basic idea if you read the code once //first make child node once //then form the parent node using them public void buildTree(int s, int e, int index){ if(s == e){ tree[index] = arr[s]; return; } //recursive case int mid = (s + e)/2; buildTree(s, mid, 2 * index); buildTree(mid + 1, e, 2*index + 1); //the condition we want from children be like this tree[index] = min(tree[2 * index], tree[2 * index + 1]); return; } //definitely index based 0 query!!! //only int index = 1!! //baaki everything is simple as fuck public long query(int s, int e, int qs , int qe, int index){ //complete overlap if(s >= qs && e <= qe){ return tree[index]; } //no overlap if(qe < s || qs > e){ return Long.MAX_VALUE; } //partial overlap int mid = (s + e)/2; long left = query( s, mid , qs, qe, 2*index); long right = query( mid + 1, e, qs, qe, 2*index + 1); return min(left, right); } //gonna do range updates for sure now!! //let's do this bois!!! (solve this problem for sure) public void updateRange(int s, int e, int l, int r, long increment, int index){ //out of bounds if(l > e || r < s){ return; } //leaf node if(s == e){ tree[index] += increment; return; //behnchoda return tera baap krvayege? } //recursive case int mid = (s + e)/2; updateRange(s, mid, l, r, increment, 2 * index); updateRange(mid + 1, e, l, r, increment, 2 * index + 1); tree[index] = min(tree[2 * index], tree[2 * index + 1]); } } public static class SegmentTreeLazy{ //so let's make a constructor function for this bad boi for sure!!! public long[] arr; public long[] tree; public long[] lazy; //COMPLEXITY (normal segment tree, stuff) //build -> O(n) //query-range -> O(logn) //lazy update-range -> O(logn) (imp) //simple iteration and stuff for sure public SegmentTreeLazy(long[] arr){ int n = arr.length; this.arr = new long[n]; for(int i= 0; i < n; i++){ this.arr[i] = arr[i]; } tree = new long[4*n + 1]; lazy = new long[100000]; //pretty big for no inconvenience (no?) } //pretty basic idea if you read the code once //first make child node once //then form the parent node using them public void buildTree(int s, int e, int index){ if(s == e){ tree[index] = arr[s]; return; } //recursive case int mid = (s + e)/2; buildTree(s, mid, 2 * index); buildTree(mid + 1, e, 2*index + 1); //the condition we want from children be like this tree[index] = min(tree[2 * index], tree[2 * index + 1]); return; } //definitely index based 0 query!!! //only int index = 1!! //baaki everything is simple as fuck public long queryLazy(int s, int e, int qs, int qe, int index){ //before going down resolve if it exist if(lazy[index] != 0){ tree[index] += lazy[index]; //non leaf node if(s != e){ lazy[2*index] += lazy[index]; lazy[2*index + 1] += lazy[index]; } lazy[index] = 0; //clear the lazy value at current node for sure } //no overlap if(s > qe || e < qs){ return Long.MAX_VALUE; } //complete overlap if(s >= qs && e <= qe){ return tree[index]; } //partial overlap int mid = (s + e)/2; long left = queryLazy(s, mid, qs, qe, 2 * index); long right = queryLazy(mid + 1, e, qs, qe, 2 * index + 1); return Math.min(left, right); } //update range in O(logn) -- using lazy array public void updateRangeLazy(int s, int e, int l, int r, int inc, int index){ //before going down resolve if it exist if(lazy[index] != 0){ tree[index] += lazy[index]; //non leaf node if(s != e){ lazy[2*index] += lazy[index]; lazy[2*index + 1] += lazy[index]; } lazy[index] = 0; //clear the lazy value at current node for sure } //no overlap if(s > r || l > e){ return; } //another case if(l <= s && e <= r){ tree[index] += inc; //create a new lazy value for children node if(s != e){ lazy[2*index] += inc; lazy[2*index + 1] += inc; } return; } //recursive case int mid = (s + e)/2; updateRangeLazy(s, mid, l, r, inc, 2*index); updateRangeLazy(mid + 1, e, l, r, inc, 2*index + 1); //update the tree index tree[index] = Math.min(tree[2*index], tree[2*index + 1]); return; } } //prime sieve public static void primeSieve(int n){ BitSet bitset = new BitSet(n+1); for(long i = 0; i < n ; i++){ if (i == 0 || i == 1) { bitset.set((int) i); continue; } if(bitset.get((int) i)) continue; primeNumbers.add((int)i); for(long j = i; j <= n ; j+= i) bitset.set((int)j); } } //number of divisors public static int countDivisors(long number){ if(number == 1) return 1; List<Integer> primeFactors = new ArrayList<>(); int index = 0; long curr = primeNumbers.get(index); while(curr * curr <= number){ while(number % curr == 0){ number = number/curr; primeFactors.add((int) curr); } index++; curr = primeNumbers.get(index); } if(number != 1) primeFactors.add((int) number); int current = primeFactors.get(0); int totalDivisors = 1; int currentCount = 2; for (int i = 1; i < primeFactors.size(); i++) { if (primeFactors.get(i) == current) { currentCount++; } else { totalDivisors *= currentCount; currentCount = 2; current = primeFactors.get(i); } } totalDivisors *= currentCount; return totalDivisors; } //primeExponentCounts public static int primeExponentsCount(int n) { if (n <= 1) return 0; int sqrt = (int) Math.sqrt(n); int remainingNumber = n; int result = 0; for (int i = 2; i <= sqrt; i++) { while (remainingNumber % i == 0) { result++; remainingNumber /= i; } } //in case of prime numbers this would happen if (remainingNumber > 1) { result++; } return result; } //now adding next permutation function to java hehe public static boolean next_permutation(int[] p) { for (int a = p.length - 2; a >= 0; --a) if (p[a] < p[a + 1]) for (int b = p.length - 1;; --b) if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } return false; } //finding the value of NCR in O(RlogN) time and O(1) space public static long getNcR(int n, int r) { long p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r > 0) { p *= n; k *= r; long m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } return p; } //is vowel function public static boolean isVowel(char c) { return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U'); } //to sort the array with better method public static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } //sort long public 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); } //for calculating binomialCoeff public static int binomialCoeff(int n, int k) { int C[] = new int[k + 1]; // nC0 is 1 C[0] = 1; for (int i = 1; i <= n; i++) { // Compute next row of pascal // triangle using the previous row for (int j = Math.min(i, k); j > 0; j--) C[j] = C[j] + C[j - 1]; } return C[k]; } //Pair with int int public static class Pair{ public int a; public int b; public int hashCode; Pair(int a , int b){ this.a = a; this.b = b; this.hashCode = Objects.hash(a, b); } @Override public String toString(){ return a + " -> " + b; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair that = (Pair) o; return a == that.a && b == that.b; } @Override public int hashCode() { return this.hashCode; } } //Triplet with int int int public static class Triplet{ public int a; public int b; public int c; Triplet(int a , int b, int c){ this.a = a; this.b = b; this.c = c; } @Override public String toString(){ return a + " -> " + b; } } //Shortcut function public static long lcm(long a , long b){ return a * (b/gcd(a,b)); } //let's make one for calculating lcm basically public static int lcm(int a , int b){ return (a * b)/gcd(a,b); } //int version for gcd public static int gcd(int a, int b){ if(b == 0) return a; return gcd(b , a%b); } //long version for gcd public static long gcd(long a, long b){ if(b == 0) return a; return gcd(b , a%b); } //for ncr calculator(ignore this code) public static long __gcd(long n1, long n2) { long gcd = 1; for (int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) { gcd = i; } } return gcd; } //swapping two elements in an array public static void swap(int[] arr, int left , int right){ int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //for char array public static void swap(char[] arr, int left , int right){ char temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //reversing an array public static void reverse(int[] arr){ int left = 0; int right = arr.length-1; while(left <= right){ swap(arr, left,right); left++; right--; } } public static long expo(long a, long b, long mod) { long res = 1; while (b > 0) { if ((b & 1) == 1L) res = (res * a) % mod; //think about this one for a second a = (a * a) % mod; b = b >> 1; } return res; } //SOME EXTRA DOPE FUNCTIONS public static long mminvprime(long a, long b) { return expo(a, b - 2, b); } public static long mod_add(long a, long b, long m) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } public static long mod_sub(long a, long b, long m) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } public static long mod_mul(long a, long b, long m) { a = a % m; b = b % m; return (((a * b) % m) + m) % m; } public static long mod_div(long a, long b, long m) { a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m; } //O(n) every single time remember that public static long nCr(long N, long K , long mod){ long upper = 1L; long lower = 1L; long lowerr = 1L; for(long i = 1; i <= N; i++){ upper = mod_mul(upper, i, mod); } for(long i = 1; i <= K; i++){ lower = mod_mul(lower, i, mod); } for(long i = 1; i <= (N - K); i++){ lowerr = mod_mul(lowerr, i, mod); } // out.println(upper + " " + lower + " " + lowerr); long answer = mod_mul(lower, lowerr, mod); answer = mod_div(upper, answer, mod); return answer; } // long[] fact = new long[2 * n + 1]; // long[] ifact = new long[2 * n + 1]; // fact[0] = 1; // ifact[0] = 1; // for (long i = 1; i <= 2 * n; i++) // { // fact[(int)i] = mod_mul(fact[(int)i - 1], i, mod); // ifact[(int)i] = mminvprime(fact[(int)i], mod); // } //ifact is basically inverse factorial in here!!!!!(imp) public static long combination(long n, long r, long m, long[] fact, long[] ifact) { long val1 = fact[(int)n]; long val2 = ifact[(int)(n - r)]; long val3 = ifact[(int)r]; return (((val1 * val2) % m) * val3) % m; } //-----------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
["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 11
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
1bc20618d792d59cbfb5466505f5f040
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 q2 { public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // public static long mod = 1000000007; public static void solve() throws Exception { String[] parts = br.readLine().split(" "); int n = Integer.parseInt(parts[0]); int k = Integer.parseInt(parts[1]); int ok = k; int[] arr = new int[n],oarr = new int[n]; String str = br.readLine(); for(int i = 0;i < n;i++){ arr[i] = str.charAt(i) - '0'; } if(k == 0){ StringBuilder sb = new StringBuilder(); for(int val : arr) sb.append(val); sb.append("\n"); for(int i = 0;i < n;i++) sb.append("0 "); System.out.println(sb); return; } int[] ans = new int[n]; if(k % 2 == 0){ for(int i = 0;i < n && k > 0;i++){ if(arr[i] == 0){ ans[i]++; k--; } } if(k % 2 == 0) ans[0] += k; else { ans[0] += k - 1; ans[n - 1] += 1; } }else{ for(int i = 0;i < n && k > 0;i++){ if(arr[i] == 1){ ans[i]++; k--; } } if(k % 2 == 0) ans[0] += k; else { ans[0] += k - 1; ans[n - 1] += 1; } } StringBuilder sb = new StringBuilder(); for(int i = 0;i < n;i++){ int val = arr[i]; if((ok - ans[i]) % 2 == 1) val ^= 1; sb.append(val); } sb.append("\n"); for(int i = 0;i < n;i++) sb.append(ans[i]).append(" "); System.out.println(sb); } public static void main(String[] args) throws Exception { int tests = Integer.parseInt(br.readLine()); for (int test = 1; test <= tests; test++) { solve(); } } // public static ArrayList<Integer> primes; // public static void seive(int n){ // primes = new ArrayList<>(); // boolean[] arr = new boolean[n + 1]; // Arrays.fill(arr,true); // // for(int i = 2;i * i <= n;i++){ // if(arr[i]) { // for (int j = i * i; j <= n; j += i) { // arr[j] = false; // } // } // } // for(int i = 2;i <= n;i++) if(arr[i]) primes.add(i); // } // public static void sort(int[] arr){ // ArrayList<Integer> temp = new ArrayList<>(); // for(int val : arr) temp.add(val); // // Collections.sort(temp); // // for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i); // } // public static void sort(long[] arr){ // ArrayList<Long> temp = new ArrayList<>(); // for(long val : arr) temp.add(val); // // Collections.sort(temp); // // for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i); // } // // public static long power(long a,long b,long mod){ // if(b == 0) return 1; // // long p = power(a,b / 2,mod); // p = (p * p) % mod; // // if(b % 2 == 1) return (p * a) % mod; // return p; // } // public static long modDivide(long a,long b,long mod){ // return ((a % mod) * (power(b,mod - 2,mod) % mod)) % mod; // } // // public static int GCD(int a,int b){ // return b == 0 ? a : GCD(b,a % b); // } // public static long GCD(long a,long b){ // return b == 0 ? a : GCD(b,a % b); // } // // public static int LCM(int a,int b){ // return a * b / GCD(a,b); // } // public static long LCM(long a,long b){ // return a * b / GCD(a,b); // } }
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 11
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
9940517611ba2a41b3fdd1b9a3e590ea
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 Solution { public static void main(String[] args) throws IOException { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); char[] arr = sc.next().toCharArray(); boolean[] temp = new boolean[n]; for (int i = 0; i < temp.length; i++) { temp[i] = arr[i] == '0' ? false : true; } int[] res = new int[n]; int count = 0; for (int i = 0; i < arr.length; i++) { if (count == k) break; if ((arr[i] == '1' && k % 2 == 1) || (arr[i] == '0' && k % 2 == 0)) { res[i]++; count++; } } res[n - 1] += k - count; for (int i = 0; i < res.length; i++) { if ((k - res[i]) % 2 == 1) temp[i] = !temp[i]; } for (Boolean bool : temp) { pw.print(bool ? "1" : "0"); } pw.println(); pw.println(toString(res)); } pw.close(); } static int f(int num) { int res = 0; while (num > 0) { if (num % 2 == 0) num /= 2; else { num = (num - 1) / 2; res++; } } return res; } static HashMap Hash(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static boolean isPrime(int n) { if (n <= 1) return false; for (int i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } public static long combination(long n, long r) { return factorial(n) / (factorial(n - r) * factorial(r)); } static long factorial(Long n) { if (n == 0) return 1; return n * factorial(n - 1); } static boolean isPalindrome(char[] str, int i, int j) { // While there are characters to compare while (i < j) { // If there is a mismatch if (str[i] != str[j]) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } public static double log2(int N) { double result = (Math.log(N) / Math.log(2)); return result; } public static double log4(int N) { double result = (Math.log(N) / Math.log(4)); return result; } public static int setBit(int mask, int idx) { return mask | (1 << idx); } public static int clearBit(int mask, int idx) { return mask ^ (1 << idx); } public static boolean checkBit(int mask, int idx) { return (mask & (1 << idx)) != 0; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } public static String toString(int[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(ArrayList arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.size(); i++) { sb.append(arr.get(i) + " "); } return sb.toString().trim(); } public static String toString(int[][] arr) { StringBuilder sb = new StringBuilder(); for (int[] i : arr) { sb.append(toString(i) + "\n"); } return sb.toString(); } public static String toString(boolean[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(Integer[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(String[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(char[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
Java
["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 11
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
adac90e0168c5d571c7597b7a2e8f342
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.lang.*; public class practice { static FastInput scn; static PrintWriter out; final static int MOD = (int) (1e9 + 7); final static int MAX = Integer.MAX_VALUE; final static int MIN = Integer.MIN_VALUE; // MAIN public static void main(String[] args) throws IOException { scn = new FastInput(); out = new PrintWriter(System.out); int t = 1; t = scn.nextInt(); while (t-- > 0) { solve(); } out.flush(); } private static void solve() throws IOException { int n = scn.nextInt(), k = scn.nextInt(); char[] arr = scn.nextCharArray(n); int[] ans = new int[n]; if (k == 0) { out.println(String.valueOf(arr)); printIntArray(ans); return; } if (k % 2 != 0) { if (arr[0] == '1') { for (int i = 1; i < n; i++) { arr[i] = arr[i] == '1' ? '0' : '1'; } ans[0] = 1; } else { if (n > 1) { int i = 1; for (; i < n; i++) { if (arr[i] == '1') break; arr[i] = (arr[i] == '0') ? '1' : '0'; } if (i == n) { i--; arr[i] = '0'; } ans[i] = 1; arr[0] = '1'; // swap remaining i++; for (; i < n; i++) { arr[i] = (arr[i] == '0') ? '1' : '0'; } } else { ans[0] = 1; } } k--; } for (int i = 0; i < n - 1; i++) { if (arr[i] == '0') { ans[i] += 1; // next zero int z = i + 1; for (; z < n; z++) { if (arr[z] == '0') break; } arr[i] = '1'; if (z == n) { z--; arr[z] = '0'; } else { arr[z] = '1'; } ans[z] += 1; k -= 2; } if (k == 0) break; } ans[n - 1] += k; out.println(String.valueOf(arr)); printIntArray(ans); } // CLASSES static class Pair implements Comparable<Pair> { int first, second; Pair(int first, int second) { this.first = first; this.second = second; } Pair(Pair o) { this.first = o.first; this.second = o.second; } public int compareTo(Pair o) { return this.first - o.first; } } // CHECK IF STRING IS NUMBER private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // FASTER SORT private static void fastSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void fastSortReverse(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void fastSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void fastSortReverse(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // QUICK MATHS private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static long power(long a, long b) { if (b == 0) return 1L; long ans = power(a, b / 2); ans *= ans; if ((b & 1) == 1) ans *= a; return ans; } private static int mod_power(int a, int b) { if (b == 0) return 1; int temp = mod_power(a, b / 2); temp %= MOD; temp = (int) ((1L * temp * temp) % MOD); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % MOD); return temp; } private static int multiply(int a, int b) { return (int) ((((1L * a) % MOD) * ((1L * b) % MOD)) % MOD); } private static boolean isPrime(int n) { for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } // STRING FUNCTIONS private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // LONGEST INCREASING AND NON-DECREASING SUBSEQUENCE private static int LIS(int arr[]) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) { list.set(idx, arr[i]); } else { list.add(arr[i]); } } return list.size(); } private static int find1(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) { list.set(idx, arr[i]); } else { list.add(arr[i]); } } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // DISJOINT SET UNION private static int find(int x, int[] parent) { if (parent[x] == x) { return x; } parent[x] = find(parent[x], parent); return parent[x]; } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) { return true; } else if (rank[lx] > rank[ly]) { parent[ly] = lx; } else if (rank[lx] < rank[ly]) { parent[lx] = ly; } else { parent[lx] = ly; rank[ly]++; } return false; } // TRIE static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } public void insert(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) { curr.children[ch - 'a'] = new Node(); } curr = curr.children[ch - 'a']; } curr.isEnd = true; } public boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) { return false; } curr = curr.children[ch - 'a']; } return curr.isEnd; } } // INPUT static class FastInput { BufferedReader br; StringTokenizer st; FastInput() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(nextLine()); return st.nextToken(); } long nextLong() throws IOException { return Long.parseLong(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } char nextCharacter() throws IOException { return next().charAt(0); } String nextLine() throws IOException { return br.readLine().trim(); } int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } int[][] next2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = nextIntArray(m); return arr; } long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } long[][] next2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = nextLongArray(m); return arr; } List<Integer> nextIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(nextInt()); return list; } List<Long> nextLongList(int n) throws IOException { List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(nextLong()); return list; } char[] nextCharArray(int n) throws IOException { return next().toCharArray(); } char[][] next2DCharArray(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = nextCharArray(m); return mat; } } // OUTPUT private static void printIntList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLongList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIntArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIntArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIntArray(arr[i]); } private static void printLongArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLongArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLongArray(arr[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 11
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
abd7e975d778d25db6491efe83b23110
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.*; public class B { public static void printArr(int arr[], int n) { StringBuilder s = new StringBuilder(); for (long no : arr) { s.append(no).append(" "); } System.out.println(s.toString()); } 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 initk = k; String s = fs.next(); int i=0; int[] arr = new int[n]; int sum = 0; StringBuilder finalAns = new StringBuilder(); while (i<n && k>0) { if (initk%2 ==0) { if (s.charAt(i) == '0') { arr[i] = 1; sum++; k--; } } else { if (s.charAt(i) == '1') { arr[i] = 1; sum++; k--; } } finalAns.append("1"); i++; } while (i<n) { char c = s.charAt(i); if (initk %2 == 0) finalAns.append(c); else finalAns.append(c == '0' ? "1" : "0"); i++; } if (k!=0) { if (k%2 == 0) { arr[n-1] += k; } else { arr[n-1] += k; finalAns = new StringBuilder(finalAns.substring(0, n - 1) + "0"); } } // int j=0; // StringBuilder finalStr = new StringBuilder(); // for (; j < i; j++) { // finalStr.append("1"); // } // for (; j < n; j++) { // char c = s.charAt(j); // if (initk %2 == 0) // finalStr.append(c); // else // finalStr.append(c == '0' ? "1" : "0"); // } System.out.println(finalAns); printArr(arr, n); } } 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 11
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
0a378c8a91acb79fd1dadb1671c217b0
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 mod = 1000000007; static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); int t = sc.nextInt(); while( t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); char arr[] = sc.next().toCharArray(); int ans[] = new int[n]; int K = k; for( int i = 0 ;i < n; i++) { if( arr[i] == '0') { // means k-i should be odd if( K %2 == 0 && k > 0) { ans[i]++; // out.println(i); k--; // arr[i] = '1'; } if( K%2 != 0) { // arr[i] = '1'; } } else { /// k-i should be even if( K%2 != 0 && k > 0) { ans[i]++; k--; } else if( k % 2 != 0) { // arr[i] = '0'; } } } if( k > 0) { ans[n-1]+=k; for( int i = 0 ;i < n ;i++) { if( (K-ans[i]) % 2 != 0) { // out.println(i); arr[i] = change(arr[i]); } } for( char x : arr) { out.print(x); } out.println(); for( int i = 0 ; i < n ;i++) { out.print(ans[i] + " "); } out.println(); } else { for( int i = 0 ;i < n ;i++) { if( (K-ans[i]) % 2 != 0) { // out.println(i); arr[i] = change(arr[i]); } } for( char x : arr) { out.print(x); } out.println(); for( int i = 0 ; i < n ;i++) { out.print(ans[i] + " "); } out.println(); } } out.flush(); } private static char change(char c) { if( c == '1') { return '0'; } return '1'; } /* * time for a change */ public static boolean ifpowof2(long n ) { return ((n&(n-1)) == 0); } static boolean isprime(long x ) { if( x== 2) { return true; } if( x%2 == 0) { return false; } for( long i = 3 ;i*i <= x ;i+=2) { if( x%i == 0) { return false; } } return true; } 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; } public static int[] nextLargerElement(int[] arr, int n) { Stack<Integer> stack = new Stack<>(); int rtrn[] = new int[n]; rtrn[n-1] = -1; stack.push( n-1); for( int i = n-2 ;i >= 0 ; i--){ int temp = arr[i]; int lol = -1; while( !stack.isEmpty() && arr[stack.peek()] <= temp){ if(arr[stack.peek()] == temp ) { lol = stack.peek(); } stack.pop(); } if( stack.isEmpty()){ if( lol != -1) { rtrn[i] = lol; } else { rtrn[i] = -1; } } else{ rtrn[i] = stack.peek(); } stack.push( i); } return rtrn; } static void mysort(int[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); int loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static void mySort(long[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); long loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static long rightmostsetbit(long n) { return n&-n; } static long leftmostsetbit(long n) { long k = (long)(Math.log(n) / Math.log(2)); return k; } static HashMap<Long,Long> primefactor( long n){ HashMap<Long ,Long> hm = new HashMap<>(); long temp = 0; while( n%2 == 0) { temp++; n/=2; } if( temp!= 0) { hm.put( 2L, temp); } long c = (long)Math.sqrt(n); for( long i = 3 ; i <= c ; i+=2) { temp = 0; while( n% i == 0) { temp++; n/=i; } if( temp!= 0) { hm.put( i, temp); } } if( n!= 1) { hm.put( n , 1L); } return hm; } static ArrayList<Long> allfactors(long abs) { HashMap<Long,Integer> hm = new HashMap<>(); ArrayList<Long> rtrn = new ArrayList<>(); for( long i = 2 ;i*i <= abs; i++) { if( abs% i == 0) { hm.put( i , 0); hm.put(abs/i, 0); } } for( long x : hm.keySet()) { rtrn.add(x); } if( abs != 0) { rtrn.add(abs); } return rtrn; } 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; } 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 11
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
7b3ad7157005fdf4cfa5b6a957126402
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; public class B { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub int testCases = Integer.parseInt(sc.nextLine()); for (int i = 1; i <= testCases; ++i) { solve(i); } } private static void solve(int t) { String [] row = sc.nextLine().split(" "); int k = Integer.parseInt(row[1]); String s = sc.nextLine(); int [] arr = new int [s.length()]; int [] val = new int [s.length()]; int last = s.length() - 1; int flips = k; for (int i = 0; i < s.length() - 1; ++i) { val[i] = s.charAt(i) - '0'; //flips = s.length(); if (val[i] % 2 != flips % 2) val[i] = 1; else if (k > 0) { val[i] = 1; ++arr[i]; --k; }else { val[i] = 0; } } arr[last] = k; val[last] = s.charAt(last) - '0'; flips -= k; if (flips % 2 == 1) val[last] = 1 - val[last]; print(val , arr); } public static void print(int [] arr, int [] val) { StringBuilder sb = new StringBuilder(); for (int num : arr) sb.append(num); System.out.println(sb); sb.setLength(0); for (int i = 0; i < val.length; ++i) { sb.append(val[i]); if (i != val.length - 1) sb.append(' '); } 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 11
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
c76465b265c89c2694c11c6f5d444b84
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 gotoJapan { public static void main(String[] args) throws java.lang.Exception { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solution solver = new Solution(); boolean isTest = true; int tC = isTest ? Integer.parseInt(in.next()) : 1; for (int i = 1; i <= tC; i++) solver.solve(in, out, i); out.close(); } /* ............................................................. */ static class Solution { InputReader in; PrintWriter out; public void solve(InputReader in, PrintWriter out, int test) { this.in = in; this.out = out; int n=ni(); int k=ni(); char x[]=n(); char p=x[n-1]; int a[]=new int[n]; int s=k; for(int i=0;i<n;i++) { if(s<=0) { if(k%2!=0) { if(x[i]=='1')x[i]='0'; else x[i]='1'; } continue; } if(k%2==0) { if(x[i]=='0') { a[i]=1; x[i]='1'; s--; } } else { if(x[i]=='1') { a[i]=1; s--; } else { x[i]='1'; } } //pn(s); } if(s>0) { a[n-1]=a[n-1]+s; x[n-1]=p; if((k%2!=0&&a[n-1]%2==0)||(k%2==0&&a[n-1]%2!=0)) { if(x[n-1]=='1')x[n-1]='0'; else x[n-1]='1'; } } for(int i=0;i<n;i++) { out.print(x[i]); } out.println(); for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); } class Pair { int x; int y; long w; Pair(int x, int y, long w) { this.x = x; this.y = y; this.w = w; } } char[] n() { return in.next().toCharArray(); } int ni() { return in.nextInt(); } long nl() { return in.nextLong(); } long[] nal(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } void pn(long zx) { out.println(zx); } void pn(String sz) { out.println(sz); } void pn(double dx) { out.println(dx); } void pn(long ar[]) { for (int i = 0; i < ar.length; i++) out.print(ar[i] + " "); out.println(); } void pn(String ar[]) { for (int i = 0; i < ar.length; i++) out.println(ar[i]); } } /* ......................Just Input............................. */ 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()); } } /* ......................Just Input............................. */ }
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 11
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
e79fac3a28b23b4e9f88c7016d88ba3a
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; public class Main { Object solve(int n, int k, String bs) { int[] b = new int[n]; int[] c = new int[n]; int k_ = k; for (int i = 0; i < n; i++) { b[i] = bs.charAt(i) == '0' ? 0 : 1; b[i] ^= (k & 1); if (i < n-1) { if (b[i] == 0 && k_ > 0) { b[i] = 1; c[i] = 1; k_--; } } else { b[i] = b[i] ^ (k_ & 1); c[i] = k_; } System.out.print(b[i]); } System.out.println(); System.out.println(toString(c)); return null; } Object readInputAndSolve(Scanner in) { int n = in.nextInt(); int k = in.nextInt(); in.nextLine(); String b = in.nextLine(); return solve(n, k, b); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int nTests = in.nextInt(); for (int iTest = 0; iTest < nTests; iTest++) { new Main().readInputAndSolve(in); } } public static String toString(int[] items) { StringBuilder sb = new StringBuilder(); for (int e : items) { sb.append(e); sb.append(" "); } return sb.toString(); } }
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 11
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
9673b144e7e61d6b033a4126bab8c13e
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.*; // Created by @thesupremeone on 4/17/22 public class B { void solve() { int ts = getInt(); for (int t = 0; t < ts; t++) { int n = getInt(); int k = getInt(); int kb = k; String s = getLine(); int[] arr = new int[n]; int[] ops = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.charAt(i)=='1' ? 1 : 0; if(k%2==1){ arr[i] = 1-arr[i]; } } for (int i = 0; i < n; i++) { if(k<=0) break; if(arr[i]==0){ arr[i] = 1; k--; ops[i]++; } } if(k>0){ int done = kb-k; arr[n-1] = s.charAt(n-1)=='1'?1:0; if((done-ops[n-1])%2==1){ arr[n-1] = 1-arr[n-1]; } ops[n-1] += k; } StringBuilder builder = new StringBuilder(); for(int e : arr){ builder.append(e); } println(builder.toString()); for(int o : ops){ print(o+" "); } println(""); } } public static void main(String[] args) throws Exception { if (isOnlineJudge()) { in = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedWriter(new OutputStreamWriter(System.out)); new B().solve(); out.flush(); } else { localJudge = new Thread(); in = new BufferedReader(new FileReader("input.txt")); out = new BufferedWriter(new FileWriter("output.txt")); localJudge.start(); new B().solve(); out.flush(); localJudge.suspend(); } } static boolean isOnlineJudge() { try { return System.getProperty("ONLINE_JUDGE") != null || System.getProperty("LOCAL") == null; } catch (Exception e) { return true; } } // Fast Input & Output static Thread localJudge = null; static BufferedReader in; static StringTokenizer st; static BufferedWriter out; static String getLine() { try { return in.readLine(); } catch (Exception ignored) { return ""; } } static String getToken() { if (st == null || !st.hasMoreTokens()) st = new StringTokenizer(getLine()); return st.nextToken(); } static int getInt() { return Integer.parseInt(getToken()); } static long getLong() { return Long.parseLong(getToken()); } static void print(Object s) { try { out.write(String.valueOf(s)); } catch (Exception ignored) { } } static void println(Object s) { try { out.write(String.valueOf(s)); out.newLine(); } catch (Exception ignored) { } } }
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 11
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
394c2d8de3297a1dfd9d2001accec1ef
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.*; //need to be careful about negative sum resulting negative mod. public class Solution { static long mod = 1000000007; static long inv(long a, long b) {return 1 < a ? b - inv(b % a, a) * b / a : 1;} static long mi(long a) {return inv(a, mod);} static InputReader sc = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { //setUp("input.txt", "output.txt"); //setUp("maxcross.in", "maxcross.out"); int T = 1; T = sc.nextInt(); while(T-- > 0){ int n = sc.nextInt(); int k = sc.nextInt(); char[] word = sc.next().toCharArray(); int[] ans = new int[n]; StringBuilder str = new StringBuilder(); int x = k; int flipped = 0; for(int i = 0 ; i < n; i++){ char ch = word[i]; if(flipped % 2 != 0){ if(ch == '0') ch = '1'; else ch = '0'; } boolean even = x % 2 == 0; if(x == 0){ str.append(ch); }else if(i == n - 1){ ans[i] = x; str.append(ch); }else{ if(ch == '0' && even){ ans[i] = 1; x--; flipped++; }else if(ch == '1' && !even){ ans[i] = 1; x--; flipped++; } str.append('1'); } } out.println(str.toString()); for(int i = 0 ; i < ans.length; i++){ out.print(ans[i] + " "); } out.println(""); } out.close(); } public static void solve(){ } static void setUp(String input, String output) throws IOException { sc = new InputReader(new FileInputStream(input)); out = new PrintWriter(new BufferedWriter(new FileWriter(output))); } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return str; } int[] readArray(int N){ int[] arr = new int[N]; for(int i = 0 ; i < N; i++){ arr[i] = sc.nextInt(); } return arr; } long[] readLongArray(int N){ long[] arr = new long[N]; for(int i = 0 ; i < N; i++){ arr[i] = sc.nextLong(); } return arr; } } public static void shuffle(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } } public static void shuffle(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static class Pair<K,V> { K key; V val; public Pair(K key, V val){ this.key = key; this.val = val; } } public static int gcd(int a, int b){ if(b == 0) return a; return gcd(b, a % b); } }
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 11
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
94e3c758e042aad12dd1742b49213404
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.StringTokenizer; public class B { String filename = null; InputReader sc; void solve() { int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); int[] flips = new int[n]; int remaining = k; for (int i = 0; i < n; i++) { if (remaining > 0 && (s.charAt(i) == '1') != (k % 2 == 0)) { flips[i] = 1; remaining--; } } flips[n - 1] += remaining; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append((s.charAt(i) == '1') == ((k - flips[i]) % 2 == 0) ? '1' : '0'); } System.out.println(sb); sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(flips[i]).append(" "); } System.out.println(sb); } public void run() throws FileNotFoundException { if (filename == null) { sc = new InputReader(System.in); } else { sc = new InputReader(new FileInputStream(filename)); } int nTests = sc.nextInt(); for (int test = 0; test < nTests; test++) { solve(); } } public static void main(String[] args) { B sol = new B(); try { sol.run(); } catch (FileNotFoundException e) { e.printStackTrace(); } } 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 float nextFloat() { return Float.parseFloat(next()); } public double nextDouble() { return Float.parseFloat(next()); } public long nextLong() { return Long.parseLong(next()); } public int[] nextArray(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 11
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
9fa93ed1ec943f2987aadea14ab5767c
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 Scanner obj=new Scanner(System.in); public static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) { int len=obj.nextInt(); while(len--!=0) { int n=obj.nextInt(); int k=obj.nextInt(); String s=obj.next(); char[] c=s.toCharArray(); int[] ans=new int[n]; int op=k; int i=0; for(i=0;i<n && op>0 ;i++) { if(c[i]=='1' ) { if(k%2!=0) { ans[i]+=1; op--; } } if(c[i]=='0') { if(k%2==0) { ans[i]+=1; op--; } c[i]='1'; } } if(i==n) { if(op%2!=0) c[n-1]='0'; ans[n-1]+=op; } else { if(k%2!=0) for(;i<n;i++) { if(c[i]=='1')c[i]='0'; else c[i]='1'; } } out.println(c); for(i=0;i<n;i++)out.print(ans[i]+" "); out.println(); } out.flush(); } }
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 11
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
6ce3af3854670b54a376894b266760b5
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 { private static int MOD = 1000000007; private static long Inf = (long) 1E15; public static void main(String[] args) throws Exception { InputStream inputStream = System.in; //InputStream inputStream = new FileInputStream(new File("/tmp/input.txt")); OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); int t = in.nextInt(); for (int i = 0; i < t; i++) { Task solver = new Task(); solver.solve(in, out); } out.close(); } static class Task { private static boolean touch(int bit, int k) { if (bit == 0) { return k % 2 == 0; } else { return k % 2 == 1; } } private static int newVal(int init, int touched, int k) { int flips = k - touched; if (flips % 2 == 0) { return init; } else { return 1 - init; } } public void solve(Scanner in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); int origK = k; char[] ar = in.next().toCharArray(); int[] bits = new int[n]; for (int i = 0; i < n; i++) { bits[i] = ar[i] - '0'; } int[] counts = new int[n]; int touched = 0; for (int i = 0; i < n && k > 0; i++) { int cBit = 0; if (touched % 2 == 0) { cBit = bits[i]; } else { cBit = 1 - bits[i]; } if (touch(cBit, k)) { touched++; counts[i] = 1; k--; } } counts[n-1] += k; for (int i = 0; i < n; i++) { out.print(newVal(bits[i], counts[i], origK)); } out.println(); for (int i = 0; i < n; i++) { out.print(counts[i]); out.print(" "); } 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 11
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
5fb0eeed898773952b72393405aafcb3
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 Bit_Flipping { static FastScanner fs; static FastWriter fw; static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null; private static final int[][] kdir = new int[][]{{-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}}; private static final int[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; private static final int iMax = Integer.MAX_VALUE, iMin = Integer.MIN_VALUE; private static final long lMax = Long.MAX_VALUE, lMin = Long.MIN_VALUE; private static final int mod1 = (int) (1e9 + 7); private static final int mod2 = 998244353; public static void main(String[] args) throws IOException { fs = new FastScanner(); fw = new FastWriter(); int t = 1; t = fs.nextInt(); for (int tt = 1; tt <= t; tt++) { // fw.out.print("Case #" + tt + ": "); solve(); } fw.out.close(); } private static void solve() { int n = fs.nextInt(), k = fs.nextInt(); char[] arr = fs.nextLine().toCharArray(); int[] ans = new int[n]; int cnt = 0; for (int i = 0; i < n; i++) { if (cnt == k) break; if (i == n - 1) ans[i] = (k - cnt); else { if (arr[i] == '1') { if ((k & 1) == 1) { ans[i] = 1; cnt++; } } else { if ((k & 1) == 0) { ans[i] = 1; cnt++; } } } } StringBuilder ans_str = new StringBuilder(); for (int i = 0; i < n; i++) { int num = arr[i] - '0'; if (((k - ans[i]) & 1) == 1) ans_str.append((num ^ 1)); else ans_str.append(num); } fw.out.println(ans_str); for (int x : ans) fw.out.print(x + " "); fw.out.println(); } private static class UnionFind { private final int[] parent; private final int[] rank; UnionFind(int n) { parent = new int[n + 5]; rank = new int[n + 5]; for (int i = 0; i <= n; i++) { parent[i] = i; rank[i] = 0; } } private int find(int i) { if (parent[i] == i) return i; return parent[i] = find(parent[i]); } private void union(int a, int b) { a = find(a); b = find(b); if (a != b) { if (rank[a] < rank[b]) { int temp = a; a = b; b = temp; } parent[b] = a; if (rank[a] == rank[b]) rank[a]++; } } } private static class SCC { private final int n; private final List<List<Integer>> adjList; SCC(int _n, List<List<Integer>> _adjList) { n = _n; adjList = _adjList; } private List<List<Integer>> getSCC() { List<List<Integer>> ans = new ArrayList<>(); Stack<Integer> stack = new Stack<>(); boolean[] vis = new boolean[n]; for (int i = 0; i < n; i++) { if (!vis[i]) dfs(i, adjList, vis, stack); } vis = new boolean[n]; List<List<Integer>> rev_adjList = rev_graph(n, adjList); while (!stack.isEmpty()) { int curr = stack.pop(); if (!vis[curr]) { List<Integer> scc_list = new ArrayList<>(); dfs2(curr, rev_adjList, vis, scc_list); ans.add(scc_list); } } return ans; } private void dfs(int curr, List<List<Integer>> adjList, boolean[] vis, Stack<Integer> stack) { vis[curr] = true; for (int x : adjList.get(curr)) { if (!vis[x]) dfs(x, adjList, vis, stack); } stack.add(curr); } private void dfs2(int curr, List<List<Integer>> adjList, boolean[] vis, List<Integer> scc_list) { vis[curr] = true; scc_list.add(curr); for (int x : adjList.get(curr)) { if (!vis[x]) dfs2(x, adjList, vis, scc_list); } } } private static List<List<Integer>> rev_graph(int n, List<List<Integer>> adjList) { List<List<Integer>> ans = new ArrayList<>(); for (int i = 0; i < n; i++) ans.add(new ArrayList<>()); for (int i = 0; i < n; i++) { for (int x : adjList.get(i)) { ans.get(x).add(i); } } return ans; } private static class Calc_nCr { private final long[] fact; private final long[] invfact; private final int p; Calc_nCr(int n, int prime) { fact = new long[n + 5]; invfact = new long[n + 5]; p = prime; fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = (i * fact[i - 1]) % p; } invfact[n] = pow_with_mod(fact[n], p - 2, p); for (int i = n - 1; i >= 0; i--) { invfact[i] = (invfact[i + 1] * (i + 1)) % p; } } private long nCr(int n, int r) { if (r > n || n < 0 || r < 0) return 0; return (((fact[n] * invfact[r]) % p) * invfact[n - r]) % p; } } private static int random_between_two_numbers(int l, int r) { Random ran = new Random(); return ran.nextInt(r - l) + l; } private static long gcd(long a, long b) { return (b == 0 ? a : gcd(b, a % b)); } private static long lcm(long a, long b) { return ((a * b) / gcd(a, b)); } private static long pow(long a, long b) { long result = 1; while (b > 0) { if ((b & 1L) == 1) { result = (result * a); } a = (a * a); b >>= 1; } return result; } private static long pow_with_mod(long a, long b, int mod) { long result = 1; while (b > 0) { if ((b & 1L) == 1) { result = (result * a) % mod; } a = (a * a) % mod; b >>= 1; } return result; } private static long ceilDiv(long a, long b) { return ((a + b - 1) / b); } private static long getMin(long... args) { long min = lMax; for (long arg : args) min = Math.min(min, arg); return min; } private static long getMax(long... args) { long max = lMin; for (long arg : args) max = Math.max(max, arg); return max; } private static boolean isPalindrome(String s, int l, int r) { int i = l, j = r; while (j - i >= 1) { if (s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } private static List<Integer> primes(int n) { boolean[] primeArr = new boolean[n + 5]; Arrays.fill(primeArr, true); for (int i = 2; (i * i) <= n; i++) { if (primeArr[i]) { for (int j = i * i; j <= n; j += i) { primeArr[j] = false; } } } List<Integer> primeList = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (primeArr[i]) primeList.add(i); } return primeList; } private static int noOfSetBits(long x) { int cnt = 0; while (x != 0) { x = x & (x - 1); cnt++; } return cnt; } private static int sumOfDigits(long num) { int cnt = 0; while (num > 0) { cnt += (num % 10); num /= 10; } return cnt; } private static int noOfDigits(long num) { int cnt = 0; while (num > 0) { cnt++; num /= 10; } return cnt; } private static boolean isPerfectSquare(long num) { long sqrt = (long) Math.sqrt(num); return ((sqrt * sqrt) == num); } private static class Pair<U, V> { private final U first; private final V second; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return first.equals(pair.first) && second.equals(pair.second); } @Override public int hashCode() { return Objects.hash(first, second); } @Override public String toString() { return "(" + first + ", " + second + ")"; } private Pair(U ff, V ss) { this.first = ff; this.second = ss; } } private static void randomizeIntArr(int[] arr, int n) { Random r = new Random(); for (int i = (n - 1); i > 0; i--) { int j = r.nextInt(i + 1); swapInIntArr(arr, i, j); } } private static void randomizeLongArr(long[] arr, int n) { Random r = new Random(); for (int i = (n - 1); i > 0; i--) { int j = r.nextInt(i + 1); swapInLongArr(arr, i, j); } } private static void swapInIntArr(int[] arr, int a, int b) { int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } private static void swapInLongArr(long[] arr, int a, int b) { long temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } private static int[] readIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = fs.nextInt(); return arr; } private static long[] readLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = fs.nextLong(); return arr; } private static List<Integer> readIntList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(fs.nextInt()); return list; } private static List<Long> readLongList(int n) { List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(fs.nextLong()); return list; } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws IOException { if (checkOnlineJudge) this.br = new BufferedReader(new FileReader("src/input.txt")); else this.br = new BufferedReader(new InputStreamReader(System.in)); this.st = new StringTokenizer(""); } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException err) { err.printStackTrace(); } } return st.nextToken(); } public String nextLine() { if (st.hasMoreTokens()) { return st.nextToken("").trim(); } try { return br.readLine().trim(); } catch (IOException err) { err.printStackTrace(); } return ""; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } private static class FastWriter { PrintWriter out; FastWriter() throws IOException { if (checkOnlineJudge) out = new PrintWriter(new FileWriter("src/output.txt")); else out = new PrintWriter(System.out); } } }
Java
["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 11
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
7d42a7102c753b6114dfb02fcfed28e0
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
//<———My cp———— //https://takeuforward.org/interview-experience/strivers-cp-sheet/?utm_source=youtube&utm_medium=striver&utm_campaign=yt_video import java.util.*; import java.io.*; public class Solution{ static PrintWriter pw = new PrintWriter(System.out); static FastReader fr = new FastReader(System.in); private static long MOD = 1_000_000_007; public static void main(String[] args) throws Exception{ int t = fr.nextInt(); while(t-->0){ int n = fr.nextInt(); int k = fr.nextInt(); String s = fr.next(); int usedOp = 0; int i=0; int[] op = new int[n]; for(i = 0;i<n-1&& usedOp<k ;i++){ char curr = s.charAt(i); if(usedOp%2==1){ if(curr=='0'){ curr='1'; }else{ curr='0'; } } pw.print(1); if(curr=='1'&&(k-usedOp)%2==1){ usedOp++; op[i]=1; }else if(curr=='0'&&(k-usedOp)%2==0){ usedOp++; op[i]=1; } } for(;i<n;i++){ char curr = s.charAt(i); if(usedOp%2==1){ if(curr=='1'){ curr='0'; }else{ curr='1'; } } op[i]=(k-usedOp); pw.print(curr); } pw.println(); for(int r=0;r<op.length;r++){ pw.print(op[r]+" "); } pw.println(); } pw.close(); } static class Pair{ int height; int width; public Pair(int height,int width){ this.height = height; this.width = width; } @Override public String toString() { return "height: "+height+" width: "+width; } } public static long opNeeded(long c,long[] vals){ long tempResult = 0; for(int j = 0;j<vals.length;j++){ tempResult=tempResult+Math.abs((long)(vals[j]-Math.pow(c,j))); } if(tempResult<0){ tempResult=Long.MAX_VALUE; } return tempResult; } static int isPerfectSquare(int vals){ int lastPow=1; while(lastPow*lastPow<vals){ lastPow++; } if(lastPow*lastPow==vals){ return lastPow*lastPow; }else{ return -1; } } public static int[] sort(int[] vals){ ArrayList<Integer> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static long[] sort(long[] vals){ ArrayList<Long> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static void reverseArray(long[] vals){ int startIndex = 0; int endIndex = vals.length-1; while(startIndex<=endIndex){ long temp = vals[startIndex]; vals[startIndex] = vals[endIndex]; vals[endIndex] = temp; startIndex++; endIndex--; } } public static void reverseArray(int[] vals){ int startIndex = 0; int endIndex = vals.length-1; while(startIndex<=endIndex){ int temp = vals[startIndex]; vals[startIndex] = vals[endIndex]; vals[endIndex] = temp; startIndex++; endIndex--; } } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } public static int GCD(int numA, int numB){ if(numA==0){ return numB; }else if(numB==0){ return numA; }else{ if(numA>numB){ return GCD(numA%numB,numB); }else{ return GCD(numA,numB%numA); } } } }
Java
["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 11
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
bb82ce465b4f4ff2190ee69d51b70e87
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 New { public static void main (String[] args) throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int testCases = Integer.parseInt(br.readLine()); while(testCases-->0){ String input[]=br.readLine().split(" "); int n=Integer.parseInt(input[0]); int k=Integer.parseInt(input[1]); char arr[]=br.readLine().toCharArray(); int temp[]=new int[n]; int xor=0; for(int i=0;i<n;i++) { if(xor==1) { if(arr[i]=='0') arr[i]='1'; else arr[i]='0'; } if(k==0) continue; if(arr[i]=='1') { if(k%2!=0) { temp[i]++; k--; xor^=1; } }else { if(k%2==0) { temp[i]++; k--; xor^=1; } arr[i]='1'; } } if(k%2!=0) { if(arr[n-1]=='0') arr[n-1]='1'; else arr[n-1]='0'; } temp[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(temp[i]+" "); } out.println(); } 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 11
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
9f5d289d1e54f0ebc705ccc94c41bf1d
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.HashSet; import java.util.Random; import java.util.Set; import java.util.StringTokenizer; /* */ public class B { public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int T=fs.nextInt(); // int T=1; for (int tt=0; tt<T; tt++) { int n = fs.nextInt(), k = fs.nextInt(); int K = k; char[] s = fs.next().toCharArray(); int[] used = new int[n]; Arrays.fill(used, k); int last = -1; for (int i = 0; i < n; i++) { if (k > 0 && used[i] % 2 == 1 && s[i] == '1') { used[i]--; last = i; k--; } else if (k > 0 && used[i] % 2 == 0 && s[i] == '0') { used[i]--; last = i; k--; } if (used[i] % 2 == 1) { if (s[i] == '0') s[i] = '1'; else s[i] = '0'; } } if (k % 2 == 1) { s[n - 1] = '0'; used[n - 1] -= k; } else { used[n - 1] -= k; } for (int i = 0; i < n; i++) out.print(s[i]); out.println(); for (int i = 0; i < n; i++) out.print(K - used[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[] b) { ArrayList<Integer> l=new ArrayList<>(); for (int i:b) l.add(i); Collections.sort(l); for (int i=0; i<b.length; i++) b[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 11
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
3fb33abf347bedc34127c4e1ef535002
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; public class BitFlipping { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pr=new PrintWriter(System.out); int t=Integer.parseInt(br.readLine()); while(t!=0){ solve(br,pr); t--; } pr.flush(); pr.close(); } public static void solve(BufferedReader br,PrintWriter pr) throws IOException{ String[] temp=br.readLine().split(" "); int n=Integer.parseInt(temp[0]); int k=Integer.parseInt(temp[1]); String s=br.readLine(); char[] bits=s.toCharArray(); int[] res=new int[n]; if(k%2==1){ for(int i=0;i<n;i++){ if(bits[i]=='0'){ bits[i]='1'; } else{ bits[i]='0'; } } } for(int i=0;i<n;i++){ if(k<=0){ break; } if(bits[i]=='0'){ res[i]=1; k--; bits[i]='1'; } } if(k!=0){ res[n-1]+=k; if(k%2!=0){ bits[n-1]=(bits[n-1]=='0'?'1':'0'); } } pr.println(new String(bits)); for(int i:res){ pr.print(i+" "); } pr.print('\n'); } }
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 11
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
d1bf45046892fd4ec1a2ec59a5a7b745
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 com.sun.security.jgss.GSSUtil; import java.io.*; import java.util.*; public class Main { private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private FastWriter wr; private Reader rd; public final int MOD = 1000000007; /************************************************** FAST INPUT IMPLEMENTATION *********************************************/ class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader( new InputStreamReader(System.in)); } public Reader(String path) throws FileNotFoundException { br = new BufferedReader( new InputStreamReader(new FileInputStream(path))); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public int ni() throws IOException { return rd.nextInt(); } public long nl() throws IOException { return rd.nextLong(); } public void yOrn(boolean flag) { if (flag) { wr.println("YES"); } else { wr.println("NO"); } } char nc() throws IOException { return rd.next().charAt(0); } public String ns() throws IOException { return rd.nextLine(); } public Double nd() throws IOException { return rd.nextDouble(); } public int[] nai(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } public long[] nal(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nl(); } return arr; } /************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/ public static class FastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter() { out = null; } public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastWriter write(char c) { return write((byte) c); } public FastWriter write(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if (x == Integer.MIN_VALUE) { return write((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if (x < 0) { write('-'); x = -x; } x += Math.pow(10, -precision) / 2; // if(x < 0){ x = 0; } write((long) x).write("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; write((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastWriter writeln(char c) { return write(c).writeln(); } public FastWriter writeln(int x) { return write(x).writeln(); } public FastWriter writeln(long x) { return write(x).writeln(); } public FastWriter writeln(double x, int precision) { return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for (int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for (long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte) '\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for (char[] line : map) write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c) { return writeln(c); } public FastWriter println(int x) { return writeln(x); } public FastWriter println(long x) { return writeln(x); } public FastWriter println(double x, int precision) { return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } /********************************************************* USEFUL CODE **************************************************/ boolean[] SAPrimeGenerator(int n) { // TC-N*LOG(LOG N) //Create Prime Marking Array and fill it with true value boolean[] primeMarker = new boolean[n + 1]; Arrays.fill(primeMarker, true); primeMarker[0] = false; primeMarker[1] = false; for (int i = 2; i <= n; i++) { if (primeMarker[i]) { // we start from 2*i because i*1 must be prime for (int j = 2 * i; j <= n; j += i) { primeMarker[j] = false; } } } return primeMarker; } private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } class Pair<F, S> { private F first; private S second; Pair(F first, S second) { this.first = first; this.second = second; } public F getFirst() { return first; } public S getSecond() { return second; } @Override public String toString() { return "Pair{" + "first=" + first + ", second=" + second + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<F, S> pair = (Pair<F, S>) o; return first == pair.first && second == pair.second; } @Override public int hashCode() { return Objects.hash(first, second); } } class PairThree<F, S, X> { private F first; private S second; private X third; PairThree(F first, S second, X third) { this.first = first; this.second = second; this.third = third; } public F getFirst() { return first; } public void setFirst(F first) { this.first = first; } public S getSecond() { return second; } public void setSecond(S second) { this.second = second; } public X getThird() { return third; } public void setThird(X third) { this.third = third; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PairThree<?, ?, ?> pair = (PairThree<?, ?, ?>) o; return first.equals(pair.first) && second.equals(pair.second) && third.equals(pair.third); } @Override public int hashCode() { return Objects.hash(first, second, third); } } public static void main(String[] args) throws IOException { new Main().run(); } public void run() throws IOException { if (oj) { rd = new Reader(); wr = new FastWriter(System.out); } else { File input = new File("input.txt"); File output = new File("output.txt"); if (input.exists() && output.exists()) { rd = new Reader(input.getPath()); wr = new FastWriter(output.getPath()); } else { rd = new Reader(); wr = new FastWriter(System.out); oj = true; } } long s = System.currentTimeMillis(); solve(); wr.flush(); tr(System.currentTimeMillis() - s + "ms"); } /*************************************************************************************************************************** *********************************************************** MAIN CODE ****************************************************** ****************************************************************************************************************************/ boolean[] sieve; public void solve() throws IOException { int t = 1; t = ni(); while (t-- > 0) { go(); } } /********************************************************* MAIN LOGIC HERE ****************************************************/ long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int lower_bound(int array[], int key) { int low = 0, high = array.length; int mid; while (low < high) { mid = low + (high - low) / 2; if (key <= array[mid]) { high = mid; } else { low = mid + 1; } } if (low < array.length && array[low] < key) { low++; } return low; } boolean isPowerOfTwo(int n) { int counter = 0; while (n != 0) { if ((n & 1) == 1) { counter++; } n = n >> 1; } return counter <= 1; } void swap(int[] arr,int i,int j){ int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } void printList(ArrayList<Integer> al){ for(int i=0;i<al.size();i++){ wr.print(al.get(i)+" "); } wr.println(""); } public void go() throws IOException { int n=ni(); int k=ni(); int temp_k=k; String st=ns(); int[] arr=new int[n]; Arrays.fill(arr,0); int val=k%2==0?1:0; int val2=k%2==0?0:1; StringBuilder ans=new StringBuilder(); for(int i=0;i<n;i++){ char ch=st.charAt(i); if(ch=='1'){ if(k-val2>=0){ k-=val2; arr[i]+=val2; } }else { if(k-val>=0){ k-=val; arr[i]+=val; } } } arr[n-1]+=k; for(int i=0;i<n;i++){ char ch=st.charAt(i); if((temp_k-arr[i])%2==0){ ans.append(ch); }else { ans.append((ch=='0'?'1':'0')); } } wr.println(ans.toString()); wr.println(arr); } }
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 11
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
29d5010fdf523a33cde412bb37a11682
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 class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } public static class pair { long num; int val; pair(long num,int val) { this.num=num; this.val=val; } } public static void main(String[] args) { FastReader obj=new FastReader(); PrintWriter out=new PrintWriter(System.out); int len=obj.nextInt(); while(len--!=0) { int n=obj.nextInt(); int k=obj.nextInt(); int temp=k; char[] ch=obj.next().toCharArray(); int[] ans=new int[n]; for(int i=0;i<n;i++) { if(temp==0)break; if(k%2==0) { if(ch[i]=='0'){ ans[i]=1; temp--; } } else{ if(ch[i]=='1') { ans[i]=1; temp--; } } } ans[n-1]+=temp; for(int i=0;i<n;i++) { int tut=k-ans[i]; if(tut%2!=0){ if(ch[i]=='0')ch[i]='1'; else ch[i]='0'; } } out.println(ch); for(int i=0;i<n;i++)out.print(ans[i]+" "); out.println(); } out.flush(); } }
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 11
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
8149c77aba271207d5e45529bb63a09c
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
//Utilities import java.io.*; import java.util.*; public class a { static int t; static int n, k; static char[] ch, f; static int[] res; public static void main(String[] args) throws IOException { t = in.iscan(); while (t-- > 0) { n = in.iscan(); k = in.iscan(); ch = in.sscan().toCharArray(); f = new char[n]; int original_k = k; for (int i = 0; i < n; i++) { f[i] = ch[i]; if (k % 2 == 1) { if (f[i] == '0') f[i] = '1'; else f[i] = '0'; } } res = new int[n]; int idx = 0; while (idx < n) { if (k == 0) break; int cur = Integer.parseInt(""+ch[idx]); boolean flag = cur == 1 && original_k % 2 == 0 || cur == 0 && original_k % 2 == 1; if (flag) { f[idx] = '1'; res[idx] = 0; } else { res[idx] = 1; f[idx] = '1'; k--; } idx++; } if (k > 0) { res[n-1] += k; if (k % 2 == 1) { if (f[n-1] == '0') f[n-1] = '1'; else f[n-1] = '0'; } } out.println(f); for (int i = 0; i < n; i++) { out.print(res[i] + " "); } out.println(); } 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
["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 11
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
71d19ca439b0c4c9533a249d3ad85c00
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.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); BBitFlipping solver = new BBitFlipping(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class BBitFlipping { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(), k = in.nextInt(); int[] arr = toIntArray(in.next()); ArrayList<Integer> z = new ArrayList<>(); ArrayList<Integer> o = new ArrayList<>(); for (int i = 0; i < n; i++) { if (arr[i] == 1) o.add(i); else z.add(i); } int[] ans = new int[n]; if (k % 2 == 1) { int i = 0; while (k-- > 0 && i < o.size()) { ans[o.get(i++)] = 1; } ans[n - 1] += k + 1; for (int j = 0; j < n; j++) { if (arr[j] == 1) { if (ans[j] % 2 == 0) arr[j] = 0; } else { if (ans[j] % 2 == 0) arr[j] = 1; } } } else { int i = 0; while (k-- > 0 && i < z.size()) { ans[z.get(i++)] = 1; } ans[n - 1] += k + 1; for (int j = 0; j < n; j++) { if (arr[j] == 1) { if (ans[j] % 2 == 1) arr[j] = 0; } else { if (ans[j] % 2 == 1) arr[j] = 1; } } } for (int a : arr) out.print(a); out.println(); out.println(ans); } int[] toIntArray(String s) { int[] A = new int[s.length()]; for (int i = 0; i < A.length; i++) { A[i] = s.charAt(i) - '0'; } return A; } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void println(int[] array) { print(array); writer.println(); } public void println() { writer.println(); } public void close() { writer.close(); } public void print(int i) { writer.print(i); } } static class InputReader { BufferedReader reader; 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()); } } }
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 11
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
fa0def6c90caab7998887ddb3ab42e1d
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 { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int t = Integer.parseInt(br.readLine()); for (int caseNum = 0; caseNum < t; caseNum++) { String[] data = br.readLine().split(" "); int n = Integer.parseInt(data[0]); int k = Integer.parseInt(data[1]); int N = n; int K = k; String input = br.readLine(); boolean[] bits = new boolean[n]; int[] numInversions = new int[n]; for (int i = 0; i < n; i++) { bits[i] = input.charAt(i) == '1'; } int index = 0; boolean needsInverted = false; while (k > 0 && index < n) { if ((bits[index] ^ needsInverted) == ((k%2)==0)) index++; else { // INVERT ARRAY EXCEPT FOR BITS bits[index] = !bits[index]; numInversions[index++]++; needsInverted = !needsInverted; k--; } } if ((k & 1) == 1) { bits[N-1] = !bits[N-1]; numInversions[N-1]++; needsInverted = !needsInverted; k--; } numInversions[N-1] += k; if (needsInverted) { for (int i = 0; i < N; i++) { bits[i] = !bits[i]; } } for (int i = 0; i < N; i++) { sb.append(bits[i] ? '1' : '0'); } sb.append(System.lineSeparator()).append(numInversions[0]); for (int i = 1; i < N; i++) { sb.append(' ').append(numInversions[i]); } sb.append(System.lineSeparator()); } System.out.print(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 11
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
9b3a35273b5aab76a0c597a1df519e20
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.Math; import java.lang.reflect.Array; import java.util.*; import javax.swing.text.DefaultStyledDocument.ElementSpec; public final class Solution { static BufferedReader br = new BufferedReader( new InputStreamReader(System.in) ); static BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(System.out) ); static StringTokenizer st; /*write your constructor and global variables here*/ static class sortCond implements Comparator<Pair<Integer, Integer>> { @Override public int compare(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) { if (p1.a <= p2.a) { return -1; } else { return 1; } } } static class Rec { int a; int b; long c; Rec(int a, int b, long c) { this.a = a; this.b = b; this.c = c; } } static class Pair<f, s> { f a; s b; Pair(f a, s b) { this.a = a; this.b = b; } } interface modOperations { int mod(int a, int b, int mod); } static int findBinaryExponentian(int a, int pow, int mod) { if (pow == 1) { return a; } else if (pow == 0) { return 1; } else { int retVal = findBinaryExponentian(a, (int) pow / 2, mod); return modMul.mod( modMul.mod(retVal, retVal, mod), (pow % 2 == 0) ? 1 : a, mod ); } } static int findPow(int a, int b, int mod) { if (b == 1) { return a % mod; } else if (b == 0) { return 1; } else { int res = findPow(a, (int) b / 2, mod); return modMul.mod(res, modMul.mod(res, (b % 2 == 1 ? a : 1), mod), mod); } } static int bleft(long ele, ArrayList<Long> sortedArr) { int l = 0; int h = sortedArr.size() - 1; int ans = -1; while (l <= h) { int mid = l + (int) (h - l) / 2; if (sortedArr.get(mid) < ele) { l = mid + 1; } else if (sortedArr.get(mid) >= ele) { ans = mid; h = mid - 1; } } return ans; } static long gcd(long a, long b) { long div = b; long rem = a % b; while (rem != 0) { long temp = rem; rem = div % rem; div = temp; } return div; } static int log(int no) { int i = 0; while ((1 << i) <= no) { i++; } if ((1 << (i - 1)) == no) { return i - 1; } else { return i; } } static modOperations modAdd = (int a, int b, int mod) -> { return (a % mod + b % mod) % mod; }; static modOperations modSub = (int a, int b, int mod) -> { return (int) ((1l * a % mod - 1l * b % mod + 1l * mod) % mod); }; static modOperations modMul = (int a, int b, int mod) -> { return (int) ((1l * (a % mod) * 1l * (b % mod)) % (1l * mod)); }; static modOperations modDiv = (int a, int b, int mod) -> { return modMul.mod(a, findBinaryExponentian(b, mod - 1, mod), mod); }; static HashSet<Integer> primeList(int MAXI) { int[] prime = new int[MAXI + 1]; HashSet<Integer> obj = new HashSet<>(); for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) { if (prime[i] == 0) { obj.add(i); for (int j = i * i; j <= MAXI; j += i) { prime[j] = 1; } } } for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) { if (prime[i] == 0) { obj.add(i); } } return obj; } static int[] factorialList(int MAXI, int mod) { int[] factorial = new int[MAXI + 1]; factorial[2] = 1; for (int i = 3; i < MAXI + 1; i++) { factorial[i] = modMul.mod(factorial[i - 1], i, mod); } return factorial; } static void put(HashMap<Integer, Integer> cnt, int key) { if (cnt.containsKey(key)) { cnt.replace(key, cnt.get(key) + 1); } else { cnt.put(key, 1); } } static long arrSum(ArrayList<Long> arr) { long tot = 0; for (int i = 0; i < arr.size(); i++) { tot += arr.get(i); } return tot; } static int ord(char b) { return (int) b - (int) 'a'; } static int optimSearch(int[] cnt, int lower_bound, int pow, int n) { int l = lower_bound + 1; int h = n; int ans = 0; while (l <= h) { int mid = l + (h - l) / 2; if (cnt[mid] - cnt[lower_bound] == pow) { return mid; } else if (cnt[mid] - cnt[lower_bound] < pow) { ans = mid; l = mid + 1; } else { h = mid - 1; } } return ans; } static Pair<Long, Integer> ret_ans(ArrayList<Integer> ans) { int size = ans.size(); int mini = 1000000000 + 1; long tit = 0l; for (int i = 0; i < size; i++) { tit += 1l * ans.get(i); mini = Math.min(mini, ans.get(i)); } return new Pair<>(tit - mini, mini); } static int factorList( HashMap<Integer, Integer> maps, int no, int maxK, int req ) { int i = 1; while (i * i <= no) { if (no % i == 0) { if (i != no / i) { put(maps, no / i); } put(maps, i); if (maps.get(i) == req) { maxK = Math.max(maxK, i); } if (maps.get(no / i) == req) { maxK = Math.max(maxK, no / i); } } i++; } return maxK; } static ArrayList<Integer> getKeys(HashMap<Integer, Integer> maps) { ArrayList<Integer> vals = new ArrayList<>(); for (Map.Entry<Integer, Integer> map : maps.entrySet()) { vals.add(map.getKey()); } return vals; } static ArrayList<Integer> getValues(HashMap<Integer, Integer> maps) { ArrayList<Integer> vals = new ArrayList<>(); for (Map.Entry<Integer, Integer> map : maps.entrySet()) { vals.add(map.getValue()); } return vals; } /*write your methods here*/ static int getMax(ArrayList<Integer> arr) { int max = arr.get(0); for (int i = 1; i < arr.size(); i++) { if (arr.get(i) > max) { max = arr.get(i); } } return max; } static int getMin(ArrayList<Integer> arr) { int max = arr.get(0); for (int i = 1; i < arr.size(); i++) { if (arr.get(i) < max) { max = arr.get(i); } } return max; } public static void main(String[] args) throws IOException { int cases = Integer.parseInt(br.readLine()), n, k, i, j; while (cases-- != 0) { st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); k = Integer.parseInt(st.nextToken()); char s[] = br.readLine().toCharArray(); if (k % 2 != 0) { for (i = 0; i < n; i++) { if (s[i] == '0') { s[i] = '1'; } else { s[i] = '0'; } } } int cnt[] = new int[n]; for (i = 0; i < n; i++) { if (s[i] == '0') { if (k > 0) { k--; cnt[i] = 1; s[i] = '1'; } } } cnt[n - 1] += k; if (k % 2 != 0) { if (s[n - 1] == '1') { s[n - 1] = '0'; } else { s[n - 1] = '1'; } } bw.write(s); bw.write("\n"); for (i = 0; i < n; i++) { bw.write(cnt[i] + " "); } bw.write("\n"); } bw.flush(); } }
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 11
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
fb249a558ddd05a6d53fbac08f07e520
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 { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); int t=Integer.parseInt(bu.readLine()); while(t-->0) { String s[]=bu.readLine().split(" "); int n=Integer.parseInt(s[0]),k=Integer.parseInt(s[1]); char c[]=bu.readLine().toCharArray(); int i,use=0,ans[]=new int[n],times[]=new int[n]; for(i=0;i<n;i++) ans[i]=c[i]-'0'; for(i=0;use<k && i<n;i++) if(ans[i]==k%2) { times[i]++; use++; } for(i=0;i<n;i++) if((k-times[i])%2==1) ans[i]^=1; use=k-use; for(i=n-1;i>=0 && use>0;i--) if(ans[i]==1) { times[i]+=use; use=0; } if(use>0) times[n-1]+=use; for(i=0;i<n;i++) { if((k-times[i])%2==1) ans[i]=(c[i]-'0')^1; else ans[i]=c[i]-'0'; sb.append(ans[i]); } sb.append("\n"); for(i=0;i<n;i++) sb.append(times[i]+" "); sb.append("\n"); } System.out.print(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 11
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
90d3cd918f6d7297b0c25769e73562f0
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 { static class Scan { private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in=System.in; } public int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(int arr[],int l1,int r1,int l2,int r2) { int tmp[]=new int[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(long arr[],int l1,int r1,int l2,int r2) { long tmp[]=new long[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void main(String args[]) throws IOException { Scan input=new Scan(); StringBuilder ans=new StringBuilder(""); int test=input.scanInt(); for(int tt=1;tt<=test;tt++) { int n=input.scanInt(); int k=input.scanInt(); StringBuilder tmp=new StringBuilder(input.scanString()); int arr[]=new int[n]; if(k%2==0) { for(int i=0;i<n;i++) { if(k==0) { continue; } if(tmp.charAt(i)=='0') { arr[i]++; k--; } else { } } if(k%2==0) { arr[0]+=k; k=0; } else { arr[0]+=(k-1); k-=(k-1); if(k!=0) { arr[n-1]+=k; } } for(int i=0;i<n;i++) { if(arr[i]%2==1) { if(tmp.charAt(i)=='0') { tmp.setCharAt(i, '1'); } else { tmp.setCharAt(i, '0'); } } } } else { for(int i=0;i<n;i++) { if(k==0) { continue; } if(tmp.charAt(i)=='0') { } else { arr[i]++; k--; } } if(k%2==0) { arr[0]+=k; k=0; } else { arr[0]+=(k-1); k-=(k-1); if(k!=0) { arr[n-1]+=k; } } for(int i=0;i<n;i++) { if(arr[i]%2==0) { if(tmp.charAt(i)=='0') { tmp.setCharAt(i, '1'); } else { tmp.setCharAt(i, '0'); } } } } ans.append(tmp+"\n"); for(int i=0;i<n;i++) { ans.append(arr[i]+" "); } ans.append("\n"); } System.out.println(ans); } }
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 11
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
cf1e806a4a10d8cbd83191d65f99a597
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 Solution { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-->0){ int[] nk = Arrays.stream(br.readLine().split("\\s+")).mapToInt(Integer::parseInt).toArray(); int n = nk[0]; int k = nk[1]; String s = br.readLine(); StringBuilder sb = new StringBuilder(); StringBuilder ans = new StringBuilder(); int times = 0; for(int i=0;i<n;i++){ char c = s.charAt(i); if(i==n-1){ // System.out.println(k + " "+times); // times = k-times; if(c=='1' && times%2==0 && times>0){ sb.append("1"); }else if(c=='1' && times%2==1){ sb.append("0"); }else if(c=='0' && times%2==0 && times>0){ sb.append("0"); }else if(times>0){ sb.append("1"); }else{ sb.append(c); } ans.append(k-times); } else if(c=='1' && k%2==1 && times<k){ times++; sb.append("1"); ans.append("1"); }else if(c=='0' && k%2==0 && times<k){ times++; sb.append("1"); ans.append("1"); }else{ if(c=='1' && k%2==1){ sb.append("0"); }else if(c=='1' && k%2==0){ sb.append("1"); }else if(c=='0' && k%2==1){ sb.append("1"); }else{ sb.append("0"); } ans.append(0); } ans.append(" "); } System.out.println(sb); System.out.println(ans.toString().trim()); } } }
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 11
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
f3dab53e5d0024d8642d51a7f94155a5
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
/* Rating: 1461 Date: 17-04-2022 Time: 20-27-57 Author: Kartik Papney Linkedin: https://www.linkedin.com/in/kartik-papney-4951161a6/ Leetcode: https://leetcode.com/kartikpapney/ Codechef: https://www.codechef.com/users/kartikpapney */ import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class B_Bit_Flipping { public static boolean debug = false; static void debug(String st) { if(debug) p.writeln(st); } public static void s() { int n = sc.nextInt(), k = sc.nextInt(); int[] opn = new int[n]; StringBuilder s = new StringBuilder(sc.nextLine()); if(k%2 != 0) { for(int i=0; i<n; i++) { s.setCharAt(i, (char)('1' - s.charAt(i)+'0')); } } for(int i=0; i<s.length() && k != 0; i++) { if(s.charAt(i) == '0') { opn[i]++; s.setCharAt(i, '1'); k--; } } if(k%2 == 1) { char c = s.charAt(s.length()-1); s.setCharAt(s.length()-1, (char)('1' - c + '0')); } opn[n-1] += k; p.writeln(s.toString()); p.writes(opn); p.writeln(); } public static void main(String[] args) { int t = 1; t = sc.nextInt(); while (t-- != 0) { s(); } p.print(); } static final Integer MOD = (int) 1e9 + 7; static final FastReader sc = new FastReader(); static final Print p = new Print(); static class Functions { 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 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 int max(int... a) { int max = Integer.MIN_VALUE; for (int val : a) max = Math.max(val, max); return max; } static int min(int... a) { int min = Integer.MAX_VALUE; for (int val : a) min = Math.min(val, min); return min; } static long min(long... a) { long min = Long.MAX_VALUE; for (long val : a) min = Math.min(val, min); return min; } static long max(long... a) { long max = Long.MIN_VALUE; for (long val : a) max = Math.max(val, max); return max; } static long sum(long... a) { long sum = 0; for (long val : a) sum += val; return sum; } static int sum(int... a) { int sum = 0; for (int val : a) sum += val; return sum; } public static long mod_add(long a, long b) { return (a % MOD + b % MOD + MOD) % MOD; } public static long pow(long a, long b) { long res = 1; while (b > 0) { if ((b & 1) != 0) res = mod_mul(res, a); a = mod_mul(a, a); b >>= 1; } return res; } public static long mod_mul(long a, long b) { long res = 0; a %= MOD; while (b > 0) { if ((b & 1) > 0) { res = mod_add(res, a); } a = (2 * a) % MOD; b >>= 1; } return res; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long factorial(long n) { long res = 1; for (int i = 1; i <= n; i++) { res = (i % MOD * res % MOD) % MOD; } return res; } public static int count(int[] arr, int x) { int count = 0; for (int val : arr) if (val == x) count++; return count; } public static ArrayList<Integer> generatePrimes(int n) { boolean[] primes = new boolean[n]; for (int i = 2; i < primes.length; i++) primes[i] = true; for (int i = 2; i < primes.length; i++) { if (primes[i]) { for (int j = i * i; j < primes.length; j += i) { primes[j] = false; } } } ArrayList<Integer> arr = new ArrayList<>(); for (int i = 0; i < primes.length; i++) { if (primes[i]) arr.add(i); } return arr; } } static class Print { StringBuffer strb = new StringBuffer(); public void write(Object str) { strb.append(str); } public void writes(Object str) { char c = ' '; strb.append(str).append(c); } public void writeln(Object str) { char c = '\n'; strb.append(str).append(c); } public void writeln() { char c = '\n'; strb.append(c); } public void yes() { char c = '\n'; writeln("YES"); } public void no() { writeln("NO"); } public void writes(int... arr) { for (int val : arr) { write(val); write(' '); } } public void writes(long... arr) { for (long val : arr) { write(val); write(' '); } } public void writeln(int... arr) { for (int val : arr) { writeln(val); } } public void print() { System.out.print(strb); } public void println() { System.out.println(strb); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } double[] readArrayDouble(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = new String(); 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 11
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
40e977d7605343413a7f3f6bfc30e708
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.*; // @author : Dinosparton public class test { static class Pair{ long x; long y; Pair(long x,long y){ this.x = x; this.y = y; } } static class Sort implements Comparator<Pair> { @Override public int compare(Pair a, Pair b) { if(a.x!=b.x) { return (int)(a.x - b.x); } else { return (int)(a.y-b.y); } } } static class Compare { void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.x!=p2.x) { return (int)(p1.x - p2.x); } else { return (int)(p1.y - p2.y); } } }); // for (int i = 0; i < n; i++) { // System.out.print(arr[i].x + " " + arr[i].y + " "); // } // System.out.println(); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]) throws Exception { Scanner sc = new Scanner(); StringBuilder res = new StringBuilder(); int tc = sc.nextInt(); while(tc-->0) { int n = sc.nextInt(); long k = sc.nextLong(); String s = sc.next(); char c[] = s.toCharArray(); long ans[] = new long[n]; long fill = 0; if(k%2==1) { for(int i=0;i<n;i++) { if(c[i]=='0') { c[i]='1'; } else { c[i]='0'; } } } for(int i=0;i<n-1;i++) { if(c[i]=='0') { if(fill<k) { ans[i] = 1; fill++; c[i] = '1'; } } } if(fill != k ) { long add = k - fill; ans[n-1] += add; if(ans[n-1]%2==1) { if(c[n-1]=='0') { c[n-1]='1'; } else { c[n-1]='0'; } } } for(char i : c) { res.append(i); } res.append("\n"); for(long i : ans) { res.append(i+" "); } res.append("\n"); } System.out.println(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 11
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
b371bf6ea363c83e24dedf8c12359ace
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
//Number of pairs import java.io.BufferedReader; import java.io.*; import java.io.InputStreamReader; import java.util.Scanner; import java.util.*; public class Yoo { 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 change(int []a, int n) { int i =0 ; for(i=0;i<n;i++) { if(i==n) continue; else { if(a[i]==0) a[i]=1; else a[i]=0; } } } public static void main(String[] args) { FastReader sc=new FastReader(); int i,j=0; int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); char[]ch = s.toCharArray(); if(k%2!=0) { for(i=0;i<n;i++) { if(ch[i]=='0') ch[i]='1'; else ch[i]='0'; } } int a[] = new int[n]; i=0; while(k>0 && i<n) { if(ch[i]=='0') { ch[i]='1'; k--; a[i]++; } i++; } if(k>0) { if(k%2==0) { a[n-1]+=k; } else { if(ch[n-1]=='0') ch[n-1]='1'; else ch[n-1]='0'; a[n-1]+=k; } } String result = String.valueOf(ch); System.out.println(result); for(i=0;i<n;i++) { System.out.print(a[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 11
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
8c000fb67497811885fa9c3e18646abd
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.print.DocFlavor.INPUT_STREAM; import java.io.*; import java.math.*; import java.sql.Array; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.SQLIntegrityConstraintViolationException; public class Main { private static class MyScanner { private static final int BUF_SIZE = 2048; BufferedReader br; private MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } private boolean isSpace(char c) { return c == '\n' || c == '\r' || c == ' '; } String next() { try { StringBuilder sb = new StringBuilder(); int r; while ((r = br.read()) != -1 && isSpace((char)r)); if (r == -1) { return null; } sb.append((char) r); while ((r = br.read()) != -1 && !isSpace((char)r)) { sb.append((char)r); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } static class Reader{ BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long mod = (long)(1e9 + 7); static void sort(long[] arr ) { ArrayList<Long> al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr ) { ArrayList<Integer> al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i); } static long mod_mul( long... a) { long ans = a[0]%mod; for(int i = 1 ; i<a.length ; i++) { ans = (ans * (a[i]%mod))%mod; } return ans; } static long mod_sum( long... a) { long ans = 0; for(long e:a) { ans = (ans + e)%mod; } return ans; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void print(long[] arr) { System.out.println("---print---"); for(long e:arr) System.out.print(e+" "); System.out.println("-----------"); } static void print(int[] arr) { System.out.println("---print---"); for(long e:arr) System.out.print(e+" "); System.out.println("-----------"); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static long modInverse(long a, long m) { long g = gcd(a, m); return power(a, m - 2); } static long lcm(long a , long b) { return (a*b)/gcd(a, b); } static int lcm(int a , int b) { return (int)((a*b)/gcd(a, b)); } static long power(long x, long y){ if(y<0) return 0; long m = mod; if (y == 0) return 1; long p = power(x, y / 2) % m; p = (int)((p * (long)p) % m); if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); } static class Combinations{ private long[] z; // factorial private long[] z1; // inverse factorial private long[] z2; // incerse number private long mod; public Combinations(long N , long mod) { this.mod = mod; z = new long[(int)N+1]; z1 = new long[(int)N+1]; z[0] = 1; for(int i =1 ; i<=N ; i++) z[i] = (z[i-1]*i)%mod; z2 = new long[(int)N+1]; z2[0] = z2[1] = 1; for (int i = 2; i <= N; i++) z2[i] = z2[(int)(mod % i)] * (mod - mod / i) % mod; z1[0] = z1[1] = 1; for (int i = 2; i <= N; i++) z1[i] = (z2[i] * z1[i - 1]) % mod; } long fac(long n) { return z[(int)n]; } long invrsNum(long n) { return z2[(int)n]; } long invrsFac(long n) { return z1[(int)n]; } long ncr(long N, long R) { if(R<0 || R>N ) return 0; long ans = ((z[(int)N] * z1[(int)R]) % mod * z1[(int)(N - R)]) % mod; return ans; } } static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } void makeSet() { for (int i = 0; i < n; i++) { parent[i] = i; } } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } } } static int max(int... a ) { int max = a[0]; for(int e:a) max = Math.max(max, e); return max; } static long max(long... a ) { long max = a[0]; for(long e:a) max = Math.max(max, e); return max; } static int min(int... a ) { int min = a[0]; for(int e:a) min = Math.min(e, min); return min; } static long min(long... a ) { long min = a[0]; for(long e:a) min = Math.min(e, min); return min; } static int[] KMP(String str) { int n = str.length(); int[] kmp = new int[n]; for(int i = 1 ; i<n ; i++) { int j = kmp[i-1]; while(j>0 && str.charAt(i) != str.charAt(j)) { j = kmp[j-1]; } if(str.charAt(i) == str.charAt(j)) j++; kmp[i] = j; } return kmp; } /************************************************ Query **************************************************************************************/ /***************************************** Sparse Table ********************************************************/ static class SparseTable{ private long[][] st; SparseTable(long[] arr){ int n = arr.length; st = new long[n][25]; log = new int[n+2]; build_log(n+1); build(arr); } private void build(long[] arr) { int n = arr.length; for(int i = n-1 ; i>=0 ; i--) { for(int j = 0 ; j<25 ; j++) { int r = i + (1<<j)-1; if(r>=n) break; if(j == 0 ) st[i][j] = arr[i]; else st[i][j] = max(st[i][j-1] , st[ i + ( 1 << (j-1) ) ][ j-1 ] ); } } } public long gcd(long a ,long b) { if(a == 0) return b; return gcd(b%a , a); } public long query(int l ,int r) { int w = r-l+1; int power = log[w]; return max(st[l][power],st[r - (1<<power) + 1][power]); } private int[] log; void build_log(int n) { log[1] = 0; for(int i = 2 ; i<=n ; i++) { log[i] = 1 + log[i/2]; } } } /******************************************************** Segement Tree *****************************************************/ static class SegmentTree{ long[] tree; long[] arr; int n; SegmentTree(long[] arr){ this.n = arr.length; tree = new long[4*n+1]; this.arr = arr; buildTree(0, n-1, 1); } void buildTree(int s ,int e ,int index ) { if(s == e) { tree[index] = arr[s]; return; } int mid = (s+e)/2; buildTree( s, mid, 2*index); buildTree( mid+1, e, 2*index+1); tree[index] = gcd(tree[2*index] , tree[2*index+1]); } long query(int si ,int ei) { return query(0 ,n-1 , si ,ei , 1 ); } private long query( int ss ,int se ,int qs , int qe,int index) { if(ss>=qs && se<=qe) return tree[index]; if(qe<ss || se<qs) return (long)(0); int mid = (ss + se)/2; long left = query( ss , mid , qs ,qe , 2*index); long right= query(mid + 1 , se , qs ,qe , 2*index+1); return min(left, right); } public void update(int index , int val) { arr[index] = val; update(index , 0 , n-1 , 1); } private void update(int id ,int si , int ei , int index) { if(id < si || id>ei) return; if(si == ei ) { tree[index] = arr[id]; return; } if(si > ei) return; int mid = (ei + si)/2; update( id, si, mid , 2*index); update( id , mid+1, ei , 2*index+1); tree[index] = Math.min(tree[2*index] ,tree[2*index+1]); } } /* ***************************************************************************************************************************************************/ // static MyScanner sc = new MyScanner(); // only in case of less memory static Reader sc = new Reader(); static int TC; static StringBuilder sb = new StringBuilder(); static PrintWriter out=new PrintWriter(System.out); public static void main(String args[]) throws IOException { int tc = 1; tc = sc.nextInt(); TC = 0; for(int i = 1 ; i<=tc ; i++) { TC++; // sb.append("Case #" + i + ": " ); // During KickStart && HackerCup TEST_CASE(); } System.out.print(sb); } static void TEST_CASE() { int n = sc.nextInt(); int k = sc.nextInt(); String str = sc.next(); int[] arr = new int[n]; for(int i = 0 ;i<n ; i++) arr[i] = str.charAt(i)-'0'; int[] ans = new int[n]; if(k%2 == 1) { for(int i =0 ;i<n-1 ; i++) { if(k == 0) break; else if(arr[i] == 1) { ans[i] = 1; k--; } } ans[n-1] = k; for(int i =0 ;i<n ; i++) { if(arr[i] == ans[i]%2) sb.append("1"); else sb.append("0"); } sb.append("\n"); for(int e:ans) sb.append(e+" "); sb.append("\n"); }else { for(int i =0 ;i<n-1 ; i++) { if(k == 0) break; else if(arr[i] == 0) { ans[i] = 1; k--; } } ans[n-1] = k; for(int i =0 ;i<n ; i++) { if(arr[i] == ans[i]%2) sb.append("0"); else sb.append("1"); } sb.append("\n"); for(int e:ans) sb.append(e+" "); sb.append("\n"); } } } /*******************************************************************************************************************************************************/ /** 6 11 101100 110011 001000 76yhb */
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 11
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
ede92bb92c8f070993f1878c2a2a56c6
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.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 k=scan.nextInt(); String s= scan.next(); char[] arr=s.toCharArray(); int[] choosebit=new int[n]; int op=0; boolean flag=false; for (int i = 0; i < n; i++) { if(op%2==1){ arr[i]=(arr[i]=='1')?'0':'1'; } if(k>0){ if( (arr[i]=='1' && k%2==1) || (arr[i]=='0' && k%2==0) ){ k--; op++; choosebit[i]++; } if(arr[i]=='0' && i==(n-1)){ flag=true; } arr[i]='1'; } } if(flag)arr[n-1]='0'; choosebit[n-1]+=k; out.println(String.valueOf(arr)); out.printArray(choosebit); } 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
["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 11
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
080eec96a1038a882c1d46b2eac8b158
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 { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int T = Integer.parseInt(br.readLine()); for (int t = 1; t <= T; t++) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); String s = br.readLine(); int[] flip = new int[n]; boolean maximize = true; if (k % 2 == 0) { // we maximize for (int i = 0; i < n; i++) { if (k == 0) break; if (s.charAt(i) == '0') { flip[i] = 1; k--; } } flip[n - 1] += k; } else { // we minimize maximize = false; for (int i = 0; i < flip.length; i++) { if (k == 0) break; if (s.charAt(i) == '1') { flip[i] = 1; k--; } } flip[n - 1] += k; } StringBuilder sb = new StringBuilder(); if (maximize) { // do not flip for (int i = 0; i < n; i++) { if ((s.charAt(i) == '1' && flip[i] % 2 == 1) || (s.charAt(i) == '0' && flip[i] % 2 == 0)) { sb.append('0'); } else { sb.append('1'); } } } else { for (int i = 0; i < n; i++) { if ((s.charAt(i) == '1' && flip[i] % 2 == 1) || (s.charAt(i) == '0' && flip[i] % 2 == 0)) { sb.append('1'); } else { sb.append('0'); } } } pw.println(sb.toString()); for (int i = 0; i < n; i++) { pw.print(flip[i] + " "); } pw.println(); } pw.flush(); pw.close(); br.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 11
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
c3dd8bda027284e3ef616f1eeab812f4
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 MyClass { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt(); String str=sc.next(); char[] arr=str.toCharArray(); int[] freq=new int[n]; if(k%2==1){ for(int i=0; i<n; i++){ if(arr[i]=='1'){ freq[i]=1; k--; if(k==0){ for(int j=i+1; j<n; j++){ arr[j]=(arr[j]=='0')?'1':'0'; } break; } } else{ arr[i]='1'; } } if(k%2==1){ arr[n-1]='0'; if(freq[n-1]==1){ freq[n-1]=0; k++; } else{ freq[n-1]=1; k--; } } freq[0]+=k; } else{ for(int i=0; i<n && k>0; i++){ if(arr[i]=='0'){ freq[i]=1; arr[i]='1'; k--; if(k==0){ break; } } } if(k%2==1){ arr[n-1]='0'; if(freq[n-1]==1){ freq[n-1]=0; k++; } else{ freq[n-1]=1; k--; } } freq[0]+=k; } String ans=new String(arr); System.out.println(ans); for(int i=0; i<n; i++){ System.out.print(freq[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 11
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
29c357c168a670473570819c0176debd
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 { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); int t=Integer.parseInt(bu.readLine()); while(t-->0) { String s[]=bu.readLine().split(" "); int n=Integer.parseInt(s[0]),k=Integer.parseInt(s[1]); char c[]=bu.readLine().toCharArray(); int i,use=0,ans[]=new int[n],times[]=new int[n]; for(i=0;i<n;i++) ans[i]=c[i]-'0'; for(i=0;use<k && i<n;i++) if(ans[i]==k%2) { times[i]++; use++; } for(i=0;i<n;i++) if((k-times[i])%2==1) ans[i]^=1; use=k-use; for(i=n-1;i>=0 && use>0;i--) if(ans[i]==1) { times[i]+=use; use=0; } if(use>0) times[n-1]+=use; for(i=0;i<n;i++) { if((k-times[i])%2==1) ans[i]=(c[i]-'0')^1; else ans[i]=c[i]-'0'; sb.append(ans[i]); } sb.append("\n"); for(i=0;i<n;i++) sb.append(times[i]+" "); sb.append("\n"); } System.out.print(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 11
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
1a970840a67a3554cb47f0b86ee3c34a
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 Practice { static boolean multipleTC = true; final static int mod2 = 1000000007; final static int mod = 998244353; final double E = 2.7182818284590452354; final double PI = 3.14159265358979323846; int MAX = 200001; void pre() throws Exception { } // All the best void solve(int TC) throws Exception { int n = ni(), k = ni(); char arr[] = nln().toCharArray(); int count[] = new int[n]; if (k % 2 == 1) { for (int i = 0; i < n; i++) { if (arr[i] == '0') arr[i] = '1'; else arr[i] = '0'; } } for(int i=0;i<n;i++) { if(arr[i]=='0' && k>0) { arr[i]='1'; count[i]++; k--; } } count[n-1]+=k; if(k%2==1) { arr[n-1]= '0'; } pn(String.valueOf(arr)); pn(count); } public static long gcd(long a, long b) { if (a > b) a = (a + b) - (b = a); if (a == 0L) return b; return gcd(b % a, a); } double dist(int x1, int y1, int x2, int y2) { double a = x1 - x2, b = y1 - y2; return Math.sqrt((a * a) + (b * b)); } int[] readArr(int n) throws Exception { int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } void sort(int arr[], int left, int right) { ArrayList<Integer> list = new ArrayList<>(); for (int i = left; i <= right; i++) list.add(arr[i]); Collections.sort(list); for (int i = left; i <= right; i++) arr[i] = list.get(i - left); } void sort(int arr[]) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < arr.length; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < arr.length; i++) arr[i] = list.get(i); } public long max(long... arr) { long max = arr[0]; for (long itr : arr) max = Math.max(max, itr); return max; } public int max(int... arr) { int max = arr[0]; for (int itr : arr) max = Math.max(max, itr); return max; } public long min(long... arr) { long min = arr[0]; for (long itr : arr) min = Math.min(min, itr); return min; } public int min(int... arr) { int min = arr[0]; for (int itr : arr) min = Math.min(min, itr); return min; } public long sum(long... arr) { long sum = 0; for (long itr : arr) sum += itr; return sum; } public long sum(int... arr) { long sum = 0; for (int itr : arr) sum += itr; return sum; } String bin(long n) { return Long.toBinaryString(n); } String bin(int n) { return Integer.toBinaryString(n); } static int bitCount(int x) { return x == 0 ? 0 : (1 + bitCount(x & (x - 1))); } static void dbg(Object... o) { System.err.println(Arrays.deepToString(o)); } int bit(long n) { return (n == 0) ? 0 : (1 + bit(n & (n - 1))); } int abs(int a) { return (a < 0) ? -a : a; } long abs(long a) { return (a < 0) ? -a : a; } void p(Object o) { out.print(o); } void pn(Object o) { out.println(o); } void pni(Object o) { out.println(o); out.flush(); } void pn(int[] arr) { int n = arr.length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(arr[i] + " "); } pn(sb); } void pn(long[] arr) { int n = arr.length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(arr[i] + " "); } pn(sb); } String n() throws Exception { return in.next(); } String nln() throws Exception { return in.nextLine(); } int ni() throws Exception { return Integer.parseInt(in.next()); } long nl() throws Exception { return Long.parseLong(in.next()); } double nd() throws Exception { return Double.parseDouble(in.next()); } public static void main(String[] args) throws Exception { new Practice().run(); } FastReader in; PrintWriter out; void run() throws Exception { in = new FastReader(); out = new PrintWriter(System.out); int T = (multipleTC) ? ni() : 1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception { br = new BufferedReader(new FileReader(s)); } String next() throws Exception { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception { String str = ""; try { str = br.readLine(); } catch (IOException e) { throw new Exception(e.toString()); } 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 11
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
df5f72409818baddb66bec25074504b6
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 11
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
f63ac22841a3b0dfd80ba4e38145a76a
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.*; import java.lang.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.math.*; import java.util.function.Consumer; import java.util.stream.Collectors; public class AAtempOneForCodeForces { @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(), k = in.nextInt(); char[] s = in.next().toCharArray(); if(k % 2 == 1){ for(int i = 0; i < n; i++){ if(s[i] == '0')s[i] = '1'; else s[i] = '0'; } } int[] choice = new int[n]; for(int i = 0; i < s.length; i++){ if(s[i] == '0' && k > 0){ s[i] = '1'; k--; choice[i]++; } } choice[n - 1] += k; if(k % 2 == 1){ s[n - 1] = '0'; } System.out.println(s); for(int i = 0; i < n; i++){ System.out.print(choice[i] + " "); } 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(); } /*Another 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(); } } /* A class representing a Mutable Multiset (Collectied From TechieDelight)*/ private static class Multiset<E> { /* List to store distinct values */ private List<E> values; /* List to store counts of distinct values */ private List<Integer> frequency; private final String ERROR_MSG = "Count cannot be negative: "; /* Constructor */ public Multiset() { values = new ArrayList<>(); frequency = new ArrayList<>(); } /** * Adds an element to this multiset specified number of times * * @param `element` The element to be added * @param `count` The number of times * @return The previous count of the element */ public int add(E element, int count) { if (count < 0) { throw new IllegalArgumentException(ERROR_MSG + count); } int index = values.indexOf(element); int prevCount = 0; if (index != -1) { prevCount = frequency.get(index); frequency.set(index, prevCount + count); } else if (count != 0) { values.add(element); frequency.add(count); } return prevCount; } /** * Adds specified element to this multiset * * @param `element` The element to be added * @return true always */ public boolean add(E element) { return add(element, 1) >= 0; } /** * Adds all elements in the specified collection to this multiset * * @param `c` Collection containing elements to be added * @return true if all elements are added to this multiset */ boolean addAll(Collection<? extends E> c) { for (E element: c) { add(element, 1); } return true; } /** * Adds all elements in the specified array to this multiset * * @param `arr` An array containing elements to be added */ public void addAll(E... arr) { for (E element: arr) { add(element, 1); } } /** * Performs the given action for each element of the Iterable, * including duplicates * * @param `action` The action to be performed for each element */ public void forEach(Consumer<? super E> action) { List<E> all = new ArrayList<>(); for (int i = 0; i < values.size(); i++) { for (int j = 0; j < frequency.get(i); j++) { all.add(values.get(i)); } all.forEach(action); } } /** * Removes a single occurrence of the specified element from this multiset * * @param `element` The element to removed * @return true if an occurrence was found and removed */ public boolean remove(Object element) { return remove(element, 1) > 0; } /** * Removes a specified number of occurrences of the specified element * from this multiset * * @param `element` The element to removed * @param `count` The number of occurrences to be removed * @return The previous count */ public int remove(Object element, int count) { if (count < 0) { throw new IllegalArgumentException(ERROR_MSG + count); } int index = values.indexOf(element); if (index == -1) { return 0; } int prevCount = frequency.get(index); if (prevCount > count) { frequency.set(index, prevCount - count); } else { values.remove(index); frequency.remove(index); } return prevCount; } /** * Check if this multiset contains at least one occurrence of the * specified element * * @param `element` The element to be checked * @return true if this multiset contains at least one occurrence * of the element */ public boolean contains(Object element) { return values.contains(element); } /** * Check if this multiset contains at least one occurrence of each element * in the specified collection * * @param `c` The collection of elements to be checked * @return true if this multiset contains at least one occurrence * of each element */ public boolean containsAll(Collection<?> c) { return values.containsAll(c); } /** * Update the frequency of an element to the specified count or * add element to this multiset if not present * * @param `element` The element to be updated * @param `count` The new count * @return The previous count */ public int setCount(E element, int count) { if (count < 0) { throw new IllegalArgumentException(ERROR_MSG + count); } if (count == 0) { remove(element); } int index = values.indexOf(element); if (index == -1) { return add(element, count); } int prevCount = frequency.get(index); frequency.set(index, count); return prevCount; } /** * Find the frequency of an element in this multiset * * @param `element` The element to be counted * @return The frequency of the element */ public int count(Object element) { int index = values.indexOf(element); return (index == -1) ? 0 : frequency.get(index); } /** * @return A view of the set of distinct elements in this multiset */ public Set<E> elementSet() { return values.stream().collect(Collectors.toSet()); } /** * @return true if this multiset is empty */ public boolean isEmpty() { return values.size() == 0; } /** * @return Total number of elements in this multiset, including duplicates */ public int size() { int size = 0; for (Integer i: frequency) { size += i; } return size; } @Override public String toString() { StringBuilder sb = new StringBuilder("["); for (int i = 0; i < values.size(); i++) { sb.append(values.get(i)); if (frequency.get(i) > 1) { sb.append(" x ").append(frequency.get(i)); } if (i != values.size() - 1) { sb.append(", "); } } return sb.append("]").toString(); } } }
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 11
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
92b016a1833ac40b2d464d3b812b1f54
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.Scanner; import java.util.StringTokenizer; import java.util.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); while (t-- > 0){ int n = s.nextInt(); int k = s.nextInt(); char[] ar = s.next().toCharArray(); int[] fr = new int[n]; int cnt = 0; for (int i = 0; i < n && cnt < k; i++){ if (k%2 == 0){ if (ar[i] == '0') { fr[i]++; cnt++; } } else { if (ar[i] == '1') { fr[i]++; cnt++; } } } fr[n-1] += (k-cnt); for (int i = 0; i < n; i++){ if ((k-fr[i])%2 == 1){ ar[i]=(char)('0'+(char)('1'-ar[i])); } } System.out.println(String.valueOf(ar)); for(int e: fr) System.out.print(e + " "); 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 11
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
3fe5eb608f4b715d26fa783b8634b4bb
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 : Ryan Ranaut __Hope is a big word, never lose it__ ------------- --------------*/ import java.io.*; import java.util.*; public class Codeforces2 { static PrintWriter out = new PrintWriter(System.out); //static final int mod = 1_000_000_007; static final int mod = 998244353; static final int max = (int)(1e6); 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 public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); while(t-->0) { int n = s.nextInt(); int k = s.nextInt(); char[] str = s.nextLine().toCharArray(); find(n, k, str); } out.close(); } public static void find(int n, int k, char[] str) { List<Integer> ls = new ArrayList<>(); int kk = k; for(int i=0;i<n;i++) { if(str[i] == '1') { if((k&1)==1 && kk>0)//Use it one time { ls.add(1); kk--; } else ls.add(0); } else { if((k&1)==0 && kk>0)//Use it one time { ls.add(1); kk--; } else ls.add(0); } } int rem = kk; int last = ls.get(n-1); ls.set(n-1, last+rem); StringBuilder sb = new StringBuilder(); for(int i=0;i<n;i++) { int count = k-ls.get(i); if((count&1) == 1)//Change the parity sb.append(str[i] == '0' ? '1' : '0'); else sb.append(str[i]); } out.println(sb); for(Integer x: ls) out.print(x+" "); out.println(); } /*-----------------------------------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%mod; } 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
["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 11
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
3a23b14fa1220d4ed7b2285becd21e26
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
/*LoudSilence*/ import java.io.*; import java.util.*; import static java.lang.Math.*; public class Solution { /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ static FastScanner s = new FastScanner(); static FastWriter out = new FastWriter(); final static int mod = (int)1e9 + 7; final static int INT_MAX = Integer.MAX_VALUE; final static int INT_MIN = Integer.MIN_VALUE; final static long LONG_MAX = Long.MAX_VALUE; final static long LONG_MIN = Long.MIN_VALUE; final static double DOUBLE_MAX = Double.MAX_VALUE; final static double DOUBLE_MIN = Double.MIN_VALUE; final static float FLOAT_MAX = Float.MAX_VALUE; final static float FLOAT_MIN = Float.MIN_VALUE; /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ static class FastScanner{BufferedReader br;StringTokenizer st; public FastScanner() {if(System.getProperty("ONLINE_JUDGE") == null){try {br = new BufferedReader(new FileReader("E:\\Competitive Coding\\input.txt"));} catch (FileNotFoundException e) {br = new BufferedReader(new InputStreamReader(System.in));}}else{br = new BufferedReader(new InputStreamReader(System.in));}} String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException e){e.printStackTrace();}}return st.nextToken();} int nextInt(){return Integer.parseInt(next());} long nextLong(){return Long.parseLong(next());} double nextDouble(){return Double.parseDouble(next());} List<Integer> readIntList(int n){List<Integer> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextInt()); return arr;} List<Long> readLongList(int n){List<Long> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextLong()); return arr;} int[] readIntArr(int n){int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = s.nextInt(); return arr;} long[] readLongArr(int n){long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = s.nextLong(); return arr;} String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}} static class FastWriter{private BufferedWriter bw;public FastWriter(){if(System.getProperty("ONLINE_JUDGE") == null){try {this.bw = new BufferedWriter(new FileWriter("E:\\Competitive Coding\\output.txt"));} catch (IOException e) {this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}}else{this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}} public void print(Object object) throws IOException{bw.append(""+ object);} public void println(Object object) throws IOException{print(object);bw.append("\n");} public void debug(int object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");} public void debug(long object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");} public void close() throws IOException{bw.close();}} public static void println(Object str) throws IOException{out.println(""+str);} public static void println(Object str, int nextLine) throws IOException{out.print(""+str);} /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public static ArrayList<Integer> seive(int n){ArrayList<Integer> list = new ArrayList<>();int arr[] = new int[n+1];for(long i = 2; i <= n; i++) {if(arr[(int)i] == 1) {continue;}else {list.add((int)i);for(long j = i*i; j <= n; j = j + i) {arr[(int)j] = 1;}}}return list;} public static long gcd(long a, long b){if(a > b) {a = (a+b)-(b=a);}if(a == 0L){return b;}return gcd(b%a, a);} public static void swap(int[] arr, int i, int j) {arr[i] = arr[i] ^ arr[j]; arr[j] = arr[j] ^ arr[i]; arr[i] = arr[i] ^ arr[j];} public static boolean isPrime(long n){if(n < 2){return false;}if(n == 2 || n == 3){return true;}if(n%2 == 0 || n%3 == 0){return false;}long sqrtN = (long)Math.sqrt(n)+1;for(long i = 6L; i <= sqrtN; i += 6) {if(n%(i-1) == 0 || n%(i+1) == 0) return false;}return true;} public static long mod_add(long a, long b){ return (a%mod + b%mod)%mod;} public static long mod_sub(long a, long b){ return (a%mod - b%mod + mod)%mod;} public static long mod_mul(long a, long b){ return (a%mod * b%mod)%mod;} public static long modInv(long a, long b){ return expo(a, b-2)%b;} public static long mod_div(long a, long b){return mod_mul(a, modInv(b, mod));} public static long expo (long a, long n){if(n == 0){return 1;}long recAns = expo(mod_mul(a,a), n/2);if(n % 2 == 0){return recAns;}else{return mod_mul(a, recAns);}} /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // Pair class public static class Pair<X extends Comparable<X>,Y extends Comparable<Y>> implements Comparable<Pair<X, Y>>{ X first; Y second; public Pair(X first, Y second){ this.first = first; this.second = second; } public String toString(){ return "( " + first+" , "+second+" )"; } @Override public int compareTo(Pair<X, Y> o) { int t = first.compareTo(o.first); if(t == 0) return second.compareTo(o.second); return t; } } /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // Code begins public static void solve() throws IOException { int n = s.nextInt(); int k = s.nextInt(); String str = s.nextLine(); int arr[] = new int[n]; int first_one = -1; for(int i = 0; i < n; i++){ arr[i] = Integer.parseInt(""+str.charAt(i)); if(arr[i] == 1 && first_one == -1){ first_one = i; } } if(first_one == -1){ first_one = n-1; } int ans[] = new int[n]; if(k%2 == 1){ ans[first_one] = 1; for(int i = 0; i < n; i++){ if(i != first_one){ arr[i] = arr[i] ^ 1; } } k--; } for(int i = 0; i < n-1; i++){ if(arr[i] == 0 && k > 0){ k--; arr[i] = 1; ans[i] += 1; } } if(k%2 == 0){ ans[n-1] += k; }else{ ans[n-1] += k; arr[n-1] = arr[n-1]^1; } for(int i = 0; i < n; i++){ println(arr[i],1); } println(""); for(int i = 0; i < n; i++){ println(ans[i]+" ",1); } println(""); } /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public static void main(String[] args) throws IOException { int test = s.nextInt(); for(int t = 1; t <= test; t++) { solve(); } 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 11
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
9433ca75282db3abd41906953982b93e
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.text.DecimalFormat; import java.util.Arrays; import java.util.*; import java.util.Scanner; import java.util.StringTokenizer; public class copy { static int log=18; static int[][] ancestor; static int[] depth; static void sieveOfEratosthenes(int n, ArrayList<Integer> arr) { 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]) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i]) { arr.add(i); } } } public static long fac(long N, long mod) { if (N == 0) return 1; if(N==1) return 1; return ((N % mod) * (fac(N - 1, mod) % mod)) % mod; } static long power(long x, long y, long p) { // Initialize result long 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 long modInverse(long n, long p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static long nCrModPFermat(long n, long r, long p) { if (n < r) return 0; // Base case if (r == 0) return 1; return ((fac(n, p) % p * (modInverse(fac(r, p), p) % p)) % p * (modInverse(fac(n - r, p), p) % p)) % p; } public static int find(int[] parent, int x) { if (parent[x] == x) return x; return find(parent, parent[x]); } public static void merge(int[] parent, int[] rank, int x, int y,int[] child) { int x1 = find(parent, x); int y1 = find(parent, y); if (rank[x1] > rank[y1]) { parent[y1] = x1; child[x1]+=child[y1]; } else if (rank[y1] > rank[x1]) { parent[x1] = y1; child[y1]+=child[x1]; } else { parent[y1] = x1; child[x1]+=child[y1]; rank[x1]++; } } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static int power(int x, int y, int p) { int 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; } // 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; } public static long[][] ncr(int n,int r) { long[][] dp=new long[n+1][r+1]; for(int i=0;i<=n;i++) dp[i][0]=1; for(int i=1;i<=n;i++) { for(int j=1;j<=r;j++) { if(j>i) continue; dp[i][j]=dp[i-1][j-1]+dp[i-1][j]; } } return dp; } public static boolean prime(long N) { int c=0; for(int i=2;i*i<=N;i++) { if(N%i==0) ++c; } return c==0; } public static int sparse_ancestor_table(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] child) { int c=0; for(int i:arr.get(x)) { if(i!=parent) { // System.out.println(i+" hello "+x); depth[i]=depth[x]+1; ancestor[i][0]=x; // if(i==2) // System.out.println(parent+" hello"); for(int j=1;j<log;j++) ancestor[i][j]=ancestor[ancestor[i][j-1]][j-1]; c+=sparse_ancestor_table(arr,i,x,child); } } child[x]=1+c; return child[x]; } public static int lca(int x,int y) { if(depth[x]<depth[y]) { int temp=x; x=y; y=temp; } x=get_kth_ancestor(depth[x]-depth[y],x); if(x==y) return x; // System.out.println(x+" "+y); for(int i=log-1;i>=0;i--) { if(ancestor[x][i]!=ancestor[y][i]) { x=ancestor[x][i]; y=ancestor[y][i]; } } return ancestor[x][0]; } public static int get_kth_ancestor(int K,int x) { if(K==0) return x; int node=x; for(int i=0;i<log;i++) { if(K%2!=0) { node=ancestor[node][i]; } K/=2; } return node; } public static ArrayList<Integer> primeFactors(int n) { // Print the number of 2s that divide n ArrayList<Integer> factors=new ArrayList<>(); if(n%2==0) factors.add(2); while (n%2==0) { n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n if(n%i==0) factors.add(i); while (n%i == 0) { n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) { factors.add(n); } return factors; } static long ans=1,mod=1000000007; public static void recur(long X,long N,int index,ArrayList<Integer> temp) { // System.out.println(X); if(index==temp.size()) { System.out.println(X); ans=((ans%mod)*(X%mod))%mod; return; } for(int i=0;i<=60;i++) { if(X*Math.pow(temp.get(index),i)<=N) recur(X*(long)Math.pow(temp.get(index),i),N,index+1,temp); else break; } } public static int upper(ArrayList<Integer> temp,int X) { int l=0,r=temp.size()-1; while(l<=r) { int mid=(l+r)/2; if(temp.get(mid)==X) return mid; // System.out.println(mid+" "+temp.get(mid)); if(temp.get(mid)<X) l=mid+1; else { if(mid-1>=0 && temp.get(mid-1)>=X) r=mid-1; else return mid; } } return -1; } public static int lower(ArrayList<Integer> temp,int X) { int l=0,r=temp.size()-1; while(l<=r) { int mid=(l+r)/2; if(temp.get(mid)==X) return mid; // System.out.println(mid+" "+temp.get(mid)); if(temp.get(mid)>X) r=mid-1; else { if(mid+1<temp.size() && temp.get(mid+1)<=X) l=mid+1; else return mid; } } return -1; } public static int[] check(String shelf,int[][] queries) { int[] arr=new int[queries.length]; ArrayList<Integer> indices=new ArrayList<>(); for(int i=0;i<shelf.length();i++) { char ch=shelf.charAt(i); if(ch=='|') indices.add(i); } for(int i=0;i<queries.length;i++) { int x=queries[i][0]-1; int y=queries[i][1]-1; int left=upper(indices,x); int right=lower(indices,y); if(left<=right && left!=-1 && right!=-1) { arr[i]=indices.get(right)-indices.get(left)+1-(right-left+1); } else arr[i]=0; } return arr; } static boolean check; public static void check(ArrayList<ArrayList<Integer>> arr,int x,int[] color,boolean[] visited) { visited[x]=true; PriorityQueue<Integer> pq=new PriorityQueue<>(); for(int i:arr.get(x)) { if(color[i]<color[x]) pq.add(color[i]); if(color[i]==color[x]) check=false; if(!visited[i]) check(arr,i,color,visited); } int start=1; while(pq.size()>0) { int temp=pq.poll(); if(temp==start) ++start; else break; } if(start!=color[x]) check=false; } static boolean cycle; public static void cycle(boolean[] stack,boolean[] visited,int x,ArrayList<ArrayList<Integer>> arr) { if(stack[x]) { cycle=true; return; } visited[x]=true; for(int i:arr.get(x)) { if(!visited[x]) { cycle(stack,visited,i,arr); } } stack[x]=false; } public static int check(char[][] ch,int x,int y) { int cnt=0; int c=0; int N=ch.length; int x1=x,y1=y; while(c<ch.length) { if(ch[x][y]=='1') ++cnt; // if(x1==0 && y1==3) // System.out.println(x+" "+y+" "+cnt); x=(x+1)%N; y=(y+1)%N; ++c; } return cnt; } public static void s(char[][] arr,int x) { char start=arr[arr.length-1][x]; for(int i=arr.length-1;i>0;i--) { arr[i][x]=arr[i-1][x]; } arr[0][x]=start; } public static void shuffle(char[][] arr,int x,int down) { int N= arr.length; down%=N; char[] store=new char[N-down]; for(int i=0;i<N-down;i++) store[i]=arr[i][x]; for(int i=0;i<arr.length;i++) { if(i<down) { // Printing rightmost // kth elements arr[i][x]=arr[N + i - down][x]; } else { // Prints array after // 'k' elements arr[i][x]=store[i-down]; } } } public static String form(int C1,char ch1,char ch2) { char ch=ch1; String s=""; for(int i=1;i<=C1;i++) { s+=ch; if(ch==ch1) ch=ch2; else ch=ch1; } return s; } public static void printArray(long[] arr) { for(int i=0;i<arr.length;i++) System.out.print(arr[i]+" "); System.out.println(); } public static boolean check(long mid,long[] arr,long K) { long[] arr1=Arrays.copyOfRange(arr,0,arr.length); long ans=0; for(int i=0;i<arr1.length-1;i++) { if(arr1[i]+arr1[i+1]>=mid) { long check=(arr1[i]+arr1[i+1])/mid; // if(mid==5) // System.out.println(check); long left=check*mid; left-=arr1[i]; if(left>=0) arr1[i+1]-=left; ans+=check; } // if(mid==5) // printArray(arr1); } // if(mid==5) // System.out.println(ans); ans+=arr1[arr1.length-1]/mid; return ans>=K; } public static long search(long sum,long[] arr,long K) { long l=1,r=sum/K; while(l<=r) { long mid=(l+r)/2; if(check(mid,arr,K)) { if(mid+1<=sum/K && check(mid+1,arr,K)) l=mid+1; else return mid; } else r=mid-1; } return -1; } public static void check(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] dist) { for(int i:arr.get(x)) { if(i!=parent) { dist[i]=dist[x]+1; check(arr,i,x,dist); } } } public static void reconstruct(boolean[][] dp,int x,int y,int[] arr,ArrayList<Integer> ans) { if(y==0) return; if(arr[x]==0) return; if(y-arr[x]>=0 && dp[x-1][y-arr[x]]) { ans.add(arr[x]); reconstruct(dp,x-1,y-arr[x],arr,ans); } else { reconstruct(dp,x-1,y,arr,ans); } } public static long check(int[] arr,int height) { long even=0,odd=0; for(int i=0;i<arr.length;i++) { if(height%2==0) { if(arr[i]%2==0) { even+=(height-arr[i])/2; } else { odd+=1; even+=(height-arr[i])/2; } } else { if(arr[i]%2!=0) { even+=(height-arr[i])/2; } else { odd+=1; even+=(height-arr[i])/2; } } } long diff=Math.abs(even-odd); long odd1=odd,even1=even; // System.out.println(odd+" "+even); if(odd>even) { return even*2+(odd-even)*2-1; } else { long days=diff/3; odd+=days*2; even-=(days); long days1=days+1; odd1+=days1*2; even1-=(days1); // System.out.println(odd+" "+even+" "+odd1+" "+even1); // System.out.println(odd*2+(even-odd)*2+" "+(even1*2+(odd1-even1)*2-1)); return Math.min(odd*2+(even-odd)*2,even1*2+(odd1-even1)*2-1); } } public static void check(int x,ArrayList<ArrayList<Integer>> arr,int parent,int[] dist) { for(int i:arr.get(x)) { if(i!=parent) { dist[i]=dist[x]+1; check(i,arr,x,dist); } } } static int fin_t; public static void check(int[] dist1,int[] dist2,ArrayList<ArrayList<Integer>> arr,int x,int parent) { if(dist1[x]<=dist2[x]) return; fin_t=Math.max(fin_t, dist1[x]*2); for(int i:arr.get(x)) { if(i!=parent) { check(dist1,dist2,arr,i,x); } } } public static void main(String[] args) throws IOException { Reader.init(System.in); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); int T=Reader.nextInt(); for(int m=1;m<=T;m++) { int N=Reader.nextInt(); int k=Reader.nextInt(); char[] ch=Reader.next().toCharArray(); int[] tot=new int[N]; int cnt=0; for(int i=0;i<N;i++) { char ch1; if(k==0) break; if(cnt%2!=0) { if(ch[i]=='1') ch1='0'; else ch1='1'; } else ch1=ch[i]; if(ch1=='1') { if(k%2!=0) { tot[i]++; cnt++; k-=1; } } if(ch1=='0') { if(k%2==0) { tot[i]++; cnt++; k-=1; } } } tot[N-1]+=k; int mov=0; for(int i=0;i<N;i++) mov+=tot[i]; for(int i=0;i<N;i++) { if((mov-tot[i])%2!=0) output.write(1-(ch[i]-48)+""); else output.write(ch[i]); } output.write("\n"); for(int i=0;i<N;i++) output.write(tot[i]+" "); output.write("\n"); } output.flush(); } } 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 long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } } class TreeNode { int data; TreeNode left; TreeNode right; TreeNode(int data) { left=null; right=null; this.data=data; } } class div { int start; int left; div(int start,int left) { this.start=start; this.left=left; } }
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 11
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
0b785096b9e5ad4f6f3f3cf83a04b426
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 whatever; // 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 Ideone { 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(); int k = scn.nextInt(); String s = scn.next(); char[] ch = s.toCharArray(); if(k%2!=0){ for(int i=0;i<n;i++){ if(ch[i]=='0'){ ch[i]='1'; }else{ ch[i]='0'; } } } int[] ans = new int[n]; int i=0; while(k>0 && i<n){ if(ch[i]=='0'){ ch[i]='1'; k--; ans[i]++; } i++; } if(k>0){ if(k%2==0){ ans[n-1]+=k; }else{ if(ch[n-1]=='0'){ ch[n-1]='1'; }else{ ch[n-1]= '0'; } ans[n-1]+=k; } } System.out.println(String.valueOf(ch)); for(i=0;i<n;i++){ System.out.print(ans[i]+" "); } System.out.println(); t--; } } }
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 11
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
ada0274103b453dbb06327123347eb74
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 { public static void main(String[] args) throws Exception { FastIO in = new FastIO(); int t = in.nextInt(); for (int tc=0; tc<t; tc++) { int n = in.nextInt(); int k = in.nextInt(); char[] bits = in.next().toCharArray(); // in.pr.println(bits.toString()); int[] ans = new int[n]; if (k%2==1) { for (int i=0; i<n; i++) { if (bits[i]=='0') bits[i] = '1'; else bits[i] = '0'; } } for (int i=0; i<n-1; i++) { if (k==0) break; if (bits[i]=='0') { bits[i] = '1'; k--; ans[i]++; } } if (k>0) { if (k%2==1) { if (bits[n-1]=='1') { bits[n-1] = '0'; } else bits[n-1] = '1'; } ans[n-1]=k; } for (int i=0; i<n; i++) in.pr.print(bits[i]); in.pr.println(); for (int i=0; i<n; i++) in.pr.print(ans[i]+" "); in.pr.println(); } in.pr.close(); } static class FastIO { BufferedReader br; StringTokenizer st; PrintWriter pr; public FastIO() throws IOException { br = new BufferedReader( new InputStreamReader(System.in)); pr = new PrintWriter(System.out); } public String next() throws IOException { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } public String nextLine() throws IOException { String str = br.readLine(); 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 11
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
9a6c9f24727c2e2669797d34db2de635
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 { 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 k = in.nextInt(); int[] a = new int[n]; int[] b = new int[n]; char[] s = in.next().toCharArray(); for(int i = 0; i < n; i++) { a[i] = s[i] - '0'; } int toSwap = k % 2; int cnt = 0; for(int i = 0; i < n - 1; i++) { if(a[i] == toSwap && cnt < k) { cnt++; a[i] = 1; b[i] = 1; continue; } if(k % 2 == 1) { a[i] = 1 - a[i]; } } b[n-1] += k - cnt; if(cnt % 2 == 1) { a[n-1] = 1 - a[n-1]; } for(int i = 0; i < n; i++) { out.print(a[i]); } out.println(); for(int i = 0; i < n; i++) { out.print(b[i] + " "); } 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
["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 11
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
b47136f247305ad565dd4d157f8660a7
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 11
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
fa33b0022c56f256c89c985ca3c8e6df
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 javafx.util.Pair; public class CodeForces { public static void main(String[] args) throws FileNotFoundException { FastScanner fs = new FastScanner(); int tt = fs.nextInt(); while(tt-->0) { int n = fs.nextInt(), k = fs.nextInt(); char[] a = fs.next().toCharArray(); if((k&1)==1) { for(int i = 0; i < a.length; i++) { a[i] = a[i] == '0' ? '1' : '0'; } } int[] ans = new int[n]; for(int i = 0; i < a.length && k > 0; i++) { if(a[i] == '0') { a[i] = '1'; k--; ans[i]+=1; } } if(k%2!=0) { a[a.length-1] = a[a.length-1] == '1' ? '0' : '1'; ans[ans.length-1]+=1; k--; } ans[ans.length-1]+=k; StringBuilder sb2 = new StringBuilder(); StringBuilder sb = new StringBuilder(); for(int i = 0; i < ans.length; i++) { sb2.append(a[i]+""); sb.append(ans[i] + " "); } System.out.println(sb2.toString()); System.out.println(sb.toString()); } } public static int gcd(int a, int b) { if(b == 0) return a; return gcd(b, a%b); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(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()); } } }
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 11
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
815f7bc907aea9459200a80e4d1dccb7
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 cin=new Scanner(System.in); int t=cin.nextInt(); while(t-->0) { int n=cin.nextInt(); int k=cin.nextInt(); StringBuilder s=new StringBuilder(cin.next()); StringBuilder s1=new StringBuilder(); StringBuilder s2=new StringBuilder(); if((k&1)==0) { for(int i=0;i<n-1;i++) { if(s.charAt(i)=='0') { if(k>0) { s1.append("1"); s2.append(1+" "); k--; } else{ s1.append("0"); s2.append(0+" "); } } else { s1.append("1"); s2.append(0+" "); } } if((s.charAt(n-1)=='0'&&(k%2==0))||(s.charAt(n-1)=='1'&&(k%2==1))) { s1.append("0"); s2.append(k); } else{ s1.append("1"); s2.append(k); } } else{ for(int i=0;i<n-1;i++) { if(s.charAt(i)=='1') { if(k>0) { s1.append("1"); s2.append(1+" "); k--; } else{ s1.append("0"); s2.append(0+" "); } } else { s1.append("1"); s2.append(0+" "); } } if((s.charAt(n-1)=='0'&&(k%2==0))||(s.charAt(n-1)=='1'&&(k%2==1))) { s1.append("1"); s2.append(k); } else{ s1.append("0"); s2.append(k); } } System.out.println(s1); System.out.println(s2); } } }
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 11
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
a475db81bb70bf9fb645a28da5a69e9f
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 { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int tt = Integer.parseInt(in.readLine()); while (tt-- > 0) { String[] tokens = in.readLine().split("\\s+"); int n = Integer.parseInt(tokens[0]); int kfixed = Integer.parseInt(tokens[1]); int k = kfixed; StringBuilder str = new StringBuilder(in.readLine()); StringBuilder res = new StringBuilder(); StringBuilder flips = new StringBuilder(); int indexOfFirst1 = 0; while (indexOfFirst1 < str.length()-1 && str.charAt(indexOfFirst1) != '1') { indexOfFirst1++; } if (k%2 == 0){ for (int i = 0; i < str.length(); i++) { if (k > 0) { if (str.charAt(i) == '0') { flips.append(1); flips.append(" "); k--; } else { flips.append(0); flips.append(" "); } } else { flips.append(0); flips.append(" "); } } } else{ StringBuilder flipped = new StringBuilder(); k--; for (int i = 0; i < str.length(); i++){ if (i != indexOfFirst1){ flipped.append(str.charAt(i) == '0' ? 1: 0); } else { flipped.append(str.charAt(i)); } } for (int i = 0; i < flipped.length(); i++){ if (k > 0) { if (flipped.charAt(i) == '0') { flips.append(1 + (indexOfFirst1 == i ? 1 : 0)); flips.append(" "); k--; } else{ flips.append(0 + (indexOfFirst1 == i ? 1 : 0)); flips.append(" "); } } else { flips.append(0 + (indexOfFirst1 == i ? 1 : 0)); flips.append(" "); } } } int lastAdd = k + flips.charAt(flips.length()-2)-'0'; if (k > 0){ int add = flips.charAt(flips.length()-2)-'0'; flips.delete(flips.length()-2,flips.length()); flips.append(k + add); } for (int i = 0; i < str.length()-1; i++){ int curr = str.charAt(i)-'0'; if (kfixed%2 == 0){ res.append((flips.charAt(2*i)-'0')%2 == 0 ? curr : Math.abs(curr-1)); } else{ res.append((flips.charAt(2*i)-'0')%2 == 1 ? curr : Math.abs(curr-1)); } } int curr = str.charAt(str.length()-1)-'0'; if (kfixed%2 == 0){ res.append(lastAdd%2==0 ? curr : Math.abs(curr-1)); } else{ res.append(lastAdd%2==1 ? curr : Math.abs(curr-1)); } System.out.println(res.toString()); System.out.println(flips.toString()); } } }
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 11
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
517a4b983d21bc335ee8857e9d5dcb9f
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.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],k=d[1]; int arr[]=new int[n]; String str=sc.readString(); int a[]=new int[n]; for(int i=0;i<n;i++){ if(str.charAt(i)=='1'){ a[i]=1; } } if(k%2==0){// even int count=0; for(int i=0;i<n;i++){ if(a[i]==0){ count++; } } int g=0; for(int i=0;i<n ;i++){ if(a[i]==0 && g<Math.min(k,count)){ a[i]=1; g++; arr[i]=1; }else{ if(a[i]==0){ }else{ } } } g=Math.max(0,k-g); if(g%2!=0){ if(a[n-1]==0) a[n-1]=1; else a[n-1]=0; arr[n-1]+=g; }else arr[n-1]+=g; for(int i=0;i<n;i++){ sb.append(a[i]); } }else{// odd int count=0; for(int i=0;i<n;i++){ if(a[i]==1){ count++; } } int g=0; for(int i=0;i<n;i++){ if(a[i]==1 && g<Math.min(k,count)){ a[i]=1; g++; arr[i]=1; }else{ if(a[i]==1){ a[i]=0; }else a[i]=1; } } g=Math.max(0,k-g); if(g%2!=0){ if(a[n-1]==0) a[n-1]=1; else a[n-1]=0; arr[n-1]+=g; }else{ arr[n-1]+=g; } for(int i=0;i<n;i++){ sb.append(a[i]); } } sb.append("\n"); for(int i=0;i<n;i++){ sb.append(arr[i]+" "); } 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
["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 11
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
3b2d3a60497a2cebda13addeff41cc8a
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
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA import java.util.*; import java.util.Map.Entry; import java.util.stream.*; import java.lang.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.io.*; public class CodeForces { static private final String INPUT = "input.txt"; static private final String OUTPUT = "output.txt"; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); static DecimalFormat df = new DecimalFormat("0.00000"); final static int mod = (int) (1e9 + 7); final static int MAX = Integer.MAX_VALUE; final static int MIN = Integer.MIN_VALUE; final static long INF = Long.MAX_VALUE; final static long NEG_INF = Long.MIN_VALUE; static Random rand = new Random(); // ======================= MAIN ================================== public static void main(String[] args) throws IOException { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; // ==== start ==== input(); preprocess(); int t = 1; t = readInt(); while (t-- > 0) { solve(); } out.flush(); // ==== end ==== if (!oj) System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } private static void solve() throws IOException { int n = readInt(), k = readInt(), K = k; int[] arr = new int[n]; String str = readLine(); StringBuilder sb = new StringBuilder(str); int i = 0; boolean flag = false; while (i < n && k > 0) { if (flag) sb.setCharAt(i, sb.charAt(i) == '0' ? '1' : '0'); if (!((k & 1) != sb.charAt(i) - '0' || i == n - 1)) { arr[i]++; k--; flag = !flag; } i++; } arr[n - 1] += k; StringBuilder ans = new StringBuilder(); for (i = 0; i < n; i++) { if (((K - arr[i]) & 1) == 1) ans.append(str.charAt(i) == '0' ? '1' : '0'); else ans.append(str.charAt(i)); } out.println(ans); printIArray(arr); } // private static void solve() throws IOException { // int n = readInt(), a = readInt(), b = readInt(); // int[] arr = readIntArray(n); // sort(arr); // long[] suf = new long[n + 1]; // for (int i = n - 1; i >= 0; i--) // suf[i] = suf[i + 1] + arr[i]; // int cap = 0; // long ans = 0L; // for (int i = 0; i < n; i++) { // ans += 1L * b * (arr[i] - cap); // if (1L * b * (suf[i + 1] - cap) >= 1L * a * (arr[i] - cap) + 1L * b * (suf[i // + 1] - arr[i])) { // cap = arr[i]; // ans += 1L * a * (arr[i] - cap); // } // } // out.println(ans); // } private static void preprocess() throws IOException { } // cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces // javac CodeForces.java // java CodeForces // javac CodeForces.java && java CodeForces // ==================== CUSTOM CLASSES ================================ static class Pair { int first, second; Pair(int f, int s) { first = f; second = s; } public int compareTo(Pair o) { if (this.first == o.first) return this.second - o.second; return this.first - o.first; } @Override public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (this.getClass() != obj.getClass()) return false; Pair other = (Pair) (obj); if (this.first != other.first) return false; if (this.second != other.second) return false; return true; } @Override public int hashCode() { return this.first ^ this.second; } @Override public String toString() { return this.first + " " + this.second; } } static class DequeNode { DequeNode prev, next; int val; DequeNode(int val) { this.val = val; } DequeNode(int val, DequeNode prev, DequeNode next) { this.val = val; this.prev = prev; this.next = next; } } // ======================= FOR INPUT ================================== private static void input() { FileInputStream instream = null; PrintStream outstream = null; try { instream = new FileInputStream(INPUT); outstream = new PrintStream(new FileOutputStream(OUTPUT)); System.setIn(instream); System.setOut(outstream); } catch (Exception e) { System.err.println("Error Occurred."); } br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(readLine()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readString() throws IOException { return next(); } static String readLine() throws IOException { return br.readLine().trim(); } static int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = readInt(); return arr; } static int[][] read2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = readIntArray(m); return arr; } static List<Integer> readIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readInt()); return list; } static long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = readLong(); return arr; } static long[][] read2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = readLongArray(m); return arr; } static List<Long> readLongList(int n) throws IOException { List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readLong()); return list; } static char[] readCharArray(int n) throws IOException { return readString().toCharArray(); } static char[][] readMatrix(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = readCharArray(m); return mat; } // ========================= FOR OUTPUT ================================== private static void printIList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIArray(arr[i]); } private static void printLArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLArray(arr[i]); } // ====================== TO CHECK IF STRING IS NUMBER ======================== private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // ==================== FASTER SORT ================================ private static void sort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void sort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // ==================== MATHEMATICAL FUNCTIONS =========================== private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static long power(long a, long b) { if (b == 0) return 1L; long ans = power(a, b >> 1); ans *= ans; if ((b & 1) == 1) ans *= a; return ans; } private static int mod_power(int a, int b, int mod) { if (b == 0) return 1; int temp = mod_power(a, b >> 1, mod); temp %= mod; temp = (int) ((1L * temp * temp) % mod); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % mod); return temp; } private static int multiply(int a, int b) { return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod); } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } private static long nCr(long n, long r) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans *= i; for (long i = 2; i <= n - r; i++) ans /= i; return ans; } // ==================== Primes using Seive ===================== private static List<Integer> getPrimes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = i * i; j <= n; j += i) prime[j] = false; } } // return prime; List<Integer> list = new ArrayList<>(); for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } private static int[] SeivePrime(int n) { int[] primes = new int[n]; for (int i = 0; i < n; i++) primes[i] = i; for (int i = 2; i * i < n; i++) { if (primes[i] != i) continue; for (int j = i * i; j < n; j += i) { if (primes[j] == j) primes[j] = i; } } return primes; } // ==================== STRING FUNCTIONS ================================ private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // ==================== LIS & LNDS ================================ private static int LIS(int arr[], int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find1(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // =============== Lower Bound & Upper Bound =========== // less than or equal private static int lower_bound(List<Integer> list, int val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(List<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(List<Long> list, long val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== UNION FIND ===================== private static int find(int x, int[] parent) { if (parent[x] == x) return x; return parent[x] = find(parent[x], parent); } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) return false; if (rank[lx] > rank[ly]) parent[ly] = lx; else if (rank[lx] < rank[ly]) parent[lx] = ly; else { parent[lx] = ly; rank[ly]++; } return true; } // ================== SEGMENT TREE (RANGE SUM & RANGE UPDATE) ================== public static class SegmentTree { int n; int[] arr, tree, lazy; SegmentTree(int arr[]) { this.arr = arr; this.n = arr.length; this.tree = new int[(n << 2)]; this.lazy = new int[(n << 2)]; build(1, 0, n - 1); } void build(int id, int start, int end) { if (start == end) tree[id] = arr[start]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; build(left, start, mid); build(right, mid + 1, end); tree[id] = tree[left] + tree[right]; } } void update(int l, int r, int val) { update(1, 0, n - 1, l, r, val); } void update(int id, int start, int end, int l, int r, int val) { distribute(id, start, end); if (end < l || r < start) return; if (start == end) tree[id] += val; else if (l <= start && end <= r) { lazy[id] += val; distribute(id, start, end); } else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; update(left, start, mid, l, r, val); update(right, mid + 1, end, l, r, val); tree[id] = tree[left] + tree[right]; } } int query(int l, int r) { return query(1, 0, n - 1, l, r); } int query(int id, int start, int end, int l, int r) { if (end < l || r < start) return 0; distribute(id, start, end); if (start == end) return tree[id]; else if (l <= start && end <= r) return tree[id]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r); } } void distribute(int id, int start, int end) { if (start == end) tree[id] += lazy[id]; else { tree[id] += lazy[id] * (end - start + 1); lazy[(id << 1)] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; } lazy[id] = 0; } } // ==================== TRIE ================================ static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } void insert(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) curr.children[ch - 'a'] = new Node(); curr = curr.children[ch - 'a']; } curr.isEnd = true; } boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) return false; curr = curr.children[ch - 'a']; } return curr.isEnd; } } // ==================== FENWICK TREE ================================ static class FT { long[] tree; int n; FT(int[] arr, int n) { this.n = n; this.tree = new long[n + 1]; for (int i = 1; i <= n; i++) { update(i, arr[i - 1]); } } void update(int idx, int val) { while (idx <= n) { tree[idx] += val; idx += idx & -idx; } } long query(int l, int r) { return getSum(r) - getSum(l - 1); } long getSum(int idx) { long ans = 0L; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } } // ==================== BINARY INDEX TREE ================================ static class BIT { long[][] tree; int n, m; BIT(int[][] mat, int n, int m) { this.n = n; this.m = m; tree = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { update(i, j, mat[i - 1][j - 1]); } } } void update(int x, int y, int val) { while (x <= n) { int t = y; while (t <= m) { tree[x][t] += val; t += t & -t; } x += x & -x; } } long query(int x1, int y1, int x2, int y2) { return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1); } long getSum(int x, int y) { long ans = 0L; while (x > 0) { int t = y; while (t > 0) { ans += tree[x][t]; t -= t & -t; } x -= x & -x; } return ans; } } }
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 11
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
3f7fb73f569699beb5e877c031a26394
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 practiceb { static FastScanner sc; static int ans; static long[] arr, arr1; static char[][] board; static long mul, mul1; static int[] spf; static char[] ch; static int n, q, m; static long k, a, x, c, y; static StringBuilder sb, sb1; static long b, sum; static Map<Integer, List<Integer>> map; static Map<Long, List<Integer>> map2; static Map<Long, List<Integer>> map3; static Map<Integer, Integer> shortest; static Map<Integer, Integer> map1, in; static List<Integer> list, list1; static long[] count; static boolean[] vis; static boolean flag; static long total, xor1; // static class Node{ // long dist; // Set<Integer> set; // Node(){ // this.set = new HashSet<>(); // } // Node(int f){ // this.dist = f; // this.set = new HashSet<>(); // } // } // public static class Lca { // int[] depth; // int[] dfs_order; // int cnt; // int[] first; // int[] minPos; // int n; // void dfs(List<Integer>[] tree, int u, int d) { // depth[u] = d; // dfs_order[cnt++] = u; // for (int v : tree[u]) // if (depth[v] == -1) { // dfs(tree, v, d + 1); // dfs_order[cnt++] = u; // } // } // void buildTree(int node, int left, int right) { // if (left == right) { // minPos[node] = dfs_order[left]; // return; // } // int mid = (left + right) >> 1; // buildTree(2 * node + 1, left, mid); // buildTree(2 * node + 2, mid + 1, right); // minPos[node] = depth[minPos[2 * node + 1]] < depth[minPos[2 * node + 2]] ? minPos[2 * node + 1] : minPos[2 * node + 2]; // } // public Lca(List<Integer>[] tree, int root) { // int nodes = tree.length; // depth = new int[nodes]; // Arrays.fill(depth, -1); // n = 2 * nodes - 1; // dfs_order = new int[n]; // cnt = 0; // dfs(tree, root, 0); // minPos = new int[4 * n]; // buildTree(0, 0, n - 1); // first = new int[nodes]; // Arrays.fill(first, -1); // for (int i = 0; i < dfs_order.length; i++) // if (first[dfs_order[i]] == -1) // first[dfs_order[i]] = i; // } // public int lca(int a, int b) { // return minPos(Math.min(first[a], first[b]), Math.max(first[a], first[b]), 0, 0, n - 1); // } // int minPos(int a, int b, int node, int left, int right) { // if (a == left && right == b) // return minPos[node]; // int mid = (left + right) >> 1; // if (a <= mid && b > mid) { // int p1 = minPos(a, Math.min(b, mid), 2 * node + 1, left, mid); // int p2 = minPos(Math.max(a, mid + 1), b, 2 * node + 2, mid + 1, right); // return depth[p1] < depth[p2] ? p1 : p2; // } else if (a <= mid) { // return minPos(a, Math.min(b, mid), 2 * node + 1, left, mid); // } else if (b > mid) { // return minPos(Math.max(a, mid + 1), b, 2 * node + 2, mid + 1, right); // } else { // throw new RuntimeException(); // } // } // } // private static long dfs(int v, int parent,long ht) { // List<Integer> list = map.get(v); // hyt[v] = ht; // if(list.size() == 1){ // if(!map3.containsKey(ht)) // map3.put(ht,new ArrayList<>()); // map3.get(ht).add(v); // return ht; // } // long maxl = -1; // for(Integer i : list){ // if(i == parent) // continue; // maxl = Math.max(maxl,dfs(i,v,ht+1)); // } // return maxl; // } // public static int N = 100005; // long a[N]; // long sfx[N]; // long pfx[N]; // public static boolean f(int width){ // // // long[] a = new long[n]; // long[] sfx = new long[n]; // long[] pfx = new long[n]; // for (int j = 0; j < n; j++){ // if (j % width == 0){ // pfx[j] = a[j]; // } // else pfx[j] = gcd(pfx[j - 1], a[j]); // } // for (int j = n - 2; j >= 0; j--){ // if (j % width == width - 1){ // sfx[j] = a[j]; // } // else sfx[j] = gcd(sfx[j + 1], a[j]); // } // // for(int j = 0; j < n - width + 1 ; j++){ // if (gcd(sfx[j], pfx[j + width - 1]) >= k) return true; // if (((j % width) == width - 1) && pfx[j] >= k){ // return true; // } // } // return false; // } public static class Node { int first; int second; Node(int v, int second) { this.first = v; this.second = second; } } public static long lcm(long a, long b) { return (b / gcd(b, a % b)) * a; } public static void SpecialSieve(int MAXN) { spf = new int[MAXN]; spf[1] = 1; for (int i = 2; i < MAXN; i++) // marking smallest prime factor for every // number to be itself. spf[i] = i; // separately marking spf for every even // number as 2 for (int i = 4; i < MAXN; i += 2) spf[i] = 2; for (int i = 3; i * i < MAXN; i++) { // checking if i is prime if (spf[i] == i) { // marking SPF for all numbers divisible by i for (int j = i * i; j < MAXN; j += i) // marking spf[j] if it is not // previously marked if (spf[j] == j) spf[j] = i; } } } public static void solve() { /* * * 5 6 3 5 6 21 30 -5*6 5*3 5*3 5*3 * * */ char[] ch1 = new char[n]; for(int i = 0 ; i < n ; i++) ch1[i] = ch[i]; k = a; boolean flag = false; List<Long> list = new ArrayList<>(); for(int i = 0 ; i < n && k > 0; i++) { if(flag) { if(ch[i] == '0') { ch[i] = '1'; } else { ch[i] = '0'; } } if(ch[i] == '0') { if(k%2 == 0) { list.add(1L); flag = !flag; k -= 1; } else { list.add(0L); } } else { if(k%2 == 1) { list.add(1L); flag = !flag; k -= 1; } else { list.add(0L); } } } if(k > 0) { list.set(list.size()-1,list.get(list.size()-1)+k); } while(list.size() != n) { list.add(0L); } for(int i = 0 ; i < list.size() ; i++) { long switched = a - list.get(i); if(switched%2 == 0) { sb.append(ch1[i]); } else { if(ch1[i] == '0') sb.append("1"); else sb.append("0"); } } sb.append("\n"); for(int i = 0 ; i < n ; i++) { // if((list.size()) <= (i+1)) // list.add(0L); sb.append(list.get(i)).append(" "); } sb.append("\n"); } public static void main(String[] args) { sc = new FastScanner(); // Scanner sc = new Scanner(System.in); // SpecialSieve(5000000+5); // long num = 1; // list = new ArrayList<>(); // for(int i = 0 ; i <= 60 ; i++) { // list.add(num); // num *= 2; // } int t = sc.nextInt(); // SpecialSieve(10001); // fact(200000+1); sb = new StringBuilder(); // int t = 1; while (t > 0) { // a = sc.nextLong(); // b = sc.nextLong(); // c = sc.nextLong(); n = sc.nextInt(); a = sc.nextLong(); // b = sc.nextLong(); // x = sc.nextLong(); // m = sc.nextInt(); // k = sc.nextInt(); // mat = new int[2][m]; // for(int i = 0 ; i < 2 ; i++) // for(int j = 0 ; j < m ; j++) // mat[i][j] = sc.nextLong(); // for(int i = 1 ; i <= 15 ; i++){ // m = i; // solve(); // // } // board = new char[3][3]; // for(int i = 0 ; i < 3 ; i++){ // String s = sc.next(); // for(int j = 0 ; j < 3 ; j++){ // board[i][j] = s.charAt(j); // } // } // x = sc.nextInt(); ch = sc.next().toCharArray(); // q = sc.nextInt(); // ch = sc.next().toCharArray(); // arr = new long[n]; // for (int i = 0; i < n; i++) // arr[i] = sc.nextLong(); // ch = sc.next().toCharArray(); // arr1 = new long[m]; // for(int i = 0 ; i < m ; i++) // arr1[i] = sc.nextLong(); // // a = new long[n-1]; // for(int i = 1 ; i < n ; i++) // a[i-1] = Math.abs(arr[i] - arr[i-1]); // // n -= 1; solve(); t -= 1; } System.out.print(sb); } // Use this instead of Arrays.sort() on an array of ints. Arrays.sort() is n^2 // worst case since it uses a version of quicksort. Although this would never // actually show up in the real world, in codeforces, people can hack, so // this is needed. 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; } //then sort Arrays.sort(a); } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static int log(double n, double base) { if (n == 0 || n == 1) return 0; if (n == base) return 1; double num = Math.log(n); double den = Math.log(base); if (den == 0) return 0; return (int) (num / den); } public static long mod(long x, long mod) { long result = x % mod; if (result < 0) { result += mod; } return result; } // 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(); } 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 11
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
a331b2f32cf12b5565db8f6dd0fc4d6a
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[] args) { Scanner sc = new Scanner(System.in); int T = Integer.parseInt(sc.next()); while(T-->0){ int n = Integer.parseInt(sc.next()); int k = Integer.parseInt(sc.next()); int K = k; String bits = sc.next(); char[] c = bits.toCharArray(); int[] times = new int[n]; for(int i =0;i<n && k>0;i++){ if(K%2==1 && c[i]=='1'){ times[i]++; k--; }else if(K%2==0 && c[i]=='0'){ times[i]++; k--; } } times[n-1] += k; // if(k%2==0){ // for(int i = 0;i<n && k>0;i++){ // if(bits.charAt(i)=='0'){ // times[i]++; // k--; // } // } // times[n-1]+=k; // }else{ // for(int i = 0;i<n && k>0;i++){ // if(bits.charAt(i)=='1'){ // times[i]++; // k--; // } // } // times[n-1]+=k; // } StringBuilder p=new StringBuilder(bits); for(int i=0;i<n;i++) { if((K-times[i])%2==1) { p.setCharAt(i,(char)('1'-(c[i]-'0'))); } } System.out.println(p); for(int i = 0;i<n;i++){ System.out.print(times[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
a61080810d78b2f053aeda9a12449f6b
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 Main2 { static void solve() { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); for (int i = 0; i < t; i++) { int n = scan.nextInt(); int m = scan.nextInt(); solveCase(n, m); } } static void solve2() { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); for (int i = 0; i < t; i++) { int n = scan.nextInt(); int k = scan.nextInt(); // int[] arr = new int[n]; char[] arr = scan.next().toCharArray(); solveCase2(n, k, arr); } } static void solveCase2(int n, int k, char[] arr) { if(k%2 != 0) { for(int i = 0; i < n; i++) { arr[i] = arr[i] == '0' ? '1' : '0'; } } int[] fr = new int[n]; for(int i = 0; i < n; i++) { if(k <= 0) { break; } if(arr[i] == '0') { arr[i] = '1'; fr[i]++; k--; } } fr[n - 1]+=k; if(k > 0 && k % 2 != 0) { arr[n - 1] = arr[n - 1] == '0' ? '1' : '0'; } System.out.println(arr); for(int v : fr) { System.out.print(v + " "); } System.out.println(); } static void solveCase(int n, int m) { char dir = '0'; int x = 0; int y = 0; int cnt = 0; if(n == 1 && m == 1) { System.out.println(0); return; } if(n == 1 && m > 2) { System.out.println(-1); return; } if(m == 1 && n > 2) { System.out.println(-1); return; } if((n == 1 && m == 2) || (n == 2 && m == 1)) { System.out.println(1); return; } int diff = Math.abs(n - m); int min = Math.min(n,m); if(diff % 2 == 0) { cnt = (min-1)*2 + diff/2 + diff/2*3; } else { cnt = (min-1)*2 + diff/2+1 + diff/2*3; } System.out.println(cnt); } public static void main(String[] args) { solve2(); } static class Node implements Comparable<Node> { long val; Node parent; List<Node> children; long max; boolean isLeave; public Node(int val) { this.val = val; this.max = val; isLeave = true; children = new ArrayList<>(); } @Override public int compareTo(Node node) { if(this.max < node.max) { return -1; } else if(this.max > node.max) { return 1; } return 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
c0907cfbd1ae449e2e26857cef932ab2
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 int[] arr = new int[300000]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); while(t>0){ int n=sc.nextInt(); int k = sc.nextInt(); sc.nextLine(); String bt = sc.nextLine(); int swi=0; StringBuilder stringBuilder = new StringBuilder(); for(int i=0;i<n-1;i++){ arr[i]=0; if(bt.charAt(i)=='1'){ if(k%2==0){ stringBuilder.append('1'); } else if(swi<k){ stringBuilder.append('1'); arr[i]=1; swi++; } else{ stringBuilder.append('0'); } } else { if(k%2==0 && swi < k){ stringBuilder.append('1'); arr[i]=1; swi++; } else if(k%2==0){ stringBuilder.append('0'); } else{ stringBuilder.append('1'); } } } arr[n-1]=0; arr[n-1]+=(k-swi); if(bt.charAt(n-1)=='1'){ if(swi%2==1){ stringBuilder.append('0'); }else{ stringBuilder.append('1'); } } else { if(swi%2==0){ stringBuilder.append('0'); }else{ stringBuilder.append('1'); } } System.out.println(stringBuilder); for(int i=0;i<n;i++){ System.out.print(arr[i]+" "); } System.out.println(); t--; } } }
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
46d8971a8213ec0576bb586bd4a35ca6
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.*; import java.util.*; public class B16559 { public static void main(String[] args) throws IOException{ StringBuffer ans = new StringBuffer(); StringTokenizer st; BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(f.readLine()); int t = Integer.parseInt(st.nextToken()); for(int j = 0; j < t; j++){ st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); int used = 0; st = new StringTokenizer(f.readLine()); String str = st.nextToken(); // if(j == 99){ // System.out.println(k + " " + str); // System.exit(0); // } StringBuffer str1 = new StringBuffer(); int[] changes = new int[n]; int p = -1; if(k % 2 == 1) for(int i = 0; i < n-1; i++){ if(str.charAt(i) == '1' && used == 0){ p = i; used++; str1.append(1); }else{ str1.append((str.charAt(i)-48)^1); } } if(k % 2 == 1){ if(used == 0){ str1.append(str.charAt(n-1)); p = n-1; }else str1.append((str.charAt(n-1)-48)^1); str = str1.toString(); k--; used = 0; } //System.out.println(used + " " + k); for(int i = 0; i < n && used < k; i++){ //System.out.println(str.charAt(i)); if(str.charAt(i) == '0'){ //System.out.println("here"); changes[i]++; used++; } } //System.out.println(str + " " + Arrays.toString(changes)); changes[n-1]+=k-used; for(int i = 0; i < n; i++){ //System.out.println(str.charAt(i)-48 + " " + (2-changes[i])%2); ans.append((str.charAt(i)-48)^((k-changes[i])%2)); } ans.append("\n"); if(p != -1) changes[p]++; for(int i = 0; i < n; i++){ ans.append(changes[i]).append(" "); } ans.append("\n"); } System.out.println(ans); f.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
8853dc9ecf29cf442cc61a538ca65106
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 class1 { public static void main(String[] args) { Scanner input =new Scanner(System.in); int t=input.nextInt(); while(t-->0) { int n=input.nextInt(); int k=input.nextInt(); int m=k,x=0; String s=input.next(); char c[]=s.toCharArray(); StringBuilder p=new StringBuilder(s); char d[]=new char[n]; int f[]=new int[n]; int tmpk=k; for(int i=0;i<n && tmpk>0;i++) { if(k%2==c[i]-'0') { f[i]=1; tmpk--; } } f[n-1]+=tmpk; for(int i=0;i<n;i++) { if((k-f[i])%2==1) { p.setCharAt(i,(char)('1'-(c[i]-'0'))); } } System.out.println(p); for(int i=0;i<n;i++) { System.out.print(f[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
d09089990191e1130b8e054407e223a7
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; public class Codeforces782_2 { public static void main(String[] args) { Scanner input =new Scanner(System.in); int t= input.nextInt(); // System.out.println(t); for(int i=0;i<t;i++){ long k; int n; n= input.nextInt(); k= input.nextLong(); String s= input.next(); StringBuilder ss=new StringBuilder(s); // System.out.println(s+" "+ss); // char [] s1=new char[n]; // for(int j=0;j<n;j++){ // s1[j]=s.charAt(j); // } long [] f=new long[n]; // long flag[]=new long[n]; // int count=0; if(k%2==0){ for(int j=0;j<n;j++){ // System.out.println(j); if(s.charAt(j)=='0'){ if(k>0) { k--; // s1[j] = '1'; ss.setCharAt(j,'1'); f[j]++; }else{ // s1[j]='0'; ss.setCharAt(j,'0'); } } else{ // s1[j]='1'; ss.setCharAt(j,'1'); } } if(k>0){ f[n-1]=f[n-1]+k; if(k%2!=0){ // s1=s1.substring(0,n-1); // s1[n-1]='0'; ss.setCharAt(n-1,'0'); } } } else{ for(int j=0;j<n;j++){ if(s.charAt(j)=='1'){ if(k>0){ k--; // s1[j]='1'; ss.setCharAt(j,'1'); f[j]++; } else{ // s1[j]='0'; ss.setCharAt(j,'0'); } } else{ // s1[j]='1'; ss.setCharAt(j,'1'); } } if(k>0){ f[n-1]=f[n-1]+k; if(k%2!=0){ // s1=s1.substring(0,n-1); // s1[n-1]='0'; ss.setCharAt(n-1,'0'); } } } System.out.println(ss); // for(int j=0;j<n;j++){ // System.out.print(s1[j]); // } // System.out.println(); for(int j=0;j<n;j++){ System.out.print(f[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
ae6276384972d76df7c94d8c6e82735b
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 Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); StringBuilder sb = new StringBuilder(); Map<Integer, Integer> map = new HashMap<>(); int index = 0; int tempK = k; if(k % 2 == 0) { while(index < n && k > 0) { if(s.charAt(index) == '1') { sb.append("1"); } else { sb.append("1"); k--; map.put(index, map.getOrDefault(index, 0) + 1); } index++; } } else { while(index < n && k > 0) { if(s.charAt(index) == '1') { sb.append("1"); k--; map.put(index, map.getOrDefault(index, 0) + 1); } else { sb.append("1"); } index++; } } if(k > 0) { if (k % 2 != 0) { sb.replace(n - 1, n, String.valueOf(sb.charAt(n - 1) == '0' ? '1' : '0')); } map.put(index-1, map.getOrDefault(index-1, 0) + k); } else { while(index < n) { if (tempK % 2 == 0) sb.append(s.charAt(index)); else sb.append(s.charAt(index) == '0' ? '1' : '0'); index++; } } System.out.println(sb.toString()); for(int i = 0; i < n; i++) { System.out.print(map.getOrDefault(i, 0) + " "); } 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
e664fe8452adcb34d11a3502827a7599
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 Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); StringBuilder sb = new StringBuilder(); Map<Integer, Integer> map = new HashMap<>(); if(k % 2 == 0) { int index = 0; while(index < n && k > 0) { if(s.charAt(index) == '1') { sb.append("1"); } else { sb.append("1"); k--; map.put(index, map.getOrDefault(index, 0) + 1); } index++; } if(k > 0) { if(k % 2 == 0) { map.put(index-1, map.getOrDefault(index-1, 0) + k); } else { sb.replace(n-1, n, String.valueOf(sb.charAt(n-1) == '0' ? '1' : '0')); map.put(index-1, map.getOrDefault(index-1, 0) + k ); } } else { while(index < n) { sb.append(s.charAt(index)); index++; } } System.out.println(sb.toString()); sb.setLength(0); for(int i = 0; i < n; i++) { System.out.print(map.getOrDefault(i, 0) + " "); } System.out.println(); } else { int index = 0; while(index < n && k > 0) { if(s.charAt(index) == '1') { sb.append("1"); k--; map.put(index, map.getOrDefault(index, 0) + 1); } else { sb.append("1"); } index++; } if(k > 0) { if(k % 2 == 0) { map.put(index-1, map.getOrDefault(index-1, 0) + k); } else { sb.replace(n-1, n, String.valueOf(sb.charAt(n-1) == '0' ? '1' : '0')); map.put(index-1, map.getOrDefault(index-1, 0) + k); } } else { while(index < n) { sb.append(s.charAt(index) == '0' ? '1' : '0'); index++; } } System.out.println(sb.toString()); for(int i = 0; i < n; i++) { System.out.print(map.getOrDefault(i, 0) + " "); } 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
dc0a6aedf071e6454940f08159883a4f
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; public class ProblemC1 { static int[] moveCount; static int k; public static char[] reverseStr(char[] chars) { char[] reverseChars = new char[chars.length]; for (int i = 0; i < chars.length; i++) { reverseChars[i] = chars[i] == '0' ? '1' : '0'; } return reverseChars; } public static void printResult(String binaryStr, int n) { moveCount = new int[n]; char[] chars = binaryStr.toCharArray(); char[] reverseChars = reverseStr(chars); char[] temp = chars; int i; for (i = 0; i < n && k > 0; i++) { if ((temp[i] == '1' && k % 2 == 0) || (temp[i] == '0' && k % 2 == 1)) { chars[i] = '1'; continue; } if ((temp[i] == '1' && k % 2 == 1) || (temp[i] == '0' && k % 2 == 0)) { if (temp == chars) { temp = reverseChars; } else { temp = chars; } chars[i] = '1'; k--; moveCount[i]++; } } for (; i < n; i++) { chars[i] = temp[i]; } if (k % 2 == 1) { chars[n - 1] = '0'; } moveCount[n - 1] += k; System.out.println(new String(chars)); printResult(moveCount); } public static void printResult(int[] result) { for (int i : result) { System.out.print(i + " "); } System.out.println(); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = Integer.parseInt(in.nextLine()); for (int i = 0; i < t; i++) { String[] s = in.nextLine().split(" "); int n = Integer.parseInt(s[0]); k = Integer.parseInt(s[1]); String binaryStr = in.nextLine(); printResult(binaryStr, n); } 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
151ee7a932d598d42ba222c6c5df5229
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 Main { private static void solve(String s, int n, int k){ int[] count = new int[n]; int remain = k; for(int i = 0; i < n; i++){ if(remain == 0){ break; } int bit = s.charAt(i) - '0'; if(k % 2 == 0){ if(bit == 0){ count[i]++; remain--; } } else{ if(bit == 1){ count[i]++; remain--; } } } if(remain > 0){ count[n-1] += remain; } for(int i = 0; i < n; i++){ int flip = k - count[i]; int bit = s.charAt(i) - '0'; out.print(bit ^ (flip % 2)); } out.print("\n"); printArray(count); } public static void main(String[] args){ MyScanner scanner = new MyScanner(); int t = scanner.nextInt(); for(int i = 0; i < t; i++){ int n = scanner.nextInt(), k = scanner.nextInt(); String s = scanner.nextLine(); solve(s, n, k); } out.close(); } private static void printArray(int[] arr){ for(int i = 0; i < arr.length; i++){ out.print(arr[i]); if(i < arr.length - 1){ out.print(' '); } else{ out.print("\n"); } } } private static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["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
8a9d890a09829bfa617bf273b9e47fdb
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
900d3afe5da91b1b5f92cce5c1f8b73b
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 c { static BufferedReader bf; static PrintWriter out; public static void main (String[] args)throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int t= nextInt(); while(t-- > 0){ solve(); } } public static void solve()throws IOException{ String[]temp = bf.readLine().split(" "); int n = toInt(temp[0]); int k = toInt(temp[1]); String s = bf.readLine(); boolean check = false; int[]count = new int[n]; StringBuilder sb = new StringBuilder(); for(int i =0;i<n;i++){ char c = '2'; if(check){ c = (s.charAt(i) == '1')?'0':'1'; } else{ c = s.charAt(i); } if(k != 0){ if(i == n-1 && c == '0'){ sb.append('0'); continue; } if(k%2 == 1 && c == '1'){ k--; count[i]++; sb.append('1'); check = !check; } else if(k%2 == 0 && c == '0'){ k--; count[i]++; sb.append('1'); check = !check; } else{ sb.append('1'); } } else{ sb.append(c); } } if(k>0){ count[n-1]+=k; } println(sb.toString()); for(int i =0;i<n;i++){ out.print(count[i]+" "); } out.println(); out.flush(); } // code for input public static void print(String s ){ System.out.print(s); } public static void print(int num ){ System.out.print(num); } public static void print(long num ){ System.out.print(num); } public static void println(String s){ System.out.println(s); } public static void println(int num){ System.out.println(num); } public static void println(long num){ System.out.println(num); } public static void println(){ System.out.println(); } public static int toInt(String s){ return Integer.parseInt(s); } public static long toLong(String s){ return Long.parseLong(s); } public static String[] nextStringArray()throws IOException{ return bf.readLine().split(" "); } public static int nextInt()throws IOException{ return Integer.parseInt(bf.readLine()); } public static long nextLong()throws IOException{ return Long.parseLong(bf.readLine()); } public static String nextString()throws IOException{ return bf.readLine(); } public static int[] nextIntArray(int n)throws IOException{ String[]str = bf.readLine().split(" "); int[]arr = new int[n]; for(int i =0;i<n;i++){ arr[i] = Integer.parseInt(str[i]); } return arr; } public static long[] nextLongArray(int n)throws IOException{ String[]str = bf.readLine().split(" "); long[]arr = new long[n]; for(int i =0;i<n;i++){ arr[i] = Long.parseLong(str[i]); } return arr; } public static int[][] newIntMatrix(int r,int c)throws IOException{ int[][]arr = new int[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Integer.parseInt(str[j]); } } return arr; } public static long[][] newLongMatrix(int r,int c)throws IOException{ long[][]arr = new long[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Long.parseLong(str[j]); } } return arr; } //someuseFull functions to use public static long gcd(long a,long b){ if(b == 0)return a; return gcd(b,a%b); } public static int gcd(int a,int b){ if(b == 0)return a; return gcd(b,a%b); } public static long lcm(long a,long b){ return (a*b)/(gcd(a,b)); } } // if a problem is related to binary string it could also be related to parenthesis // try to use binary search in the question it might work // try sorting // try to think in opposite direction of question it might work in your way // if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+ or a,b,c,d in general // gcd(1.p1,2.p2,3.p3,4.p4....n.pn) cannot be greater than 2 it has been proved
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
c973ed07aa93324a8abd4ac5a9a1a355
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 problemB { public static void main(String[] args)throws IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(br.readLine()); for(int i=0; i<t; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); int curr = k; String str = br.readLine(); char[] arr = str.toCharArray(); int[] rep = new int[n]; for(int j=0; j<n-1; j++) { int sub = 0; if(((arr[j]-'0')&1)==(k&1))sub++; if(curr-sub>=0) { curr-=sub; out.write("1"); rep[j] = sub; } else out.write("0"); } int sub = 0; if(((arr[n-1]-'0')&1)==(k&1))sub++; if(((curr-sub)&1)==0) { rep[n-1] = curr; curr-=sub; out.write("1"); } else if(curr-sub>=0) { rep[n-1] = curr; curr-=sub; out.write("0"); } else out.write("0"); out.write("\n"); for(int j=0; j<n; j++)out.write(rep[j]+" "); out.write("\n"); } out.flush(); 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
c474c9c9d3167d97f30bcd53885c1665
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.*; /* Goal: Become better in CP! Key: Consistency and Discipline Desire: SDE @ Google USA Motto: Do what i Love <=> Love what i do If you don't use your brain 100%, it deteriorates gradually */ public class Coder { static StringBuffer str=new StringBuffer(); static char c[]; static int n, k; static void solve(){ int cnt[]=new int[n]; if(k%2!=0){ int pos=n-1; for(int i=0;i<n-1;i++){ if(c[i]=='1'){ pos=i; break; }else c[i]='1'; } cnt[pos]++; for(int i=pos+1;i<n;i++) c[i]=(char)('0'+'1'-c[i]); k--; } Queue<Integer> q=new LinkedList<>(); for(int i=0;i<n;i++) if(c[i]=='0') q.add(i); while(q.size()>1 && k>1){ int i=q.remove(); int j=q.remove(); cnt[i]++; cnt[j]++; for(int p=i;p<=j;p++) c[p]='1'; k-=2; } if(q.size()==1 && k>0){ int i=q.remove(); if(i<n-1){ for(int p=0;p<n;p++){ c[p]='1'; } c[n-1]='0'; k-=2; cnt[i]++; cnt[n-1]++; } } cnt[n-1]+=k; for(int i=0;i<n;i++) str.append(c[i]); str.append("\n"); for(int i=0;i<n;i++) str.append(cnt[i]).append(" "); str.append("\n"); } public static void main(String[] args) throws java.lang.Exception { BufferedReader bf; PrintWriter pw; boolean lenv=false; if(lenv){ bf = new BufferedReader( new FileReader("input.txt")); pw=new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); }else{ bf = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new OutputStreamWriter(System.out)); } int q = Integer.parseInt(bf.readLine().trim()); while (q-- > 0) { String s[]=bf.readLine().trim().split("\\s+"); n=Integer.parseInt(s[0]); k=Integer.parseInt(s[1]); c=bf.readLine().trim().toCharArray(); solve(); } pw.print(str); pw.flush(); // System.out.print(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
0088f8ad37199250b532d6fd91f764ce
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.*; /* Challenge 1: Newbie to CM in 1year (Dec 2021 - Nov 2022) 5* Codechef Challenge 2: CM to IM in 1 year (Dec 2022 - Nov 2023) 6* Codechef Challenge 3: IM to GM in 1 year (Dec 2023 - Nov 2024) 7* Codechef Goal: Become better in CP! Key: Consistency and Discipline Desire: SDE @ Google USA Motto: Do what i Love <=> Love what you do */ public class Coder { static StringBuffer str=new StringBuffer(); static int n, k; static char c[]; static void solve(){ int f[]=new int[n]; int tk=k; for(int i=0;i<n && tk>0;i++){ f[i]=((('1'-c[i])&(1-k%2)) | ((c[i]-'0') & (k%2))); if(f[i]==1) tk--; } f[n-1]+=tk; for(int i=0;i<n;i++){ if((k-f[i])%2!=0) c[i]=(char)('1'-c[i]+'0'); } for(int i=0;i<n;i++) str.append(c[i]); str.append("\n"); for(int i=0;i<n;i++) str.append(f[i]).append(" "); str.append("\n"); } public static void main(String[] args) throws java.lang.Exception { boolean lenv=false; BufferedReader bf; PrintWriter pw; if(lenv){ bf = new BufferedReader( new FileReader("input.txt")); pw=new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); }else{ bf = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new OutputStreamWriter(System.out)); } int q1 = Integer.parseInt(bf.readLine().trim()); while (q1-->0) { String s[]=bf.readLine().trim().split("\\s+"); n=Integer.parseInt(s[0]); k=Integer.parseInt(s[1]); c=bf.readLine().trim().toCharArray(); solve(); } pw.print(str); pw.flush(); // System.out.print(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
7c1494b7aed79b33bd27b9c64a993227
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; public class Main { public static void solve(int n, int k, String s) { int[] count = new int[n]; StringBuilder sb = new StringBuilder(s); int lastIdx = -1, idx = 0, cnt = 0; while (k > 0 && idx < n) { //制造更多的高位0 if(k%2 == 0) { while(idx < n-1 && (cnt+sb.charAt(idx)-'0')%2 == 1) { idx++; } } //制造更多的高位1 else { while(idx < n-1 && (cnt+sb.charAt(idx)-'0')%2 == 0) { idx++; } } if(idx != n-1) { sb.setCharAt(idx,'1'); } else { if((cnt+sb.charAt(idx)-'0')%2 == 0) { sb.setCharAt(idx,'0'); } else { sb.setCharAt(idx,'1'); } } for(int i = lastIdx+1; i < idx; i++) { sb.setCharAt(i,'1'); } lastIdx = idx; count[idx]++; idx++; k--; cnt++; } for(int i = lastIdx+1; i < n; i++) { if((cnt+sb.charAt(i)-'0')%2 == 0) { sb.setCharAt(i,'0'); } else { sb.setCharAt(i,'1'); } } System.out.println(sb); count[n-1] += k; for(int i = 0; i < n; i++) { System.out.print(count[i]+" "); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int i = 0; i < t; i++) { int n = in.nextInt(); int k = in.nextInt(); in.nextLine(); String s = in.nextLine(); solve(n,k,s); 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
74731261fcb0ad54e5fa3c073ee226c0
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.*; public class Contest782B{ public static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); //n length, k move = all bits flipped except selected while(t-- > 0){ //input int[] test = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); String bitString = br.readLine(); //initialize StringBuilder bitBuilder = new StringBuilder(); int count = 0; int[] arr = new int[test[0]]; //logic for(int i = 0; i < arr.length - 1; i++){ if(bitString.charAt(i) - '0' == (test[1] % 2)){ if(count < test[1]){ count++; arr[i]++;; bitBuilder.append(1); } else{ bitBuilder.append(0); } } else{ bitBuilder.append(1); } } arr[arr.length - 1] += test[1] - count; bitBuilder.append((bitString.charAt(arr.length - 1) - '0' == (count % 2) ? 0 : 1)); pw.println(bitBuilder); for(int i = 0; i < arr.length; i++){ pw.print(arr[i] + " "); } pw.println(); } pw.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
1e61f4c38705fd56ce500de7d6a2498b
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.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.StringTokenizer; public class CodeForces { static int mod = (int)1e9+7; public static void main(String[] args) throws InterruptedException { // PrintWriter out = new PrintWriter("output.txt"); // File input = new File("input.txt"); // FastScanner fs = new FastScanner(input); FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int testNumber =fs.nextInt(); for (int T =0;T<testNumber;T++){ int n= fs.nextInt(); int k = fs.nextInt(); char[] arr = fs.next().toCharArray(); int [] f = new int[n]; int c=0; for (int i=0;i<n;i++)if(k-c>0){ if(k%2==0){ if(arr[i]=='0'){ f[i]=1; c++; }else f[i]=0; }else { if(arr[i]=='1'){ f[i]=1; c++; }else f[i]=0; } } if(k-c>0){ f[n-1]+=(k-c); } for (int i=0;i<n;i++){ if((k-f[i])%2==0){ out.print(arr[i]); }else { out.print(arr[i]=='0'?'1':'0'); } } out.print("\n"); for (int i:f)out.print(i+" "); out.print("\n"); } out.flush(); } static long modInverse( long n, long p) { return FastPower(n, p - 2, p); } static int[] factorials(int max,int mod){ int [] ans = new int[max+1]; ans[0]=1; for (int i=1;i<=max;i++){ ans[i]=ans[i-1]*i; ans[i]%=mod; } return ans; } static String toBinary(int num,int bits){ String res =Integer.toBinaryString(bits); while(res.length()<bits)res="0"+res; return res; } static String toBinary(long num,int bits){ String res =Long.toBinaryString(bits); while(res.length()<bits)res="0"+res; return res; } static long LCM(long a,long b){ return a*b/gcd(a,b); } static long FastPower(long x,long p,long mod){ if(p==0)return 1; long ans =FastPower(x, p/2,mod); ans%=mod; ans*=ans; ans%=mod; if(p%2==1)ans*=x; ans%=mod; return ans; } static double FastPower(double x,int p){ if(p==0)return 1.0; double ans =FastPower(x, p/2); ans*=ans; if(p%2==1)ans*=x; return ans; } static int FastPower(int x,int p){ if(p==0)return 1; int ans =FastPower(x, p/2); ans*=ans; if(p%2==1)ans*=x; return ans; } static ArrayList<Vertex> vertices = new ArrayList<>(); static class Vertex { public ArrayList<Integer>edges = new ArrayList<>(); public boolean isEnd=false; public Vertex (){ for(int i=0;i<26;i++){ edges.set(i, -1); } } } static class Trie { private int root=0; public Trie(){ vertices.add(new Vertex()); } public void InsertWord(String s){ int current = root; for(char c:s.toCharArray()){ int pos = c-'a'; if(vertices.get(current).edges.get(pos)==-1){ vertices.add(new Vertex()); Vertex x = vertices.get(current); x.edges.set(pos,vertices.size()-1); vertices.set(current, x); } current=vertices.get(current).edges.get(pos); } } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(){ br=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); } public FastScanner(File f){ try { br=new BufferedReader(new FileReader(f)); st=new StringTokenizer(""); } catch(FileNotFoundException e){ br=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); } } String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readLongArray(int n) { long[] a =new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } public static long factorial(int n){ if(n==0)return 1; return (long)n*factorial(n-1); } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } static void sort (int[]a){ ArrayList<Integer> b = new ArrayList<>(); for(int i:a)b.add(i); Collections.sort(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static void sortReversed (int[]a){ ArrayList<Integer> b = new ArrayList<>(); for(int i:a)b.add(i); Collections.sort(b,new Comparator<Integer>(){ @Override public int compare(Integer o1, Integer o2) { return o2-o1; } }); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static void sort (long[]a){ ArrayList<Long> b = new ArrayList<>(); for(long i:a)b.add(i); Collections.sort(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static ArrayList<Integer> sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } ArrayList<Integer> ans = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (prime[i] == true) ans.add(i); } return ans; } static int binarySearchSmallerOrEqual(int arr[], int key) { int n = arr.length; int left = 0, right = n; int mid = 0; while (left < right) { mid = (right + left) >> 1; if (arr[mid] == key) { while (mid + 1 < n && arr[mid + 1] == key) mid++; break; } else if (arr[mid] > key) right = mid; else left = mid + 1; } while (mid > -1 && arr[mid] > key) mid--; return mid; } static int binarySearchSmallerOrEqual(long arr[], long key) { int n = arr.length; int left = 0, right = n; int mid = 0; while (left < right) { mid = (right + left) >> 1; if (arr[mid] == key) { while (mid + 1 < n && arr[mid + 1] == key) mid++; break; } else if (arr[mid] > key) right = mid; else left = mid + 1; } while (mid > -1 && arr[mid] > key) mid--; return mid; } public static int binarySearchStrictlySmaller(int[] arr, int target) { int start = 0, end = arr.length-1; if(end == 0) return -1; if (target > arr[end]) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } public static int binarySearchStrictlySmaller(long[] arr, long target) { int start = 0, end = arr.length-1; if(end == 0) return -1; if (target > arr[end]) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } static int binarySearch(int arr[], int x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } static int binarySearch(long arr[], long x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } static void init(int[]arr,int val){ for(int i=0;i<arr.length;i++){ arr[i]=val; } } static void init(int[][]arr,int val){ for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[i].length;j++){ arr[i][j]=val; } } } static void init(long[]arr,long val){ for(int i=0;i<arr.length;i++){ arr[i]=val; } } static<T> void init(ArrayList<ArrayList<T>>arr,int n){ for(int i=0;i<n;i++){ arr.add(new ArrayList()); } } static int binarySearchStrictlySmaller(ArrayList<Pair> arr, int target) { int start = 0, end = arr.size()-1; if(end == 0) return -1; if (target > arr.get(end).y) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr.get(mid).y >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } static int binarySearchStrictlyGreater(int[] arr, int target) { int start = 0, end = arr.length - 1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] <= target) { start = mid + 1; } else { ans = mid; end = mid - 1; } } return ans; } public static long pow(long n, long pow) { if (pow == 0) { return 1; } long retval = n; for (long i = 2; i <= pow; i++) { retval *= n; } return retval; } static String reverse(String s){ StringBuffer b = new StringBuffer(s); b.reverse(); return b.toString(); } static String charToString (char[] arr){ String t=""; for(char c :arr){ t+=c; } return t; } int[] copy (int [] arr , int start){ int[] res = new int[arr.length-start]; for (int i=start;i<arr.length;i++)res[i-start]=arr[i]; return res; } static int[] swap(int [] A,int l,int r){ int[] B=new int[A.length]; for (int i=0;i<l;i++){ B[i]=A[i]; } int k=0; for (int i=r;i>=l;i--){ B[l+k]=A[i]; k++; } for (int i=r+1;i<A.length;i++){ B[i]=A[i]; } return B; } static int mex (int[] d){ int [] a = Arrays.copyOf(d, d.length); sort(a); if(a[0]!=0)return 0; int ans=1; for(int i=1;i<a.length;i++){ if(a[i]==a[i-1])continue; if(a[i]==a[i-1]+1)ans++; else break; } return ans; } static int[] mexes(int[] arr){ int[] freq = new int [100000+7]; for (int i:arr)freq[i]++; int maxMex =0; for (int i=0;i<=100000+7;i++){ if(freq[i]!=0)maxMex++; else break; } int []ans = new int[arr.length]; ans[arr.length-1] = maxMex; for (int i=arr.length-2;i>=0;i--){ freq[arr[i+1]]--; if(freq[arr[i+1]]<=0){ if(arr[i+1]<maxMex) maxMex=arr[i+1]; ans[i]=maxMex; } else { ans[i]=ans[i+1]; } } return ans; } static int [] freq (int[]arr,int max){ int []b = new int[max]; for (int i:arr)b[i]++; return b; } static int[] prefixSum(int[] arr){ int [] a = new int[arr.length]; a[0]=arr[0]; for (int i=1;i<arr.length;i++)a[i]=a[i-1]+arr[i]; return a; } static class Pair { int x; long y; int extra; public Pair(int x,long y){ this.x=x; this.y=y; } public Pair(int x,long y,int extra){ this.x=x; this.y=y; this.extra=extra; } // @Override // public boolean equals(Object o) { // if(o instanceof Pair){ // if(o.hashCode()!=hashCode()){ // return false; // } else { // return x==((Pair)o).x&&y==((Pair)o).y; // } // } // // return false; // // } // // // // // // @Override // public int hashCode() { // return x+(int)y*2; // } // // } }
Java
["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
9c6d3c06fb37a96f696cfd44344279a9
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 javafx.util.*; public class Main { static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static int INF = Integer.MAX_VALUE; static int NINF = Integer.MIN_VALUE; public static void main (String[] args) throws java.lang.Exception { //check if you have to take product or the constraints are big int t = i(); while(t-- > 0){ int n = i(); int k = i(); char[] ch = in.nextLine().toCharArray(); int[] ans = new int[n]; if(k == 0){ out.println(String.valueOf(ch)); for(int i = 0;i < n;i++){ out.print(0 + " "); } out.println(); continue; } if(k%2 != 0){ for(int i = 0;i < n;i++){ if(ch[i] == '1'){ k--; ans[i]++; if(k == 0){ for(int j = i + 1;j < n;j++){ if(ch[j] == '0'){ ch[j] = '1'; }else{ ch[j] = '0'; } } break; } }else{ ans[i] = 0; ch[i] = '1'; } } if(k%2 != 0){ ch[n-1] = '0'; if(ans[n-1] == 1){ ans[n-1] = 0; k++; }else{ ans[n-1] = 1; k--; } } ans[0] += k; }else{ for(int i = 0;i < n;i++){ if(ch[i] == '0'){ ch[i] = '1'; ans[i]++; k--; if(k == 0){ break; } }else{ ans[i] = 0; } } if(k%2 != 0){ ch[n-1] = '0'; if(ans[n-1] == 1){ ans[n-1] = 0; k++; }else{ ans[n-1] = 1; k--; } } ans[0] += k; } out.println(String.valueOf(ch)); for(int i = 0;i < n;i++){ out.print(ans[i] + " "); } out.println(); } out.close(); } public static int solve(String s){ int ans = -1; int count = 0; int n = s.length(); for(int i = 0;i < n;i++){ if(s.charAt(i) == 'R'){ count++; }else{ ans = Math.max(ans,count); count = 0; } } ans = Math.max(ans,count); return ans; } public static void sort(int[] arr){ ArrayList<Integer> ls = new ArrayList<>(); for(int x : arr){ ls.add(x); } Collections.sort(ls); for(int i = 0;i < arr.length;i++){ arr[i] = ls.get(i); } } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } } class Pair implements Comparable<Pair>{ int x; int y; Pair(int x, int y){ this.x = x; this.y = y; } @Override public int compareTo(Pair obj) { // we sort objects on the basis of Student Id return (this.x - obj.x); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["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
ce344f2a5152adfa2989f7983dc0cde0
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 PrintWriter out; public static void main(String[] args)throws IOException{ Scanner sc=new Scanner(); out=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt(); String s=sc.next(); if(k==0) { out.println(s); for(int i=0;i<s.length();i++) { out.print(0+" "); } out.println(); continue; } /*if(n==1) { if(n=='1'&&k%2==0) { out.println('1'); }else if(n=='1'&&k%2==1) { out.println('0'); }else if(n=='0'&&k%2==0) { out.println('0'); }else { out.println('1'); } out.println(k); }*/ int a[]=new int[n]; int count=k; for(int i=0;i<s.length()&&count>0;i++) { if(s.charAt(i)=='0'&&k%2==0) { a[i]=1; count--; }else if(s.charAt(i)=='0'&&k%2==1) { a[i]=0; }else if(s.charAt(i)=='1'&&k%2==0) { a[i]=0; }else if(s.charAt(i)=='1'&&k%2==1){ a[i]=1; count--; } } StringBuilder ans=new StringBuilder(); for(int i=0;i<s.length();i++) { if(s.charAt(i)=='0'&&((k-count)-a[i])%2==1) { ans.append('1'); }else if(s.charAt(i)=='0'&&((k-count)-a[i])%2==0) { ans.append('0'); }else if(s.charAt(i)=='1'&&((k-count)-a[i])%2==1) { ans.append('0'); }else if(s.charAt(i)=='1'&&((k-count)-a[i])%2==0) { ans.append('1'); } } if(count>0) { StringBuilder temp=new StringBuilder(); int index=-1; for(int i=0;i<ans.length();i++) { if(ans.charAt(i)=='1') { index=i; break; } } if(count%2==1) { if(index!=-1) { a[index]+=count; for(int i=0;i<ans.length();i++) { if(i!=index) { temp.append(""+(1-(ans.charAt(i)-'0'))); } } }else { a[n-1]+=count; for(int i=0;i<ans.length()-1;i++) { temp.append(""+(1-(ans.charAt(i)-'0'))); } temp.append(ans.charAt(n-1)); } out.println(temp.toString()); for(int i:a) { out.print(i+" "); } out.println(); continue; }else { out.println(ans.toString()); a[n-1]+=count; for(int i:a) { out.print(i+" "); } out.println(); continue; } } out.println(ans.toString()); for(int i:a) { out.print(i+" "); } out.println(); } out.close(); } public static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["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
5084e066a83b96f41d95dc88f9a248db
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.TreeSet; public class B { 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) { String[] inp = br.readLine().split(" "); int n = Integer.parseInt(inp[0]); int k = Integer.parseInt(inp[1]); String s= br.readLine(); int[] ans = new int[s.length()]; char[] tmp = new char[s.length()]; if(k%2==1) { boolean found = false; for(int i=0;i<s.length();i++) { if(s.charAt(i)=='0') tmp[i] = '1'; else if(!found) { tmp[i] = '1'; found = true; ans[i]=1; } else tmp[i] = '0'; } if(!found) { ans[tmp.length-1]++; tmp[tmp.length-1]='0'; } k--; } else tmp = s.toCharArray(); TreeSet<Integer> zeros = new TreeSet<>(); for(int i =0;i<tmp.length;i++) if(tmp[i]=='0') zeros.add(i); if(zeros.size()%2==0) { while(k>0 && zeros.size()>0) { int i1=zeros.pollFirst(); int i2 = zeros.pollFirst(); ans[i1]++; ans[i2]++; tmp[i1]='1'; tmp[i2] = '1'; k-=2; } ans[0]+=k; } else { while(k>0 && zeros.size()>1) { int i1=zeros.pollFirst(); int i2 = zeros.pollFirst(); ans[i1]++; ans[i2]++; tmp[i1]='1'; tmp[i2] = '1'; k-=2; } if(k>=2 && zeros.size()==1) { int ind = zeros.pollFirst(); if(ind < tmp.length-1) { ans[ind]++; ans[tmp.length-1]++; tmp[ind] = '1'; tmp[tmp.length-1]='0'; k-=2; } } ans[0]+=k; } for(char c: tmp) pw.print(c); pw.println(); for(int i: ans) pw.print(i+" "); pw.println(); } pw.flush(); pw.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
6d778be09b9511e4f9acd887d2ada1cf
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.*; public class Main { static int t; static int n; static int[] a; static String s; static FastReader fr = new FastReader(); static PrintWriter out = new PrintWriter(System.out); public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static void main(String[] args) { int t = fr.nextInt(); while ((t --) > 0) { int n = fr.nextInt(); int k = fr.nextInt(); String s = fr.nextString(); char[] cs = s.toCharArray(); int bh = 0; int[] cnt = new int[n]; for (int i = 0; i < n; i ++) { int c = cs[i] - '0'; c = (c + bh) % 2; if (k == 0) { if (bh == 0) break; cs[i] = (char) (c + '0'); continue; } if (i == n -1 ) { cnt[i] = k; cs[i] = (char) (c + '0'); continue; } if (c == 1) { cs[i] = '1'; if (k % 2 == 0) { continue; } else{ bh += 1; bh %= 2; cs[i] = '1'; k --; cnt[i] = 1; } } else if (c == 0) { cs[i] = '1'; if (k % 2 == 0) { bh = (bh + 1) % 2; cs[i] = '1'; k --; cnt[i] = 1; } else { continue; } } } System.out.println(new String(cs)); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i ++) { sb.append(cnt[i]); if (i != n - 1) sb.append(" "); } System.out.println(sb.toString()); } return; } static long ask(String op, int a, int b) { System.out.println(op + " " + a + " " + b); System.out.flush(); long gcd = fr.nextLong(); return gcd; // return gcd(12354 + a, 12354 + b); } static class FastReader { private BufferedReader bfr; private StringTokenizer st; public FastReader() { bfr = new BufferedReader(new InputStreamReader(System.in)); } String next() { if (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bfr.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return next().toCharArray()[0]; } String nextString() { return next(); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } double[] nextDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextDouble(); return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } int[][] nextIntGrid(int nL, int m) { int[][] grid = new int[n][m]; for (int i = 0; i < n; i++) { char[] line = fr.next().toCharArray(); for (int j = 0; j < m; j++) grid[i][j] = line[j] - 48; } return grid; } } }
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
e85bcc0da69bd64c4098fa69bb3a60b3
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.util.concurrent.LinkedBlockingDeque; import javax.sql.rowset.spi.SyncResolver; import java.io.*; import java.nio.channels.NonReadableChannelException; import java.text.DateFormatSymbols; public class CpTemp { static int a[]; static long count=0l; static long row = 1l; static long col = 1l; static FastScanner fs = null; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); outer: while (t-- > 0) { int n = fs.nextInt(); int k = fs.nextInt(); int tmp = k; String s = fs.next(); int a[] = new int[n]; int c1=0; for(int i=0;i<n;i++){ if(s.charAt(i)=='1'){ c1++; } } if(k%2==0) { int c0 = n - c1; boolean flag = false; int prev = -1; int prev0=-1; for(int i=0;i<n;i++){ if(s.charAt(i)=='1'){ a[i] = 0; c1--; prev = i; }else{ prev0 = i; if(k>1 && c0>1) { a[i] = 1; k--; c0--; }else if(k==1 && c0>0){ a[i]=1; k--; }else if(c0==1 && k>0){ if(k%2!=0){ a[i] = k; k = 0; }else{ if(c1>0) { a[i] = 1; k--; flag = true; }else{ a[i]=k; k=0; } } }else if(c0>0 && k==0){ a[i]=0; } } } if(flag){ a[prev] = k; k=0; } if(k>0){ if(prev0!=-1){ a[prev0] = k; }else{ a[prev]= k; } } /* for (int i = 0; i < n; i++) { if (s.charAt(i) == '1') { a[i] = 0; } else { if (k>0) { a[i] = 1; k--; } else { a[i] = k; } } }*/ } else{ for(int i=0;i<n-1;i++){ if(s.charAt(i)=='1'){ if(k>0) { a[i] = 1; k--; }else{ a[i]=0; } }else{ a[i]=0; } } if(k>0){ a[n-1] = k; } } for(int i=0;i<n;i++){ if(tmp%2==0) { if (a[i] % 2 == 0) { out.print(s.charAt(i)); } else { out.print(s.charAt(i) == '1' ? '0' : '1'); } }else{ if(a[i]%2==0){ out.print(s.charAt(i) == '1' ? '0' : '1'); }else{ out.print(s.charAt(i)); } } } out.println(); for(int v:a){ out.print(v+" "); } out.println(); } out.close(); } static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return this.y-o.y; } } static long power(long x, long y, long p) { if (y == 0) return 1; if (x == 0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if (y == 0) return 1; if (x == 0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readlongArray(int n){ long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } static boolean prime[]; static void sieveOfEratosthenes(int n) { prime = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } } public static int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static long nCk(int n, int k) { long res = 1; for (int i = n - k + 1; i <= n; ++i) res *= i; for (int i = 2; i <= k; ++i) res /= i; return res; } }
Java
["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
3035a8c234461638167c8846ec4f09a7
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.util.StringTokenizer; /** * * @author eslam */ public class IceCave { 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 input = new FastReader(); static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException { int t = input.nextInt(); while (t-- > 0) { int n = input.nextInt(); int k = input.nextInt(); int ans[] = new int[n]; char ch[] = input.next().toCharArray(); int one = 0; int zero = 0; for (int i = 0; i < n; i++) { if (ch[i] == '1') { one++; } else { zero++; } } if (k % 2 == 1) { for (int i = 0; i < n && k > 0; i++) { if (ch[i] == '1') { ans[i] = 1; k--; } } ans[n-1]+=k; for (int i = 0; i < n; i++) { if (ch[i] == '0') { if(ans[i]%2==0) ch[i] = '1'; } else if (ch[i] == '1') { if (ans[i] % 2 == 0) { ch[i] = '0'; } } } } else { for (int i = 0; i < n && k > 0; i++) { if (ch[i] == '0') { ans[i] = 1; k--; } } ans[n-1]+=k; for (int i = 0; i < n; i++) { if (ch[i] == '0') { if (ans[i] % 2 == 0) { ch[i] = '0'; } else { ch[i] = '1'; } }else{ if(ans[i]%2==1){ ch[i] = '0'; } } } } for (int i = 0; i < n; i++) { log.write(ch[i]); } log.write("\n"); for (int i = 0; i < n; i++) { log.write(ans[i]+" "); } log.write("\n"); } log.flush(); } public static long binaryToDecimal(String w) { long r = 0; long l = 0; for (int i = w.length() - 1; i > -1; i--) { long x = (w.charAt(i) - '0') * (long) Math.pow(2, l); r = r + x; l++; } return r; } public static String decimalToBinary(long n) { String w = ""; while (n > 0) { w = n % 2 + w; n /= 2; } return w; } public static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } public static void print(int a[]) throws IOException { for (int i = 0; i < a.length; i++) { log.write(a[i] + " "); } log.write("\n"); } public static void read(int[] a) { for (int i = 0; i < a.length; i++) { a[i] = input.nextInt(); } } static class pair { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } } public static long LCM(long x, long y) { return (x * y) / GCD(x, y); } public static long GCD(long x, long y) { if (y == 0) { return x; } return GCD(y, x % y); } }
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
8419be8c231dde3ba9db753dae4a8121
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 { static TreeSet<String> tSet; static PrintWriter pw; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt(); String s = sc.next(); int [] arr= new int [s.length()]; int tmp=k; if(k%2==0) { for (int i = 0; i < s.length(); i++) { if(s.charAt(i)=='0'&& k>0) { arr[i]=1; k--; } } } else { for (int i = 0; i < s.length(); i++) { if(s.charAt(i)=='1' && k>0) { arr[i]=1; k--; } } } if(k>0) { arr[arr.length-1]+=k; } // pw.println(Arrays.toString(arr)); StringBuilder sb =new StringBuilder(); char a='x'; for (int i = 0; i < s.length(); i++) { a=s.charAt(i); int num =tmp-arr[i]; if(num%2!=0) a =flip(a); sb.append(a+""); // pw.println(arr); //pw.println(sb); // pw.println(Arrays.toString(arr)); } pw.println(sb); for (int i = 0; i < arr.length; i++) { pw.print(arr[i]+" "); } pw.println(); } pw.close(); pw.flush(); } public static char flip(char x) { if(x=='0') return '1'; return'0'; } public static int dfsOnAdjacencyList(ArrayList<Integer>[] graph, int curr, boolean[] vis, int m, int conscats, int[] cats) { // same DFS code but a7la mn ellyfoo2 fel writing if (conscats > m) return 0; if (graph[curr].size() == 1 && curr != 1) { if (cats[curr] == 1) conscats++; else conscats = 0; if (conscats > m) return 0; return 1; } vis[curr] = true; if (cats[curr] == 1) conscats++; else conscats = 0; int res = 0; for (int x : graph[curr]) { if (!vis[x]) res += dfsOnAdjacencyList(graph, x, vis, m, conscats, cats); } return res; } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static int lcm(int a, int b) { return a * b / gcd(a, b); } public static long fac(long i) { long res = 1; while (i > 0) { res = res * i--; } return res; } public static long combination(long x, long y) { return 1l * (fac(x) / (fac(x - y) * fac(y))); } public static long permutation(long x, long y) { return combination(x, y) * fac(y); } static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public int compareTo(Pair p) { if(this.x==p.x) return this.y-p.y; return this.x-p.x; } } 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 int[] nextIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public long[] nextlongArray(int n) throws IOException { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public Long[] nextLongArray(int n) throws IOException { Long[] array = new Long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public char[] nextCharArray(int n) throws IOException { char[] array = new char[n]; String string = next(); for (int i = 0; i < n; i++) { array[i] = string.charAt(i); } return array; } 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
39fef41caa364cbee475d57daa34dacf
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
d636900aff0aab05a96b28cb41c709eb
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 _1659b { FastScanner scn; PrintWriter w; PrintStream fs; int MOD = 1000000007; int MAX = 200005; long mul(long x, long y) {long res = x * y; return (res >= MOD ? res % MOD : res);} long power(long x, long y) {if (y < 0) return 1; long res = 1; x %= MOD; while (y!=0) {if ((y & 1)==1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;} void ruffleSort(int[] a) {int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {int oi=r.nextInt(n), temp=a[i];a[i]=a[oi];a[oi]=temp;}Arrays.sort(a);} void reverseSort(int[] arr){List<Integer> list = new ArrayList<>();for (int i=0; i<arr.length; i++){list.add(arr[i]);}Collections.sort(list, Collections.reverseOrder());for (int i = 0; i < arr.length; i++){arr[i] = list.get(i);}} boolean LOCAL; void debug(Object... o){if(LOCAL)System.err.println(Arrays.deepToString(o));} //SPEED IS NOT THE CRITERIA, CODE SHOULD BE A NO BRAINER, CMP KILLS, MOCIM void solve(){ int t=scn.nextInt(); while(t-->0) { int n =scn.nextInt(); long m=scn.nextLong(); long orm = m; String s = scn.next(); long[] ar = new long[s.length()]; int idx = -1; for(int i=0;i<n-1;i++){ int val = s.charAt(i)-'0'; if(orm==0){ break; } if(val==0&&(orm&1)==0){ ar[i]++; m--; }else if(val==1&&(orm&1)!=0){ ar[i]++; m--; } if(m==0){ idx = i; break; } } if(m>0) { for(int i=0;i<n-1;i++){ w.print(1); } int vv = s.charAt(n-1)-'0'; long xm = orm-m; if((vv==0&&(xm&1)==0)||(vv==1&&(xm&1)!=0)) w.println(0); else{ w.println(1); } debug("rem",m); ar[n-1] = m; for(int i=0;i<n;i++){ w.print(ar[i]+" "); } w.println(); }else{ for(int i=0;i<=idx;i++){ w.print(1); } for(int i=idx+1;i<n;i++){ int val = s.charAt(i)-'0'; if(val==1&&(orm&1)==0){ w.print(1); }else if(val==1&&(orm&1)!=0){ w.print(0); }else if(val==0&&(orm&1)==0){ w.print(0); }else{ w.print(1); } } w.println(); for(int i=0;i<n;i++){ w.print(ar[i]+" "); } w.println(); } } } void run() { try { long ct = System.currentTimeMillis(); scn = new FastScanner(new File("input.txt")); w = new PrintWriter(new File("output.txt")); fs=new PrintStream("error.txt"); System.setErr(fs); LOCAL=true; solve(); w.close(); System.err.println(System.currentTimeMillis() - ct); } catch (FileNotFoundException e) { e.printStackTrace(); } } void runIO() { scn = new FastScanner(System.in); w = new PrintWriter(System.out); LOCAL=false; solve(); w.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long[] nextLongArray(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()); } } int lowerBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid-1 >= 0 && arr[mid-1] == arr[mid]){ei = mid-1;}else{return mid;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; } int upperBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid+1 < n && arr[mid+1] == arr[mid]){si = mid+1;}else{return mid + 1;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; } int upperBound(ArrayList<Integer> list, int x){int n = list.size(), si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(list.get(mid) == x){if(mid+1 < n && list.get(mid+1) == list.get(mid)){si = mid+1;}else{return mid + 1;}}else if(list.get(mid) > x){ei = mid - 1; }else{si = mid+1;}}return si; } void swap(int[] arr, int i, int j){int temp = arr[i];arr[i] = arr[j];arr[j] = temp;} long nextPowerOf2(long v){if (v == 0) return 1;v--;v |= v >> 1;v |= v >> 2;v |= v >> 4;v |= v >> 8;v |= v >> 16;v |= v >> 32;v++;return v;} int gcd(int a, int b) {if(a == 0){return b;}return gcd(b%a, a);} // TC- O(logmax(a,b)) boolean nextPermutation(int[] arr) {if(arr == null || arr.length <= 1){return false;}int last = arr.length-2;while(last >= 0){if(arr[last] < arr[last+1]){break;}last--;}if (last < 0){return false;}if(last >= 0){int nextGreater = arr.length-1;for(int i=arr.length-1; i>last; i--){if(arr[i] > arr[last]){nextGreater = i;break;}}swap(arr, last, nextGreater);}int i = last + 1, j = arr.length - 1;while(i < j){swap(arr, i++, j--);}return true;} public static void main(String[] args) { new _1659b().runIO(); } }
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
fc920d85a0686882bb2ba442e8f47bc3
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.PrintWriter; import java.util.Arrays; import java.util.Scanner; /* 1 6 1 111001 */ public class B { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); in.nextLine(); while (t-- > 0) { int len = in.nextInt(); int moves = in.nextInt(), originalMoves = moves; in.nextLine(); String s = in.nextLine(); char[] input = s.toCharArray(); int[] swaps = new int[len]; for (int i = 0; i < len; i++) { char ch = input[i]; if (originalMoves % 2 == 1) { if (ch == '1' && moves > 0) { moves--; swaps[i]++; } else if (ch == '1') { input[i] = '0'; } else { input[i] = '1'; } } else { if (ch == '0' && moves > 0) { input[i] = '1'; moves--; swaps[i]++; } } } int lastIndex = len - 1; if (moves > 0) { swaps[lastIndex] += moves; if (moves % 2==1) input[lastIndex] = '0'; } // out.println(s + " " + originalMoves + " result " + Arrays.toString(input)); // out.println("swaps " + Arrays.toString(swaps)); out.println(new String(input)); for (int i = 0; i < len; i++) out.print(swaps[i] + " "); out.println(); } out.close(); in.close(); } private static void invert(char[] input) { for (int i = 0; i < input.length; i++) { input[i] = input[i] == '0' ? '1' : '0'; } } private static int count(char[] input, char ch) { int count = 0; for (char c : input) { if (c == ch) { count++; } } return count; } }
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
d211f027c82f2a89aeaa7c8ad7ee2252
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) { Scanner sc = new Scanner(new BufferedInputStream(System.in)); int t = sc.nextInt(); System.out.println(); while (t-- != 0){ int n = sc.nextInt(); int k = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); char[] chs = s.toCharArray(); int[] ans = new int[n]; if (k % 2 == 0){ for (int i = 0; i < n - 1; i++){ if(k > 0) { if (chs[i] == '0') { ans[i] = 1; k--; } chs[i] = '1'; } } if (k != 0){ ans[n - 1] = k; if (k % 2 != 0){ if (chs[n - 1] == '0'){ chs[n - 1] = '1'; }else{ chs[n - 1] = '0'; } } } }else{ for (int i = 0; i < n - 1; i++){ if (k > 0) { if (chs[i] == '1') { ans[i] = 1; k--; } chs[i] = '1'; }else{ if (chs[i] == '1'){ chs[i] = '0'; }else{ chs[i] = '1'; } } } if (k != 0){ ans[n - 1] = k; if (k % 2 == 0){ if (chs[n - 1] == '0'){ chs[n - 1] = '1'; }else{ chs[n - 1] = '0'; } } }else{ if (chs[n - 1] == '1'){ chs[n - 1] = '0'; }else{ chs[n - 1] = '1'; } } } System.out.println(String.valueOf(chs)); for (int num : ans){ System.out.print(num + " "); } 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
7a85d1420934b7af63b97d0bf8204e66
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.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) throws IOException{ /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(br.readLine()); while(t--> 0){ String s1[] = br.readLine().split("\\s+"); int n = Integer.parseInt(s1[0]); int k = Integer.parseInt(s1[1]); int l = k; String s2 = br.readLine(); char s3[] = s2.toCharArray(); // for(int i=0;i<n;i++) // bw.write(s3[i]+""); int c[] = new int[n]; // bw.write("\n"); int x = 0; if(k%2==1){ for(int i=0;i<n;i++){ if(k>0 && s3[i]=='1'){ k--; if(i == n-1)x++; c[i]++; }else{ if(s3[i] == '1')s3[i] = '0'; else s3[i] = '1'; if(i == n-1)x++; } } } else{ for(int i=0;i<n;i++){ if(k>0 && s3[i] == '0'){ k--; s3[i] = '1'; c[i]++; } } } if(k>0){ x = x+k; l = l-x; if(k%2==1){ if(s3[n-1] == '1')s3[n-1] = '0'; else s3[n-1] = '1'; } c[n-1]+= k; } for(int i=0;i<n;i++) bw.write(s3[i]+""); bw.write("\n"); for(int i=0;i<n;i++) bw.write(c[i]+" "); bw.write("\n"); } bw.flush(); } }
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
994866432eb7adbf0b1a6bbf6a4a0e35
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 mod=(long)1e9+7; static long[]fac=new long[1002]; static int n, x=0,me,op; static int[]pe,a,aa, prime=new int[(int)1e7+1]; static int[][]perm; static long[][]memo; static Integer[]ps; static TreeSet<Long>p=new TreeSet<Long>(); public static void main(String[] args) throws Exception{ int t =sc.nextInt(); while(t-->0) { int n =sc.nextInt(); int k =sc.nextInt(); char[]a=sc.next().toCharArray(); int[]out = new int[n]; int x = k; int flips = 0; for (int i = 0; i < a.length; i++) { if(a[i]=='0'&&flips%2==0||a[i]=='1'&&flips%2==1) { if(k>0&&k%2==0) { out[i]++; k--; flips++; } }else { if(k%2==1) { out[i]++; k--; flips++; } } } if(k>0) { // if(k%2==0) { // for (int i = 0; i < out.length; i++) { // if(out[i]%2==0) { // out[i]+=k; // break; // } // } // }else { out[n-1]+=k; // } } for (int i = 0; i < out.length; i++) { if((x-out[i])%2==1) { a[i]=a[i]=='0'?'1':'0'; } } pw.println(new String(a)); for (int i = 0; i < out.length; i++) { pw.print(out[i]+" "); } pw.println(); } pw.close(); } public static long solFul(int m, int o) { if(o<m)return 0; if(o == op)return 1; if(memo[m][o]!=-1) { return memo[m][o]; } return memo[m][o] = (solFul(m+1,o)+solFul(m, o+1))%mod; } public static long solFree(int m, int o) { if(m<=o)return 0; if(m == me)return 1; if(o == op&&m>op)return 1; if(memo[m][o]!=-1)return memo[m][o]; return memo[m][o] = (solFree(m+1,o)+solFree(m, o+1))%mod; } public static String rev(String s) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { sb.append(s.charAt(s.length()-1-i)); } return sb.toString(); } public static int divNum(int n) { int total = 1; while(n>1) { int p=prime[n]; int count =1; while(n%p==0) { n/=p; count++; } total*=count; } return total; } public static void sieve() { for (int i = 2; i < prime.length; i++) { if(prime[i]==0) { p.add((long)i); for (int j = i; j < prime.length; j+=i) { prime[j]++; } } } } public static long[] Extended(long p, long q) { if (q == 0) return new long[] { p, 1, 0 }; long[] vals = Extended(q, p % q); long d = vals[0]; long a = vals[2]; long b = vals[1] - (p / q) * vals[2]; return new long[] { d, a, b }; } public static int LIS(int[] a) { int n = a.length; int[] ser = new int[n]; Arrays.fill(ser, Integer.MAX_VALUE); int cur = -1; for (int i = 0; i < n; i++) { int low = 0; int high = n - 1; int mid = (low + high) / 2; while (low <= high) { if (ser[mid] < a[i]) { low = mid + 1; } else { high = mid - 1; } mid = (low + high) / 2; } cur = Math.max(cur, high + 1); ser[high + 1] = Math.min(ser[high + 1], a[i]); } return cur + 1; } public static void permutation(int idx,int v) { if(v==(1<<n)-1) { perm[x++]=pe.clone(); return ; } for (int i = 0; i < n; i++) { if((v&1<<i)==0) { pe[idx]=aa[i]; permutation(idx+1, v|1<<i); } } return ; } public static void sort(int[]a) { mergesort(a, 0, a.length-1); } public static void sortIdx(long[]a,long[]idx) { mergesortidx(a, idx, 0, a.length-1); } public static long C(int a,int b) { long x=fac[a]; long y=fac[a-b]*fac[b]; return x*pow(y,mod-2)%mod; } public static long pow(long a,long b) { long ans=1;a%=mod; for(long i=b;i>0;i/=2) { if((i&1)!=0) ans=ans*a%mod; a=a*a%mod; } return ans; } public static void pre(){ fac[0]=1; fac[1]=1; fac[2]=2; for (int i = 3; i < fac.length; i++) { fac[i]=fac[i-1]*i%mod; } } public static long eval(String s) { long p=1; long res=0; for (int i = 0; i < s.length(); i++) { res+=p*(s.charAt(s.length()-1-i)=='1'?1:0); p*=2; } return res; } public static String binary(long x) { String s=""; while(x!=0) { s=(x%2)+s; x/=2; } return s; } public static boolean allSame(String s) { char x=s.charAt(0); for (int i = 0; i < s.length(); i++) { if(s.charAt(i)!=x)return false; } return true; } public static boolean isPalindrom(String s) { int l=0; int r=s.length()-1; while(l<r) { if(s.charAt(r--)!=s.charAt(l++))return false; } return true; } public static boolean isSubString(String s,String t) { int ls=s.length(); int lt=t.length(); boolean res=false; for (int i = 0; i <=lt-ls; i++) { if(t.substring(i, i+ls).equals(s)) { res=true; break; } } return res; } public static boolean isSorted(long[]a) { for (int i = 0; i < a.length-1; i++) { if(a[i]>a[i+1])return false; } return true; } public static long evaln(String x,int n) { long res=0; for (int i = 0; i < x.length(); i++) { res+=Long.parseLong(x.charAt(x.length()-1-i)+"")*Math.pow(n, i); } return res; } static void mergesort(int[] arr,int b,int e) { if(b<e) { int m=b+(e-b)/2; mergesort(arr,b,m); mergesort(arr,m+1,e); merge(arr,b,m,e); } return; } static void merge(int[] arr,int b,int m,int e) { int len1=m-b+1,len2=e-m; int[] l=new int[len1]; int[] r=new int[len2]; for(int i=0;i<len1;i++)l[i]=arr[b+i]; for(int i=0;i<len2;i++)r[i]=arr[m+1+i]; int i=0,j=0,k=b; while(i<len1 && j<len2) { if(l[i]<r[j])arr[k++]=l[i++]; else arr[k++]=r[j++]; } while(i<len1)arr[k++]=l[i++]; while(j<len2)arr[k++]=r[j++]; return; } static void mergesortidx(long[] arr,long[]idx,int b,int e) { if(b<e) { int m=b+(e-b)/2; mergesortidx(arr,idx,b,m); mergesortidx(arr,idx,m+1,e); mergeidx(arr,idx,b,m,e); } return; } static void mergeidx(long[] arr,long[]idx,int b,int m,int e) { int len1=m-b+1,len2=e-m; long[] l=new long[len1]; long[] lidx=new long[len1]; long[] r=new long[len2]; long[] ridx=new long[len2]; for(int i=0;i<len1;i++) { l[i]=arr[b+i]; lidx[i]=idx[b+i]; } for(int i=0;i<len2;i++) { r[i]=arr[m+1+i]; ridx[i]=idx[m+1+i]; } int i=0,j=0,k=b; while(i<len1 && j<len2) { if(l[i]<=r[j]) { arr[k++]=l[i++]; idx[k-1]=lidx[i-1]; } else { arr[k++]=r[j++]; idx[k-1]=ridx[j-1]; } } while(i<len1) { idx[k]=lidx[i]; arr[k++]=l[i++]; } while(j<len2) { idx[k]=ridx[j]; arr[k++]=r[j++]; } return; } static long mergen(int[] arr,int b,int m,int e) { int len1=m-b+1,len2=e-m; int[] l=new int[len1]; int[] r=new int[len2]; for(int i=0;i<len1;i++)l[i]=arr[b+i]; for(int i=0;i<len2;i++)r[i]=arr[m+1+i]; int i=0,j=0,k=b; long c=0; while(i<len1 && j<len2) { if(l[i]<r[j])arr[k++]=l[i++]; else { arr[k++]=r[j++]; c=c+(long)(len1-i); } } while(i<len1)arr[k++]=l[i++]; while(j<len2)arr[k++]=r[j++]; return c; } static long mergesortn(int[] arr,int b,int e) { long c=0; if(b<e) { int m=b+(e-b)/2; c=c+(long)mergesortn(arr,b,m); c=c+(long)mergesortn(arr,m+1,e); c=c+(long)mergen(arr,b,m,e); } return c; } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static long sumd(long x) { long sum=0; while(x!=0) { sum+=x%10; x=x/10; } return sum; } public static ArrayList<Integer> findDivisors(int n){ ArrayList<Integer>res=new ArrayList<Integer>(); for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { // If divisors are equal, print only one if (n/i == i) res.add(i); else { res.add(i); res.add(n/i); } } } return res; } public static void sort2darray(Integer[][]a){ Arrays.sort(a,Comparator.<Integer[]>comparingInt(x -> x[0]).thenComparingInt(x -> x[1])); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } public int[] nextArrint(int size) throws IOException { int[] a=new int[size]; for (int i = 0; i < a.length; i++) { a[i]=sc.nextInt(); } return a; } public long[] nextArrlong(int size) throws IOException { long[] a=new long[size]; for (int i = 0; i < a.length; i++) { a[i]=sc.nextLong(); } return a; } public int[][] next2dArrint(int rows,int columns) throws IOException{ int[][]a=new int[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { a[i][j]=sc.nextInt(); } } return a; } public long[][] next2dArrlong(int rows,int columns) throws IOException{ long[][]a=new long[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { a[i][j]=sc.nextLong(); } } return a; } } static class Pair{ long x; long y; public Pair(long x,long y) { this.x=x; this.y=y; } } 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
2e408ae000676b46ec4c3cc27e976431
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 a { public static void main(String[] args){ FastScanner sc = new FastScanner(); int t = sc.nextInt(); while(t-- > 0){ int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); StringBuilder ans1 = new StringBuilder(); StringBuilder ans2 = new StringBuilder(); if(k == 0){ System.out.println(s); for(int i=0; i<n; i++){ ans2.append("0 "); } System.out.println(ans2); continue; } int arr[] = new int[n]; for(int i=0; i<n; i++){ if(s.charAt(i) == '0'){ if(k%2 == 0){ arr[i] = 1; } else{ arr[i] = 0; } } else{ if(k%2 == 0){ arr[i] = 0; } else{ arr[i] = 1; } } } int count = 0; int idx = -1; for(int i=0; i<n; i++){ if(arr[i] == 1){ count++; } if(count == k){ idx = i+1; } } if(idx == -1 || idx == n){ int rem = k-count; if(rem%2 == 0){ for(int i=0; i<n; i++){ ans1.append(1); } System.out.println(ans1); for(int i=0; i<n; i++){ if(i == n-1){ ans2.append(arr[i] + rem); } else{ ans2.append(arr[i] + " "); } } System.out.println(ans2); } else{ for(int i=0; i<n; i++){ if(i == n-1){ ans1.append(0); } else{ ans1.append(1); } } System.out.println(ans1); for(int i=0; i<n; i++){ if(i == n-1){ ans2.append(arr[i] + rem); } else{ ans2.append(arr[i] + " "); } } System.out.println(ans2); } } else{ for(int i=0; i<idx; i++){ ans1.append('1'); } for(int i=idx; i<n; i++){ if(k%2 == 0){ ans1.append(s.charAt(i)); } else{ if(s.charAt(i) == '1'){ ans1.append(0); } else{ ans1.append(1); } } } System.out.println(ans1); for(int i=0; i<idx; i++){ ans2.append(arr[i] + " "); } for(int i=idx; i<n; i++){ ans2.append("0 "); } System.out.println(ans2); } } } } class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
Java
["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
284dd35ef8ba3e1b70a77b708fa6b99a
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.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author real */ 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); BBitFlipping solver = new BBitFlipping(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class BBitFlipping { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); String s = in.readString(); int parity = k % 2; int initK = k; int num[] = new int[n]; int ans[] = new int[n]; for (int i = 0; i < n; i++) { num[i] = s.charAt(i) - '0'; } for (int i = 0; i < n; i++) { if (k <= 0) break; int val = num[i] + initK; if (val % 2 == 0) { ans[i] = 1; k--; } } ans[n - 1] += k; for (int i = 0; i < n; i++) { int val = num[i] + initK - ans[i]; if (val % 2 != 0) { out.print('1'); } else { out.print('0'); } } out.println(); for (int i = 0; i < n; i++) { out.print(ans[i] + " "); } out.println(); } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar; private int snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { //*-*------clare-----anjlika--- //remeber while comparing 2 non primitive data type not to use == //remember Arrays.sort for primitive data has worst time case complexity of 0(n^2) bcoz it uses quick sort //again silly mistakes ,yr kb tk krta rhega ye mistakes //try to write simple codes ,break it into simple things //knowledge>rating /* public class Main implements Runnable{ public static void main(String[] args) { new Thread(null,new Main(),"Main",1<<26).start(); } public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; libraries.InputReader in = new libraries.InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC();//chenge the name of task solver.solve(1, in, out); out.close(); } */ 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 = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String next() { return readString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -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
6c4844b48373c5830e0d85b9c73c66ef
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.BigInteger; public class code{ public static class Pair{ int a; int b; Pair(int i,int j){ a=i; b=j; } } public static int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } public static void shuffle(int a[], int n) { for (int i = 0; i < n; i++) { // getting the random index int t = (int)Math.random() * a.length; // and swapping values a random index // with the current index int x = a[t]; a[t] = a[i]; a[i] = x; } } public static PrintWriter out = new PrintWriter(System.out); public static long[][] dp; //@SuppressWarnings("unchecked") public static void main(String[] arg) throws IOException{ //Reader in=new Reader(); //PrintWriter out = new PrintWriter(System.out); //Scanner in = new Scanner(System.in); FastScanner in=new FastScanner(); int t=in.nextInt(); while(t-- > 0){ int n=in.nextInt(); int k=in.nextInt(); String s=in.next(); int[] arr=new int[n]; int[] count=new int[n]; if(k==0){ out.println(s); for(int i=0;i<n;i++) out.print(0+" "); out.println(); continue; } //int f=n-1; for(int i=0;i<n;i++){ arr[i]=s.charAt(i)-'0'; // if(arr[i]==1){ // if(f==n-1) f=i; // } } int c=0; int last=arr[n-1]; for(int i=0;i<n;i++){ int r=k-c; if(arr[i]==0){ if(c%2==0){ if(r>0 && r%2==0){ c++; arr[i]=1; count[i]=1; } if(r>0 && r%2==1){ arr[i]=1; } } else{ arr[i]=1; if(r>0 && r%2==1){ c++; count[i]=1; } } } else{ if(c%2==0){ if(r>0 && r%2==1){ c++; count[i]=1; } } else{ arr[i]=0; if(r>0 && r%2==0){ c++; count[i]=1; arr[i]=1; } else if(r>0 && r%2==1){ arr[i]=1; } } } //out.println("check __ "+ i + " "+ arr[i]+" "+count[i]+" "+c); } if(k-c>0){ count[n-1]+=(k-c); int r=k-count[n-1]; if(r%2==0) arr[n-1]=last; else arr[n-1]=last^1; } for(int i=0;i<n;i++) out.print(arr[i]); out.println(); for(int i=0;i<n;i++) out.print(count[i]+" "); out.println(); } out.flush(); } } class Fenwick{ int[] bit; public Fenwick(int n){ bit=new int[n]; //int sum=0; } public void update(int index,int val){ index++; for(;index<bit.length;index += index&(-index)) bit[index]+=val; } public int presum(int index){ int sum=0; for(;index>0;index-=index&(-index)) sum+=bit[index]; return sum; } public int sum(int l,int r){ return presum(r+1)-presum(l); } } class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
Java
["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
a81a1b3d509a2dbc14dfc6b2527069e4
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; public class BitFlipping { public static void main(String[] args){ Scanner sc=new Scanner(System.in); int t= sc.nextInt(); for(int i=0;i<t;i++){ int n= sc.nextInt(); int k=sc.nextInt(); int rest=k; String s=sc.next(); int[] times=new int[n]; for(int j=0;j<n;j++){ if(s.charAt(j)=='0'){ if(k%2==0){ if(rest>0){ rest--; times[j]+=1; } } } if (s.charAt(j)=='1'){ if (k%2==1){ if (rest>0){ rest--; times[j]+=1; } } } } if (rest%2==0){ times[0]+=rest; } else { times[n-1]+=rest; } StringBuffer res=new StringBuffer(); if(k%2==0) { for (int j = 0; j < n; j++) { if (s.charAt(j) == '0') { if (times[j] % 2 == 0) { res.append(0); } else { res.append(1); } } else if (s.charAt(j) == '1') { if (times[j] % 2 == 0) { res.append(1); } else { res.append(0); } } } } else { for (int j = 0; j < n; j++) { if (s.charAt(j) == '0') { if (times[j] % 2 == 0) { res.append(1); } else { res.append(0); } } else if (s.charAt(j) == '1') { if (times[j] % 2 == 0) { res.append(0); } else { res.append(1); } } } } System.out.println(res); for (int time:times){ System.out.print(time+" "); } } } }
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
b24dd08c11f91f7c79d9a5792d03e601
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.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.PriorityQueue; import java.util.Scanner; import java.util.StringTokenizer; import javax.print.DocFlavor.INPUT_STREAM; public class Main { public static void main(String[] args) throws Exception { Sol obj=new Sol(); obj.runner(); } } class Sol{ FastScanner fs=new FastScanner(); Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); void runner() throws Exception{ int T=1; //T=sc.nextInt(); //preCompute(); T=fs.nextInt(); while(T-->0) { solve(T); } out.close(); System.gc(); } private void preCompute() { } private void solve(int T) throws Exception { int n=fs.nextInt(); int k=fs.nextInt(); String s=fs.next(); char arr[]=s.toCharArray(); int t[]=new int[ n ]; int temp=k; if( k%2==0 ) { for(int i=0;i<arr.length&& k>0;i++) { if( arr[i]=='0' ) { t[i]++; k--; } } t[n-1]+=k; }else { for(int i=0;i<arr.length&& k>0;i++) { if( arr[i]=='1' ) { t[i]++; k--; } } t[n-1]+=k; } StringBuilder str=new StringBuilder(); for(int i=0;i<n;i++ ) { if( (temp-t[i])%2==0 ) { //System.out.print(arr[i]); str.append(arr[i]); }else { if( arr[i]=='1' ) //System.out.print('0'); str.append("0"); else { //System.out.print('1'); str.append("1"); } } } System.out.println(str.toString()); for(int i=0;i<n;i++) { System.out.print(t[i]+" "); } System.out.println(); /* int n=fs.nextInt(); int r=fs.nextInt(); int b=fs.nextInt(); char f , s; if( r> b ) { f='R'; s='B'; }else { f='B'; s='R'; r=(r+b)-(b=r); } int dif=( r+b-1 )/b; while( r > 0 || b > 0 ) { for( int i=0;i<dif;i++ ) { System.out.print(f); } r-=dif; b--; System.out.print(s); } System.out.println(); /**/ } static class FastScanner { BufferedReader br; StringTokenizer st ; FastScanner(){ br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } FastScanner(String file) { try { br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); st = new StringTokenizer(""); } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("file not found"); e.printStackTrace(); } } String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } String readLine() 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
244bd78bf93586908f0201c8429d8be4
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.StreamTokenizer; import java.math.BigInteger; import static java.lang.System.out; import static java.lang.Math.*; import java.util.*; public class Main { public static void main(String[] args) { Read in = new Read(System.in); int tN = in.nextInt(); while(tN > 0) { tN--; int n=in.nextInt(); int k = in.nextInt(); String s=in.next(); int ans [] =new int [n]; int r [] =new int [n]; int cot=k; for(int i=0;i< n;i++) { int a=s.charAt(i) - '0'; if(a==0&&k%2==0&&cot>0) { ans[i]++; r[i]=1; cot--; }else if(a==1&&k%2==1&&cot>0) { ans[i]++; r[i]=1; cot--; }else { r[i]=a^(k%2); } } if(cot>0) { r[n-1]=r[n-1]^(cot%2); ans[n-1]+=cot; } StringBuilder ss=new StringBuilder(); StringBuilder sss=new StringBuilder(); for(int i=0;i<n;i++) { ss.append(r[i]); } for(int i=0;i<n;i++) { sss.append(ans[i]); sss.append(' '); } out.println(ss.toString()); out.println(sss.toString()); } } static class Read {//自定义快读 Read public BufferedReader reader; public StringTokenizer tokenizer; public Read(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 String nextLine() { String str = null; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public Double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } static long gcd(long a, long b) { return (a % b == 0) ? b : gcd(b, a % b); } }
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
d5ee4f60393ad874c5c1885a40c23b1d
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 Pair { int f;int s; // Pair(){} Pair(int f,int s){ this.f=f;this.s=s;} } static class Fast { BufferedReader br; StringTokenizer st; public Fast() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArray1(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (IOException e) { e.printStackTrace(); } return str; } } /* static long noOfDivisor(long a) { long count=0; long t=a; for(long i=1;i<=(int)Math.sqrt(a);i++) { if(a%i==0) count+=2; } if(a==((long)Math.sqrt(a)*(long)Math.sqrt(a))) { count--; } return count; }*/ static boolean isPrime(long a) { for (long i = 2; i <= (long) Math.sqrt(a); i++) { if (a % i == 0) return false; } return true; } static void primeFact(int n) { int temp = n; HashMap<Integer, Integer> h = new HashMap<>(); for (int i = 2; i * i <= n; i++) { if (temp % i == 0) { int c = 0; while (temp % i == 0) { c++; temp /= i; } h.put(i, c); } } if (temp != 1) h.put(temp, 1); } static void reverseArray(int a[]) { int n = a.length; for (int i = 0; i < n / 2; i++) { a[i] = a[i] ^ a[n - i - 1]; a[n - i - 1] = a[i] ^ a[n - i - 1]; a[i] = a[i] ^ a[n - i - 1]; } } 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 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); } public static void main(String args[]) throws IOException { Fast sc = new Fast(); PrintWriter out = new PrintWriter(System.out); int t1 = sc.nextInt(); while (t1-- > 0) { int n=sc.nextInt();int k=sc.nextInt(); String s2=sc.nextLine();char s[]=s2.toCharArray(); int ans[]=new int[n];Arrays.fill(ans,0); if(k%2==0) { StringBuffer ans1 = new StringBuffer(); TreeSet<Integer> ts = new TreeSet<>(); int k1 = 0; for (int i = 0; i < n; i++) { if (i != n - 1 && s[i] == '0' && k1 < k) { ans1.append('1'); ts.add(i); k1++; ans[i] = 1; } else if (i!=n-1&&k1<k&&s[i] == '1') ans1.append('1'); else if (k1 >= k) ans1.append(s[i]); } if (k1 < k) { ans[n - 1] = ans[n - 1] + k - k1; if ((k-ans[n - 1]) % 2 == 0) ans1.append(s[n - 1]); else ans1.append((char)((1 - (s[n - 1] - '0')) + 48)); } out.println(ans1); } else { // StringBuffer ans1 = new StringBuffer(); TreeSet<Integer> ts = new TreeSet<>(); int k1 = 0; for (int i = 0; i < n; i++) { if (i != n - 1 && s[i] == '1' && k1 < k) { ans1.append('1'); ts.add(i); k1++; ans[i] = 1; } else if (i!=n-1&&k1<k&&s[i] == '0') ans1.append('1'); else if (k1 >= k) { if(s[i]=='0') ans1.append('1'); else ans1.append('0'); } } if (k1 < k) { ans[n - 1] = ans[n - 1] + k - k1; if ((k-ans[n - 1]) % 2 == 0) ans1.append(s[n - 1]); else ans1.append((char)((1 - (s[n - 1] - '0')) +48)); } out.println(ans1); } for(int ne:ans) out.print(ne+" "); out.println(); } out.close(); } } /* 8 7 2 1110010 7 2 1110011 7 3 1110010 7 3 1110011 7 8 1110010 7 8 1110010 7 5 1110010 7 5 1110011 */
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
6cb80a100d3ce68258b991b381132a3e
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 org.omg.PortableInterceptor.INACTIVE; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Scanner; import java.util.StringTokenizer; public class B { static StringTokenizer st; static PrintWriter pw; static BufferedReader br; static String nextToken() { try { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } static int nextInt() { return Integer.parseInt(nextToken()); } static long nextLong() { return Long.parseLong(nextToken()); } public static void main(String[] args) { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); run(); pw.close(); } private static void run() { int t = nextInt(); if(t == 0){ } for (int i = 0; i < t; i++) { int n = nextInt(); int k = nextInt(); String s = nextToken(); int chars[] = new int[n]; for (int j = 0; j < n; j++) { chars[j] = s.charAt(j) - '0'; } int[] ans = new int[n]; if (k % 2 == 1) { int c = n - 1; for (int j = 0; j < n; j++) { if (chars[j] == 1) { c = j; break; } } swapAll(chars); chars[c] ^= 1; ans[c]++; k--; } int l = -1; for (int j = 0; k > 0 && j < n; j++) { if (chars[j] == 0) { if (k % 2 == 0) { l = j; k--; } else { ans[l]++; ans[j]++; chars[l] = 1; chars[j] = 1; l = -1; k--; } } } if (k == 0) { myPrint(chars, ans); } else { if (k % 2 == 1) { k--; ans[l]++; chars[l] ^= 1; ans[n - 1]++; chars[n - 1] ^= 1; } ans[n - 1] += k; myPrint(chars, ans); } } } private static void swapAll(int[] chars) { for (int i = 0; i < chars.length; i++) { chars[i] ^= 1; } } private static void myPrint(int[] chars, int[] ans) { for (int j = 0; j < chars.length; j++) { pw.print(chars[j]); } pw.println(); for (int j = 0; j < ans.length; j++) { pw.print(ans[j] + " "); } pw.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
71a599fea9a0c2678aabb53b937dbb72
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: rickytsung(En Chi Tsung) * Date: 2022/7/30 * Problem: CF Round 782 */ import java.util.*; import java.time.*; import java.io.*; import java.math.*; public class Main { public static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); public static long ret,ans=0,x,y; public static int reti,rd; public static boolean neg; public static final int mod=998244353,mod2=1_000_000_007,ma=600005; public static Useful us=new Useful(mod); public static int[] A=new int[ma]; public static int[] Aans=new int[ma]; public static int[] B=new int[ma]; public static int[] Bans=new int[ma]; public static void main(String[] args) throws Exception{ //String [] in=br.readLine().split(" "); final int t=readint(); for(int z=0;z<t;z++) { final int n=readint(); final int k=readint(); br.readLine().split(" "); final String in=(br.readLine().split(" "))[0]; for(int i=0;i<n;i++) { //System.out.println(in); A[i]=in.charAt(i)-'0'; Aans[i]=0; if((k&1)==1) { A[i]^=1; } } int j=k; for(int i=0;i<n&&j>0;i++) { if(A[i]==0) { A[i]=1; j--; Aans[i]=1; } } Aans[n-1]+=j; A[n-1]^=(j&1); for(int i=0;i<n;i++) { bw.write(A[i]+""); } bw.write("\n"); for(int i=0;i<n;i++) { bw.write(Aans[i]+" "); } bw.write("\n"); } bw.flush(); } public static int readint() throws Exception{ reti=0; neg=false; while(rd<48||rd>57) { rd=br.read(); if(rd=='-') { neg=true; } } while(rd>47&&rd<58) { reti*=10; reti+=(rd&15); rd=br.read(); } if(neg)reti*=-1; return reti; } public static long readlong() throws Exception{ ret=0; neg=false; while(rd<48||rd>57) { rd=br.read(); if(rd=='-') { neg=true; } } while(rd>47&&rd<58) { ret*=10; ret+=(rd&15); rd=br.read(); } if(neg)ret*=-1; return ret; } } /* */ /* */ class Pii{ int x,y,z; Pii(int a,int b,int c){ x=a; y=b; z=c; } @Override public boolean equals(Object o) { if (this==o) return true; if (!(o instanceof Pii)) return false; Pii key = (Pii) o; return x==key.x&&y==key.y; } @Override public int hashCode() { long result=x; result=31*result+y; return (int)(result%998244353); } } class Pll{ long x,y; Pll(long a,long b){ x=a; y=b; } @Override public boolean equals(Object o) { if (this==o) return true; if (!(o instanceof Pll)) return false; Pll key = (Pll) o; return x==key.x&&y==key.y; } @Override public int hashCode() { long result=x; result=31*result+y; return (int)(result%998244353); } } class Useful{ long mod; Useful(long m){mod=m;} void al(ArrayList<ArrayList<Integer>> a,int n){for(int i=0;i<n;i++) {a.add(new ArrayList<Integer>());}} void arr(int[] a,int init) {for(int i=0;i<a.length;i++) {a[i]=init;}} void arr(long[] a,long init) {for(int i=0;i<a.length;i++) {a[i]=init;}} void arr(int[][] a,int init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {a[i][j]=init;}}} void arr(long[][] a,long init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {a[i][j]=init;}}} void arr(int[][][] a,int init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {a[i][j][k]=init;}}}} void arr(long[][][] a,long init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {a[i][j][k]=init;}}}} void arr(int[][][][] a,int init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {Arrays.fill(a[i][j][k],init);}}}} void arr(long[][][][] a,long init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {Arrays.fill(a[i][j][k],init);}}}} long fast(long x,long pw) { if(pw<=0)return 1; if(pw==1)return x; long h=fast(x,pw>>1); if((pw&1)==0) { return h*h%mod; } return h*h%mod*x%mod; } long[][] mul(long[][] a,long[][] b){ long[][] c=new long[a.length][b[0].length]; for(int i=0;i<a.length;i++) { for(int j=0;j<b[0].length;j++) { for(int k=0;k<a[0].length;k++) { c[i][j]+=a[i][k]*b[k][j]; c[i][j]%=mod; } } } return c; } long[][] fast(long[][] x,int pw){ if(pw==1)return x; long[][] h=fast(x,pw>>1); if((pw&1)==0) { return mul(h,h); } else { return mul(mul(h,h),x); } } int gcd(int a,int b) { if(a==0)return b; if(b==0)return a; return gcd(b,a%b); } long gcd(long a,long b) { if(a==0)return b; if(b==0)return a; return gcd(b,a%b); } long lcm(long a, long b){ return a*(b/gcd(a,b)); } double log2(int x) { return (Math.log(x)/Math.log(2)); } double log2(long x) { return (Math.log(x)/Math.log(2)); } }
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
81865ef55d7515044150abf63dfa0cdd
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 Balabizo { public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tt = sc.nextInt(); while(tt-->0){ int n = sc.nextInt() , k = sc.nextInt() , kk = k; char[] c = sc.next().toCharArray(); int[] f = new int[n]; for(int i=0; i<n && kk>0 ;i++){ if(k%2 == c[i] - '0'){ f[i] = 1; kk--; } } f[n-1] += kk; for(int i=0; i<n ;i++){ if((k-f[i])%2 == 1 ) c[i] = (char) ('1' - (c[i] - '0')); } pw.println(new String(c)); for(int i : f) pw.print(i+" "); pw.println(); } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws IOException { br = new BufferedReader(new FileReader(file)); } 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 String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["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
213a720f805dabbf4e3ef432cdb30260
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
//---#ON_MY_WAY--- //---#THE_SILENT_ONE--- import static java.lang.Math.*; import java.io.*; import java.math.*; import java.util.*; public class apples { static FastReader x = new FastReader(); static OutputStream outputStream = System.out; static PrintWriter out = new PrintWriter(outputStream); /*---------------------------------------CODE STARTS HERE-------------------------*/ public static void main(String[] args) throws NumberFormatException, IOException { long startTime = System.nanoTime(); int mod = 1000000007; int t = x.nextInt(); StringBuilder str = new StringBuilder(); while (t > 0) { int n = x.nextInt(), k = x.nextInt(); char a[] = x.next().toCharArray(); int b[] = new int[n]; int flip = 0; for(int i = 0; i < n&&flip<k; i++) { if(k%2==0) { if(a[i]=='0') { b[i] = 1; flip++; } else b[i] = 0; } else { if(a[i]=='0') b[i] = 0; else { b[i] = 1; flip++; } } if(flip==k) break; } if(flip!=k) b[n-1] += (k-flip); for(int i = 0; i < n; i++) { if((k-b[i])%2==0) str.append(a[i]); else { if(a[i]=='0') str.append(1); else str.append(0); } } str.append("\n"); for(int i : b) str.append(i+" "); str.append("\n"); t--; } out.println(str); out.flush(); long endTime = System.nanoTime(); //System.out.println((endTime-startTime)/1000000000.0); } /*--------------------------------------------FAST I/O-------------------------------*/ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } char nextchar() { char ch = ' '; try { ch = (char) br.read(); } catch (IOException e) { e.printStackTrace(); } return ch; } } /*--------------------------------------------HELPER---------------------------------*/ static class pair implements Comparable<pair> { int x, y; public pair(int a, int b) { x = a; y = b; } @Override public int hashCode() { int hash = 3; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final pair other = (pair) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(apples.pair o) { if(this.x==o.x) return this.y-o.y; return this.x-o.x; } } /*--------------------------------------------BOILER PLATE---------------------------*/ static int[] readarr(int n) { int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = x.nextInt(); } return arr; } static int[] sortint(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) { al.add(i); } Collections.sort(al); for (int i = 0; i < a.length; i++) { a[i] = al.get(i); } return a; } static long[] sortlong(long a[]) { ArrayList<Long> al = new ArrayList<>(); for (long i : a) { al.add(i); } Collections.sort(al); for (int i = 0; i < al.size(); i++) { a[i] = al.get(i); } return a; } static long pow(long x, long y) { long result = 1; while (y > 0) { if (y % 2 == 0) { x = x * x; y = y / 2; } else { result = result * x; y = y - 1; } } return result; } static long pow(long x, long y, long mod) { long result = 1; x %= mod; while (y > 0) { if (y % 2 == 0) { x = (x % mod * x % mod) % mod; y /= 2; } else { result = (result % mod * x % mod) % mod; y--; } } return result; } static int[] revsort(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) { al.add(i); } Collections.sort(al, Comparator.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = al.get(i); } return a; } static int[] gcd(int a, int b, int ar[]) { if (b == 0) { ar[0] = a; ar[1] = 1; ar[2] = 0; return ar; } ar = gcd(b, a % b, ar); int t = ar[1]; ar[1] = ar[2]; ar[2] = t - (a / b) * ar[2]; return ar; } static boolean[] esieve(int n) { boolean p[] = new boolean[n + 1]; Arrays.fill(p, true); for (int i = 2; i * i <= n; i++) { if (p[i] == true) { for (int j = i * i; j <= n; j += i) { p[j] = false; } } } return p; } static ArrayList<Integer> primes(int n) { boolean p[] = new boolean[n + 1]; ArrayList<Integer> al = new ArrayList<>(); Arrays.fill(p, true); int i = 0; for (i = 2; i * i <= n; i++) { if (p[i] == true) { al.add(i); for (int j = i * i; j <= n; j += i) { p[j] = false; } } } for (i = i; i <= n; i++) { if (p[i] == true) { al.add(i); } } return al; } static int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } }
Java
["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
ee6286ed157dccb23eb4213b0382ff8f
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 practice { public static void main(String[] args) throws Exception { Scanner s= new Scanner(System.in); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int k=s.nextInt(); String str=s.next(); int arr[]= new int[n]; StringBuilder ans= new StringBuilder(); if(k%2==0) { for(int i=0; i<n;i++) { if(k!=0) { if(str.charAt(i)=='0') { ans.append(1); arr[i]++; k--; }else { ans.append(1); } }else { if(str.charAt(i)=='1') { ans.append(1); }else { ans.append(0); } } } if(k!=0) { if(k%2!=0) { ans.deleteCharAt(n-1); ans.append(0); } arr[n-1]+=k; } }else { for(int i=0; i<n;i++) { if(k!=0) { if(str.charAt(i)=='1') { ans.append(1); arr[i]++; k--; }else { ans.append(1); } }else { if(str.charAt(i)=='1') { ans.append(0); }else { ans.append(1); } } } if(k!=0) { if(k%2!=0) { ans.deleteCharAt(n-1); ans.append(0); } arr[n-1]+=k; } } System.out.println(ans); StringBuilder sb= new StringBuilder(); for(int a :arr) { sb.append(a+" "); } 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
378b7b030900ee389e90927ba1bd463f
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 final class Main { //int 2e9 - long 9e18 static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(0, 1), new Pair(1, 0), new Pair(0, -1)}; static int mod = (int) (1e9 + 7); static int mod2 = 998244353; public static void main(String[] args) { int tt = i(); while (tt-- > 0) { solve(); } out.flush(); } public static void solve() { int n = i(); int k = i(); int m = k; String s = s(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.charAt(i) - '0'; } int[] ans = new int[n]; for (int i = 0; i < n; i++) { if (m == 0) { break; } if (arr[i] == 1) { if (k % 2 == 0) { } else { ans[i] = 1; m--; } } else { if (k % 2 == 0) { ans[i] = 1; m--; } else { } } } ans[n - 1] += m; for (int i = 0; i < n; i++) { int kk = k - ans[i]; if (kk % 2 == 1) { arr[i] = 1 - arr[i]; } } for (int i = 0; i < n; i++) { out.print(arr[i]); } out.println(); print(ans); } // (10,5) = 2 ,(11,5) = 3 static long upperDiv(long a, long b) { return (a / b) + ((a % b == 0) ? 0 : 1); } static long sum(int[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static int[] preint(int[] a) { int[] pre = new int[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] pre(int[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] post(int[] a) { long[] post = new long[a.length + 1]; post[0] = 0; for (int i = 0; i < a.length; i++) { post[i + 1] = post[i] + a[a.length - 1 - i]; } return post; } static long[] pre(long[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static void print(char A[]) { for (char c : A) { out.print(c); } out.println(); } static void print(boolean A[]) { for (boolean c : A) { out.print(c + " "); } out.println(); } static void print(int A[]) { for (int c : A) { out.print(c + " "); } out.println(); } static void print(long A[]) { for (long i : A) { out.print(i + " "); } out.println(); } static void print(List<Integer> A) { for (int a : A) { out.print(a + " "); } } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static double d() { return in.nextDouble(); } static String s() { return in.nextLine(); } static String c() { return in.next(); } static int[][] inputWithIdx(int N) { int A[][] = new int[N][2]; for (int i = 0; i < N; i++) { A[i] = new int[]{i, in.nextInt()}; } return A; } static int[] input(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] inputLong(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) { A[i] = in.nextLong(); } return A; } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long GCD(long a, long b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long LCM(int a, int b) { return (long) a / GCD(a, b) * b; } static long LCM(long a, long b) { return a / GCD(a, b) * b; } // find highest i which satisfy a[i]<=x static int lowerbound(int[] a, int x) { int l = 0; int r = a.length - 1; while (l < r) { int m = (l + r + 1) / 2; if (a[m] <= x) { l = m; } else { r = m - 1; } } return l; } static void shuffle(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } } static void shuffleAndSort(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static void shuffleAndSort(int[][] arr, Comparator<? super int[]> comparator) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int[] temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr, comparator); } static void shuffleAndSort(long[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); long temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static boolean isPerfectSquare(double number) { double sqrt = Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static void swap(int A[], int a, int b) { int t = A[a]; A[a] = A[b]; A[b] = t; } static void swap(char A[], int a, int b) { char t = A[a]; A[a] = A[b]; A[b] = t; } static long pow(long a, long b, int mod) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow = (pow * x) % mod; } x = (x * x) % mod; b /= 2; } return pow; } static long pow(long a, long b) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow *= x; } x = x * x; b /= 2; } return pow; } static long modInverse(long x, int mod) { return pow(x, mod - 2, mod); } static boolean isPrime(long N) { if (N <= 1) { return false; } if (N <= 3) { return true; } if (N % 2 == 0 || N % 3 == 0) { return false; } for (int i = 5; i * i <= N; i = i + 6) { if (N % i == 0 || N % (i + 2) == 0) { return false; } } return true; } public static String reverse(String str) { if (str == null) { return null; } return new StringBuilder(str).reverse().toString(); } public static void reverse(int[] arr) { for (int i = 0; i < arr.length / 2; i++) { int tmp = arr[i]; arr[arr.length - 1 - i] = tmp; arr[i] = arr[arr.length - 1 - i]; } } public static String repeat(char ch, int repeat) { if (repeat <= 0) { return ""; } final char[] buf = new char[repeat]; for (int i = repeat - 1; i >= 0; i--) { buf[i] = ch; } return new String(buf); } public static int[] manacher(String s) { char[] chars = s.toCharArray(); int n = s.length(); int[] d1 = new int[n]; for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1); while (0 <= i - k && i + k < n && chars[i - k] == chars[i + k]) { k++; } d1[i] = k--; if (i + k > r) { l = i - k; r = i + k; } } return d1; } public static int[] kmp(String s) { int n = s.length(); int[] res = new int[n]; for (int i = 1; i < n; ++i) { int j = res[i - 1]; while (j > 0 && s.charAt(i) != s.charAt(j)) { j = res[j - 1]; } if (s.charAt(i) == s.charAt(j)) { ++j; } res[i] = j; } return res; } } class Pair { int i; int j; Pair(int i, int j) { this.i = i; this.j = j; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Pair pair = (Pair) o; return i == pair.i && j == pair.j; } @Override public int hashCode() { return Objects.hash(i, j); } } class ThreePair { int i; int j; int k; ThreePair(int i, int j, int k) { this.i = i; this.j = j; this.k = k; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } ThreePair pair = (ThreePair) o; return i == pair.i && j == pair.j && k == pair.k; } @Override public int hashCode() { return Objects.hash(i, j); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } class Node { int val; public Node(int val) { this.val = val; } } class ST { int n; Node[] st; ST(int n) { this.n = n; st = new Node[4 * Integer.highestOneBit(n)]; } void build(Node[] nodes) { build(0, 0, n - 1, nodes); } private void build(int id, int l, int r, Node[] nodes) { if (l == r) { st[id] = nodes[l]; return; } int mid = (l + r) >> 1; build((id << 1) + 1, l, mid, nodes); build((id << 1) + 2, mid + 1, r, nodes); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } void update(int i, Node node) { update(0, 0, n - 1, i, node); } private void update(int id, int l, int r, int i, Node node) { if (i < l || r < i) { return; } if (l == r) { st[id] = node; return; } int mid = (l + r) >> 1; update((id << 1) + 1, l, mid, i, node); update((id << 1) + 2, mid + 1, r, i, node); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } Node get(int x, int y) { return get(0, 0, n - 1, x, y); } private Node get(int id, int l, int r, int x, int y) { if (x > r || y < l) { return new Node(0); } if (x <= l && r <= y) { return st[id]; } int mid = (l + r) >> 1; return comb(get((id << 1) + 1, l, mid, x, y), get((id << 1) + 2, mid + 1, r, x, y)); } Node comb(Node a, Node b) { if (a == null) { return b; } if (b == null) { return a; } return new Node(GCD(a.val, b.val)); } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } }
Java
["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
237977e63bfa024d62d8c07182d8c952
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; public class B1659 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for (int t=0; t<T; t++) { int N = in.nextInt(); int K = in.nextInt(); char[] S = in.next().toCharArray(); if (K%2 == 1) { for (int n=0; n<N; n++) { S[n] = (S[n] == '0') ? '1' : '0'; } } int[] A = new int[N]; for (int n=0; n<N; n++) { if (S[n] == '0' && K > 0) { S[n] = '1'; K--; A[n] = 1; } } if (K%2 == 1) { S[N-1] = '0'; } A[N-1] += K; StringBuilder out = new StringBuilder(); out.append(new String(S)).append('\n'); for (int a : A) { out.append(a).append(' '); } System.out.println(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
570f25620568055b262adfd54f32151f
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 class1 { public static void main(String[] args) { Scanner input =new Scanner(System.in); int t=input.nextInt(); while(t-->0) { int n=input.nextInt(); int k=input.nextInt(); int m=k,x=0; String s=input.next(); char c[]=s.toCharArray(); StringBuilder p=new StringBuilder(s); char d[]=new char[n]; int f[]=new int[n]; int tmpk=k; for(int i=0;i<n && tmpk>0;i++) { if(k%2==c[i]-'0') { f[i]=1; tmpk--; } } f[n-1]+=tmpk; for(int i=0;i<n;i++) { if((k-f[i])%2==1) { p.setCharAt(i,(char)('1'-(c[i]-'0'))); } } System.out.println(p); for(int i=0;i<n;i++) { System.out.print(f[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
dd1c73e00e7dec117756d86b00fc91a0
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.*; /* Challenge 1: Newbie to CM in 1year (Dec 2021 - Nov 2022) 5* Codechef Challenge 2: CM to IM in 1 year (Dec 2022 - Nov 2023) 6* Codechef Challenge 3: IM to GM in 1 year (Dec 2023 - Nov 2024) 7* Codechef Goal: Become better in CP! Key: Consistency and Discipline Desire: SDE @ Google USA Motto: Do what i Love <=> Love what you do */ public class Coder { static StringBuffer str=new StringBuffer(); static int n, k; static char c[]; static void solve(){ int f[]=new int[n]; int tk=k; for(int i=0;i<n && tk>0;i++){ f[i]=((('1'-c[i])&(1-k%2)) | ((c[i]-'0') & (k%2))); if(f[i]==1) tk--; } f[n-1]+=tk; for(int i=0;i<n;i++){ if((k-f[i])%2!=0) c[i]=(char)('1'-c[i]+'0'); } for(int i=0;i<n;i++) str.append(c[i]); str.append("\n"); for(int i=0;i<n;i++) str.append(f[i]).append(" "); str.append("\n"); } public static void main(String[] args) throws java.lang.Exception { boolean lenv=false; BufferedReader bf; PrintWriter pw; if(lenv){ bf = new BufferedReader( new FileReader("input.txt")); pw=new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); }else{ bf = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new OutputStreamWriter(System.out)); } int q1 = Integer.parseInt(bf.readLine().trim()); while (q1-->0) { String s[]=bf.readLine().trim().split("\\s+"); n=Integer.parseInt(s[0]); k=Integer.parseInt(s[1]); c=bf.readLine().trim().toCharArray(); solve(); } pw.print(str); pw.flush(); // System.out.print(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