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 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    软件RAID 0
    逻辑卷管理lvm
    磁盘配额quota
    合并 CentOS 6.8 的两个ISO镜像
    挂载mount
    非交互式添加分区
    磁盘分区-gdisk用法
    C博客作业01--分支、顺序结构
    C语言--第0次作业
    浅议正则表达式
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5277449.html
Copyright © 2011-2022 走看看