• POJ3246


    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

     1 #include<algorithm>
     2 #include <cstdio>
     3 #include <cmath>
     4 using namespace std;
     5 int a[50001];
     6 int dp[50001][16];//2^16长度
     7 int DP[50001][16];
     8 int n,q;
     9 void ST()
    10 {
    11     for (int i = 1; i <=n ; ++i) {
    12         dp[i][0]=a[i];
    13         DP[i][0]=a[i];
    14     }
    15 
    16     for (int j = 1; (1<<j) <=n ; ++j) {
    17         for (int i = 1; i+(1<<j)-1 <= n ; ++i) {
    18             dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);//需要注意+的优先级高于<<
    19             DP[i][j]=max(DP[i][j-1],DP[i+(1<<(j-1))][j-1]);
    20         }
    21     }
    22 }
    23 int main()
    24 {
    25     scanf("%d%d",&n,&q);
    26     for (int i = 1; i <=n ; ++i) {
    27         scanf("%d",&a[i]);
    28     }
    29     ST();
    30     int x,y;
    31     for (int i = 0; i <q ; ++i) {
    32         scanf("%d%d",&x,&y);
    33         int m=floor(log((double)(y-x+1))/log(2.0));
    34         int MAX=max(DP[x][m],DP[y-(1<<m)+1][m]);
    35         int MIN=min(dp[x][m],dp[y-(1<<m)+1][m]);
    36         printf("%d
    ",MAX-MIN);
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    非常不错的漂浮广告代码(须调用外部JS文件)
    终于也进入了博客园
    ARM伪指令必读
    细说嵌入式Linux文件系统的制作方法
    使用OpenJTAG来检查硬件焊接问题
    应对艰难职场环境的五条策略
    成为高端人才必看的二十大箴言
    神奇的platform_get_resource函数
    女生奋斗励志篇?现代女孩都应该看看
    中国40位40岁以下的商界精英
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9400006.html
Copyright © 2020-2023  润新知