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 ...
随机推荐
- [原创] 初识Agile/CMMI/Scrum
一.背景介绍 在朋友(aehyok)的建议下,初步去了解Visual Studio Online,简称VS Online(即原来的 Team Foundation Service,简称TFS) VS ...
- Incorrect integer value: '' for column 'id' at row 1
最近在写个查询 插入语句的时候 我是这么写的 insert into test values('',$row[contentid],'".$tn."'); 结果搞死没插入进去 然 ...
- Laravel 5 基础(十)- 日期,Mutator 和 Scope
在我们前面的解决方案中,直接给 published_at 赋值为当前日期实际上是一个临时解决方案,我们需要设定发布日期,可能是未来2天后才发布,让我们修改这个问题. 首先修改控制器: public f ...
- WinSock编程基础
一.套接字模式 1.阻塞模式 创建套接字时,默认是阻塞模式,对recv函数调用会使程序进入等待状态,知道接收到数据才返回. 2.非阻塞模式: 可以调用ioctlsocke ...
- 八数码难题 (codevs 1225)题解
[问题描述] 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初始状态)和目标布局( ...
- Python 初学(一) 标识符
Python 标识符 在python里,标识符由字母.数字.下划线组成: python中的标识符是区分大小写的: 1)以单下划线开头(_foo)的代表不能直接访问的类属性,需要通过类提供的接口进行访问 ...
- o2o的一些看法
最近一段时间o2o(online to offline)真是越来越来热了,尤其是微信支持支付功能之后.现在随便哪个地方都可以看到二维码,然后手机一扫就可以看到跑到线上看到卖家一些产品信息了.看到这里面 ...
- R Tools for Visual Studio
https://www.visualstudio.com/en-us/features/rtvs-vs.aspx https://www.microsoft.com/en-us/cloud-platf ...
- Hadoop和大数据:60款顶级大数据开源工具
一.Hadoop相关工具 1. Hadoop Apache的Hadoop项目已几乎与大数据划上了等号.它不断壮大起来,已成为一个完整的生态系统,众多开源工具面向高度扩展的分布式计算. 支持的操作系统: ...
- ubuntu 修改ssh远程主机名称,mac开机运行命令,静默方式启动virtual box虚拟机,静默执行run脚本
一.修改主机名 ssh登陆 vi /etc/hostname vi /etc/hosts hostname ulocal (执行这个命令,无须重启服务器) 保证127.0.0.1 的hostname与 ...