# 【BZOJ4149】[AMPPZ2014]Global Warming

## Sample Input

10
8 3 2 5 2 3 4 6 3 6

6 4

## HINT

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=500010;
int n,m,top,ans,pos;
int v[maxn],mn[20][maxn],Log[maxn],lm[maxn],rm[maxn],ln[maxn],rn[maxn],st[maxn];
vector<int> p[maxn];
struct number
{
int val,org;
}num[maxn];
bool cmp(const number &a,const number &b)
{
return a.val<b.val;
}
inline int getmn(int a,int b)
{
int k=Log[b-a+1];
return min(mn[k][a],mn[k][b-(1<<k)+1]);
}
inline void updata(int a,int b)
{
if(a>ans||(a==ans&&b<pos))	ans=a,pos=b;
}
inline int rd()
{
int ret=0,f=1;	char gc=getchar();
while(gc<'0'||gc>'9')	{if(gc=='-')	f=-f;	gc=getchar();}
while(gc>='0'&&gc<='9')	ret=ret*10+(gc^'0'),gc=getchar();
return ret*f;
}
int main()
{
n=rd();
int i,j,a,b;
for(i=1;i<=n;i++)	num[i].val=rd(),num[i].org=i;
sort(num+1,num+n+1,cmp);
for(i=1;i<=n;i++)
{
if(i==1||num[i].val>num[i-1].val)	m++;
v[num[i].org]=m;
}
for(i=2;i<=n;i++)	Log[i]=Log[i>>1]+1;
for(i=1;i<=n;i++)	p[v[i]].push_back(i),mn[0][i]=v[i];
for(j=1;(1<<j)<=n;j++)	for(i=1;i+(1<<j)-1<=n;i++)	mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1<<(j-1))]);
for(st[top=0]=0,i=1;i<=n;i++)
{
while(top&&v[st[top]]<v[i])	top--;
lm[i]=st[top]+1,st[++top]=i;
}
for(st[top=0]=0,i=1;i<=n;i++)
{
while(top&&v[st[top]]>v[i])	top--;
ln[i]=st[top]+1,st[++top]=i;
}
for(st[top=0]=n+1,i=n;i>=1;i--)
{
while(top&&v[st[top]]<v[i])	top--;
rm[i]=st[top]-1,st[++top]=i;
}
for(st[top=0]=n+1,i=n;i>=1;i--)
{
while(top&&v[st[top]]>v[i])	top--;
rn[i]=st[top]-1,st[++top]=i;
}
for(i=1;i<=n;i++)
{
j=getmn(lm[i],rm[i]);
b=lower_bound(p[j].begin(),p[j].end(),i)-p[j].begin(),a=b-1;
if(a>=0&&rn[p[j][a]]>=i)	updata(min(rm[i],rn[p[j][a]])-max(lm[i],ln[p[j][a]])+1,max(lm[i],ln[p[j][a]]));
if(b<(int)p[j].size()&&ln[p[j][b]]<=i)	updata(min(rm[i],rn[p[j][b]])-max(lm[i],ln[p[j][b]])+1,max(lm[i],ln[p[j][b]]));
}
printf("%d %d\n",ans,pos);
return 0;
}
posted @ 2017-11-26 11:20  CQzhangyu  阅读(240)  评论(0编辑  收藏