zoukankan      html  css  js  c++  java
  • 数据结构(RMQ):POJ 3624 Balanced Lineup

    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 ≤ 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

      这题是一个裸的RMQ问题。
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 const int maxn=50010;
     6 int mm[maxn],Min[maxn][20],Max[maxn][20],a[maxn];
     7 int main(){
     8 #ifndef ONLINE_JUDGE
     9     //freopen(".in","r",stdin);
    10     //freopen(".out","w",stdout);
    11 #endif    
    12     
    13     int n,Q;
    14     scanf("%d%d",&n,&Q);
    15     for(int i=1;i<=n;i++)
    16         scanf("%d",&a[i]);
    17     mm[0]=-1;
    18     for(int i=1;i<=n;i++){
    19         mm[i]=(i&(i-1))?mm[i-1]:mm[i-1]+1;
    20         Max[i][0]=a[i];
    21         Min[i][0]=a[i];
    22     }
    23     for(int k=1;k<=mm[n];k++)
    24         for(int i=1;i+(1<<(k-1))<=n;i++){
    25             Max[i][k]=max(Max[i][k-1],Max[i+(1<<(k-1))][k-1]);
    26             Min[i][k]=min(Min[i][k-1],Min[i+(1<<(k-1))][k-1]);
    27         }
    28         
    29     int a,b;
    30     while(Q--)
    31     {
    32         scanf("%d%d",&a,&b);
    33         printf("%d
    ",max(Max[a][mm[b-a+1]],Max[b-(1<<mm[b-a+1])+1][mm[b-a+1]])-min(Min[a][mm[b-a+1]],Min[b-(1<<mm[b-a+1])+1][mm[b-a+1]]));
    34     }    
    35     return 0;
    36 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    objdump man
    python c cpp extention
    http,get,head,post
    三种客户端访问wcf服务端的方法 C#
    使用HttpWebRequest POST 文件,带参数
    ASP.NET MVC3 HtmlHelper用法大全
    Windows安装memcached图文教程(转)
    sort排序应用
    WPA密码攻击宝典
    Bind和Eval的不同用法 (转)
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5277449.html
Copyright © 2011-2022 走看看