Codeforces Round #305 (Div. 2) 548D - Mike and Feet

题意:给你一个n,有n个数A[i]
要求输出ans[i]
ans[i]就是最大的区间长度为i的区间内的最小值
思路:
这个思路以前做01矩阵的时候遇到过
l[i]就是以i为最小值往左延伸到最远的距离

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
#define maxn 200005
#define f(x) (x*1.0)
#define inf 0x3f3f3f3f
#define maxm maxn*maxn
#define lowbit(x) (x&(-x))
#define cheak(i) printf("%d ",i)
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
const double PAI=acos(-1.0);
int A[maxn],l[maxn],r[maxn],ans[maxn];
int main()
{
    int i,n;
    scanf("%d",&n);
    rfor(i,1,n) scanf("%d",&A[i]);
    A[0]=A[n+1]=-inf;
    rfor(i,1,n)
    {
        int t=i-1;
        while(A[t]>=A[i]) t=l[t];
        l[i]=t;
    }
    lfor(i,n,1)
    {
        int t=i+1;
        while(A[t]>=A[i]) t=r[t];
        r[i]=t;
    }
    rfor(i,1,n)
    {
        int len=r[i]-l[i]-1;
        //printf("%d %d\n",l[i],r[i]);
        ans[len]=max(ans[len],A[i]);
    }
    lfor(i,n-1,1) ans[i]=max(ans[i],ans[i+1]);
    rfor(i,1,n) printf("%d ",ans[i]);
    return 0;
}

你可能感兴趣的:(codeforces)