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

来源:互联网 发布:双十一 淘宝 消费人群 编辑:程序博客网 时间:2024/06/05 09:54

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;}
阅读全文
0 0
原创粉丝点击