cf 547B. Mike and Feet dp

题意:

n个矩阵排成一排,n<=2e5,高度分别为hei[i],宽度为1

对于一些连续的矩阵,矩阵的size为矩阵的个数,矩阵的strength为这些矩阵中高度最低的那一个高度

求:for each x such that 1 ≤ x ≤ n the maximum strength among all groups of size x.

 

对于每一个矩阵,我们先求出这个矩阵的l,r

l表示这个矩阵左边最靠近它的小于它的矩阵的下标

r表示这个矩阵右边最靠近它的小于它的矩阵的下标

即在区间(l,r)内,strength 等于这个矩阵的高度

注意:不包括l,r这2个矩阵

 

求l,r的值可以用dp

 

求完l,r后只需要把这些矩阵按照高度小到大sort一遍,

然后遍历一遍,不断更新ans数组即可

 

 

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

const int MAXN = 200000+5;

int ans[MAXN];
struct Node
{
    int hei,l,r;
};
Node node[MAXN];

bool cmp(Node x,Node y)
{
    return x.hei < y.hei;
}

void solve()
{
    int n;
    scanf("%d",&n);
    n++;
    for(int i=1;i<n;i++){
        scanf("%d",&node[i].hei);
    }

    node[0].hei = 0;
    node[n].hei = 0;

    for(int i=1;i<n;i++){
        node[i].l = i - 1;
        while(i > 0 && node[node[i].l].hei >= node[i].hei){
            node[i].l = node[node[i].l].l;
        }
    }
    for(int i=n-1;i>0;i--){
        node[i].r = i + 1;
        while(i < n && node[node[i].r].hei >= node[i].hei){
            node[i].r = node[node[i].r].r;
        }
    }
    
    memset(ans,-1,sizeof ans);

    sort(node+1,node+n,cmp);

    for(int i=1;i<n;i++){
        int pos = node[i].r - node[i].l - 1;
        ans[pos] = max(ans[pos],node[i].hei);
    }

    for(int i=n-1;i>0;i--){
        ans[i] = max(ans[i],ans[i+1]);
    }

    for(int i=1;i<n-1;i++)
        printf("%d ",ans[i]);
    printf("%d\n",ans[n-1]);

    return ;
}

int main()
{
    solve();
    return 0;
}

 

posted on 2015-12-18 02:02  _fukua  阅读(394)  评论(0编辑  收藏  举报