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 solve(){
    a[n+1]=-1;
    for(int i=1;i<=n+1;i++){
        if(top==0||a[stk[top-1]]<a[i]){
            l[i]=i;
            stk[top++]=i;
        }
        else if(a[stk[top-1]]==a[i]) continue;
        else{
            while(top>0&&a[stk[top-1]]>a[i]){
                top--;
                int len=i-l[stk[top]];
                ans[len]=max(ans[len],a[stk[top]]);
            }
            if(top==0) l[i]=1;
            else l[i]=l[stk[top]];
            stk[top++]=i;
        }
    }
    for(int i=n-1;i>=1;i--){
        ans[i]=max(ans[i],ans[i+1]);
    }
    for(int i=1;i<=n;i++){
        cout<<ans[i]<<‘ ‘;
    }
    puts("");
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    solve();
    return 0;
}

每次进来一个比栈顶元素大的元素,就把他压入栈中,并把他能覆盖的左边界设置为他的下标位置。 如果遇到一个比栈顶小的,说明前面有一些元素不能覆盖当前元素了,这样就可以得到前面元素覆盖的最远右边界了。(其实也可以用其他数据结构实现,但是单调栈效率更高)

时间: 2024-08-27 21:01:03

Codeforces 547B. Mike and Feet[单调栈/队列]的相关文章

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

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

Codeforces Round #305 (Div. 2)D---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 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)

CodeForces 548D 单调栈

Mike and Feet Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 548D Appoint description: Description Mike is the president of country What-The-Fatherland. There are n bears living in this

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

【NOIP数据结构专项】单调队列单调栈

[洛谷P1901 ]发射站 http://www.luogu.org/problem/show?pid=1901 题目描述 某地有 N 个能量发射站排成一行,每个发射站 i 都有不相同的高度 Hi,并能向两边(当 然两端的只能向一边)同时发射能量值为 Vi 的能量,并且发出的能量只被两边最近的且比 它高的发射站接收. 显然,每个发射站发来的能量有可能被 0 或 1 或 2 个其他发射站所接受,特别是为了安 全,每个发射站接收到的能量总和是我们很关心的问题.由于数据很多,现只需要你帮忙计 算出接收

Educational Codeforces Round 23 D. Imbalanced Array(单调栈)

题目链接:Educational Codeforces Round 23 D. Imbalanced Array 题意: 给你n个数,定义一个区间的不平衡因子为该区间最大值-最小值. 然后问你这n个数所有的区间的不平衡因子和 题解: 对每一个数算贡献,a[i]的贡献为 当a[i]为最大值时的 a[i]*(i-l+1)*(r-i+1) - 当a[i]为最小值时的a[i]*(i-l+1)*(r-i+1). 计算a[i]的l和r时,用单调栈维护.具体看代码,模拟一下就知道了. 然后把所有的贡献加起来.

小结:单调栈 &amp; 单调队列

概要: 对于维护信息具有单调性的性质或者问题可以转化为具有单调性质的模型的题,我们可以考虑用单调栈或单调队列. 技巧及注意: 技巧很多,只要能将问题转化为单调性问题,就好解决了. 当维护固定长度的单调区间,我们考虑用单调队列,如[BZOJ]3314: [Usaco2013 Nov]Crowded Cows(单调队列) 单调栈维护长度时要进行及时更新,例如:[BZOJ]3039: 玉蟾宫(DP/单调栈) 假设完美状态后再进行减法原理,例如:[BZOJ]1628 && 1683: [Usaco