https://www.luogu.org/problemnew/show/P2880

题目背景

题目描述:

每天,农夫 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.

输入输出样例

输入样例#1: 复制

6 3
1
7
3
4
2
5
1 5
4 6
2 2
输出样例#1: 复制

6
3
0 ST表维护一个最大最小值就可以了
 #include <cstdio>

 #define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b) inline void read(int &x)
{
x=; register char ch=getchar();
for(; ch>''||ch<''; ) ch=getchar();
for(; ch>=''&&ch<=''; ch=getchar()) x=x*+ch-'';
}
const int N(5e5+);
int n,q,t,log2[N];
int st[N][][]; int Presist()
{
read(n),read(q);
for(int i=; i<=n; ++i)
{
read(st[i][][]),
st[i][][]=st[i][][];
log2[i]=(<<t+==i)?++t:t;
}
for(int j=; <<j<=n; ++j)
for(int i=; i+(<<j)<=n+; ++i)
st[i][j][]=max(st[i][j-][],st[i+(<<j-)][j-][]),
st[i][j][]=min(st[i][j-][],st[i+(<<j-)][j-][]);
for(int a,b,c,max_,min_; q--; )
{
read(a),read(b);
c=log2[b-a+];
max_=max(st[a][c][],st[b-(<<c)+][c][]);
min_=min(st[a][c][],st[b-(<<c)+][c][]);
printf("%d\n",max_-min_);
}
return ;
} int Aptal=Presist();
int main(int argc,char**argv){;}

洛谷—— P2880 [USACO07JAN]平衡的阵容Balanced Lineup的更多相关文章

  1. 洛谷P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解

    题目链接: https://www.luogu.org/problemnew/show/P2880 分析: ST表实现即可,一个最大值数组和最小值数组同时维护 代码: #include<cstd ...

  2. 【洛谷】P2880 [USACO07JAN]平衡的阵容Balanced Lineup(st表)

    题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连 ...

  3. ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup

    题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring&g ...

  4. P2880 [USACO07JAN]平衡的阵容Balanced Lineup(RMQ的倍增模板)

    题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup RMQ问题:给定一个长度为N的区间,M个询问,每次询问Li到Ri这段区间元素的最大值/最小值. RMQ的高级写法一般 ...

  5. P2880 [USACO07JAN]平衡的阵容Balanced Lineup

    P2880 [USACO07JAN]平衡的阵容Balanced Lineup RMQ RMQ模板题 静态求区间最大/最小值 (开了O2还能卡到rank9) #include<iostream&g ...

  6. 【luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2880 是你逼我用ST表的啊qaq #include <cstdio> #include < ...

  7. Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup (ST表模板)

    传送门(ST表裸题) ST表是一种很优雅的算法,用于求静态RMQ 数组l[i][j]表示从i开始,长度为2^j的序列中的最大值 注意事项: 1.核心部分: ; (<<j) <= n; ...

  8. [USACO07JAN]平衡的阵容Balanced Lineup

    [USACO07JAN]平衡的阵容Balanced Lineup 题目描述 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) a ...

  9. [USACO07JAN]平衡的阵容Balanced Lineup BZOJ 1699

    题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连 ...

随机推荐

  1. ubuntu 16.04下如何打造 sublime python编程环境

    一.安装python3     ubuntu自身是安装python2的,例如在ubuntu 16.04中安装的就是python2.7.但我想在python3的环境下进行开发所以就要安装python3. ...

  2. 爬虫制作入门学习笔记2:[转]python爬虫实例项目大全

    WechatSogou [1]- 微信公众号爬虫.基于搜狗微信搜索的微信公众号爬虫接口,可以扩展成基于搜狗搜索的爬虫,返回结果是列表,每一项均是公众号具体信息字典. DouBanSpider [2]- ...

  3. LeetCode(289)Game of Life

    题目 According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cel ...

  4. Linux学习-登录档的轮替(logrotate)

    rsyslogd 利用的是 daemon 的方式来启动的, 当有需求的时候立刻就会被执行的,但是 logrotate 却是在规定的时间到了之后才来进行登录档的轮 替, 所以这个 logrotate 程 ...

  5. WPF触控程序开发(二)——整理的一些问题

    上一篇(WPF触控程序开发)介绍了几个比较不错的资源,比较基础.等到自己真正使用它们时,问题就来了,现把我遇到的几个问题罗列下,大家如有遇到其他问题或者有什么好的方法还望赐教. 问题1.如何获取触控点 ...

  6. Setting title-center on "< h1> " element on Android does not work, fix

    app.scss: h1.title-center{ text-align: center!important; }

  7. [转] 彻底搞懂word-break、word-wrap、white-space

    white-space.word-break.word-wrap(overflow-wrap)估计是css里最基本又最让人迷惑的三个属性了,我也是用了n次都经常搞混,必须系统整理一下,今天我们就把这三 ...

  8. C++异常安全的赋值运算符重载 【微软面试100题 第五十五题】

    题目要求: 类CMyString的声明如下: class CMyString { public: CMyString(char *pData=NULL); CMyString(const CMyStr ...

  9. Jmeter Cluster

    Jmeter 是开源软件,100%纯java应用程序,专门为负载测试和性能测试. Jmeter的特性包括: 1.负载测试和性能测试许多不同的服务器/协议类型: Web - HTTP, HTTPS SO ...

  10. AutoEncoder and DenoiseAutoEncoder

    AutoEncoder and DenoiseAutoEncoder 第一部分 首先我们将实现一个如上图结构的最简单的AutoEncoder. 加载数据 在这里,我们使用MNIST手写数据集来进行实验 ...