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;
    }
  • 相关阅读:
    mui h5 动态实现数据的移除和数据操作完后的重新获取
    mui H5 js动态添加不同类型的数据
    hbuider 中点击就显示出一个单选的列表 ,然后后台跨域向里面动态添加数据,注意里面的格式是json object
    H-UI的前端处理验证,判断是否已经存在,比较健全的模板,可以自己添加一些校验
    spring mvc 和mybatis整合 的异常处理
    列表显示数据 但是数据的字体颜色要js添加
    hadoop环境都配置好后,当运行sbin下的start-hdfs.sh时报WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform...错误
    VisualVM初次使用BTrace功能方法步骤
    二叉树--递归实现
    二叉树--非递归实现
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5346572.html
Copyright © 2011-2022 走看看