题目:

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
  1. 0000110000111110
    5
    1
    2
    3
    4
    5
Output
  1. 5
    8
    9
    9
    9
  2.  
  3. 思路:
      对于每个区间[l,r],如果sum[r]-sum[l-1]<=k,则说明可以区间内所有1踢掉,然后还可以加入k-(sum[r]-sum[l1])个0进来。
      所以合法区间的贡献为(2*sum[l-1]-l)+(r-2*sum[r])+k+1.
      将判断合法的等式变形,可以得到:sum[r]-k<=sum[l-1]
      可以看出合法的r单调,所以可以枚举l,用单调队列维护答案。
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define MP make_pair
  6. #define PB push_back
  7. typedef long long LL;
  8. typedef pair<int,int> PII;
  9. const double eps=1e-;
  10. const double pi=acos(-1.0);
  11. const int K=1e6+;
  12. const int mod=1e9+;
  13.  
  14. int n,m,k,sum[K],q[K];
  15. char ss[K];
  16. int main(void)
  17. {
  18. scanf("%s",ss+);
  19. n=strlen(ss+);
  20. for(int i=;i<=n;i++) sum[i]=sum[i-]+(ss[i]==''?:);
  21. scanf("%d",&m);
  22. while(m--)
  23. {
  24. int k,ans=,st=,se=;
  25. scanf("%d",&k);
  26. for(int i=,j=;i<=n;i++)
  27. {
  28. while(j<=n&&sum[j]-k<=sum[i-])
  29. {
  30. while(st>se&&q[st]-*sum[q[st]]<=j-*sum[j]) st--;
  31. q[++st]=j++;
  32. }
  33. while(st>se&&q[se+]<i) se++;
  34. ans=max(ans,*sum[i-]-i+q[se+]-*sum[q[se+]]+k+);
  35. }
  36. ans=max(,ans);
  37. ans=min(ans,n-sum[n]);
  38. printf("%d\n",ans);
  39. }
  40. return ;
  41. }
  1.  

2016-2017 National Taiwan University World Final Team Selection Contest J - Zero Game的更多相关文章

  1. 2016-2017 National Taiwan University World Final Team Selection Contest

    A. Hacker Cups and Balls 二分答案,将$\geq mid$的数看成$1$,$<mid$的数看成$0$,用线段树进行区间排序检查即可.时间复杂度$O(n\log^2n)$. ...

  2. 2016-2017 National Taiwan University World Final Team Selection Contest (Codeforces Gym) 部分题解

      D 考虑每个点被删除时其他点对它的贡献,然后发现要求出距离为1~k的点对有多少个. 树分治+FFT.分治时把所有点放一起做一遍FFT,然后减去把每棵子树单独做FFT求出来的值. 复杂度$nlog^ ...

  3. 2016-2017 National Taiwan University World Final Team Selection Contest C - Crazy Dreamoon

    题目:Statements Dreamoon likes algorithm competitions very much. But when he feels crazy because he ca ...

  4. 2016-2017 National Taiwan University World Final Team Selection Contest A - Hacker Cups and Balls

    题目: Dreamoon likes algorithm competitions very much. But when he feels crazy because he cannot figur ...

  5. sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)

    The Android University ACM Team Selection Contest Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里 ...

  6. 【转】2016/2017 Web 开发者路线图

    链接:知乎 [点击查看大图] 原图来自LearnCodeAcademy最火的视频,learncode是YouTube上最火的Web开发教学频道,介绍包括HTML/CSS/JavaScript/Subl ...

  7. luogu 1327 数列排序 & 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J题 循环节

    luogu 1327 数列排序 题意 给定一个数列\(\{an\}\),这个数列满足\(ai≠aj(i≠j)\),现在要求你把这个数列从小到大排序,每次允许你交换其中任意一对数,请问最少需要几次交换? ...

  8. Moscow Pre-Finals Workshop 2016. National Taiwan U Selection

    A. As Easy As Possible 每个点往右贪心找最近的点,可以得到一棵树,然后倍增查询即可. 时间复杂度$O((n+m)\log n)$. #include <bits/stdc+ ...

  9. Mindjet MindManager 2016/2017 折腾记录

    https://community.mindjet.com/mindjet/topics/ensure-2017-64-bit-version-installation Mindmanager sho ...

随机推荐

  1. 剑指 offer set 24 扑克牌的顺子

    题目 从扑克牌中任意抽取出 5 张牌, 判断是不是顺子, 并且大小王可以看成任意的数字 思路 1. 把大小王当做 0 插入到数组中, 然后对数组排序 2. 统计相邻两个数之间的空隙数, 若空隙数大于 ...

  2. shell脚本学习总结12--系统信号

    信号是Linux系统中一种进程通信机制.我们可以使用特定的信号来中断进程.每一种信号都同一个整数值相关联. kill命令可用来想进程发送信号,而trap命令用来处理所接收的信号. kill 列出所有可 ...

  3. Java连接MongoDB样例

    package com.moonlit.example; import com.mongodb.BasicDBObject; import com.mongodb.BulkWriteOperation ...

  4. 模拟http请求 带 chunked解析办法二

    以PHP代码为例 //这个是解析chuned块 get_chunk_data($fsock) { $data = ''; while(true) { $len = hexdec(fgets($fsoc ...

  5. 合并图片、EUI顺序、CacheAsBitmap对drawcall影响的测试

    一 什么是DrawCall Draw Call 理解和优化: http://blog.csdn.net/sakyaer/article/details/44459881 draw call是openG ...

  6. jquery如何获取type=hidden的input元素的值?

    function setHiddenFields() { var hiddens = $("input:hidden"); $.each(hiddens, function (in ...

  7. Block Formatting Contexts (块级格式化上下文) 使用参考

    转自:http://kayosite.com/block-formatting-contexts-in-detail.html 在上文<详说清除浮动>中,Kayo 较为详细地介绍了 BFC ...

  8. 巨蟒python全栈开发flask13项目开始5

    1.Toy回复App消息 2.离线维度消息数量存储 3.Toy批量收取消息 4.Toy主动发起消息&&AI对接 5.Toy_info 1.Toy回复App消息 2.离线维度消息数量存储 ...

  9. python console

    print(sys.stdout.encoding, locale.getpreferredencoding ()) windows console : chcp 65001; 在设置了这个环境变量时 ...

  10. Storm-源码分析- timer (backtype.storm.timer)

    mk-timer timer是基于PriorityQueue实现的(和PriorityBlockingQueue区别, 在于没有阻塞机制, 不是线程安全的), 优先级队列是堆数据结构的典型应用 默认情 ...