codeforces 2016-2017 NTUWFTSC J Zero Game

38 篇文章 0 订阅
26 篇文章 0 订阅

You are given one string S consisting of only '0' and '1'. You are bored, so you start to play with the string. In each operation, you can move any character of this string to some other position in the string. For example, suppose . Then you can move the first zero to the tail, and S will become '0100'.

Additionally, you have Q numbers K1, K2, ..., KQ. For each i, you wonder what can be the maximum number of consecutive zeroes in the string if you start with S and use at most Ki operations. In order to satisfy your curiosity, please write a program which will find the answers for you.

Input

The first line of input contains one string S. The second line of input contains one integer Q. Each of the following Q lines contains one integer Ki indicating the maximum number of operations in i-th query.

  • 2 ≤ N ≤ 106
  • the length of S is exactly N characters
  • S consists of only '0' and '1'
  • 1 ≤ Q ≤ 105
  • N × Q ≤ 2 × 107
  • 1 ≤ Ki ≤ 106
Output

For each query, output one line containing one number: the answer for this query.

Example
input
0000110000111110
5
1
2
3
4
5
output
5
8
9
9
9
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

贪心+单调队列+思路~

我们先求一个前缀和a[i]表示到i为止的1的个数。

枚举区间[l+1,r],首先区间要满足a[r]-a[l]<=k,然后,我们会把区间所有的1都取走,然后用剩余步数尽量多地放0进来,所以答案就是(r-l-(a[r]-a[l]))+(k-(a[r]-a[l]))=(r-2*a[r])-(l-2*a[l])+k,对于每一个位置i我们记录f[i]=i-2*a[i],这样每次用f[r]-f[l]更新答案,最后再+k即可。

这样计算的话,有的时候已经没有0可以挪入区间了,所以我们最后ans=min(ans,n-a[n]),确保不会出现非法情况。

ans有可能<0,所以ans=max(ans,0)。

单调队列的写法很神奇,见代码!


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

int n,m,k,a[1000001],f[1000001],q[1000001],he,ta,ans;
char s[1000001];

int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}

int main()
{
	scanf("%s",s+1);n=strlen(s+1);
	for(int i=1;i<=n;i++) a[i]=a[i-1]+(s[i]=='1');
	for(int i=1;i<=n;i++) f[i]=i-2*a[i];
	m=read();
	while(m--)
	{
		k=read();he=1;ta=0;ans=-1e7;
		for(int i=0,j=0;i<=n;i++)
		{
			while(j<=n && a[j]-a[i]<=k)
			{
				while(he<=ta && f[j]>=f[q[ta]]) ta--;
				q[++ta]=j++;
			}
			while(he<=ta && q[he]<=i) he++;
			if(he<=ta) ans=max(ans,f[q[he]]-f[i]);
		}
		ans+=k;
		ans=max(ans,0);ans=min(ans,n-a[n]);
		printf("%d\n",ans); 
	}
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值