【洛谷】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 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连 ...
随机推荐
- TeamTalk源码分析(十一) —— pc客户端源码分析
--写在前面的话 在要不要写这篇文章的纠结中挣扎了好久,就我个人而已,我接触windows编程,已经六七个年头了,尤其是在我读研的三年内,基本心思都是花在学习和研究windows程序上 ...
- 如何制作dll库的API文档,自动生成微软风格的chm文件 Sandcastle Help File Builder 使用方法
当你开发了一个库的时候,就需要给库开发一个api文档,微软提供了一个C#库的自动生成工具.我在使用的过程中记录了相关的信息,以供大家学习和查阅,如有不正之处,欢迎指出. 首先先下载一个软件,下载地址在 ...
- C# 超级狗 二次开发 读写数据 激活验证 存储数据库连接字符串
本文主要讲解如果使用C#语言来对超级狗进行二次开发,如果仅仅是做个激活的功能,可以参照另一篇博客,地址:http://www.cnblogs.com/dathlin/p/8487842.html 如果 ...
- learn go memoization
package main // 参考文章: // https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.12.md i ...
- 20179223《Linux内核原理与分析》第二周学习笔记
第二周实验 本周学习情况: 学习了X86 cpu的几个寄存器及X86汇编指令: movl %eax,%edx edx=eax %表示一个寄存器,把eax内容放入edx,等号相当于把eax赋值给edx, ...
- Git 更安全的强制推送,--force-with-lease
由于 git rebase 命令的存在,强制将提交推送到远端仓库似乎也有些必要.不过都知道 git push --force 是不安全的,这让 git rebase 命令显得有些鸡肋. 本文将推荐 - ...
- svg图片的缩放拖拽
svg是一种不错的矢量图,有时候我们会使用这样的图片来作为展示图,因为它不会因为放大缩小而失真. 好了,不扯淡了,废话少说,直入主题吧. 首先确定你是要深入学习还是要简单的把遇到的小任务解决一下,以后 ...
- 【Netty】netty学习之nio网络编程的模型
[一]NIO服务器编程结构 [二]Netty3.x服务端线程模型
- Kubernetes Helm
Helm is a tool for managing Kubernetes charts. Charts are packages of pre-configured Kubernetes reso ...
- springboot中 简单的SpringMvc全局异常处理
1.全局异常处理类:GlobalExceptionHandler.java package com.lf.exception; import java.util.HashMap; import jav ...