poj 3264 Balanced Lineup 【RMQ 裸题】
来源:互联网 发布:嘉兴 行李箱 知乎 编辑:程序博客网 时间:2024/06/06 08:30
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 31734251 54 62 2
Sample Output
630
题意:给你一个N个数的序列,有M次查询,查询区间[x, y]的最大值与最小值之差。
AC代码:
#include <cstdio>#include <cstring>#include <algorithm>#define MAXN 50000+10using namespace std;int A[MAXN];int Amax[MAXN][30];int Amin[MAXN][20];int N, M;void RMQ_init(){ for(int i = 1; i <= N; i++) Amax[i][0] = Amin[i][0] = A[i]; for(int j = 1; (1<<j) <= N; j++) { for(int i = 1; i + (1<<j) - 1 <= N; i++) { Amax[i][j] = max(Amax[i][j-1], Amax[i + (1<<(j-1))][j-1]); Amin[i][j] = min(Amin[i][j-1], Amin[i + (1<<(j-1))][j-1]); } }}int query(int L, int R){ int k = 0; while((1 << (k+1)) <= R-L+1) k++; return max(Amax[L][k], Amax[R - (1<<k) + 1][k]) - min(Amin[L][k], Amin[R - (1<<k) + 1][k]);}int main(){ while(scanf("%d%d", &N, &M) != EOF) { for(int i = 1; i <= N; i++) scanf("%d", &A[i]); RMQ_init(); int x, y; while(M--) { scanf("%d%d", &x, &y); printf("%d\n", query(x, y)); } } return 0;}
- poj 3264 Balanced Lineup 【RMQ 裸题】
- POJ 3264 Balanced Lineup 【RMQ裸题 】
- 【POJ 3264】Balanced Lineup 【RMQ裸题】
- POJ 3264 Balanced Lineup(RMQ)
- POJ-3264 Balanced Lineup【RMQ】
- poj 3264 Balanced Lineup RMQ
- POJ 3264 Balanced Lineup [RMQ]
- 【RMQ】poj 3264 Balanced Lineup
- POJ 3264 Balanced Lineup (RMQ)
- POJ 3264 Balanced Lineup,RMQ
- POJ 3264 Balanced Lineup(RMQ)
- poj 3264 Balanced Lineup【RMQ】
- POJ-3264 Balanced Lineup【RMQ】
- POJ 3264 Balanced Lineup (RMQ)
- POJ 3264 Balanced Lineup (RMQ)
- 【POJ 3264】 Balanced Lineup (RMQ)
- POJ---3264-Balanced Lineup (RMQ)
- POJ 3264 Balanced Lineup (RMQ)
- double格式小数位
- 测试测试
- 【使用Property创建存取器】
- log4j 自定义 level
- GEM5与SystemC混合仿真的实现
- poj 3264 Balanced Lineup 【RMQ 裸题】
- 【暑期基础2】N HDU 2039 三角形
- Linux下大文件分割和小文件合并
- 运营商的三大垄断性思维
- Android开发面试经——1.常见人事面试问题
- 取消京津冀手机漫游费更像文字游戏
- 我为什么选择 iBatis 而不是 Hibernate(对于正在选型的人的建议)
- Android Studio之新建Studio项目
- Hdu 2066 一个人的旅行(dijkstra算法)