Mike is the president of country What-the-fatherland. There isNBears living in this country besides Mike. All of them is standing in a line and they is numbered from1ToNFrom 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 "the Bear" that group.
Mike is a curious to know for each x such this 1?≤? x? ≤? n The maximum strength among all groups of size X.
Input
The first line of input contains integer n (1?≤? N? ≤?2?x?105), the number of bears.
The second line containsNIntegers separated by space, a1,? a 2,?...,? a N (1?≤? a i? ≤?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
101 2 3 4 5 4 3 2 1 6
Output
6 4 4 3 3 2 2 1 1 1
This problem can be done with a monotone stack, maintaining a stack, recording the minmum (the minimum value of the interval) and count (the total length of the interval).
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include < algorithm>using namespace std; #define MAXN 200060int 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--;}
/* The calculated ans[i] is the minimum value of the interval of continuous length I, but this minimum is the maximum of the interval length of all consecutive lengths I, if ANS[I+1] is larger than ans[i], then ans[i] can be updated to ans[i+1] because if i+ The maximum value of the minimum value of 1 consecutive number intervals is B, so the maximum value of the continuous number interval of length I can be achieved by removing one number. */
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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces Round #305 (Div. 1) B. Mike and Feet