547B.Mike and Feet
link:http://codeforces.com/contest/548/problem/D
维护一个单调递增的栈, 来记录每个数最大的范围是多少(这样的话要用到map来离散化,但是这样其实是没什么必要的,直接换成i大的范围最大的是多少,这样更快一点)
这代码写的比较挫, 还请见谅.
我其实是故意不写注释的…(逃
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <iostream>
#define LL long long
#define pb push_back
#define lb lower_bound
#define eps 1e-8
#define INF 0x3f3f3f3f
using namespace std;
const int N = 200005;
int a[N], n;
map<int, int> mp;
struct node{
int x;
int l, r;
node(){}
node(int aa, int bb, int cc){x = aa; l = bb; r = cc;}
}stk[N];
int top = 0;
void solve()
{
for(int i=1; i<=n+1; i++){
if(top == 0) stk[top++] = (node){a[i], i, i};
else{
if(a[i] < stk[top-1].x){
int tl, tr = stk[top-1].r;
while(a[i] < stk[top-1].x && top>0){
node t = stk[--top];
if(mp.find(t.x) == mp.end()) mp[t.x] = 0;
mp[t.x] = max(mp[t.x], tr - t.l + 1);
tl = t.l;
}
stk[top++] = (node){a[i], tl, i};
}
else stk[top++] = (node){a[i], i, i};
}
}
map<int, int>::iterator it = mp.end();
it--;
for(int i=1; i<=n; i++)
{
if(i>1) printf(" ");
if(it->second >= i) printf("%d", it->first);
else{
while(it->second < i && it != mp.begin()) it--;
printf("%d", it->first);
}
}
}
void input()
{
scanf("%d", &n);
for(int i=1; i<=n; i++) scanf("%d", a+i);
a[n+1] = 0;
mp.clear();
solve();
}
int main(void)
{
#ifdef DK
freopen("C:\\Users\\dell\\Desktop\\1.in","r",stdin);
#endif // DK
input();
return 0;
}