poj-3264-Balanced Lineup
poj 3264 Balanced Lineup
link: http://poj.org/problem?id=3264
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
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
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的更多相关文章
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
- POJ 3264 Balanced Lineup 【ST表 静态RMQ】
传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total S ...
- poj 3264 Balanced Lineup (RMQ)
/******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...
- POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 53703 Accepted: 25237 ...
- POJ - 3264——Balanced Lineup(入门线段树)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 68466 Accepted: 31752 ...
- poj 3264 Balanced Lineup 题解
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Subm ...
- poj 3264:Balanced Lineup(线段树,经典题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32820 Accepted: 15447 ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- poj 3264 Balanced Lineup (线段树)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 42489 Accepted: 20000 ...
- poj 3264 Balanced Lineup(RMQ裸题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 43168 Accepted: 20276 ...
随机推荐
- IE7浏览器窗口大小改变事件执行多次bug(转)
var resizeTimer = null; $(window).resize(function() { if (resizeTimer) clearTimeout(resizeTimer); re ...
- Hui之Hui.js 官方文档
基础 // 判断值是否是指定数据类型 var result = hui.isTargetType("百签软件", "string"); //=>true ...
- Android Studio安装配置、环境搭建详细步骤及基本使用
前言 Android Studio的安装配置及使用篇终于来啦~ 废话不多说,以下针对JDK正确安装(及其环境变量配置完毕,即Java开发环境下).Android Studio的安装,配置,以及创建工程 ...
- SharePoint 2013 状态机工作流之扩展自定义状态
当我们使用SharePoint 2013的状态机工作流时,发现一个非常不爽的事情,就是SharePoint 所有的工作流状态,都是固定的那些,没办法显示我们自定义的状态,后来经过Google发现,原来 ...
- iOS 开发之路(WKWebView内嵌HTML5之图片上传) 五
HTML5页面的图片上传功能在iOS端的实现. 首先,页面上用的是plupload组件,在wkwebview上存在两个坑需要修复才能正常使用. 问题:在webview上点击选择照片/相机拍摄,就会出现 ...
- 在CentOS7上安装JDK1.8
在CentOS7上安装JDK1.8 1 通过 SecureCRT 连接到阿里云 CentOS7 服务器: 2 进入到目录 /usr/local/ 中: cd /usr/local/ 3 创建目录 to ...
- 有氧运动 && 无氧运动
有氧运动也叫做有氧代谢运动,是指人体在氧气充分供应的情况下进行的体育锻炼.有氧运动的好处是:可以提升氧气的摄取量,能更好地消耗体内多余的热量.也就是说,在运动过程中,人体吸入的氧气与需求相等,达到生理 ...
- .NET应用架构设计—用户端的防腐层作用及设计
阅读目录: 1.背景介绍 2.SOA架构下的显示端架构腐化 3.有效使用防腐层来隔离碎片服务导致显示端逻辑腐烂 4.剥离服务调用的技术组件让其依赖接口 5.将服务的DTO与显示端的ViewModel之 ...
- 统计文件种类数+获取子shell返回值的其它方法
前言 只是作为一个shell的小小练习和日常统计用,瞎折腾的过程中也是摸到了获取子shell返回值的几种方法: 肯定还有别的方法,跟进程间的通信相关,希望你能提出建议和补充,谢谢~ 完整程序: #! ...
- 今天发现一些很有意思的ubuntu命令
跑火车的sl/LS 终端数字雨cmatrix 可能是名言警句也可能是逗你玩的笑话的fortune/fortune-zh 一只会说话的牛 一只会吟诗的牛 上真牛喽! 炫酷 炫酷到这里了!!!