首页 技术 正文
技术 2022年11月16日
0 收藏 369 点赞 3,854 浏览 2399 个字

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

USACO 2007 January Silver

题解:

快速找到一个区间[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;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,960
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,484
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,330
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,113
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,745
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,779