poj2452(st表+二分)

区间最小和最大是十分好解决的。。然而2端点直接枚举会T啊。。

所以最多枚举一个端点。。然后其实根据最小值可以二分求出满足最小值为左端的区间。。。

然后在此基础上把区间最大值的位置求出来就可以了。。


求最小最大什么的用st表就很方便。。




/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 200005
#define nm 1000005
#define pi 3.1415926535897931
using namespace std;
const ll inf=998244353;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}



int a[NM],st[NM][32],_st[NM][32],ans,n,mn[NM];

void init(){
	inc(i,2,n)mn[i]=mn[i/2]+1;
	inc(i,1,n)st[i][0]=_st[i][0]=i;
	for(int j=1;succ(j)<=n;j++)
		for(int i=1;i+succ(j)-1<=n;i++)
			st[i][j]=a[st[i][j-1]]>a[st[i+succ(j-1)][j-1]]?st[i][j-1]:st[i+succ(j-1)][j-1],
			_st[i][j]=a[_st[i][j-1]]<a[_st[i+succ(j-1)][j-1]]?_st[i][j-1]:_st[i+succ(j-1)][j-1];
}
int rmq(int x,int y){int k=mn[y-x];return a[st[x][k]]>a[st[y-succ(k)+1][k]]?st[x][k]:st[y-succ(k)+1][k];}
int _rmq(int x,int y){int k=mn[y-x];return a[_st[x][k]]<a[_st[y-succ(k)+1][k]]?_st[x][k]:_st[y-succ(k)+1][k];}

int main(){
	while(~scanf("%d",&n)){
		mem(st);mem(_st);ans=0;mem(mn);
		inc(i,1,n)a[i]=read();
		init();
		inc(i,1,n){
			int s=i;
			for(int l=i,r=n;l<=r;){
				int t=l+r>>1;
				if(a[_rmq(i,t)]<a[i])r=t-1;
				else l=t+1,s=t;
			}
			ans=max(ans,rmq(i,s)-i);
		}
		if(!ans)ans=-1;printf("%d\n",ans);
	}
	return 0;
}




Sticks Problem
Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 10969 Accepted: 2929

Description

Xuanxuan has n sticks of different length. One day, she puts all her sticks in a line, represented by S1, S2, S3, ...Sn. After measuring the length of each stick Sk (1 <= k <= n), she finds that for some sticks Si and Sj (1<= i < j <= n), each stick placed between Si and Sj is longer than Si but shorter than Sj.

Now given the length of S1, S2, S3, …Sn, you are required to find the maximum value j - i.

Input

The input contains multiple test cases. Each case contains two lines.
Line 1: a single integer n (n <= 50000), indicating the number of sticks.
Line 2: n different positive integers (not larger than 100000), indicating the length of each stick in order.

Output

Output the maximum value j - i in a single line. If there is no such i and j, just output -1.

Sample Input

4
5 4 3 6
4
6 5 4 3

Sample Output

1
-1

Source

POJ Monthly,static

[Submit]   [Go Back]   [Status]   [Discuss]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值