【思维-数列-字典序-贪心】CF-Div3-Round 598——D Binary String Minimizing

前言

一波三折,还好在最后几分钟AC了,超激动qwq...

题目

time limit per test:1 second

memory limit per test:256 megabytes

input:standard input

output:standard output

You are given a binary string of length nn (i. e. a string consisting of nn 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 kk 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 ii and i+1i+1 arbitrary (possibly, zero) number of times. Each such swap is considered a separate move.

You have to answer qq independent test cases.

Input

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

The first line of the test case contains two integers nn and kk (1≤n≤106,1≤k≤n21≤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 nn characters '0' and '1'.

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

Output

For each test case, print the answer on it: the lexicographically minimum possible string of length nn you can obtain from the given one if you can perform no more than kk 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: 

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

题目大意

给你一个长度为n的01串,可以最做k次操作,每次操作把任意两个相邻的数交换

求字典序最小的操作后(可以不进行操作)的01串

分析

因为要求字典序最小且原数列只包含0和1,所以要尽可能地把0往前移

一来就想到暴力:

找到每个0,如果前一个数是1,、还能进行操作(k>=1)并且0还能往前移(不在位置1上),就一直交换

但是没想第二个点就TLE.../太不给面子了QAQ!


认识到暴力交换不行,就考虑换个更优的算法:算出每个0能到达的位置

首先如果不考虑k的限制,那么每个0肯定是尽可能往前移,直到遇到0停下

0就在最前面“堆起来了”:100011101——>000011111

如果加上k的限制,其实变化不大,只是要看“ k能把当前的0尽可能往前移到哪个位置 ”,两种情况:

设 f 为当前0的位置,last 为当前0的前面一个0的位置 

1.k >= f - last - 1(k大于等于0能往前移的最多步数)

2.k< f - last - 1

标记有0的位置(ans[ i ]=1),最后输出时判断该位置是0还是1即可qwq~

一番TLE后,又WA了,原来是k没开long long ,离比赛结束只有几分钟,终于A了,紧张死我了


做题要点总结

1.本题多组数据,初始化不能用memset,不然会超时得很惨...

2.好好看数据范围!记得开long long !原题目范围:

3.多组数据,输出记得换行

TLE代码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1e6;
int a[MAXN+5],id[MAXN+5];
int n,k,cnt;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		//memset(id,0,sizeof(id));
		cnt=0;
		scanf("%d%d",&n,&k);
		for(int i=1;i<=n;i++)
		{
			scanf("%1d",&a[i]);
			if(a[i]==0)
				id[++cnt]=i;
		}
		for(int i=1;i<=cnt;i++)
		{
			int f=id[i];
			while(k>=1&&f>1)//还能操作 
			{
				if(a[f-1])
				{
					swap(a[f-1],a[f]);
					k--,f--;
				}
				else
					break;
			} 
		}
		for(int i=1;i<=n;i++)
			printf("%d",a[i]);
		printf("\n");
	}
	return 0;
}

AC代码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=1e6;
ll a[MAXN+5],id[MAXN+5],ans[MAXN+5];
ll n,k,tot;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		tot=0;
		scanf("%lld%lld",&n,&k);
		for(int i=1;i<=n;i++)
		{
			scanf("%1d",&a[i]);
			if(a[i]==0)
			{
				id[++tot]=i;
				ans[i]=1;//顺便把ans初始化,memset要超时 
			}
			else
				ans[i]=0;
		}
		if(!tot||tot==n)//简单判断特殊情况 
		{
			for(int i=1;i<=n;i++)
				if(ans[i])
					printf("0");
				else
					printf("1");
			printf("\n");
			continue;
		}
		ll last=0;//记录当前0前面一个0的位置 
		for(int i=1;i<=tot;i++)
		{
			ll f=id[i];
			if(k>=f-last-1)//能移到前一个0的后面一位 
			{
				k-=(f-last-1);
				last++;
				ans[f]=0,ans[last]=1;//原位置不再是0,新位置是0 
			}
			else//k<f-last-1
			{
				ans[f]=0,ans[f-k]=1;//往前最多能移k位 
				break;
			}
		}
		for(int i=1;i<=n;i++)
			if(ans[i])
				printf("0");
			else
				printf("1");
		printf("\n");
	}
	return 0;
}

总结

这道题在比赛时做了有点久,主要是一开始没意识到memset会超时

不过最终还是在比赛结束前AC了,开心

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值