标签:poj namespace iostream algorithm
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 10141 | Accepted: 2682 |
Description
Input
Output
Sample Input
4 5 4 3 6 4 6 5 4 3
Sample Output
1 -1
#include <iostream> #include <cstdio> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <vector> #include <sstream> #define PI acos(-1.0) #define eps 1e-8 const int inf = (1<<30) - 10; using namespace std; const int maxx = 50000 + 10; int n; int a[maxx]; int dpmax[maxx][20]; int dpmin[maxx][20]; int mn[maxx]; inline void init_rmq(int a[],int n){ ///构建rmq mn[0]=-1; for(int i = 1;i <= n; ++i){ if((i&(i-1))==0){ mn[i] = mn[i-1]+1; }else{ mn[i] = mn[i-1]; } dpmax[i][0] = a[i]; dpmin[i][0] = a[i]; } for(int j = 1;j <= mn[n]; ++j){ for(int i = 1;i+(1<<j)-1 <= n; ++i){ dpmax[i][j] = max(dpmax[i][j-1],dpmax[i+(1<<(j-1))][j-1]); dpmin[i][j] = min(dpmin[i][j-1],dpmin[i+(1<<(j-1))][j-1]); } } } inline int max_rmq(int l,int r){ //查询区间最大值 int k = mn[r-l+1]; return max(dpmax[l][k],dpmax[r-(1<<k)+1][k]); } inline int min_rmq(int l,int r){ //查询区间最小值 int k = mn[r-l+1]; return min(dpmin[l][k],dpmin[r-(1<<k)+1][k]); } int main(){ while(scanf("%d",&n)==1){ for(int i = 1;i <= n; ++i){ scanf("%d",&a[i]); } init_rmq(a,n); int ans = -1; for(int i = 1;i <= n; ++i){ int l = i; int r = n+1; while(l+1 != r){ int mid = (l+r)>>1; if(min_rmq(i,mid) == a[i]){ l = mid; }else{ r = mid; } } int ll = i; int rr = l; if(l != i){ int temp = max_rmq(ll,rr); while(ll+1 != rr){ int mid = (ll+rr)>>1; if(max_rmq(i,mid) < temp){ ll = mid; }else{ rr = mid; } } } if(rr-i>ans) ans = rr-i; } if(ans) printf("%d\n",ans); else printf("-1\n"); } return 0; } /** 7 1 2 3 7 4 5 6 */
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:poj namespace iostream algorithm
原文地址:http://blog.csdn.net/u012844301/article/details/47254133