zoukankan      html  css  js  c++  java
  • poj 3264(RMQ或者线段树)

    Balanced Lineup
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 42929   Accepted: 20184
    Case Time Limit: 2000MS

    Description

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

    Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

    Input

    Line 1: Two space-separated integers, N and Q.
    Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
    Lines N+2..N+Q+1: Two integers A and B (1 ≤ ABN), representing the range of cows from A to B inclusive.

    Output

    Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

    Sample Input

    6 3
    1
    7
    3
    4
    2
    5
    1 5
    4 6
    2 2

    Sample Output

    6
    3
    0

    Source

    题意:区间最大值与最小值之差RMQ版:(不懂的可以参考blog)

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    #define N 50010
    
    int a[N];
    int max_dp[N][20];
    int min_dp[N][20];
    int MAX(int i,int j){
        if(i>=j) return i;
        return j;
    }
    int MIN(int i,int j){
        if(i<=j) return i;
        return j;
    }
    void init_MAX_RMQ(int n){
        for(int i=1;i<=n;i++) max_dp[i][0]=a[i];
        for(int j=1;(1<<j)<=n;j++){
            for(int i=1;i<=n-(1<<j)+1;i++){
                ///F[i, j]=max(F[i,j-1], F[i + 2^(j-1),j-1])。
                max_dp[i][j] = MAX(max_dp[i][j-1],max_dp[i+(1<<(j-1))][j-1]);
            }
        }
    }
    int MAX_RMQ(int a,int b){
        int k = (int)(log(b-a+1.0)/log(2.0));
        ///RMQ(A, i, j)=min{F[i,k],F[j-2^k+1,k]}
        return MAX(max_dp[a][k],max_dp[b-(1<<k)+1][k]);
    }
    void init_MIN_RMQ(int n){
        for(int i=1;i<=n;i++) min_dp[i][0]=a[i];
        for(int j=1;(1<<j)<=n;j++){
            for(int i=1;i<=n-(1<<j)+1;i++){
                min_dp[i][j] = MIN(min_dp[i][j-1],min_dp[i+(1<<(j-1))][j-1]);
            }
        }
    }
    int MIN_RMQ(int a,int b){
        int k = (int)(log(b-a+1.0)/log(2.0));
        return MIN(min_dp[a][k],min_dp[b-(1<<k)+1][k]);
    }
    int main()
    {
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF){
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
            }
            init_MAX_RMQ(n);
            init_MIN_RMQ(n);
            while(m--){
                int a,b;
                scanf("%d%d",&a,&b);
                printf("%d
    ",MAX_RMQ(a,b)-MIN_RMQ(a,b));
            }
        }
        return 0;
    }

    线段树:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    #define N 50010
    
    struct Tree{
        int l,r;
        int Max,Min;
    }tree[4*N];
    int a[N];
    int MAX_VALUE;
    int MIN_VALUE;
    int MAX(int i,int j){
        if(i>=j) return i;
        return j;
    }
    int MIN(int i,int j){
        if(i<=j) return i;
        return j;
    }
    void PushUp(int idx){
        tree[idx].Max = MAX(tree[idx<<1].Max,tree[idx<<1|1].Max);
        tree[idx].Min = MIN(tree[idx<<1].Min,tree[idx<<1|1].Min);
    }
    void build(int l,int r,int idx){
        tree[idx].l = l;
        tree[idx].r = r;
        if(l==r) {
            tree[idx].Max = tree[idx].Min = a[l];
            return ;
        }
        int mid=(l+r)>>1;
        build(l,mid,idx<<1);
        build(mid+1,r,idx<<1|1);
        PushUp(idx);
    }
    void query(int l,int r,int idx){
        if(tree[idx].l==l&&tree[idx].r==r){
            MAX_VALUE = MAX(MAX_VALUE,tree[idx].Max);
            MIN_VALUE = MIN(MIN_VALUE,tree[idx].Min);
            return;
        }
        int mid=(tree[idx].l+tree[idx].r)>>1;
        if(mid>=r) query(l,r,idx<<1);
        else if(mid<l) query(l,r,idx<<1|1);
        else{
            query(l,mid,idx<<1);
            query(mid+1,r,idx<<1|1);
        }
    }
    int main()
    {
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF){
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
            }
            build(1,n,1);
            while(m--){
                int b,c;
                scanf("%d%d",&b,&c);
                MAX_VALUE=-1;
                MIN_VALUE=1000001;
                query(b,c,1);
                printf("%d
    ",MAX_VALUE-MIN_VALUE);
            }
        }
        return 0;
    }
  • 相关阅读:
    Spring警告: Could not load driverClass com.mysql.jdbc.Driver(待解决)
    马士兵_JAVA自学之路(为那些目标模糊的码农们)
    Spring报错:java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
    安装spring报错:Cannot complete the install because of a conflicting dependency.
    MYSQL外键(Foreign Key)的使用
    MongoDB(Roboit3T)中导出集合数据
    Express安装
    ES6学习
    C#中添加log4net(日志文件)
    登录MES系统后台服务的操作
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5346572.html
Copyright © 2011-2022 走看看