zoukankan      html  css  js  c++  java
  • POJ3264(KB7-G RMQ)

    Balanced Lineup

    Time Limit: 5000MS Memory Limit: 65536K  
    otal Submissions: 52651 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 ≤ 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

     
     1 //2017-05-17
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 #include <cmath>
     7 
     8 using namespace std;
     9 
    10 const int N = 50005;
    11 int a[N], DPmin[N][20], DPmax[N][20];
    12 
    13 void init(int n)
    14 {
    15     for(int j = 1; j<=(int)log2(n); j++)
    16       for(int i = 1; i<=n; i++){
    17           DPmax[i][j] = DPmax[i][j-1];
    18           DPmin[i][j] = DPmin[i][j-1];
    19           if(i+(1<<j)-1 <= n){
    20               DPmax[i][j] = max(DPmax[i][j-1], DPmax[i+(1<<(j-1))][j-1]);
    21               DPmin[i][j] = min(DPmin[i][j-1], DPmin[i+(1<<(j-1))][j-1]);
    22           }
    23       }
    24 }
    25 
    26 int query(int l, int r)
    27 {
    28     int k = (int)log2(r-l+1);
    29     return max(DPmax[l][k], DPmax[r-(1<<k)+1][k])-min(DPmin[l][k], DPmin[r-(1<<k)+1][k]);
    30 }
    31 
    32 int main()
    33 {
    34     int n, q;
    35     while(scanf("%d%d", &n, &q)!=EOF)
    36     {
    37         for(int i = 1; i <= n; i++){
    38             scanf("%d", &DPmax[i][0]);
    39             DPmin[i][0] = DPmax[i][0];
    40         }
    41         init(n);    
    42         int l, r;
    43         while(q--)
    44         {
    45             scanf("%d%d", &l, &r);
    46             printf("%d
    ", query(l, r));
    47         }
    48     }
    49 
    50     return 0;
    51 }
  • 相关阅读:
    Openwrt智能路由系统开发--内容总结
    EdgeX foundry树莓派搭建与MQTT通讯
    C语言学习截图保存
    使用python生成Latex公式语法
    数据结构与算法之排序
    数据结构与算法之常用数据结构
    MySQL数据库规范 (设计规范+开发规范+操作规范)
    动态规划之LIS(最长上升子序列)
    动态规划之区间DP
    动态规划之状态压缩DP
  • 原文地址:https://www.cnblogs.com/Penn000/p/6872924.html
Copyright © 2011-2022 走看看