题目
题目链接:http://codeforces.com/problemset/problem/548/D
题目来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105944#problem/M
简要题意:对于同一长度所有的子段,求出子段最小元素的最大值。
题解
使用单调栈或者递推求出某位置左边第一个比它小的和右边第一比它小的。
于是可以知道以当前位置为最小值的最大长度然后进行更新,取前缀的最大值。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 2E5+5;
int a[N];
int l[N];
int r[N];
int res[N];
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a+i);
l[i] = r[i] = i;
}
for (int i = 1; i <= n; i++) {
while (a[i] <= a[l[i]-1]) {
l[i] = l[l[i]-1];
}
}
for (int i = n; i > 0; i--) {
while (a[i] <= a[r[i]+1]) {
r[i] = r[r[i]+1];
}
}
for (int i = 1; i <= n; i++) {
int len = r[i] - l[i] + 1;
res[len] = max(res[len], a[i]);
}
for (int i = n-1; i > 0; i--) {
res[i] = max(res[i], res[i+1]);
}
for (int i = 1; i <= n; i++) {
printf("%d%c", res[i], i==n ? '\n' : ' ');
}
return 0;
}