题意:给出n个数,假设区间长度为k,给定一个起点求区间最小值,由于有很多起点,所以要求的是这些的最大值。然后k的范围是1-n。。(感觉好别扭)。所以要输出n个答案。
做法:我们都知道单调栈可以处理出一个数为最小值的最长区间,那么先处理出这个东西。再把数字带着他的最长区间降序排个序。设指针p为1,我们可以发觉对于区间长度从小到大来说答案是不增的,所以如果当前数字最长区间为x,那么我们只需要更新p到x为这个数。因为小于p的区间长度已经是当前数字之前数字为答案了(比当前大)。
AC代码:
#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll __int64
#define ull unsigned long long
#define eps 1e-8
#define NMAX 10000000
#define MOD 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
#define mp make_pair
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = 0;
ret=0;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c == '-')
{
flag = 1;
c = getchar();
}
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
if(flag) ret = -ret;
}
const int maxn = 200000+10;
int st[maxn][2],a[maxn];
struct node
{
int val,ran;
bool operator < (const node &t) const
{
return val > t.val;
}
}no[maxn];
int l[maxn],r[maxn];
int ans[maxn];
int main()
{
#ifdef GLQ
freopen("input.txt","r",stdin);
// freopen("o.txt","w",stdout);
#endif
int n;
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
no[i].val = a[i];
}
int top = 0;
st[0][0] = st[0][1] = 0;
for(int i = 1; i <= n; i++)
{
while(a[i] <= st[top][0]) top--;
l[i] = st[top][1]+1;
st[++top][0] = a[i];
st[top][1] = i;
}
top = 0;
st[0][0] = 0;
st[0][1] = n+1;
for(int i = n; i >= 1; i--)
{
while(a[i] <= st[top][0]) top--;
r[i] = st[top][1]-1;
st[++top][0] = a[i];
st[top][1] = i;
}
for(int i = 1; i <= n; i++)
{
no[i].ran = r[i]-l[i]+1;
}
sort(no+1,no+1+n);
int p = 1;
for(int i = 1; i <= n; i++)
{
if(no[i].ran < p) continue;
while(p <= no[i].ran)
ans[p++] = no[i].val;
}
for(int i = 1; i <= n; i++)
printf("%d%c",ans[i],(i==n)?'\n':' ');
return 0;
}