poj 3264 Balanced Lineup 区间极值RMQ
题目链接:http://poj.org/problem?id=3264
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.
题意描述:n个数排成一排形成数组,Q个询问,每个询问给出两个数a和b,求出数组中[a,b]里最大值和最小值的差。
算法分析:可以用线段树做,在这里介绍RMQ算法。
RMQ:预处理O(nlogn),然后每次查询O(1)。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#define inf 0x7fffffff
using namespace std;
const int maxn=+; int n,q;
int an[maxn],dmax[maxn][],dmin[maxn][]; void RMQ_init()
{
for (int i= ;i<=n ;i++) dmax[i][]=dmin[i][]=an[i];
for (int j= ;(<<j)<=n ;j++)
{
for (int i= ;i+(<<j)-<=n ;i++)
{
dmax[i][j]=max(dmax[i][j-],dmax[i+(<<(j-))][j-]);
dmin[i][j]=min(dmin[i][j-],dmin[i+(<<(j-))][j-]);
}
}
} int RMQ(int L,int R)
{
int k=;
while ((<<(k+))<=R-L+) k++;
int maxnum=max(dmax[L][k],dmax[R-(<<k)+][k]);
int minnum=min(dmin[L][k],dmin[R-(<<k)+][k]);
return maxnum-minnum;
} int main()
{
while (scanf("%d%d",&n,&q)!=EOF)
{
for (int i= ;i<=n ;i++) scanf("%d",&an[i]);
RMQ_init();
int a,b;
for (int i= ;i<q ;i++)
{
scanf("%d%d",&a,&b);
printf("%d\n",RMQ(a,b));
}
}
return ;
}
poj 3264 Balanced Lineup 区间极值RMQ的更多相关文章
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- POJ 3264 Balanced Lineup 区间最值
POJ3264 比较裸的区间最值问题.用线段树或者ST表都可以.此处我们用ST表解决. ST表建表方法采用动态规划的方法, ST[I][J]表示数组从第I位到第 I+2^J-1 位的最值,用二分的思想 ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
- POJ 3264 Balanced Lineup 【ST表 静态RMQ】
传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total S ...
- POJ - 3264 Balanced Lineup (RMQ问题求区间最值)
RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就 ...
- poj 3264 Balanced Lineup (RMQ)
/******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...
- POJ 3264 Balanced Lineup(RMQ)
点我看题目 题意 :N头奶牛,Q次询问,然后给你每一头奶牛的身高,每一次询问都给你两个数,x y,代表着从x位置上的奶牛到y位置上的奶牛身高最高的和最矮的相差多少. 思路 : 刚好符合RMQ的那个求区 ...
- poj 3264 Balanced Lineup(RMQ裸题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 43168 Accepted: 20276 ...
- POJ 3264 Balanced Lineup 【线段树/区间最值差】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 62103 Accepted: 29005 Cas ...
随机推荐
- Asp.NET MVC 中使用 SignalR 实现推送功能
一,简介Signal 是微软支持的一个运行在 Dot NET 平台上的 html websocket 框架.它出现的主要目的是实现服务器主动推送(Push)消息到客户端页面,这样客户端就不必重新发送请 ...
- 仿淘宝颜色属性选择展示代码(jQuery)
模仿淘宝商品选择颜色和尺寸的效果,即选择商品颜色和尺寸的时候,把选择的颜色和尺寸放到一个页面容器里面,不足之处,还望指教. <!DOCTYPE HTML> <html lang=&q ...
- SequoiaDB的数据分区操作
在SequoiaDB集群环境中,用户往往将数据存放在不同的逻辑节点与物理节点中,以达到并行计算的目的. 分区:把包含相同数据的一组数据节点叫一个分区,如上图绿色方块组成三个分区. 分区键:切分时,所依 ...
- ASP.net UrlRewrite的防盗链功能
ASP.net中如何实现基于UrlRewrite的防盗链. ASP.net中最快实现UrlRewrite的方法这篇文章中说了如何做UrlRewrite,那只是一个最简单的应用 其实利用UrlRewri ...
- 13)Java static
1.static变量 按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的区别是: ...
- Laravel 5 基础(十一)- Eloquent 关系
一个用户可能有多个文章,一个文章是某个用户书写的,这就是关系.同样文章中可能包含多个 TAG,而一个 TAG 可能关联多个文章. 在项目中,我们已经有了 User.php,也就是用户模型,查看一下,相 ...
- reader,字符流
1. public class Demo1 { public static void main(String[] args) throws IOException { File file = new ...
- STM32F0_新建软件工程详细过程
前言 由于ST公司推出比STM32F1性价比更高的F0芯片,现在市面上F0芯片的占有率也非常高.F0芯片属于M0内核,主频48M(当然,可以超频的,但尽量不要超的太多),资源大小可根据项目需求来选型. ...
- Python初学者笔记(4)-简单的通讯录
要求: 编写一个简单的通讯录 1.通讯录包含至少包含姓名.电话号码.电子邮箱:2.通讯录的信息能够保存在本地磁盘:3.通讯录查找特定人员的信息:4.通讯录能够修改特定人员的信息:5.通讯录能够删除特定 ...
- rails笔记
rake -T 列出全部taskconfig.active_record.schema_format = :sql #remove the old db/schema.rb file, create ...