poj   3264  Balanced Lineup

link: http://poj.org/problem?id=3264

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

Source

题解:

快速找到一个区间[a, b] 之间的最大值和最小值的差;

经典的RMQ问题。 利用Sparse Table算法, 动态规划求解。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 50005; int n, m, num[maxn], dp1[maxn][18], dp2[maxn][18]; void BuildIndex(){
for(int i=0; i<n;++i){
dp1[i][0] = i;
dp2[i][0] = i;
}
for(int i=1; (1<<i)<=n; ++i){
for(int j=0; j+(1<<i)-1<n; ++j){
// find max
if(num[dp1[j][i-1]] > num[dp1[j+(1<<(i-1))][i-1]]){
dp1[j][i] = dp1[j][i-1];
}else{
dp1[j][i] = dp1[j+(1<<(i-1))][i-1];
} // find min
if(num[dp2[j][i-1]] < num[dp2[j+(1<<(i-1))][i-1]]){
dp2[j][i] = dp2[j][i-1];
}else{
dp2[j][i] = dp2[j+(1<<(i-1))][i-1];
}
}
}
} int FindMaxIndex(int start, int end){
int k = (int)((log((end - start + 1)*1.0))/log(2.0));
if(num[dp1[start][k]] > num[dp1[end-(1<<k)+1][k]]){
return dp1[start][k];
}else{
return dp1[end-(1<<k)+1][k];
}
}
int FindMinIndex(int start, int end){
int k = (int)((log((end - start + 1)*1.0))/log(2.0));
if(num[dp2[start][k]] > num[dp2[end-(1<<k)+1][k]]){
return dp2[end-(1<<k)+1][k];
}else{
return dp2[start][k];
}
} int main(){
freopen("in.txt", "r", stdin); int ans1, ans2, x, y;
while(scanf("%d %d", &n, &m) != EOF){
for(int i=0; i<n; ++i){
scanf("%d", &num[i]);
}
BuildIndex();
while(m--){
scanf("%d %d", &x, &y);
if(x > y){ swap(x, y); }
ans1 = FindMinIndex(x-1, y-1);
ans2 = FindMaxIndex(x-1, y-1);
printf("%d\n", (num[ans2] - num[ans1]) );
}
}
return 0;
}

  

poj-3264-Balanced Lineup的更多相关文章

  1. Poj 3264 Balanced Lineup RMQ模板

    题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...

  2. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

  3. poj 3264 Balanced Lineup (RMQ)

    /******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...

  4. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  5. POJ - 3264——Balanced Lineup(入门线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 68466   Accepted: 31752 ...

  6. poj 3264 Balanced Lineup 题解

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Subm ...

  7. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  8. POJ 3264 Balanced Lineup 线段树 第三题

    Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...

  9. poj 3264 Balanced Lineup (线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 42489   Accepted: 20000 ...

  10. poj 3264 Balanced Lineup(RMQ裸题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 43168   Accepted: 20276 ...

随机推荐

  1. js的闭包概念

    一.变量的作用域要懂得闭包,起首必须懂得Javascript特别的变量作用域.变量的作用域无非就是两种:全局变量和局部变量.Javascript说话的特别之处,就在于函数内部可以直接读取全局变量. J ...

  2. 基本排序算法——基数排序java实现

    基数排序 package basic.sort; import java.util.Arrays; import java.util.Random; public class RadixSort { ...

  3. 环信SDK报错处理方法obtain an updated library from the vendor, or disable bitcode for this target. for archit

    ld: '/Users/momo/Desktop/ThreeFingers/Pods/EaseMobSDKFull/EaseMobSDKFull/lib/libEaseMobClientSDK_arm ...

  4. va_start和va_end使用详解

    本文主要介绍va_start和va_end的使用及原理. 介绍这两个宏之前先看一下C中传递函数的参数时的用法和原理: 1.在C中,当我们无法列出传递函数的所有实参的类型和数目时,可以用省略号指定参数表 ...

  5. C# Enum,Int,String的互相转换

    版权声明:本文为博主原创文章,未经博主允许不得转载. Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用Int32.编程语言通常提供语法来声明由一组已 ...

  6. ORA-12523: TNS: 监听程序无法找到适用于客户机连接的例程

    今天使用PL/SQL Developer连接到一台新的测试服务器时,遇到ORA错误:ORA-12523: TNS: 监听程序无法找到适用于客户机连接的例程.对应的监听日志文件里面错误为TNS-1252 ...

  7. 杀毒软件导致YourSQLDba备份失败

    有一朋友咨询我,他配置的YourSQLDba 备份作业经常出现备份失败的情况,我帮忙查看了一下,首先从告警邮件中查看出错的具体信息,执行了下面SQL语句(来自于告警邮件) Exec YourSQLDb ...

  8. Linux 如何查看修改DNS配置

    DNS服务器介绍 DNS是计算机域名系统(Domain Name System 或Domain Name Service) 的缩写,它是由域名解析器和域名服务器组成的.域名服务器是指保存有该网络中所有 ...

  9. x01.os.19: linux 0.0

    linux 0.0 是一个丢失的版本,但赵炯老师又在 linux 0.11 的基础上,使它起死回生.www.oldlinux.org 有大量资源可供下载,值得一看. 1.要编译运行,首先需安装:sud ...

  10. 配置TortoiseSVN客户端, 强制签入前加注释

    正如上篇提到, 总有一些人在签入代码到SVN前没有加注释, 然后, 像这样: 鬼才知道改了什么东西. ①有些人可能就是没有写注释的习惯, ②有些人可能是忘记写注释 && SVN服务端和 ...