zoukankan      html  css  js  c++  java
  • TOJ1698: Balanced Lineup

    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 ≤ A ≤ B ≤ N), 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

    USACO January 2007

    输出区间最大最小值之差,RMQ

     

    #include<bits/stdc++.h>
    using namespace std;
    const int N=80005;
    int dmi[N][30],dma[N][30],f[N];
    int n,q,l,r;
    void RMQ_init()
    {
        for(int j=1; (1<<j)<=n; j++)
            for(int i=1; i+j-1<=n; i++)
                dmi[i][j]=min(dmi[i][j-1],dmi[i+(1<<(j-1))][j-1]),dma[i][j]=max(dma[i][j-1],dma[i+(1<<(j-1))][j-1]);
    }
    int RMQ(int l,int r)
    {
        int k=f[r-l+1];
        return max(dma[l][k],dma[r-(1<<k)+1][k])-min(dmi[l][k],dmi[r-(1<<k)+1][k]);
    }
    int main()
    {
        f[0]=-1;
        scanf("%d%d",&n,&q);
        for(int i=1; i<=n; i++)
            scanf("%d",&dma[i][0]),dmi[i][0]=dma[i][0],f[i]=((i&(i-1))==0)?f[i-1]+1:f[i-1];
        RMQ_init();
        while(q--)
        {
            scanf("%d%d",&l,&r);
            printf("%d
    ",RMQ(l,r));
        }
        return 0;
    }

     

    线段树

    #include<bits/stdc++.h>
    using namespace std;
    #define lson l,mi,rt<<1
    #define rson mi+1,r,rt<<1|1
    struct T
    {
        int ma,mi,l,r;
    }tree[200000];
    int h[50005];
    int ma,mi;
    void build(int l,int r,int rt)
    {
        tree[rt].l=l,tree[rt].r=r;
        if(l==r)
        {
            tree[rt].ma=tree[rt].mi=h[l];
            return;
        }
        int mi=(l+r)>>1;
        build(lson);
        build(rson);
        tree[rt].ma=max(tree[rt<<1].ma,tree[rt<<1|1].ma);
        tree[rt].mi=min(tree[rt<<1].mi,tree[rt<<1|1].mi);
    }
    void findma(int l,int r,int rt)
    {
        if(tree[rt].l==l&&tree[rt].r==r)
        {
            if(tree[rt].ma>ma)ma=tree[rt].ma;
            return;
        }
        int mi=(tree[rt].l+tree[rt].r)>>1;
        if(mi>=r)
            findma(l,r,rt<<1);
        else if(mi<l)
            findma(l,r,rt<<1|1);
        else
            findma(lson),findma(rson);
    }
    
    void findmi(int l,int r,int rt)
    {
        if(tree[rt].l==l&&tree[rt].r==r)
        {
            if(tree[rt].mi<mi)mi=tree[rt].mi;
            return;
        }
        int mi=(tree[rt].l+tree[rt].r)>>1;
        if(mi>=r)
            findmi(l,r,rt<<1);
        else if(mi<l)
            findmi(l,r,rt<<1|1);
        else
            findmi(lson),findmi(rson);
    }
    int main()
    {
        int n,q,i,a,b;
        scanf("%d%d",&n,&q);
        for(i=1;i<=n;i++)
            scanf("%d",&h[i]);
        build(1,n,1);
        while(q--)
        {
            ma=0;
            mi=1e9;
            scanf("%d%d",&a,&b);
            findma(a,b,1),findmi(a,b,1);
            printf("%d
    ",ma-mi);
        }
        return 0;
    }

     

  • 相关阅读:
    搭建 mariadb 数据库主从同步
    MySQL--MVCC
    剑指 Offer 07. 重建二叉树
    剑指 Offer 06. 从尾到头打印链表
    MySQL--数据库范式
    剑指 Offer 05. 替换空格
    剑指 Offer 04. 二维数组中的查找
    剑指offer_03_数组中重复的数字(Java)
    Redis
    MySQL--SQL执行过程
  • 原文地址:https://www.cnblogs.com/BobHuang/p/8698477.html
Copyright © 2011-2022 走看看