CodeForces 548D 单调栈

Mike and Feet

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Appoint description: 

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 548D 单调栈

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

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个数中取出连续的k个(k 从1~n)组成一组,组内的值为这一组的最小值,现在要求所有大小为k的组的最大值。
思路:
单调栈。维护一个单调递增栈(从栈底到栈顶单调递增),存每个点贡献的长度和他的值。
 
比如1 2 3 4 5 4 3 2 1 6
 
第一个1,栈为空,那么把它加入栈中,贡献的长度为1,值为1。 
依次直到第6个数 4,由于栈顶元素比4大,那么弹出栈顶元素,并且计算现在的长度(统计现在长度),即弹出的这些
数字能够贡献的长度,知道栈顶元素比当前的值小,然后将当前的4入栈,它能贡献的长度就是统计的长度。
依次类推,要注意最后栈不一定是空的,所以剩下的元素也要处理。
/*
* Author: sweat123
* Created Time: 2016/7/12 10:39:09
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
int a[MAXN],n,ans[MAXN];
struct node{
int len;
int val;
node(){}
node(int tlen,int tval):len(tlen),val(tval){}
};
stack<node>s;
int main(){
while(~scanf("%d",&n)){
for(int i = ; i <= n; i++){
scanf("%d",&a[i]);
}
while(!s.empty())s.pop();
memset(ans,,sizeof(ans));
for(int i = ; i <= n; i++){
int len = ;//表示当前弹出的这些元素能够贡献多少的长度
while(!s.empty()){
node tp = s.top();
if(tp.val < a[i])break;
s.pop();
int ret = tp.len + len;//这个元素能够贡献的长度 (也就是说这个元素已经统计过了多少长度,在这里也是可以连续的)
ans[ret] = max(ans[ret],tp.val);
len += tp.len;
}
s.push(node(len+,a[i]));
}
int len = ;
while(!s.empty()){
node tp = s.top();
s.pop();
int ret = len + tp.len;
ans[ret] = max(ans[ret],tp.val);
len += tp.len;
}
for(int i = n; i >= ; i--){
ans[i] = max(ans[i],ans[i+]);
}
for(int i = ; i <= n; i++){
printf("%d ",ans[i]);
}
printf("\n");
}
return ;
}
上一篇:Android--Activity(活动)


下一篇:DB2 Enterprise Server Edition(DB2 ESE)9.1在Windows Server 2008 下出现无法新建数据库的情况,及解决办法