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

B. Mike and Feet

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/547/problem/B

Description

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.

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

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 strength of 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 Input

10
1 2 3 4 5 4 3 2 1 6

Sample Output

6 4 4 3 3 2 2 1 1 1 

 

HINT

 

题意

给你一个堆数,对于(1,n)长度,让你找到线段的最小值的最大值是多少

 

题解:

用一个类似单调栈的思想,处理以这个点为最小值可以往左右延伸多少,然后乱搞一下就好了

代码:

 

//qscqesze

#include <cstdio>

#include <cmath>

#include <cstring>

#include <ctime>

#include <iostream>

#include <algorithm>

#include <set>

#include <vector>

#include <sstream>

#include <queue>

#include <typeinfo>

#include <fstream>

#include <map>

#include <stack>

typedef long long ll;

using namespace std;

//freopen("D.in","r",stdin);

//freopen("D.out","w",stdout);

#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)

#define test freopen("test.txt","r",stdin)  

#define maxn 200001

#define mod 1000000007

#define eps 1e-9

int Num;

char CH[20];

//const int inf=0x7fffffff;   //нчоч╢С

const int inf=0x3f3f3f3f;

inline ll read()

{

    ll x=0,f=1;char ch=getchar();

    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}

    return x*f;

}

inline void P(int x)

{

    Num=0;if(!x){putchar('0');puts("");return;}

    while(x>0)CH[++Num]=x%10,x/=10;

    while(Num)putchar(CH[Num--]+48);

    puts("");

}

//**************************************************************************************



int a[maxn];

int dp[maxn];

int l[maxn];

int r[maxn];

int main()

{

    //test;

    int n=read();

    for(int i=1;i<=n;i++)   

        a[i]=read();

    a[0]=-1,a[n+1]=-1;

    

    for(int i=1;i<=n;i++)

    {

        int j=i-1;

        while(a[j]>=a[i])j=l[j];

        l[i]=j;

    }

    for(int i=n;i>=1;i--)

    {

        int j=i+1;

        while(a[j]>=a[i])j=r[j];

        r[i]=j;

    }

    for(int i=1;i<=n;i++)

    {

        int len=r[i]-l[i]-1;

        dp[len]=max(dp[len],a[i]);

    }

    for(int i=n-1;i>=1;i--)

        dp[i]=max(dp[i+1],dp[i]);

    for(int i=1;i<=n;i++)

        cout<<dp[i]<<" ";

}

 

 

 

10
1 2 3 4 5 4 3 2 1 6

你可能感兴趣的:(codeforces)