poj 2452(RMQ+二分查找)

题目链接:

http://poj.org/problem?id=2452

题意:在区间[1,n]上找到满足 a[i]<a[k]<a[j] (i<=k<=j) 的最大子区间 (j-i)如不存在输出 -1.

思路:枚举i,找到 i右边第一个不大于(不是小于) a[i]的数a[k](二分查找+RMQ某段区间的最小值是否小于a[i].最后确定到一个点),于是
我们可以得到在区间[i,k-1]范围内的数都会大于 a[i] ,所以对于下标i,它对应的最长区间必定在[i,k-1]之间。

所以,我们只要找到这个区间最大值 a[j] (这里又要用到RMQ查找区间最大值下标) 所以我们可以得到相对于点 i,其对应的最长子区间是[i,j]

例: 2 3 4 5 3 2 6 7

我们先枚举下标1 ,算出 右边不大于它的点是 a[6] = 2

然后对于 区间 2 3 4 5 3 2 最大值是 a[4] = 5 ,所以相对于下标1最长的区间是 1 - 4,依此类推。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = 50005;
///储存区间最大最小值的下标
int max_dp[2*N][30];
int min_dp[2*N][30];
int a[N];
int MAX(int i,int j){
    if(a[i]>a[j]) return i;
    return j;
}
int MIN(int i,int j){
    if(a[i]>a[j]) return j;
    return i;
}
void init_RMQ(int n){
    for(int i=1;i<=n;i++){
        max_dp[i][0] = min_dp[i][0]=i;
    }
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
          max_dp[i][j] = MAX(max_dp[i][j-1],max_dp[i+(1<<(j-1))][j-1]);
          min_dp[i][j] = MIN(min_dp[i][j-1],min_dp[i+(1<<(j-1))][j-1]);
        }
    }
}
int MAX_RMQ(int l,int r){
    int k  = (int)(log(r-l+1.0)/log(2.0));
    return MAX(max_dp[l][k],max_dp[r-(1<<k)+1][k]);
}
int MIN_RMQ(int l,int r){
    int k = (int)(log(r-l+1.0)/log(2.0));
    return MIN(min_dp[l][k],min_dp[r-(1<<k)+1][k]);
}
int binary(int value,int l,int r){
    while(l<=r){
        if(l==r) return l;
        int mid = (l+r)>>1;
        //printf("MIN_RMQ:%d\n",a[MIN_RMQ(l,mid)]);
        if(value<a[MIN_RMQ(l,mid)]){
            l = mid+1;
        }else r = mid;
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        init_RMQ(n);
        int ans = 0;
        for(int i=1;i<=n;i++){
            int value = a[i];
            int k = binary(value,i+1,n);

            int j = MAX_RMQ(i,k);
            //printf("%d %d %d\n",i,k,j);
            if(a[j]>a[i])
            ans = ans>j-i?ans:j-i;
        }
        if(ans==0) printf("-1\n");
        else printf("%d\n",ans);
    }

}
时间: 2024-11-09 17:00:14

poj 2452(RMQ+二分查找)的相关文章

POJ 2452 (RMQ + 二分)

Sticks Problem Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 2682 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 th

poj 1151 Atlantis 二分查找+离散化

Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17464   Accepted: 6654 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of

poj 3579 Median 二分查找与lower_bound

题意: 给n个数,他们两两之间较大数减去较小数总共有n*(n-1)/2个数,要求这些数的中位数. 分析: 两次二分,第一次枚举答案,第二次判断该数是否可能成为中位数. 代码: //poj 3579 //sep9 #include <iostream> #include <algorithm> using namespace std; const int maxN=1e5+10; int a[maxN]; int n,m; int test(int x) { int sum=0; f

poj 3122 (二分查找)

链接:http://poj.org/problem?id=3122 Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10448   Accepted: 3694   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N

poj 3122(二分查找)

Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13564   Accepted: 4650   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of

POJ 1064 Cable master(二分查找+精度)(神坑题)

POJ 1064 Cable master 一开始把 int C(double x) 里面写成了  int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条件写错了,wa了n次,当 c(mid)<=k时,令ub=mid,这个判断是错的,因为要找到最大切割长度,当满足这个条件时,可能已经不是最大长度了,此时还继续缩小区间,自然就wa了,(从大到小递减,第一次满足这个条件的值,就是最大的值),正确的判断是当 c(mid)<k时,令ub=mid,这样循环1

POJ 2289--Jamie&#39;s Contact Groups【二分图多重匹配问题 &amp;&amp;二分查找最大值的最小化 &amp;&amp; 最大流】

Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 6902   Accepted: 2261 Description Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very long contact list in her cell phone. The con

[ACM] poj 2456 Aggressive cows (二分查找)

Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...

POJ 3663 Costume Party (二分查找)

Costume Party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12297   Accepted: 4891 Description It's Halloween! Farmer John is taking the cows to a costume party, but unfortunately he only has one costume. The costume fits precisely two