[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.

输入输出样例

输入样例#1:

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

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的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. 洛谷—— P2880 [USACO07JAN]平衡的阵容Balanced Lineup

    https://www.luogu.org/problemnew/show/P2880 题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序 ...

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

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

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

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

  9. [USACO07JAN]平衡的阵容Balanced Lineup RMQ模板题

    Code: #include<cstdio> #include<algorithm> using namespace std; const int maxn = 50000 + ...

随机推荐

  1. OpenCV——Skewing

    // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...

  2. BrowserSync(省时的浏览器同步测试工具)

    第一步:安装node 第二步:安装BrowserSync npm install -g browser-sync 第三部:启动BrowserSync 假如我在D盘建立一个文件test,里面分别包括in ...

  3. TCP/IP--VLSM

    ##########################VLSM################### 实际案例: 192.168.1.0/28 有几个网络号:2的4次方:16个网络号 : 有几个主机号: ...

  4. css3渐变gradient

    参考: http://www.w3cplus.com/content/css3-gradient

  5. 网络工具 NetCat

    http://netcat.sourceforge.net/ windows 版本 https://joncraton.org/blog/46/netcat-for-windows/ https:// ...

  6. bzoj4453

    单调栈+set+后缀数组 一道奇妙的题 这道题如果对于每个询问$r$是固定的,那么就很简单了,可惜并不是 由于r会变化,那么对于两个子串$[i...r],[j...r]$,他们的大小关系随着r的变化也 ...

  7. nginx中有关命令和日志切割,配置文件加载的详细阐述

    一.Nginx简介 Nginx (“engine x”) 是俄罗斯人Igor Sysoev(塞索耶夫)编写的一款高性能的 HTTP 和反向代理服务器.Nginx 已经在俄罗斯最大的门户网站── Ram ...

  8. 关于REST的一些想法

    REST and RESTful 最近入手了REST,谈谈自己的体会. 所谓REST, 我觉得是一种网址的设计风格.过去我们用Struts 或Spring MVC 时从来没有考虑过URL的设计风格.所 ...

  9. POJ-2718

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12158   Accepted: 3 ...

  10. 文本情感分析(一):基于词袋模型(VSM、LSA、n-gram)的文本表示

    现在自然语言处理用深度学习做的比较多,我还没试过用传统的监督学习方法做分类器,比如SVM.Xgboost.随机森林,来训练模型.因此,用Kaggle上经典的电影评论情感分析题,来学习如何用传统机器学习 ...