码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #598 (Div. 3) D - Binary String Minimizing

时间:2019-11-05 13:27:45      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:cout   end   guarantee   htm   思路   col   ons   efi   time   

原文链接:https://www.cnblogs.com/xwl3109377858/p/11797618.html

Codeforces Round #598 (Div. 3)

D - Binary String Minimizing

You are given a binary string of length n (i. e. a string consisting of n characters ‘0‘ and ‘1‘).

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.

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.

You have to answer q independent test cases.

Input

The first line of the input contains one integer q (1≤q≤104) — the number of test cases.

The first line of the test case contains two integers n and k (1≤n≤106,1≤k≤n2) — the length of the string and the number of moves you can perform.

The second line of the test case contains one string consisting of n characters ‘0‘ and ‘1‘.

It is guaranteed that the sum of n over all test cases does not exceed 106 (∑n≤106).

Output

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.

Example

input

3

8 5

11011010

7 9

1111100

7 11

1111100

output

01011110

0101111

0011111

Note

In the first example, you can change the string as follows: 110–––11010→10–––111010→011110–––10→01110–––110→0110–––1110→01011110.

In the third example, there are enough operations to make the string sorted.

 

题意:题意大概是给你一个二进制字符串长度为n,你可以最多操作k次,

交换字符串中相邻两个字符的位置,求能得到的字符串的最小字典序。

思路:贪心的思想,每次最好将0换到最前面,这样就能保证得到最小字典序的字符串。

而 k 最大为 n^2 ,显然不能对原字符串一个一个的交换操作,那我们就采用标记的方法,

,每找到一个 0 字符,贪心的判断他能前移到那个位置或者不动,然后标记位置,

最后根据标记输出 0 1 就行,具体看代码。

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<map>
 7 #include<set>
 8 #include<vector>
 9 #include<queue>
10 #include<list>
11 //#include<unordered_map>
12 using namespace std;
13 #define ll long long 
14 const int inf=1e9+7;
15 const int mod=1e9+7;
16 
17 int main()
18 {
19     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
20     
21     int T;
22     cin>>T;
23     
24     ll int n,k;
25     string str;
26     
27     while(T--)
28     {
29         cin>>n>>k;
30         cin>>str;
31         
32         int cnt=0;//记录前面放了几个0 
33         
34         vector<int>book0(n,0);//标记数组 
35         
36         for(int i=0;i<str.size();i++)
37         {
38             if(str[i]==1)
39                 continue;
40             
41             int now=i-cnt;//需要放到最前面的次数 
42             
43             if(k>=now)//直接放 
44             {
45                 k-=now;
46                 book0[cnt]=1;//标记位置 
47                 cnt++;
48             }
49             else//尽量往前放 
50             {
51                 book0[i-k]=1;
52                 k=0;
53             }
54             
55         }
56         
57         for(int i=0;i<n;i++)
58         {
59             if(book0[i]==1)//输出 
60                 cout<<0;
61             else
62                 cout<<1;
63         }
64         cout<<endl;
65         
66     }
67     
68     return 0;
69 }

 

Codeforces Round #598 (Div. 3) D - Binary String Minimizing

标签:cout   end   guarantee   htm   思路   col   ons   efi   time   

原文地址:https://www.cnblogs.com/xwl3109377858/p/11797618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!