zoukankan      html  css  js  c++  java
  • 2016-2017 National Taiwan University World Final Team Selection Contest J

    题目:

    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

    思路:
      对于每个区间[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-6;
    10 const double pi=acos(-1.0);
    11 const int K=1e6+7;
    12 const int mod=1e9+7;
    13 
    14 int n,m,k,sum[K],q[K];
    15 char ss[K];
    16 int main(void)
    17 {
    18     scanf("%s",ss+1);
    19     n=strlen(ss+1);
    20     for(int i=1;i<=n;i++)   sum[i]=sum[i-1]+(ss[i]=='1'?1:0);
    21     scanf("%d",&m);
    22     while(m--)
    23     {
    24         int k,ans=0,st=0,se=0;
    25         scanf("%d",&k);
    26         for(int i=1,j=1;i<=n;i++)
    27         {
    28             while(j<=n&&sum[j]-k<=sum[i-1])
    29             {
    30                 while(st>se&&q[st]-2*sum[q[st]]<=j-2*sum[j]) st--;
    31                 q[++st]=j++;
    32             }
    33             while(st>se&&q[se+1]<i) se++;
    34             ans=max(ans,2*sum[i-1]-i+q[se+1]-2*sum[q[se+1]]+k+1);
    35         }
    36         ans=max(0,ans);
    37         ans=min(ans,n-sum[n]);
    38         printf("%d
    ",ans);
    39     }
    40     return 0;
    41 }
    
    
  • 相关阅读:
    04 16 团队竞技(第二场) 赛后总结
    HDU 1863 畅通工程 克鲁斯卡尔算法
    HUD 2544 最短路 迪杰斯特拉算法
    hdoj 4526 威威猫系列故事——拼车记
    HDU 3336 Count the string 查找匹配字符串
    Linux command line exercises for NGS data processing
    肿瘤基因组学数据库终结者:cBioPortal---转载
    lncRNA研究利器之"TANRIC"
    Python的collections模块中的OrderedDict有序字典
    python set
  • 原文地址:https://www.cnblogs.com/weeping/p/7301661.html
Copyright © 2011-2022 走看看