• 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 }
  • 相关阅读:
    C51学习笔记
    Keil C51与Keil ARM共存
    keil c51笔记
    css实现三角形
    微信小程序倒计时实现
    微信小程序公共组件的引用与控制
    mac上查找nginx安装位置
    charles抓取移动端app数据
    封装react组件——三级联动
    前端基础(http协议相关篇)
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9400006.html
Copyright © 2020-2023  润新知