poj3264 - Balanced Lineup(RMQ_ST)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 45243 | Accepted: 21240 | |
| 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
分析 既可以用线段树也可以用ST来做
#include<iostream>
#include<cstdio>
#include<cmath>
#define maxx 50005
using namespace std;
int maxsum[maxx][],minsum[maxx][],n,Q;
int _max(int a,int b)
{
return a>b?a:b;
}
int _min(int a,int b)
{
return a<b?a:b;
}
int main()
{ while(~scanf("%d %d\n",&n,&Q))
{
for(int i=;i<=n;i++)
{
scanf("%d\n",&maxsum[i][]);
minsum[i][]=maxsum[i][];
}
for(int j=;j<=;j++)// 预处理
for(int i=;i<=n;i++) //j 循环在 i 循环外
{
if(i+(<<j)-<=n) // 注意左右区间
{
maxsum[i][j]=_max(maxsum[i][j-],maxsum[i+(<<(j-))][j-]);
minsum[i][j]=_min(minsum[i][j-],minsum[i+(<<(j-))][j-]);
}
}
while(Q--)
{int a,b;
scanf("%d %d\n",&a,&b);
int k=(int)((log((double)(b-a+)))/log(2.0)); //注意要包含端点并转换成double
int maxl=_max(maxsum[a][k],maxsum[b-(<<k)+][k]);//不然有些测试要出问题 比如poj
int minl=_min(minsum[a][k],minsum[b-(<<k)+][k]);
printf("%d\n",maxl-minl);
}
}
return ;
}
poj3264 - Balanced Lineup(RMQ_ST)的更多相关文章
- POJ 3264 Balanced Lineup(RMQ_ST)
题目链接:http://poj.org/problem? id=3264 Description For the daily milking, Farmer John's N cows (1 ≤ N ...
- poj3264 Balanced Lineup(树状数组)
题目传送门 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 64655 Accepted: ...
- Balanced Lineup(RMQ)
原题传送门 就是裸RMQ啊.. 求区间最大值和区间最小值,一看就像RMQ,当然线段树貌似也可以. 至于算法嘛.自己学~(好吧,放个传送门...) 然后就是最后把maxsum-minsum就好啦233~ ...
- POJ 题目3264 Balanced Lineup(RMQ)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 39046 Accepted: 18291 ...
- POJ-3264 Balanced Lineup(区间最值,线段树,RMQ)
http://poj.org/problem?id=3264 Time Limit: 5000MS Memory Limit: 65536K Description For the daily ...
- POJ3264——Balanced Lineup(线段树)
本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中.取一段区间.然后在区间中找出最大的数和最小的数字.求这两个数字的差. 分析:按区间取值,非常明显使 ...
- kuangbin专题七 POJ3264 Balanced Lineup (线段树最大最小)
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...
- 【POJ3264】Balanced Lineup(RMQ)
题意:每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛 ...
- [POJ3264]Balanced Lineup(线段树,区间最值差)
题目链接:http://poj.org/problem?id=3264 一排牛按1~n标号记录重量,问每个区间最重的和最轻的差值. 线段树维护当前节点下属叶节点的两个最值,查询后作差即可. #incl ...
随机推荐
- 关于ILDASM.EXE的知识整理
因为现在用的VS2010,发现,这个工具自己就带着ILDASM.EXE这个反编译工具 具体的查找方式为: C:\Program Files\Microsoft SDKS\Windows\V7.0\bi ...
- javascript立即执行函数 (function(){})()
看到一段代码: (function(){ var outer = $('#subject'); outer.find('li').on('mouseover', mouseover); })() ( ...
- 嵌入式Linux内核I2C子系统详解
1.1 I2C总线知识 1.1.1 I2C总线物理拓扑结构 I2C总线在物理连接上非常简单,分别由SDA(串行数据线)和SCL(串行时钟线)及上拉电阻组成.通信原理是通过对SCL和SDA线高 ...
- win7出现无法连接到代理服务器的错误,不能上网的问题的解决
今天晚上突然停电,等我打开电脑发现不然上网,用google浏览器出现这个错误: 用IE诊断错误如下: 说是不能连到代理服务器,但是我没有连接到代理服务器啊,但是我的QQ能登,就是不能用浏览器上网,经过 ...
- JavaScript 基础(七) 箭头函数 generator Date JSON
ES6 标准新增了一种新的函数: Arrow Function(箭头函数). x => x *x 上面的箭头相当于: function (x){ return x*x; } 箭头函数相当于匿名函 ...
- hive中order by,sort by, distribute by, cluster by作用以及用法
1. order by Hive中的order by跟传统的sql语言中的order by作用是一样的,会对查询的结果做一次全局排序,所以说,只有hive的sql中制定了order by所有的 ...
- 用Jenkins配置自动化构建
公司培训内容 -------------->记一笔 dubbo 微服务soadiamond-server 配置中心kafka rocketmq消息队列cas-server 单点登录spring ...
- XproerIM V1,2,12,65376 发布。
客户端下载:http://yunpan.cn/QTCxKvcpC4Iet 访问密码 9141 更新说明:1.增加表情功能. 更新代码截图:
- 【软件工程】电梯调度程序需求分析 李亚文&&郭莉莉
2014年3月4日(14:00-16:00) 为了进一步理解电梯工作的原理,我们特地到石家庄铁道大学春晖楼坐了电梯:春晖楼东办共有电梯两部,最高楼层为11层,最低楼层为-1,两电梯可共同使用.结合调查 ...
- android_开发环境配置
在学习 Android 之前,需要配置开发环境. 我用的是 eclipse ,简单说下 eclipse 的 Android 的环境配置. 如果没有JDK得先下载:http://www.oracle.c ...