poj 2452

    技术2022-05-19  18

     

    Sticks Problem Time Limit: 6000MS Memory Limit: 65536KTotal Submissions: 7061 Accepted: 1719

    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

     

    分析:二分+rmq,暴力貌似也能过。。。⊙﹏⊙b汗

    #include<iostream> using namespace std; int f[50010][17],g[50010][17],a[50010],n; int maxrmq(int i,int j){return(a[i]>a[j]?i:j);} int minrmq(int i,int j){return(a[i]<a[j]?i:j);} void prermq() { for(int i=1;i<=n;++i)f[i][0]=g[i][0]=i; for(int j=1;(1<<j)<=n;++j) for(int i=1;i+(1<<j)-1<=n;++i) { f[i][j]=maxrmq(f[i][j-1],f[i+(1<<(j-1))][j-1]); g[i][j]=minrmq(g[i][j-1],g[i+(1<<(j-1))][j-1]); } } int askrmq(int l,int r,int s) { int k=0; while(l+(1<<k)<r-(1<<k)+1)++k; if(s==1)return maxrmq(f[l][k],f[r-(1<<k)+1][k]); else return minrmq(g[l][k],g[r-(1<<k)+1][k]); } int find(int x) { int l=1,r=x-1,m; while(l<r) { m=(l+r)>>1; if(a[askrmq(m,r,1)]<a[x])r=m-1; else l=m+1; } return(askrmq(l,x,2)); } int main() { //freopen("car.in","r",stdin); //freopen("car.out","w",stdout); while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;++i)scanf("%d",&a[i]); prermq(); int ans=0; for(int i=n;i>0;--i)ans=max(ans,i-find(i)); if(ans<1)ans=-1; printf("%d/n",ans); } return 0; }  

     

    添加线段树与队列两种方法

    线段树:

    #include<stdio.h> int n,s[50005],ans,maxind,minind; struct segment{int l,r,x,y;}p[200000]; //线段树实现 inline int max(int a,int b){return a>b?a:b;} void build(int l,int r,int top) { p[top].l=l;p[top].r=r; if(r-l>1) { int mid=(l+r)>>1,now=top<<1; build(l,mid,now); build(mid,r,now+1); p[top].x=s[p[now].x]<s[p[now+1].x]?p[now].x:p[now+1].x; p[top].y=s[p[now].y]>s[p[now+1].y]?p[now].y:p[now+1].y; } else if(r-l==1) { p[top].x=s[l]<s[r]?l:r; p[top].y=s[l]>s[r]?l:r; } } void get(int f,int t,int top) { if(f<=p[top].l&&t>=p[top].r) { minind=s[minind]<s[p[top].x]?minind:p[top].x; maxind=s[maxind]>s[p[top].y]?maxind:p[top].y; return ; } if(p[top].r-p[top].l==1)return ; int now=top<<1; if(f<p[now].r) get(f,t,now); if(t>p[now+1].l) get(f,t,now+1); } void solve(int f,int t) //分治,把问题规模变小 { if(t<=f) return ; maxind=minind=f; int x,y; get(f,t,1); x=minind;y=maxind; //一定要再复制,maxind,minind会发生变化 if(y>x) { ans=max(ans,y-x); --x;++y; for(;x>f;--x) //以最小为尾的递减序列删除 if(s[x-1]<s[x]) break; for(;y<t;++y) //以最大为首的递减序列删除 if(s[y+1]>s[y]) break; solve(f,x); solve(y,t); } else if(y<x) { solve(f,y); solve(x,t); ++y;--x; for(;x>f;--x) if(s[x-1]<s[x]) break; for(;y<t;++y) if(s[y+1]>s[y]) break; solve(y,x); } } inline void get(int& a) { char ch=getchar(); while (ch<'0'||ch>'9') ch=getchar(); for (a=0; ch>='0'&&ch<='9'; ch=getchar()) a=a*10+ch-48; } int main() { while(scanf("%d",&n)!=-1) { for(int i=0;i<n;++i)get(s[i]); build(0,n-1,1); ans=-1; solve(0,n-1); printf("%d/n",ans); } return 0; } 

    队列(想出来竟然和某牛一致,懒得敲代码,copy+优化。。。):

    #include <stdio.h> const int maxn=50001; int a[maxn],big[maxn],lit[maxn],n; inline void get(int& a) { char ch=getchar(); while (ch<'0'||ch>'9') ch=getchar(); for (a=0; ch>='0'&&ch<='9'; ch=getchar()) a=a*10+ch-48; } inline int min(int e,int s){return a[e]<a[s]?e:s;} inline int max(int e,int s){return e>s?e:s;} int main() { int i,j, ans; while (scanf("%d",&n)!=EOF) { for (i=0; i<n; ++i) get(a[i]); ans=0; for (i=0; i<n; ++i) { lit[i]=i; for (j=i-1; j>=0&&a[j]<a[i]; j=big[j])lit[i]=min(lit[i],lit[j]); big[i]=j; } for (i=n-1; i>0; --i) if (i>ans)ans=max(ans,i-lit[i]); if(ans==0)ans=-1; printf("%d/n",ans); } return 0; }  

     


    最新回复(0)