题目链接:http://codeforces.com/problemset/problem/548/D
给你n个数,对于(1,n)长度,让你找到线段的最小值的最大值是多少
#include<iostream> #include<cstring> #include<cstdio> #include<queue> #include<algorithm> #define N 200050 #define INF 1e16 #define met(a, b) memset(a, b, sizeof(a)) using namespace std; typedef long long LL; int a[N], ans[N], n, L[N], R[N]; int main() { while(scanf("%d", &n)!=EOF) { a[0]=-1, a[n+1]=-1; for(int i=1; i<=n; i++) scanf("%d", &a[i]); for(int i=1; i<=n; i++)///以a[i]为最小值的左边界; { int j = i-1; while(a[j]>=a[i])j=L[j]; L[i] = j; } for(int i=n; i>=1; i--)///以a[i]为最小值的右边界; { int j=i+1; while(a[j]>=a[i])j=R[j]; R[i] = j; } for(int i=1; i<=n; i++) { int len=R[i]-L[i]-1; ans[len]=max(ans[len], a[i]);///ans[len]表示长度为len的最小值,和当前的a[i]取最小值; } for(int i=n-1; i>=1; i--)///更新未更新的值; ans[i]=max(ans[i], ans[i+1]); for(int i=1; i<=n; i++) printf("%d%c", ans[i], i==n?' ':' '); } return 0; }