zoukankan      html  css  js  c++  java
  • POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264

    Balanced Lineup
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 66241   Accepted: 30833
    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

    题意概括:

    给出一段长度为 N 的序列,和 Q 次查询。

    每次输入区间,求解该区间 最大值 - 最小值的结果。

    解题思路:

    RMQ问题可以线段树维护(甚至树状数组)复杂度 预处理 O(NlongN) 单次查询 O(logN)

    不过这里用的是 ST表 预处理O(NlogN) 单次查询 O(1)

    ST表 本质思想是 dp,这里用两个dp 维护区间最大值和最小值。

    设 起点是 i 区间长度为

    区间 【i , i +(1 << j )】的最大值为 dpmax[ i, j ],最小值为 dpmin[ i, j ]

    转移方程:

    dpmax[ i, j ] = max( dpmax[ i ][ j-1 ], dpmax[ i + (1<<(j-1)) ][ j ] );

    dpmin[ i, j ] = min( dpmin[ i ][ j-1 ], dpmin[ i + (1<<(j-1)) ][ j ] );

    实质就是按照二的幂次方关系,把一个区间分成了两个区间。

    AC code:

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <cmath>
     5 #define INF 0x3f3f3f3f
     6 #define LL long long
     7 using namespace std;
     8 const int MAXN = 5e4+50;
     9 int dpmax[MAXN][20];
    10 int dpmin[MAXN][20];
    11 int num[MAXN];
    12 
    13 void make_maxRMQ(int N, int b[])
    14 {
    15     for(int i = 0; i < N; i++){
    16         dpmax[i][0] = b[i];
    17     }
    18     for(int ilen = 1; (1<<ilen) <= N; ilen++)
    19     for(int i = 0; i+(1<<ilen)-1 < N; i++){
    20         dpmax[i][ilen] = max(dpmax[i][ilen-1], dpmax[i+(1<<(ilen-1))][ilen-1]);
    21     }
    22 }
    23 
    24 int get_max(int ll, int rr)
    25 {
    26     int k = (int)(log(rr-ll+1.0)/log(2.0));
    27     return max(dpmax[ll][k], dpmax[rr-(1<<k)+1][k]);
    28 }
    29 
    30 void make_minRMQ(int N, int a[])
    31 {
    32     for(int i = 0; i < N; i++){
    33         dpmin[i][0] = a[i];
    34     }
    35     for(int ilen = 1; (1<<ilen) <= N; ilen++)
    36     for(int i = 0; i+(1<<ilen)-1 < N; i++){
    37         dpmin[i][ilen] = min(dpmin[i][ilen-1], dpmin[i+(1<<(ilen-1))][ilen-1]);
    38     }
    39 }
    40 
    41 int get_min(int ll, int rr)
    42 {
    43     int k = (int)(log(rr-ll+1.0)/log(2.0));
    44     return min(dpmin[ll][k], dpmin[rr-(1<<k)+1][k]);
    45 }
    46 
    47 int main()
    48 {
    49     int N, Q;
    50     int L, R;
    51     int ans;
    52     while(~scanf("%d%d", &N, &Q)){
    53         for(int i = 0; i < N; i++){
    54             scanf("%d", &num[i]);
    55         }
    56         make_maxRMQ(N, num);
    57         make_minRMQ(N, num);
    58 
    59         while(Q--){
    60             scanf("%d%d", &L, &R);
    61             L--, R--;
    62             ans = get_max(L, R) - get_min(L, R);
    63             printf("%d
    ", ans);
    64         }
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    Vue生命周期(转)
    Gulp的简单使用
    webpack的简单使用
    面试----手写正则表达式
    面试----你可以手写一个promise吗
    baidu.com跳转www.baidu.com
    php 操作时间、日期类函数
    php操作文件类的函数
    sphinx搜索 笔记
    bash下输入命令的几个常用快捷键
  • 原文地址:https://www.cnblogs.com/ymzjj/p/10032772.html
Copyright © 2011-2022 走看看