codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1256D.md (1892B)


      1 Source: https://codeforces.com/problemset/problem/1256/D
      2 
      3 D. Binary String Minimizing
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You are given a binary string of length $n$ (i. e. a string consisting of $n$ characters '0' and '1').
     22 
     23 In one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you can obtain from the given one if you can perform no more than $k$ moves? It is possible that you do not perform any moves at all.
     24 
     25 Note that you can swap the same pair of adjacent characters with indices $i$ and $i+1$ arbitrary (possibly, zero) number of times. Each such swap is considered a separate move.
     26 
     27 You have to answer $q$ independent test cases.
     28 
     29 Input
     30 
     31 The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of test cases.
     32 
     33 The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6, 1 \le k \le n^2$) — the length of the string and the number of moves you can perform.
     34 
     35 The second line of the test case contains one string consisting of $n$ characters '0' and '1'.
     36 
     37 It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$).
     38 
     39 Output
     40 
     41 For each test case, print the answer on it: the lexicographically minimum possible string of length $n$ you can obtain from the given one if you can perform no more than $k$ moves.
     42 
     43 Example
     44 
     45 Input
     46 
     47 ```
     48 3
     49 8 5
     50 11011010
     51 7 9
     52 1111100
     53 7 11
     54 1111100
     55 ```
     56 
     57 Output
     58 
     59 ```
     60 01011110
     61 0101111
     62 0011111
     63 ```
     64 
     65 Note
     66 
     67 In the first example, you can change the string as follows: $1\underline{10}11010 \rightarrow \underline{10}111010 \rightarrow 0111\underline{10}10 \rightarrow 011\underline{10}110 \rightarrow 01\underline{10}1110 \rightarrow 01011110$.
     68 
     69 In the third example, there are enough operations to make the string sorted.