zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Elasticsearch的介绍与安装配置启动问题
    代码发布项目
    gitpython模块
    Paramiko模块
    gojs插件的介绍与使用
    django中如何实现websocket,真正通过websocket实现群聊功能
    如何实现服务端主动给客户端推送消息,websocket详解,以及django如何使用websocket问题
    简单爬取汽车之家新闻(requests模块+bs4)
    http协议版本,响应状态码,正反向代理的区别,与伪静态
    web开发经验——富头像上传编辑器的使用
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9400006.html
Copyright © 2011-2022 走看看