codeforces 2016-2017 TW Wrold Final Contest J. Zero Game

J. Zero Game
time limit per test1 second
memory limit per test512 mebibytes
inputstandard input
outputstandard output
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


【分析】

单调队列,占坑待填。


【代码】

//codeforces 2016-2017 Wrold Final Contest J. Zero Game
#include<bits/stdc++.h>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=1000005;
char s[mxn];
int n,m,h,t,K,ans;
int q[mxn],a[mxn],f[mxn];
int main()
{
    int i,j,Q;
    scanf("%s",s+1);
    n=strlen(s+1);
    fo(i,1,n) a[i]+=a[i-1]+s[i]-'0';
    fo(i,1,n) f[i]=i-a[i]-a[i];
    scanf("%d",&Q);
    while(Q--)
    {
        scanf("%d",&K);
        h=1,t=0,ans=-n;
        for(i=0,j=0;i<=n;i++)
        {
            while(j<=n && a[j]-a[i]<=K)
            {
                while(h<=t && f[j]>=f[q[t]]) t--;
                q[++t]=j++;
            }
            while(h<=t && q[h]<=i) h++;
            if(h<=t) ans=max(ans,f[q[h]]-f[i]);
        }
        ans+=K,ans=min(ans,n-a[n]);
        printf("%d\n",max(ans,0));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值