码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-13 22:34:37      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:单调栈

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

这题可以用单调栈做,维护一个栈,记录minmum(该区间的最小值)和count(区间的总长度)。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 200060
int ans[maxn];
struct node{
	int count,minmum;
}stack[maxn];
int main()
{
	int n,m,i,j,top,count,b;
	while(scanf("%d",&n)!=EOF)
	{
		memset(ans,0,sizeof(ans));
		top=0;
		for(i=1;i<=n;i++){
			scanf("%d",&b);
			count=0;
			while(top>0 && stack[top].minmum>=b){
				stack[top].count+=count;
				count=stack[top].count;
				if(ans[count]<stack[top].minmum){
					ans[count]=stack[top].minmum;
				}
				top--;
			}
			top++;
			stack[top].minmum=b;
			stack[top].count=count+1;
		}
		count=0;
		while(top>0){
				stack[top].count+=count;
				count=stack[top].count;
				if(ans[count]<stack[top].minmum){
					ans[count]=stack[top].minmum;
				}
				top--;
		}
                /*这里算出来的ans[i]是连续长度为i的区间的最小值,但这个最小值是所有连续长度为i的区间长度的最大值,下面如果ans[i+1]比ans[i]大,那么ans[i]可以更新为ans[i+1],因为如果i+1个连续数区间的最小值的最大值是b,那么去掉一个数,一定可以做到长度为i的连续数区间的最大值是b。*/
		for(i=n;i>=2;i--){
			if(ans[i]>ans[i-1]){
				ans[i-1]=ans[i];
			}
		}
		for(i=1;i<=n-1;i++){
			printf("%d ",ans[i]);
		}
		printf("%d\n",ans[i]);
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:单调栈

原文地址:http://blog.csdn.net/kirito_acmer/article/details/46867203

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!