POJ 3264 区间最大最小值Sparse_Table算法
题目链接:http://poj.org/problem?id=3264
Balanced Lineup
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 47515 | Accepted: 22314 | |
| 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 题目大意:给定一个数组,然后给一个区间[a,b],输出从a到b中的最大值和最小值的差。
RMQ 和 线段树 均可
RMQ代码:
#include <stdio.h>
#define MAX(a,b) (a>b ? a:b)
#define MIN(a,b) (a>b ? b:a)
#define N 50010
int a[N],ma[N][],mi[N][]; void ST(int n)
{
int i,j;
for(i=;i<=n;i++)
mi[i][]=ma[i][]=a[i]; for (j = ; (<<j) <= n; j ++)
for (i = ; i + (<<j)- <= n; i ++)
{
ma[i][j]=MAX(ma[i][j-],ma[i+(<<(j-))][j-]);
mi[i][j]=MIN(mi[i][j-],mi[i+(<<(j-))][j-]);
}
} int rmq(int a,int b)
{
int k = ;
while((<<(k+)) <= b-a+) k++;
return MAX(ma[a][k],ma[b-(<<k)+][k])-MIN(mi[a][k],mi[b-(<<k)+][k]);
} int main()
{
int n,i,q,x,y;
while(scanf("%d %d",&n,&q)!=-)
{
for(i=;i<=n;i++)
scanf("%d",a+i);
ST(n);
for(i=;i<=q;i++)
{
scanf("%d %d",&x,&y);
printf("%d\n",rmq(x,y));
}
}
return ;
}
POJ 3264 区间最大最小值Sparse_Table算法的更多相关文章
- poj 3264 区间最大最小值 RMQ问题之Sparse_Table算法
Balanced Lineup Time Limit: 5000 MS Memory Limit: 0 KB 64-bit integer IO format: %I64d , %I64u Java ...
- POJ 3264 Balanced Lineup RMQ ST算法
题意:有n头牛,编号从1到n,每头牛的身高已知.现有q次询问,每次询问给出a,b两个数.要求给出编号在a与b之间牛身高的最大值与最小值之差. 思路:标准的RMQ问题. RMQ问题是求给定区间内的最值问 ...
- POJ 3171 区间覆盖最小值&&线段树优化dp
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4715 Accepted: 1590 D ...
- poj 3264 Balanced Lineup【RMQ-ST查询区间最大最小值之差 +模板应用】
题目地址:http://poj.org/problem?id=3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0分析:标 ...
- POJ - 3264 线段树模板题 询问区间最大最小值
这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...
- 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模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
- poj 3264 & poj 3468(线段树)
poj 3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0 求任一区间的最大值和最小值的差 #include<io ...
随机推荐
- 制作Linux下程序安装包——使用脚本打包bin、run等安装包
制作简单的安装包的时候可以简单的用cat命令连接两个文件,然后头部是脚本文件,执行的时候把下面的文件分解出来就行了.一般这个后部分的文件是个压缩 包,那样,就能够打包很多文件了,在脚本中解压出来即可. ...
- MySql插入记录时判断
我们在开发数据库相关的逻辑过程中, 经常检查表中是否已经存在这样的一条记录, 如果存在则更新或者不做操作, 如果没有存在记录,则需要插入一条新的记录. 这样的逻辑固然可以通过两条sql语句完成. SE ...
- UITextView实现图文混排效果
用UITextView实现图文混排效果的展示,首先要禁用UITextView的编辑功能,将属性editable设置为NO 1.首先创建一个NSTextAttachment对象,这个对象有一个image ...
- Visual studio 2013安装及单元测试
vs安装过程 单元测试: 创建c#类库 创建单元测试 测试结果
- Linux登陆和欢迎信息修改
edit file: /etc/issue,/etc/motd 这样还改不了,生成的脚本在目录/etc/update-motd.d/中: 假如要打开一个文本文件,可以修改10-help-text: ( ...
- Vm下 linux与windowsxp文件共享的方法
我的PC Operating System Host是Windows XP,Guest是Linux,virtualPC是VMware workstation.方法介绍大全请参见:Windows与Vmw ...
- sql server日志不能shrink或truncate
Backup log [dbxxx] with truncate_only sql server 2008之后不支持此操作,需要改为: BACKUP LOG dbxxx TO DISK='NUL:' ...
- C# 对MongoDB 进行增删改查的简单操作 (转)
运用到的MongoDB支持的C#驱动,当前版本为1.6.0 下载地址:https://github.com/mongodb/mongo-csharp-driver/downloads 1,连接数据库 ...
- ajax 中的一些方法应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- (转)SQLLite数据操作
原文:http://dreamboy.blog.51cto.com/3180937/722352 SQLLite数据操作 一般数据采用的固定的静态数据类型,而SQLite采用的是动态数据类型,会根据存 ...