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;
}
时间: 2024-12-29 09:36:06

cf 547B. Mike and Feet dp的相关文章

Codeforces 547B. Mike and Feet[单调栈/队列]

这道题用单调递增的单调栈维护每个数能够覆盖的最大区间即可. 对于   1 2 3 4 5 4 3 2 1 6 这组样例, 1能够覆盖的最大区间是10,2能够覆盖的最大区间是7,以此类推,我们可以使用单调栈来实现这种操作. 具体看代码: *Code: #include<bits/stdc++.h> using namespace std; int n,l[200005],r[200005],ans[200005],a[200005]; int stk[200005],top=0; void so

Codeforces 547B Mike and Feet(单调栈)

题目链接:http://codeforces.com/problemset/problem/547/B 题目大意:有一个长度为n的序列,序列有长度为1...n的连续子序列,一个连续子序列里面最小的值称作这个子序列的子序列的strength,要求出每种长度的连续子序列的最大的strength.解题思路:可以用栈求出每个点的l[i],表示值小于当前位置并且在左侧的最接近这个点的位置.同理可以求出r[i],表示值小于当前位置并且在右侧侧的最接近这个点的位置.求l[i]过程如下:stack s // i

set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

题目传送门 1 /* 2 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 3 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 4 查找左右相邻的位置,更新长度为r - l - 1的最大值,感觉线段树结构体封装不错! 5 详细解释:http://blog.csdn.net/u010660276/article/details/46045777 6 其实还有其他解法,先掌握这种:) 7 */ 8 #include <cstd

Codeforces Round #305 (Div. 1) B. Mike and Feet

Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high. A group of bears

Codeforces548D:Mike and Feet(单调栈)

Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high. A group of bears

CF 463D Gargari and Permutations [dp]

给出一个长为n的数列的k个排列(1?≤?n?≤?1000; 2?≤?k?≤?5).求这个k个数列的最长公共子序列的长度 dp[i]=max{dp[j]+1,where j<i 且j,i相应的字符在k个排列中都保持同样的相对位置} #include <iostream> #include <vector> #include <cstring> #include <cstdio> #include <cmath> #include <al

CF 548B Mike and Fun

Descripe Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in an n × m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number i by (i, j). Mik

CF 486D vailid set 树形DP

As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are given an integer d and a tree consisting of n nodes. Each node i has a value ai associated with it. We call a set S of tree nodes valid if following con

cf Beautiful numbers(数位dp)

Beautiful numbers Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 55D Description Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number i