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 is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strengthof
a group is the minimum height of the bear in that group.

Mike is a curious to know for each x such that 1?≤?x?≤?n the
maximum strength among all groups of size x.

Input

The first line of input contains integer n (1?≤?n?≤?2?×?105),
the number of bears.

The second line contains n integers separated by space, a1,?a2,?...,?an (1?≤?ai?≤?109),
heights of bears.

Output

Print n integers in one line. For each x from 1 to n,
print the maximum strength among all groups of size x.

Sample test(s)

input

10
1 2 3 4 5 4 3 2 1 6

output

6 4 4 3 3 2 2 1 1 1 

题意:
给出n个数,这n个数在区间长度为i(1~n)的时候能够切割成一些区间。这每一个区间都会有一个最小值。在相同长度的这些区间的最小值中,输出最大值

思路:
使用单调栈,保持栈内的数的单调递增性

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 200005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)

/*
num栈顶的元素,width宽度,跟如今入栈的数之间相隔了多少个数,由于要求的是连续区间。所以这个也是必须记录的
*/
struct node
{
    int num,width;
    node() {};
    node(int _num,int _width):num(_num),width(_width) {}
};

stack<node> S;
int a[N],ans[N];

int main()
{
    int n,i,j;
    scanf("%d",&n);
    for(i = 0; i<n; i++)
        scanf("%d",&a[i]);
    a[n++] = 0;
    MEM(ans,0);
    for(i = 0; i<n; i++)
    {
        int len = 0;//连续区间的长度
        node k;
        while(!S.empty())
        {
            k = S.top();
            if(k.num<a[i])
                break;
                //新入栈的元素比栈顶元素要小,那么对于这个连续区间而言,这个比新入栈的元素就没实用了,能够出栈
            int ls=k.width+len;//出栈的同一时候获得其长度
            if(k.num>ans[ls])//ans记录ls区间的时候的最大值
            {
                ans[ls]=k.num;
            }
            len+=k.width;
            S.pop();
        }
        S.push(node(a[i],len+1));
    }
    for(i = n-1; i>=1; i--)//由于上面仅仅更新了一部分的点,所以如今要对那些没有更新的点也更新
        ans[i]=max(ans[i],ans[i+1]);
    printf("%d",ans[1]);
    for(i = 2; i<n; i++)
        printf(" %d",ans[i]);
    printf("\n");

    return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-12-23 23:25:07

Codeforces548D:Mike and Feet(单调栈)的相关文章

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

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

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

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

(单调栈)poj-2559 Largest Rectangle in a Histogram

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the

【单调栈】hdu1506 Largest Rectangle in a Histogram

单调栈的介绍及一些基本性质 http://blog.csdn.net/liujian20150808/article/details/50752861 依次把矩形塞进单调栈,保持其单增,矩形中的元素是一个三元组,存储其位置,高度,以及以其为高度的情况下,大矩形的左边界最多扩展到哪里. 每次将新的元素塞进栈的时候,其左边界就是其左侧第一个小于它的矩形的位置+1. 然后,每个矩形出栈的时候,记录其右边界为当前往栈里面塞的矩形的位置-1,然后更新答案即可. 注意最后把所有的矩形出栈,更新答案. #in

BZOJ 3238 AHOI 2013 差异 后缀数组+单调栈

题目大意: 思路:一看各种后缀那就是后缀数组没跑了. 求出sa,height之后就可以乱搞了.对于height数组中的一个值,height[i]来说,这个值能够作为lcp值的作用域只在左边第一个比他小的位置到右边第一个比他小的位置.这个东西很明显可以倍增RMQ+二分/单调栈. 之后就是数学题了 Σlen[Ti] + len[Tj] = (len + 1) * len * (len - 1),之后吧所有求出来的Σ2 * lcp(Ti,Tj)减掉就是答案. 记得答案开long long CODE:

51nod 1215 数组的宽度&amp;poj 2796 Feel Good(单调栈)

单调栈求每个数在哪些区间是最值的经典操作. 把数一个一个丢进单调栈,弹出的时候[st[top-1]+1,i-1]这段区间就是弹出的数为最值的区间. poj2796 弹出的时候更新答案即可 #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> #include<cmath