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

原文链接: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 }

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

时间: 2024-08-27 22:55:25

Codeforces Round #598 (Div. 3) D - Binary String Minimizing的相关文章

贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

题目传送门 1 /* 2 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 3 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 #include <iostream> 10 using namespace std; 11 12 const int MAX

Codeforces Round #598 (Div. 3)

比赛链接:传送门 A. Payment Without Change 代码: #include <iostream> #include <map> #include <algorithm> #include <cstdio> #include <cstring> #include <set> #include <vector> #include <string> #include <queue> #

Codeforces Round #604 (Div. 2) A. Beautiful String

链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecutive characters are equal. For example, "ababcb", "a" and "abab" are beautiful strings, while "aaaaaa", "abaa&

Codeforces Round #282 Div.1 B Obsessive String --DP

题意: 给两个串S,T,问能找出多少的S的(a1,b1)(a2,b2)..(ak,bk),使Sa1---Sb1,...Sak---Sbk都包含子串T,其中k>=1,且(a1,b1)...(ak,bk)互不相交. 比如S = "abacaba",T="aba", 当k=1时,(0,6)满足,还有其他只包含一个aba串的也满足,k-2时,(0,2)(3,6)满足,(0,2)(4,6)也满足,(0,3)(4,6)也满足,所以总共有12种. 解法:dp.先用kmp找出

Codeforces Round #598 (Div. 3) E. Yet Another Division Into Teams dp

There are nn students at your university. The programming skill of the ii-th student is aiai. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has 2⋅1052⋅105 s

Codeforces Round #402 (Div. 2) D. String Game

题目链接:Codeforces Round #402 (Div. 2) D. String Game 题意: 给你两个字符串a,b,然后给你n=strlen(a)个数字n1,n2,...,nn,表示依次删a[ni-1]个字符. 当a串删到有k(k任意)个子串组合起来(顺序不变)刚好等于b串时,就不能删了. 比如 abba->(ab a) 刚好包括 aba ,bba不包括aab. 问最多能删多少次. 题解: 最开始还以为是要用某种数据结构啥的,结果都想复杂了,直接二分答案就行. 然后线性判断删掉后

Codeforces Round #286 (Div. 2)A. Mr. Kitayuta&#39;s Gift(暴力,string的应用)

由于字符串的长度很短,所以就暴力枚举每一个空每一个字母,出现行的就输出.这么简单的思路我居然没想到,临场想了很多,以为有什么技巧,越想越迷...是思维方式有问题,遇到问题先分析最简单粗暴的办法,然后一步一步的优化,不能盲目的想. 这道题要AC的快需要熟悉string的各种用法.这里做个简单总结:C++中string的常见用法. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstrin

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un

Codeforces Round #262 (Div. 2) 总结:二分

B. Little Dima and Equation 思路:本来前两题都很快做出来的,然后觉得ranting应该可以增加了.然后觉得代码没问题了,又想到了一组cha人的数据,然后就锁了,然后刚锁就被别人cha了看了代码才发现尼玛忘了判断小于10^9了,然后C反正想了好多种方法都不会就没心情了,就这样rating又降了 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #in