zoukankan      html  css  js  c++  java
  • Balanced Lineup 倍增思想到ST表RMQ

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


    题目抽象:求数组区间[a,b]之间的最大值与最小值的差。



     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <cmath>
     5 #include <algorithm>
     6 using namespace std;
     7 const int INF=0x4fffffff;
     8 const int MS=50005;
     9 
    10 int a[MS];
    11 int minv[MS][20];
    12 int maxv[MS][20];
    13 
    14 int N,Q;
    15 
    16 void RMQ_init()
    17 {
    18     for(int i=0;i<N;i++)
    19         minv[i][0]=maxv[i][0]=a[i];
    20     for(int j=1;(1<<j)<=N;j++)
    21     {
    22         for(int i=0;i+(1<<j)-1<N;i++)
    23         {
    24             minv[i][j]=min(minv[i][j-1],minv[i+(1<<(j-1))][j-1]);
    25             maxv[i][j]=max(maxv[i][j-1],maxv[i+(1<<(j-1))][j-1]);
    26         }
    27     }
    28 }
    29 
    30 int query_min(int l,int r)
    31 {
    32     int k=0;
    33     while((1<<(k+1))<(r-l+1))
    34         k++;
    35     return min(minv[l][k],minv[r-(1<<k)+1][k]);
    36 }
    37 
    38 int query_max(int l,int r)
    39 {
    40     int k=0;
    41     while((1<<(k+1))<(r-l+1))
    42         k++;
    43     return max(maxv[l][k],maxv[r-(1<<k)+1][k]);
    44 }
    45 
    46 int main()
    47 {
    48     scanf("%d%d",&N,&Q);
    49     for(int i=0;i<N;i++)
    50         scanf("%d",&a[i]);
    51     RMQ_init();
    52     while(Q--)
    53     {
    54         int l,r;
    55         scanf("%d%d",&l,&r);
    56         l--;
    57         r--;
    58         int max_v=query_max(l,r);
    59         int min_v=query_min(l,r);
    60         printf("%d
    ",max_v-min_v);
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    屏蔽打开文件时提示“您尝试打开的文件xxx.xls的格式与文件扩展名指定的格式不一致。打开文件前请验证文件没有损坏且来源可信。是否立即打开该文 件?”
    sql实现MD5加密
    微信小程序在苹果手机的New Date坑
    命令行同步互联网时钟
    excel怎么把单元格内某个字标红,其他字不变
    js 字符串排序 String.prototype.localeCompare
    vscode 使用键盘运行 npm命令
    vscode 使用键盘打开 git分支面板
    给img标签的src属性使用file文件对象
    DOM0级 与 DOM2级
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4307898.html
Copyright © 2011-2022 走看看