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 ...
随机推荐
- No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- entityframework 入门-来自微软
必备条件 要完成本演练,需要安装 Visual Studio 2010 或 Visual Studio 2012. 如果使用的是 Visual Studio 2010,还需要安装 NuGet. 1.创 ...
- 计算hashCode的常见方法
把某个非零常数值,比如说17,保存在一个叫result的int类型的变量中. 2.对于对象中每一个关键域f(值equals方法中考虑的每一个域),完成以下步骤: a.为该域计算int类型的散列吗c: ...
- 移动前端头部标签(HTML5 meta)
在移动前端开发中添加一些webkit专属的HTML5头部标签,帮助浏览器更好解析html代码 <!DOCTYPE html> 使用 HTML5 doctype,不区分大小写 <he ...
- Python 集合方法总结
1.添加一个元素: add(...) Addan element to a set. 1 2 3 4 >>> a = {'shaw',11,22} >>>a. ...
- VC++ 关于 ON_UPDATE_COMMAND_UI 相关的作用.
ON_COMMAND_RANGE(ID_SORTING_GROUPBYTYPE, ID_SORTING_SORTBYACCESS, OnSort) ON_UPDATE_COMMAND_UI_RANGE ...
- java基础九[网络与线程](阅读Head First Java记录)
网络socket连接 Java API的网络功能包(java.net)已经将底层的TCP连接等都封装好了,我们只需要通过Socket对象来建立客户端和服务器的连接,然后客户端能向服务器发送请求,并接收 ...
- ssh 互通脚本
实现了 主机到指定机器的ssh免密码登录. 若要实现互通, 则在机器列表的每台机器上执行该脚本. 192.168.1.22 root test 192.168.1.25 root test 192.1 ...
- removeClass() 方法
删除元素的class类别用removeClass() 方法,与addClass()方法对应.具体使用如下: 如果要删除 p 标记是 cls0 的类别,可以使用如下的代码: $("p" ...
- POJ 1696 - Space Ant 凸包的变形
Technorati Tags: POJ,计算几何,凸包 初学计算几何,引入polygon后的第一个挑战--凸包 此题可用凸包算法做,只要把压入凸包的点从原集合中排除即可,最终形成图形为螺旋线. 关于 ...