【数据结构】 CF 547B Mike and Feet

点击打开链接

长度为n的一个数列

定义 一个区间内的 最小的值为 这个区间的strength

求长度为 1-n 的区间 最大的strength

先求出每个位置左边/右边 的比它小的值的位置

这样在 L+1 R-1 区间内这个位置是最小值

然而 区间[len]的值是小于等于区间[len-1]

即可以推出


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 200009;//点数的最大值
const int MAXM = 604000;//边数的最大值
const LL INF = 1<<30;
const LL mod= 1000000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int low[MAXN],hig[MAXN],a[MAXN],ans[MAXN];
int main()
{
    int n;
    cin>>n;
    stack<int>st;
    for(int i=1;i<=n;i++)
    {
        ans[i]=0;
        cin>>a[i];
    }
	while(!st.empty()) st.pop();
    for(int i=1;i<=n;i++)
    {
        while(!st.empty()&&a[st.top()]>=a[i])
            st.pop();
        if(st.empty()) low[i]=0;
        else low[i]=st.top();
        st.push(i);
    }
	while(!st.empty()) st.pop();
    for(int i=n;i>=1;i--)
    {
        while(!st.empty()&&a[st.top()]>=a[i])
            st.pop();
        if(st.empty()) hig[i]=n+1;
        else hig[i]=st.top();
        st.push(i);
    }
    for(int i=1;i<=n;i++)
    {
        int len=hig[i]-low[i]-1;
        ans[len]=max(ans[len],a[i]);
    }
    ans[n+1]=0;
    for(int i=n;i>=0;i--)
        ans[i]=max(ans[i],ans[i+1]);
    for(int i=1;i<=n;i++)
        printf("%d ",ans[i]);
    return 0;
}

相关文章

【啊哈!算法】算法3:最常用的排序——快速排序       ...
匿名组 这里可能用到几个不同的分组构造。通过括号内围绕的正...
选择排序:从数组的起始位置处开始,把第一个元素与数组中其...
public struct Pqitem { public int priority; ...
在编写正则表达式的时候,经常会向要向正则表达式添加数量型...
来自:http://blog.csdn.net/morewindows/article/details/6...