zoukankan      html  css  js  c++  java
  • bzoj 2086 [Poi2010]Blocks 单调栈

     [Poi2010]Blocks

    Time Limit: 20 Sec  Memory Limit: 259 MB
    Submit: 788  Solved: 356
    [Submit][Status][Discuss]

    Description

    给出N个正整数a[1..N],再给出一个正整数k,现在可以进行如下操作:每次选择一个大于k的正整数a[i],将a[i]减去1,选择a[i-1]或a[i+1]中的一个加上1。经过一定次数的操作后,问最大能够选出多长的一个连续子序列,使得这个子序列的每个数都不小于k。
    总共给出M次询问,每次询问给出的k不同,你需要分别回答。

    Input

    第一行两个正整数N (N <= 1,000,000)和M (M <= 50)。
    第二行N个正整数,第i个正整数表示a[i] (a[i] <= 10^9)。
    第三行M个正整数,第i个正整数表示第i次询问的k (k <= 10^9)。

    Output

    共一行,输出M个正整数,第i个数表示第i次询问的答案。

    Sample Input

    5 6
    1 2 1 1 5
    1 2 3 4 5 6




    Sample Output

    5 5 2 1 1 0

    HINT

     

    题解:

    显然一段的平均数超过K即为合法解

    将a[i]减去K,求其前缀和

    若i>j且sum[i]>=sum[j]则j作为左端点比i更优

    所以只要维护一个sum的单调递减栈,一个指针从n->0,不断弹栈,更新答案

     1 #include<cmath>
     2 #include<queue>
     3 #include<vector>
     4 #include<cstdio>
     5 #include<cstring>
     6 #include<cstdlib>
     7 #include<iostream>
     8 #include<algorithm>
     9 #define pa pair<int,int>
    10 #define inf 1000000000
    11 #define eps 1e-8
    12 #define ll long long
    13 using namespace std;
    14 ll read()
    15 {
    16     ll x=0,f=1;char ch=getchar();
    17     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    18     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    19     return x*f;
    20 }
    21 int n,m,top;
    22 int a[1000005],q[1000005];
    23 ll sum[1000005];
    24 void solve(int x)
    25 {
    26     int ans=0;
    27     for(int i=1;i<=n;i++)
    28         sum[i]=sum[i-1]+a[i]-x;
    29     top=0;
    30     for(int i=1;i<=n;i++)
    31         if(sum[q[top]]>sum[i])q[++top]=i;
    32     for(int i=n,j=top;i>=0;i--)
    33     {
    34         while(j&&sum[i]>=sum[q[j-1]])j--;
    35         ans=max(ans,i-q[j]);
    36     }
    37     printf("%d",ans);
    38 }
    39 int main()
    40 {
    41     n=read();m=read();
    42     for(int i=1;i<=n;i++)
    43         a[i]=read();
    44     while(m--)
    45     {
    46         int x=read();
    47         solve(x);
    48         if(m)printf(" ");
    49         else puts("");
    50     }
    51 }
  • 相关阅读:
    (转+原)python中的浅拷贝和深拷贝
    (原)torch7中添加新的层
    (原+转)ubuntu终端输出彩色文字
    (原)torch中显示nn.Sequential()网络的详细情况
    (原)python中使用plt.show()时显示图像
    eclipse 注释模板
    leetcode 11 最大盛水容器
    leetcode 9 回文数字
    leetcode8 字符串转整数
    利用Intent启动activity的例子
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8847595.html
Copyright © 2011-2022 走看看