zoukankan      html  css  js  c++  java
  • B. Balanced Lineup

    B. Balanced Lineup

    5000ms
    5000ms
    65536KB
     
    64-bit integer IO format: %lld      Java class name: Main
     

    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

    解题:RMQ

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <vector>
     6 #include <climits>
     7 #include <algorithm>
     8 #include <cmath>
     9 #define LL long long
    10 using namespace std;
    11 int mn[50010][32],mx[50010][32],d[50010];
    12 int main(){
    13     int n,m,x,y,i,j;
    14     while(~scanf("%d %d",&n,&m)){
    15         for(i = 0; i < n; i++){
    16             scanf("%d",d+i);
    17         }
    18         memset(mn,0,sizeof(mn));
    19         memset(mx,0,sizeof(mx));
    20         for(i = n-1; i >= 0; i--){
    21             mn[i][0] = mx[i][0] = d[i];
    22             for(j = 1; i+(1<<j)-1 < n; j++){
    23                 mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
    24                 mx[i][j] = max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
    25             }
    26         }
    27         for(i = 0; i < m; i++){
    28             scanf("%d %d",&x,&y);
    29             if(x > y) swap(x,y);
    30             int r = y - x + 1;
    31             r = log2(r);
    32             int theMax,theMin;
    33             theMax = max(mx[x-1][r],mx[y-(1<<r)][r]);
    34             theMin = min(mn[x-1][r],mn[y-(1<<r)][r]);
    35             printf("%d
    ",theMax-theMin);
    36         }
    37     }
    38     return 0;
    39 }
    View Code
  • 相关阅读:
    3D游戏常用技巧Normal Mapping (法线贴图)原理解析——高级篇
    [工作积累] OpenGL ES3.0: glInvalidateFramebuffer
    引擎设计跟踪(九.14.2c) 最近一些小的更新
    引擎设计跟踪(九.14.2b) 骨骼动画基本完成
    最近在玩的游戏
    引擎设计跟踪(九.14.2a) 导出插件问题修复和 Tangent Space 裂缝修复
    [工作积累] Google/Amazon平台的各种坑
    [工作积累] NDK通过Java获取package name 和version
    [工作记录] Android OpenSL ES: references & AAC related
    [工作记录] Android OpenGL ES: non-square texture
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3841068.html
Copyright © 2011-2022 走看看