poj 3264 Balanced Lineup (简单 RMQ )
来源:互联网 发布:计算机通信与网络答案 编辑:程序博客网 时间:2024/06/15 22:01
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
Source
#include<cstdio>#include<iostream>#include<cstring>//#include<algorithm>#include<cmath>using namespace std;int N;int h[50010];int dp1[50010][40];int dp2[50010][40];int min1(int a,int b){ return a>b?b:a;}int max1(int a,int b){ return a>b?a:b;}void mk_rmq(){ int i,j; for(i=1;i<=N;i++) { dp1[i][0]=h[i]; dp2[i][0]=h[i]; } for(j=1;(1<<j)<=N;j++) { for(i=1;i+(1<<j)-1<=N;i++) { dp1[i][j]=max1(dp1[i][j-1],dp1[i+(1<<(j-1))][j-1]); dp2[i][j]=min1(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]); } }}int q_rmq(int l,int r){ int k=(int)(log((r-l+1)*1.0)/log(2.0)); int min_h=min1(dp2[l][k],dp2[r-(1<<k)+1][k]); int max_h=max1(dp1[l][k],dp1[r-(1<<k)+1][k]); return max_h-min_h;}int main(){ int Q,i,l,r; while(scanf("%d%d",&N,&Q)!=EOF) { for(i=1;i<=N;i++) scanf("%d",&h[i]); mk_rmq(); for(i=0;i<Q;i++) { scanf("%d%d",&l,&r); printf("%d\n",q_rmq(l,r)); } } return 0;}
12027611
fukan
3264
Accepted
15972K
1891MS
C++
1142B
2013-08-23 09:57:31
- 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)
- 删除有着奇葩文件名的文件
- Codeforces Beta Round #91 (Div. 2) / 122A Lucky Division (数论)
- Valid Palindrome
- jQuery中RadioButtonList的功能及用法
- 比较两个list中不同的值简单方法(小记)
- poj 3264 Balanced Lineup (简单 RMQ )
- JSF的Session
- 日常书信英语
- SM2的实现及优化(一)
- 文件大小单位转换
- android源码分享 http://www.linuxdiyf.com/viewarticle.php?id=286513
- cocos2d-x项目模板创建新项目
- Telnet 命令
- vim使用16进制打开二进制文件