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

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表维护一个最大最小值就可以了
 1 #include <cstdio>
 2
 3 #define max(a,b) (a>b?a:b)
 4 #define min(a,b) (a<b?a:b)
 5
 6 inline void read(int &x)
 7 {
 8     x=0; register char ch=getchar();
 9     for(; ch>‘9‘||ch<‘0‘; ) ch=getchar();
10     for(; ch>=‘0‘&&ch<=‘9‘; ch=getchar()) x=x*10+ch-‘0‘;
11 }
12 const int N(5e5+5);
13 int n,q,t,log2[N];
14 int st[N][20][2];
15
16 int Presist()
17 {
18     read(n),read(q);
19     for(int i=1; i<=n; ++i)
20     {
21         read(st[i][0][0]),
22         st[i][0][1]=st[i][0][0];
23         log2[i]=(1<<t+1==i)?++t:t;
24     }
25     for(int j=1; 1<<j<=n; ++j)
26       for(int i=1; i+(1<<j)<=n+1; ++i)
27           st[i][j][0]=max(st[i][j-1][0],st[i+(1<<j-1)][j-1][0]),
28           st[i][j][1]=min(st[i][j-1][1],st[i+(1<<j-1)][j-1][1]);
29     for(int a,b,c,max_,min_; q--; )
30     {
31         read(a),read(b);
32         c=log2[b-a+1];
33         max_=max(st[a][c][0],st[b-(1<<c)+1][c][0]);
34         min_=min(st[a][c][1],st[b-(1<<c)+1][c][1]);
35         printf("%d\n",max_-min_);
36     }
37     return 0;
38 }
39
40 int Aptal=Presist();
41 int main(int argc,char**argv){;}
时间: 2024-10-01 23:01:04

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

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

题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup RMQ问题:给定一个长度为N的区间,M个询问,每次询问Li到Ri这段区间元素的最大值/最小值. RMQ的高级写法一般有两种,即为线段树(并不很会╥﹏╥...)和ST表(一种利用dp求解区间最值的倍增算法) 定义:maxx[i][j]和minn[i][j]分别表示i到i+2^j-1这段区间的最大值和最小值. 预处理:maxx[i][0]=minn[i][0]=a[i].即i到i区间的最大值.最小值都是a[i]. 状

P2880 [USACO07JAN]平衡的阵容Balanced Lineup

P2880 [USACO07JAN]平衡的阵容Balanced Lineup RMQ RMQ模板题 静态求区间最大/最小值 (开了O2还能卡到rank9) #include<iostream> #include<cstdio> #include<cstring> #include<cctype> using namespace std; template <typename T> inline T min(T &a,T &b) {

洛谷P2879 [USACO07JAN]区间统计Tallest Cow

To 洛谷.2879 区间统计 题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 ≤ H ≤ 1,000,000) of the tallest cow along with th

洛谷 P2879 [USACO07JAN]区间统计Tallest Cow 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=2879 题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told on

洛谷P2878 [USACO07JAN]保护花朵Protecting the Flowers

题目描述 Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. When he returned, he found to his horror that the cluster of cows was in his garden eating his beautiful flowers. Wanting to minimize the subsequent

洛谷 P2878 [USACO07JAN]保护花朵Protecting the Flowers 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=2878 (本来想放bzoj的链接  然而bzoj这道题挂了) [题目描述] 约翰留下他的N(N<=100000)只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小,约翰赶紧采取行动,把牛们送回牛棚. 牛们从1到N编

洛谷 P2879 [USACO07JAN]区间统计Tallest Cow

传送门 题目大意: n头牛,其中最高身高为h,给出r对关系(x,y) 表示x能看到y,当且仅当y>=x并且x和y中间的牛都比 他们矮的时候,求每头牛的最高身高. 题解:贪心+差分 将每头牛一开始都设为最高高度. 每一对关系(x,y),我们将[x+1,y-1]这个区间的身高变为 min(x,y)-1.这样是不对了.因为要维护[x+1,y-1]这个区间里 各个元素的大小关系,所以要将[x+1,y-1]的元素身高都减1. 一开始我是用线段树做的,后来发现题解用的差分. 没有询问的区间修改,差分做就好了

洛谷P4016 负载平衡问题(最小费用最大流)

题目描述 GG 公司有 nn 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 nn 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. 输入输出格式 输入格式: 文件的第 11 行中有 11 个正整数 nn ,表示有 nn 个仓库. 第 22 行中有 nn 个正整数,表示 nn 个仓库的库存量. 输出格式: 输出最少搬运量. 输入输出样例 输入样例#1: 复制 5 17 9 14 16 4 输出样例#1: 复制 11 说明 1 \leq n \leq

洛谷P1290 欧几里德的游戏 数学 博弈论 模拟

洛谷P1290 欧几里德的游戏 数学 博弈论 模拟 这道题我们因为当 x 大于 y 时 你也只能在合法范围 内取 1 个 y 两个 y 也就是说 能取的y大于等于2时,则你本质不同的取法共有两种,此时你必定获胜,因为本质不同,而在最优策略下,则说明胜利者也不同,也就是说这时候你可以决定自己的输赢 ,我们称这种必胜局为 v 局 2.但是如果 v 局后面还有v 局怎么办,这个不必担心,因为先拿到 v局的人,有两种本质不同的取法,也就是说 他可以控制自己下次必定拿到 v 局,这样就 能确保胜利了 所以