POJ 3264 Balanced Lineup
来源:互联网 发布:易语言cc攻击源码 编辑:程序博客网 时间:2024/06/07 12:08
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 cowi
Lines N+2..N+Q+1: Two integers A and B (1 ≤A ≤B ≤ N), representing the range of cows from A toB inclusive.
Output
Sample Input
6 31734251 54 62 2
Sample Output
630
RMQ问题,在一个数组的指定区间内找寻最大值和最小值,并输出他们的差。
套了模板过。
#include <stdio.h>#include <algorithm>#define N 50005using namespace std;int dpmin[N][100];int dpmax[N][100];int a[N];int minn,maxn;void RMQ_init(int n){for(int i=1;i<=n;i++) dpmin[i][0]=a[i],dpmax[i][0]=a[i];for(int j=1;(1<<j)<=n;j++)for(int i=1;i+(1<<j)-1<=n;i++) dpmin[i][j]=min(dpmin[i][j-1],dpmin[i+(1<<(j-1))][j-1]),dpmax[i][j]=max(dpmax[i][j-1],dpmax[i+(1<<(j-1))][j-1]);}void RMQ(int l,int r){int k=0;while(1<<(k+1)<r-l+1) k++;minn=min(dpmin[l][k],dpmin[r-(1<<k)+1][k]),maxn=max(dpmax[l][k],dpmax[r-(1<<k)+1][k]);}int main(){ int n,q; while(scanf("%d%d",&n,&q)>0) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); RMQ_init(n); while(q--) { int l,r; scanf("%d%d",&l,&r); if (l > r) swap(l, r); RMQ(l,r); printf("%d\n",maxn-minn); } } return 0;}
- POJ 3264 Balanced Lineup
- POJ 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- Poj 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- POJ-3264-Balanced Lineup
- poj 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- POJ 3264-Balanced Lineup
- poj 3264 Balanced Lineup
- POJ 3264 Balanced Lineup
- POJ-3264-Balanced Lineup
- POJ 3264 Balanced Lineup
- POJ 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- Hdu oj 1233 还是畅通工程
- swift-03-数据类型转换
- WIN7下PS/2等键盘失灵无法使用的解决办法--实测有效
- Attribute xx is only used in API level xx and higher (current min is xx)的解决方法
- 例题6.7 冰人 HDU2936
- POJ 3264 Balanced Lineup
- 小问题:data too long for column 'picture' at row 1
- 算法竞赛入门经典:第五章 基础题目选解 5.6 数组模拟高精度乘法
- Linux电源管理(1)_整体架构
- [JavaScript] Google Closure: 糟糕的JavaScript
- 回车符,换行符与'\0'
- leetcode 233: Number of Digit One
- SQL Server 2008 R2的发布订阅配置实践
- 关于Apple的Captive Network Assistant