TOJ1698: Balanced Lineup
Description
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.
Input
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.
Output
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.
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
Source
输出区间最大最小值之差,RMQ
#include<bits/stdc++.h>
using namespace std;
const int N=;
int dmi[N][],dma[N][],f[N];
int n,q,l,r;
void RMQ_init()
{
for(int j=; (<<j)<=n; j++)
for(int i=; i+j-<=n; i++)
dmi[i][j]=min(dmi[i][j-],dmi[i+(<<(j-))][j-]),dma[i][j]=max(dma[i][j-],dma[i+(<<(j-))][j-]);
}
int RMQ(int l,int r)
{
int k=f[r-l+];
return max(dma[l][k],dma[r-(<<k)+][k])-min(dmi[l][k],dmi[r-(<<k)+][k]);
}
int main()
{
f[]=-;
scanf("%d%d",&n,&q);
for(int i=; i<=n; i++)
scanf("%d",&dma[i][]),dmi[i][]=dma[i][],f[i]=((i&(i-))==)?f[i-]+:f[i-];
RMQ_init();
while(q--)
{
scanf("%d%d",&l,&r);
printf("%d\n",RMQ(l,r));
}
return ;
}
线段树
#include<bits/stdc++.h>
using namespace std;
#define lson l,mi,rt<<1
#define rson mi+1,r,rt<<1|1
struct T
{
int ma,mi,l,r;
}tree[];
int h[];
int ma,mi;
void build(int l,int r,int rt)
{
tree[rt].l=l,tree[rt].r=r;
if(l==r)
{
tree[rt].ma=tree[rt].mi=h[l];
return;
}
int mi=(l+r)>>;
build(lson);
build(rson);
tree[rt].ma=max(tree[rt<<].ma,tree[rt<<|].ma);
tree[rt].mi=min(tree[rt<<].mi,tree[rt<<|].mi);
}
void findma(int l,int r,int rt)
{
if(tree[rt].l==l&&tree[rt].r==r)
{
if(tree[rt].ma>ma)ma=tree[rt].ma;
return;
}
int mi=(tree[rt].l+tree[rt].r)>>;
if(mi>=r)
findma(l,r,rt<<);
else if(mi<l)
findma(l,r,rt<<|);
else
findma(lson),findma(rson);
} void findmi(int l,int r,int rt)
{
if(tree[rt].l==l&&tree[rt].r==r)
{
if(tree[rt].mi<mi)mi=tree[rt].mi;
return;
}
int mi=(tree[rt].l+tree[rt].r)>>;
if(mi>=r)
findmi(l,r,rt<<);
else if(mi<l)
findmi(l,r,rt<<|);
else
findmi(lson),findmi(rson);
}
int main()
{
int n,q,i,a,b;
scanf("%d%d",&n,&q);
for(i=;i<=n;i++)
scanf("%d",&h[i]);
build(,n,);
while(q--)
{
ma=;
mi=1e9;
scanf("%d%d",&a,&b);
findma(a,b,),findmi(a,b,);
printf("%d\n",ma-mi);
}
return ;
}
TOJ1698: Balanced Lineup的更多相关文章
- poj 3264:Balanced Lineup(线段树,经典题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32820 Accepted: 15447 ...
- Balanced Lineup(树状数组 POJ3264)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 40493 Accepted: 19035 Cas ...
- 三部曲一(数据结构)-1022-Gold Balanced Lineup
Gold Balanced Lineup Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Othe ...
- poj 3264 Balanced Lineup (RMQ)
/******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...
- poj3264 - Balanced Lineup(RMQ_ST)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 45243 Accepted: 21240 ...
- bzoj 1637: [Usaco2007 Mar]Balanced Lineup
1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer John ...
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
- POJ3264 Balanced Lineup
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 44720 Accepted: 20995 ...
- POJ 3274 Gold Balanced Lineup
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...
随机推荐
- 四道java语言练习基础题:
一.==符的使 首先看一段比较有意思的代码 Integer a = 1000,b=1000; Integer c = 100,d=100; public void mRun(final String ...
- 洛谷 P1079 Vigenère 密码
题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国南 ...
- 洛谷 P2905 [USACO08OPEN]农场危机Crisis on the Farm
题目描述 约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每一堆里,30只奶牛一只踩在另一只的背上,叠成一座牛塔.牧场 里还有M(1 < ...
- LibreOJ #2037. 「SHOI2015」脑洞治疗仪
线段树区间合并问题 恶心... 屠龙宝刀点击就送 #include <cstdio> #define N 200005 struct Segment { int l,r,mid,sum,l ...
- 【TensorFlow入门完全指南】模型篇·线性回归模型
首先呢,进行import,对于日常写代码来说,第二行经常写成:import numpy as np,这样会更加简洁.第三行import用于绘图. 定义了学习率.迭代数epoch,以及展示的学习步骤,三 ...
- iperf安装与使用
从官网下载相应版本. https://iperf.fr/iperf-download.php centos7 安装 rpm -i iperf3-3.1.3-1.fc24.x86_64.rpm ubun ...
- UIViewController 的 edgesForExtendedLayout、automaticallyAdjustsScrollViewInsets属性
1.有时你命名设置了某控件的y坐标为0,确总是被导航栏遮挡住,如下: UILabel *label = [[UILabel alloc] init]; label.text = @"请 ...
- JAVA实现webSocket网页聊天室
一.什么是webSocket WebSocket 是一种网络通信协议,是持久化协议.RFC6455 定义了它的通信标准. WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全 ...
- Convert HTML Entities-freecodecamp算法题目
Convert HTML Entities 1.要求 将字符串中的字符 &.<.>." (双引号), 以及 ' (单引号)转换为它们对应的 HTML 实体. 2.思路 利 ...
- Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery)
Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery) 体验 冒号搜索 1. 获取谷歌搜索api 谷歌搜索api教程 2. 后台调用 程序入口 main.go // ...