[USACO07JAN]平衡的阵容Balanced Lineup
[USACO07JAN]平衡的阵容Balanced Lineup
题目描述
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
题解:
一道裸的倍增,维护两个数组保存最大值和最小值,注意查找的时候实际上只需要查找两次就可以了。
然后用最大值和最小值做一下差即为答案。
一下是AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,mmin[][],mmax[][],ansmax,ansmin;
int gi()
{
int ans=,f=;
char i=getchar();
while(i<''||i>''){if(i=='-')f=-;i=getchar();}
while(i>=''&&i<=''){ans=ans*+i-'';i=getchar();}
return ans*f;
}
int main()
{
int i,j;
n=gi();m=gi();
for(i=;i<=n;i++)mmax[i][]=mmin[i][]=gi();
int op=log2(n);
for(i=;i<=op;i++)
{
for(j=;j<=n;j++)
if(j+(<<i)-<=n)
{
mmin[j][i]=min(mmin[j][i-],mmin[j+(<<i-)][i-]);
mmax[j][i]=max(mmax[j][i-],mmax[j+(<<i-)][i-]);
}
}
for(i=;i<=m;i++)
{
int l=gi(),r=gi(),k=r-l+;
k=log2(k);
ansmax=max(mmax[l][k],mmax[r-(<<k)+][k]);
ansmin=min(mmin[l][k],mmin[r-(<<k)+][k]);
printf("%d\n",ansmax-ansmin);
}
return ;
}
[USACO07JAN]平衡的阵容Balanced Lineup的更多相关文章
- 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 ...
- 【洛谷】P2880 [USACO07JAN]平衡的阵容Balanced Lineup(st表)
题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连 ...
- [USACO07JAN]平衡的阵容Balanced Lineup BZOJ 1699
题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连 ...
- 洛谷—— P2880 [USACO07JAN]平衡的阵容Balanced Lineup
https://www.luogu.org/problemnew/show/P2880 题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序 ...
- Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup (ST表模板)
传送门(ST表裸题) ST表是一种很优雅的算法,用于求静态RMQ 数组l[i][j]表示从i开始,长度为2^j的序列中的最大值 注意事项: 1.核心部分: ; (<<j) <= n; ...
- 【luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup】 题解
题目链接:https://www.luogu.org/problemnew/show/P2880 是你逼我用ST表的啊qaq #include <cstdio> #include < ...
- [USACO07JAN]平衡的阵容Balanced Lineup RMQ模板题
Code: #include<cstdio> #include<algorithm> using namespace std; const int maxn = 50000 + ...
随机推荐
- 构建基于虚拟用户的vsftpd服务器
安装: [root@server ~]# yum install -y vsftpd [root@server ~]# rpm -ql vsftpd /etc/logrotate.d/vsftpd / ...
- nginx 轮询模式 nginx_upstream_jvm_route 插件安装
使用nginx_upstream_jvm_route插件的目的是为了保证在轮询机制下的session的共享 前提:源码方式安装nginx.patch命令 1.下载nginx_upstream_jvm_ ...
- tyvj1659救援队——最小生成树
题目:http://www.joyoi.cn/problem/tyvj-1659 想清楚了是非常简单的最小生成树: 1.树中每条边都会被走两边: 2.每个点会走度数遍,起点又多走一遍: 根据以上两条处 ...
- pig ERROR 2997: Encountered IOException. File or directory null does not exist.
grunt> ls 2014-03-30 19:58:31,344 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 2997: Enc ...
- OpenType字体与TrueType字体的区别
TrueType采用几何学中二次B样条曲线及直线来描述字体的外形轮廓,其特点是:TrueType既可以作打印字体,又可以用作屏幕显示:由于它是由指令对字形进行描述,因此它与分辨率无关,输出时总是按照打 ...
- mysql 1069 数据库无法启动解决办法
mysql无缘无故的启动不了了. 在控制台里面用root连接,报错10061. 在服务管理里面启动,报错1069. 在网上找了一些解决方法,删除my.ini之类的,都无效.后来在百度经验里面找到了可行 ...
- js中push(),pop(),unshift(),shift()的用法
js中push(),pop(),unshift(),shift()的用法小结 1.push().pop()和unshift().shift() 这两组同为对数组的操作,并且会改变数组的本身的长度及 ...
- 无法搜索联机扩展 因为尝试与服务器联系 Visual studio 怎么解决?
根目录: devenv.exe.config 编辑: 修改如下即可: <system.net> <defaultProxy useDefaultCredentials="t ...
- JavaScript DOM知识 (一)
特性.方法 类型.返回类型 说明 nodeName String 节点的名字:根据节点类型而定义 nodeValue String 节点的值:根据节点的类型而定义 nodeType Number 节点 ...
- Unity surface shader 2
UV滚动 Shader "Nafio/ScrollUV" { Properties { _Tex("T",2D) = "white" {} ...