Balanced Lineup(RMQ)(POJ 3264)
来源:互联网 发布:嵌入式linux常用命令 编辑:程序博客网 时间:2024/06/04 01:03
题目:
RMQ
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
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.
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 31734251 54 62 2
Sample Output
630
代码:
/********************************************************** ** @name:poj 3264 ** @author:npufz ***********************************************************//*#include <bits/stdc++.h>*/#include <iostream>#include <cstdlib>#include <cmath>#include <cstdio>using namespace std;int dp1[50003][16];int dp2[50003][16];int rmq_st1(int n){ int m=(int )(log(1.0*n)/log(2.0)); for(int j=1;j<=m;j++) { int t=n-(1<<j)+1; for(int i=1;i<=t;i++) { dp1[i][j]=min(dp1[i][j-1],dp1[i+(1<<(j-1))][j-1]); } } return 0;}int rmq_st2(int n){ int m=(int )(log(1.0*n)/log(2.0)); for(int j=1;j<=m;j++) { int t=n-(1<<j)+1; for(int i=1;i<=t;i++) { dp2[i][j]=max(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]); } } return 0;}int rmq_find1(int l,int r){ int k=(int ) (log(1.0*(r-l+1))/log(2.0)); return min(dp1[l][k],dp1[r-(1<<k)+1][k]);}int rmq_find2(int l,int r){ int k=(int ) (log(1.0*(r-l+1))/log(2.0)); return max(dp2[l][k],dp2[r-(1<<k)+1][k]);}int main(){ int m,num,left,right,max1,min1; while(~scanf("%d%d",&num,&m)) { for(int i=1;i<=num;i++) { scanf("%d",&dp1[i][0]); dp2[i][0]=dp1[i][0]; } rmq_st1(num); rmq_st2(num); for(int i=0;i<m;i++) { scanf("%d%d",&left,&right); min1=rmq_find1(left,right); max1=rmq_find2(left,right); printf("%d\n",max1-min1); }}return 0;}反思:这是一道很典型的裸题,直接用线段树也可以过,而且省了不少内存,用RMQ_ST也可以过,代码如上
0 0
- poj 3264 Balanced Lineup (RMQ))
- poj 3264 Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup(简单RMQ)
- Balanced Lineup(RMQ)(POJ 3264)
- 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)
- POJ 3264 Balanced Lineup(RMQ)
- POJ - 3264 - Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup (RMQ)
- Julia:V0.3.0 正式发布,好消息!
- 图论总结
- 理解 Linux 的硬链接与软链接(三)
- fedora 解决android studio ADB not responding.
- C语言无符号有符号比较问题总结
- Balanced Lineup(RMQ)(POJ 3264)
- python学习笔记_文件遍历
- Python 爬虫把美空的图片都扒下来了
- POJ3169Layout差分约束
- A - Message Flood(字典树)
- 【1241】Oil Deposits(HDU)
- 快速计算子网掩码和主机数
- UNION运算符
- PHP中break及continue两个流程控制指令解析