【洛谷】P2880 [USACO07JAN]平衡的阵容Balanced Lineup(st表)
题目背景
题目描述:
每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的选择和所有牛的身高 (1 <= 身高 <= 1,000,000). 他想知道每一组里面最高和最低的牛的身高差别.
输入:
第1行:N,Q
第2到N+1行:每头牛的身高
第N+2到N+Q+1行:两个整数A和B,表示从A到B的所有牛。(1<=A<=B<=N)
输出:
输出每行一个数,为最大数与最小数的差
题目描述
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头牛,每头牛的高度不同,我们需要找出最高的牛和最低的牛的高度差。
输入输出格式
输入格式:
Line 1: Two space-separated integers, N and Q.
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.
输出格式:
Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.
输入输出样例
6 3
1
7
3
4
2
5
1 5
4 6
2 2
6
3
0
----------------------------------------------------------------
分析:用st表分别统计区间最大值与最小值,查询时减一下就可以了。我的代码260ms。
#include <cstdio>
#include <algorithm>
using namespace std;
int maxv[][],minv[][],a[],n,q;
void RMQ_init()
{
for(int i=;i<n;i++)
{
maxv[i][]=a[i];
minv[i][]=a[i];
}
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)<=n;i++)
{
maxv[i][j]=max(maxv[i][j-],maxv[i+(<<(j-))][j-]);
minv[i][j]=min(minv[i][j-],minv[i+(<<(j-))][j-]);
}
}
int RMQ(int l,int r)
{
int k=,bigger,smaller;
while(<<(k+)<=r-l+) k++;
bigger=max(maxv[l][k],maxv[r-(<<k)+][k]);
smaller=min(minv[l][k],minv[r-(<<k)+][k]);
return bigger-smaller;
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<n;i++) scanf("%d",&a[i]);
RMQ_init();
for(int i=;i<q;i++)
{
int a,b;
scanf("%d%d",&a,&b);//我的编号是从0开始的
printf("%d\n",RMQ(a-,b-));
}
return ;
}
完成了【洛谷】P2880 [USACO07JAN]平衡的阵容Balanced Lineup,打卡
【洛谷】P2880 [USACO07JAN]平衡的阵容Balanced Lineup(st表)的更多相关文章
- 洛谷—— P2880 [USACO07JAN]平衡的阵容Balanced Lineup
https://www.luogu.org/problemnew/show/P2880 题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序 ...
- 洛谷P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解
题目链接: https://www.luogu.org/problemnew/show/P2880 分析: ST表实现即可,一个最大值数组和最小值数组同时维护 代码: #include<cstd ...
- ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup
题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring&g ...
- P2880 [USACO07JAN]平衡的阵容Balanced Lineup(RMQ的倍增模板)
题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup RMQ问题:给定一个长度为N的区间,M个询问,每次询问Li到Ri这段区间元素的最大值/最小值. RMQ的高级写法一般 ...
- P2880 [USACO07JAN]平衡的阵容Balanced Lineup
P2880 [USACO07JAN]平衡的阵容Balanced Lineup RMQ RMQ模板题 静态求区间最大/最小值 (开了O2还能卡到rank9) #include<iostream&g ...
- 【luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup】 题解
题目链接:https://www.luogu.org/problemnew/show/P2880 是你逼我用ST表的啊qaq #include <cstdio> #include < ...
- Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup (ST表模板)
传送门(ST表裸题) ST表是一种很优雅的算法,用于求静态RMQ 数组l[i][j]表示从i开始,长度为2^j的序列中的最大值 注意事项: 1.核心部分: ; (<<j) <= n; ...
- [USACO07JAN]平衡的阵容Balanced Lineup
[USACO07JAN]平衡的阵容Balanced Lineup 题目描述 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) a ...
- [USACO07JAN]平衡的阵容Balanced Lineup BZOJ 1699
题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连 ...
随机推荐
- c++下使用邮槽实现进程间通信
Windows API提供了邮槽和命名管道两种机制来实现进程间通信,在这里使用C++实现邮槽. 邮槽是Windows提供的一种进程间单向通信的机制,进程中的一方只能读取(或写入)数据,而另一方只能写入 ...
- PHPStorm 使用正则批量查询替换并自动转换大小写的方法
PHPStorm 的项目查询替换功能那是非常非常强大的, 速度也很快, 配合正则更加灵活强大. 一般的正则查询替换没什么太多好说的, 这里主要说说比较少用的 大小写自动转换的问题, 也是比较少用但很有 ...
- 怎么样编译DeepMind?
可以通过下面的文章来编译著名的deepmind系统. How to build DeepMind LabDeepMind Lab uses Bazel as its build system. Its ...
- 《Tomcat内核设计剖析》勘误表
<Tomcat内核设计剖析>勘误表 书中第95页图request部分印成了reqiest. 书中第311页两个tomcat3,其中一个应为tomcat4. 书中第5页URL应为URI. 书 ...
- UE4 Sequencer的事件调用
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/73554691 作者:car ...
- HDU 1806
http://acm.hdu.edu.cn/showproblem.php?pid=1806 非常玄妙的rmq问题,这个st算法有点神 #include <iostream> #inclu ...
- 安装redis-3.2.10单节点
前段时间安装好的redis,今天用脚本安装的时候突然出现版本异常的问题,所以更新一篇为大家提供参考 本次安装在CentOS6.5,采用的redis-3.2.10,最新的redis-4.0.1安装同样适 ...
- getPropertyValue (实现 js框架中 css 的最终调用的函数)
,取得元素最终计算出的css 样式 var a = document.getElementById("content"); alert("style "+ ...
- 每天一个linux命令(文件操作):【转载】whiereis命令
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和find相比,whereis查找的速度非 ...
- HTTP错误类别
http_status_bad_request (400) the request could not be processed by the server due to invalid syntax ...