Codeforces Round #598 (Div. 3) D. Binary String Minimizing 贪心
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.
题意
现在有t组数据,每组数据给你n和k;表示二进制的长度和操作次数。
每次操作你可以选择一个数和他相邻的位置进行交换,在不超过k次的操作次数情况下,使得这个字符串变得最小。
题解
贪心,每次操作最小的数,使得他尽可能的放在前面;由于里面只有0和1,其实就是操作0,把0尽可能的往前面放。
代码
#include<bits/stdc++.h>
using namespace std;
int n;
long long k;
string s;
vector<int>p;
void solve(){
scanf("%d%lld",&n,&k);
cin>>s;
p.clear();
for(int i=0;i<s.size();i++){
if(s[i]=='0'){
p.push_back(i);
}
}
int la=-1;
for(int i=0;i<p.size();i++){
// cout<<"before "<<p[i]<<" "<<k<<endl;
if(k>p[i]-la-1){
int cost=p[i]-la-1;
k-=cost;
p[i]=la+1;
la=p[i];
}else{
p[i]-=k;
break;
}
//cout<<"aft "<<p[i]<<" "<<k<<endl;
}
vector<int>o(s.size(),1);
for(int i=0;i<p.size();i++){
o[p[i]]=0;
}
for(int i=0;i<o.size();i++){
cout<<o[i];
}
cout<<endl;
}
int main(){
int t;
scanf("%d",&t);
while(t--)solve();
}
Codeforces Round #598 (Div. 3) D. Binary String Minimizing 贪心的更多相关文章
- Codeforces Round #598 (Div. 3) D. Binary String Minimizing
You are given a binary string of length nn (i. e. a string consisting of nn characters '0' and '1'). ...
- Codeforces Round #604 (Div. 2) A. Beautiful String(贪心)
题目链接:https://codeforces.com/contest/1265/problem/A 题意 给出一个由 a, b, c, ? 组成的字符串,将 ? 替换为 a, b, c 中的一个字母 ...
- Codeforces Round #598 (Div. 3) B. Minimize the Permutation 贪心
B. Minimize the Permutation You are given a permutation of length n. Recall that the permutation is ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划
Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划 [Problem Description] 给你\( ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
- Codeforces Round #598 (Div. 3)
传送门 A. Payment Without Change 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/4 21:19:19 */ #i ...
- Codeforces Round #598 (Div. 3) A,B,C,D{E,F待补}
A. Payment Without Change #include<bits/stdc++.h> using namespace std; #define int long long ...
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
随机推荐
- Oracle - crfclust.bdb文件太大
今天在检查oracle rac集群时,突然才发现服务器的根目录下面占用了很多空间,照道理不应该出现这种情况,初步猜想可能是哪个日志或跟踪文件太大导致.切换到跟目录,使用du -sh *来一层一层查看到 ...
- 学习之Redis(二)
Redis的对象和数据结构 一.字符串对象(请参考学习之Redis(一):https://www.cnblogs.com/wbq1994/p/12029516.html) 二.列表对象 列表对象的编码 ...
- 关于字符串的格式化----format与%
格式化字符串一般有两种方法 1.%(d整数,s字符,f浮点数) 2.format 用处极为广泛且限制不多 注意:第一种对于数组的传递会报TypeError,所以必须传递数组 a = (1, 2, 3) ...
- mysql学习体系
1. MySQL的安装和配置 -- 安装的步骤 -- 配置参数的设置 -- 全局变量量与会话变量量的定义及区别 -- 常⻅见参数有哪些,有何定义,影响范围是什什么 -- 如何查看参数的值 -- 配置⽂ ...
- linux之任务调度,磁盘分区,yum下载
一.crond任务调度 调度机制: 基本语法 crontab [选项] -e : bianji crontab定时任务 -l : 查询crontab -r : 删除当前用户所有的crontab任务 例 ...
- TestNG如何用命令行运行
TestNG如何用命令行运行 调用TestNG最简单的方法事下面的: java org.testng.TestNG testng1.xml 以上是我在网上搜索到的最多的答案,但对于第一次接触testn ...
- Spring afterPropertiesSet方法
1.init-method方法,初始化bean的时候执行,可以针对某个具体的bean进行配置.init-method需要在applicationContext.xml配置文档中bean的定义里头写明. ...
- java之包装类
针对八种基本数据类型定义相应的引用类型--包装类: 有了类的特点,接可以调用类中的方法: 基本数据类型 包装类 boolean Bollean byte Byte short Short int In ...
- Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near ';characterEncoding=UTF
今天在使用springboot整合SSM的时候,配置好以后启动项目,报了一个这样的异常 java.sql.SQLNonTransientConnectionException: Cannot load ...
- Java 添加Word脚注、尾注
Word中的脚注和尾注都是对文本的补充说明.脚注一般是附在书页最左下端的注文,用以解释.说明特定内容:而尾注则是位于文档末尾,用于列出引文的出处.脚注和尾注都可以是针对某些文字或者段落来添加.本文中, ...