POJ 3264 Balanced Lineup(RMQ_ST)
id=3264
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
PS:
百度百科RMQ:http://baike.baidu.com/view/1536346.htm?fr=aladdin
RMQ:http://blog.csdn.net/zztant/article/details/8535764
代码例如以下:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
const int MAXN = 100117;
int n,query;
int num[MAXN]; int F_Min[MAXN][20],F_Max[MAXN][20]; void Init()
{
for(int i = 1; i <= n; i++)
{
F_Min[i][0] = F_Max[i][0] = num[i];
} for(int i = 1; (1<<i) <= n; i++) //按区间长度递增顺序递推
{
for(int j = 1; j+(1<<i)-1 <= n; j++) //区间起点
{
F_Max[j][i] = max(F_Max[j][i-1],F_Max[j+(1<<(i-1))][i-1]);
F_Min[j][i] = min(F_Min[j][i-1],F_Min[j+(1<<(i-1))][i-1]);
}
}
} int Query_max(int l,int r)
{
int k = (int)(log(double(r-l+1))/log((double)2));
return max(F_Max[l][k], F_Max[r-(1<<k)+1][k]);
} int Query_min(int l,int r)
{
int k = (int)(log(double(r-l+1))/log((double)2));
return min(F_Min[l][k], F_Min[r-(1<<k)+1][k]);
} int main()
{
int a,b;
scanf("%d %d",&n,&query);
for(int i = 1; i <= n; i++)
scanf("%d",&num[i]);
Init();
while(query--)
{
scanf("%d %d",&a,&b);
//printf("区间%d到%d的最大值为:%d\n",a,b,Query_max(a,b));
//printf("区间%d到%d的最小值为:%d\n",a,b,Query_min(a,b));
printf("%d\n",Query_max(a,b)-Query_min(a,b));
}
return 0;
}
POJ 3264 Balanced Lineup(RMQ_ST)的更多相关文章
- POJ 3264 Balanced Lineup(RMQ)
点我看题目 题意 :N头奶牛,Q次询问,然后给你每一头奶牛的身高,每一次询问都给你两个数,x y,代表着从x位置上的奶牛到y位置上的奶牛身高最高的和最矮的相差多少. 思路 : 刚好符合RMQ的那个求区 ...
- poj 3264:Balanced Lineup(线段树,经典题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32820 Accepted: 15447 ...
- POJ - 3264——Balanced Lineup(入门线段树)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 68466 Accepted: 31752 ...
- POJ 3264 Balanced Lineup(ST模板)
链接:http://poj.org/problem?id=3264 题意:给n个数,求一段区间L,R的最大值 - 最小值,Q次询问 思路:ST表模板,预处理区间最值,O(1)复杂度询问 AC代码: # ...
- poj 3264 Balanced Lineup (线段树)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 42489 Accepted: 20000 ...
- poj 3264 Balanced Lineup(RMQ裸题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 43168 Accepted: 20276 ...
- POJ 题目3264 Balanced Lineup(RMQ)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 39046 Accepted: 18291 ...
- poj3264 - Balanced Lineup(RMQ_ST)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 45243 Accepted: 21240 ...
- POJ 3264 Balanced Lineup (线段树)
Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...
随机推荐
- Socket网络编程初探
http://altboy.blog.51cto.com/5440160/1921720 客户端/服务器架构 即C/S架构,其实web服务在某种意义上也算是C/S架构 一个特点是服务器端持续运行对外提 ...
- python基础一 day2
内容: 3%%s 输出:3%s 后面的全部转义 结果: 如果是因为执行break语句导致循环提前结束,就不会执行else. 单位换算: 编码方式: ascii unicode u ...
- 爬虫学习之第一次获取网页内容及BeautifulSoup处理
from urllib.request import urlopen from urllib.request import HTTPError from bs4 import BeautifulSou ...
- spring中注解的实现原理
@Autowired和@Resource的区别: 在Java中使用@Autowired和@Resource注解进行装配,这两个注解分别是:1.@Autowired按照默认类型(类名称)装配依赖对象,默 ...
- java缓存的使用
缓存1,缓存的定义与作用2,缓存的使用范围(命中率高.高访问量)3,缓存策略(命中率,最大元素,清空策略);4,缓存介质(内存缓存,硬盘缓存,数据库缓存)(本地缓存(ehcache,oscache)与 ...
- C和C++中动态链接库的创建和链接(原创,装载请注明原处)
C和C++中动态链接库的创建和链接 1.创建DLL(动态链接库)-C++方式 1.创建DLL(动态链接库-C++方式) 1.在VS(以VS2017为例)中创建DLL动态链接库. 解决方案名称为:MyD ...
- 牛客OI赛制测试赛2 D 星光晚餐
链接:https://www.nowcoder.com/acm/contest/185/D来源:牛客网 题目描述 Johnson和Nancy要在星光下吃晚餐.这是一件很浪漫的事情. 为了增加星光晚餐那 ...
- Java中的线程安全和非线程安全以及锁的几个知识点
1. 线程安全就是多线程访问时,采用了加锁机制,当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读取完,其他线程才可使用.不会出现数据不一致或者数据污染. 线程不安全就是不提供 ...
- Linux制作本地yum
首先在vm上安装centos 1.首先查看挂载光盘的位置:#df -h [root@lang ~]# df -hFilesystem Size Used Avai ...
- Shell函数和正则表达式
1. shell函数 shell中允许将一组命令集合或语句形成一段可用代码,这些代码块称为shell函数.给这段代码起个名字称为函数名,后续可以直接调用该段代码. 格式: func() { #指定 ...