Codeforces Round #305 (Div. 2)D. Mike and Feet(单调栈)

题意

n个值代表n个熊的高度 对于size为x的group strength值为这个group(连续的几个熊)中熊的最小的height值

对于x(1<=x<=n) 求出最大的strength值

http://codeforces.com/contest/548/problem/D

思路

我们把每个数作为最小值能最远向左和右用单调栈处理出来,那么可以发现对于x长度的所有group,某个数延伸的区间长度如果大于等于x,则这个数对答案有贡献。

对于样例,我们处理出来后可以观察发现确有此规律:

然后就可以搞啦~

代码

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
int g[N],L[N],R[N],a[N],h[N],mx[N];
int main()
{
    std::ios::sync_with_stdio(false);
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%d",&a[i]);
    a[n+1]=-1;
    stack<int> st;
    for(int i=1; i<=n; i++)
    {
        while(!st.empty()&&a[i]<=a[st.top()])
        {
            st.pop();
        }
        if(st.empty())
        {
            L[i]=1;
        }
        else
        {
            L[i]=st.top()+1;
        }
        st.push(i);
    }
    while(!st.empty()) st.pop();
    for(int i=n; i>=1; i--)
    {
        while(!st.empty()&&a[i]<=a[st.top()])
            st.pop();
        if(st.empty())
        {
            R[i]=n;
        }
        else
            R[i]=st.top()-1;
        st.push(i);
    }
    /*  for(int i=1; i<=n; i++)
      {
          cout<<L[i]<<" "<<R[i]<<" "<<endl;
      }
      cout<<endl;*/
    for(int i=1; i<=n; i++)
        h[R[i]-L[i]+1]=max(h[R[i]-L[i]+1],a[i]);
    for(int i=n;i>=1;i--)
        mx[i]=max(mx[i+1],h[i]);
    for(int i=1;i<=n;i++)
    {
        printf("%d ",mx[i]);
    }
    puts("");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/mcq1999/p/11592605.html