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;}
- codeforces 2016-2017 TW Wrold Final Contest J. Zero Game
- codeforces 2016-2017 NTUWFTSC J Zero Game
- Codeforces 730 J. Bottles DP 0-1背包- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest
- The 11th UESTC Programming Contest Final J题
- [SCU4445] Right turn [2015 Sichuan Province Contest Final J]
- Codeforces Gym 100814J Game 简单博弈
- Codeforces 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest J题(BFS)
- CodeForces 589J ——2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest J Bottles
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest J dp
- 2017 ACM Amman Collegiate Programming Contest J
- [费用流] ACM-ICPC Asia China-Final Contest J. Mr.Panda and TubeMaster
- codeforces April Fools Day Contest 2014 A. The Great Game
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest J.Bottles(背包,好题)
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest J Bottle动态规划
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest - J. Bottles(DP)
- 2016-2017 ACM-ICPC, NEERC, Central Subregional Contest J.Architect of Your Own Fortune
- codeforces 2016-2017 NTUWFTSC I Tree Game
- 二叉堆
- 游戏UI框架设计(一) : 架构设计理论篇
- 职场必杀技之横向领导力
- Spring (六) 使用Spring 内置的JdbcTemplate操作数据库
- 游戏UI框架设计(二) : 最简版本设计
- codeforces 2016-2017 TW Wrold Final Contest J. Zero Game
- 关于CSS的规范
- 【codevs1732】Fibonacci数列 2(矩阵快速幂)
- 【计算机网络】TCP协议详解
- Python中的函数(三)
- 19纯虚函数和抽象类
- Ubuntu下使用Pylon相机
- iOS 视频录制 包括预览图片与视频还有写入自己自定义的App相册 微信小视频
- R-CNN,SPP-NET, Fast-R-CNN,Faster-R-CNN, YOLO,系列深度学习检测方法