poj 3264

http://poj.org/problem?id=3264

洛谷 P2880

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

题目描述

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 RMQ的裸题,直接用ST算法AC就可以。
跑一遍最大值再来一遍最小值。
AC Code:
#include<cstdio>
#include<algorithm>
using namespace std;
int n,q;
int lg[];
int f[][],p[][];
int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
{
scanf("%d",&f[i][]);
p[i][]=f[i][];
}
for(int i=;i<=n;i++)
lg[i]=lg[i>>]+;
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
{
f[i][j]=max(f[i][j-],f[i+(<<(j-))][j-]);
p[i][j]=min(p[i][j-],p[i+(<<(j-))][j-]);
}
for(int i=;i<=q;i++)
{
int x,y;
scanf("%d%d",&x,&y);
int k=lg[y-x+];
printf("%d\n",max(f[x][k],f[y-(<<k)+][k])-min(p[x][k],p[y-(<<k)+][k]));
}
return ;
}

USACO Balanced Lineup的更多相关文章

  1. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  2. Balanced Lineup(树状数组 POJ3264)

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 40493 Accepted: 19035 Cas ...

  3. poj3264 - Balanced Lineup(RMQ_ST)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 45243   Accepted: 21240 ...

  4. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

  5. POJ3264 Balanced Lineup

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 44720   Accepted: 20995 ...

  6. POJ 3274 Gold Balanced Lineup

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...

  7. 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...

  8. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  9. [POJ] 3264 Balanced Lineup [ST算法]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

随机推荐

  1. Python中xlrd和xlwt模块使用方法----》》数据库数据导出(之一)

    xlrd模块实现对excel文件内容读取,xlwt模块实现对excel文件的写入. (1) 打开excel文件并获取所有sheet >>> import xlrd >>& ...

  2. C语言——初见指针

    指针 指针是存放变量地址的变量. 由此我们可以知道了,指针存放的是变量地址且只能应用于内存中对象,指针本身也是一个变量.但是指针不仅仅可以存储变量地址,还可以是其他例如函数指针. C语言程序运行时,内 ...

  3. python实现语音信号处理常用度量方法

    信噪比(SNR) 有用信号功率与噪声功率的比(此处功率为平均功率),也等于幅度比的平方 $$SNR(dB)=10\log_{10}\frac{\sum_{n=0}^{N-1}s^2(n)}{\sum_ ...

  4. Redis的Sorted-Sets排行榜功能实现

    Redis的ZSet排行榜功能实现 1. 功能需求 类似给用户n张图片, 用户左滑不喜欢右滑喜欢.所以每个用户就会有一些喜欢的图片集合和不喜欢的图片集合.现在我们要做一个将按照一个算法将喜欢的排到前面 ...

  5. VBA基础 - 函数和模块

    概要 对于一般的 VBA 程序来说, 可能一个或几个函数就行了. 毕竟, VBA 只是作为 excel 的辅助工具来用的. 但是, 随着 VBA 写的越来越多, 用个工程来管理就有必要了, 而一个代码 ...

  6. JSONObject解析json数据

    首先先看一下我们要解析的json数据是什么样子的: 代码: String url="http://113.57.190.228:8001/Web/Report/GetBigMSKReport ...

  7. 使用NumPy、Numba的简单使用(一)

    Numpy是python的一个三方库,主要是用于计算的,数组的算数和逻辑运算.与线性代数有关的操作. 很多情况下,我们可以与SciPy和 Matplotlib(绘图库)一起使用.来替代MatLab,下 ...

  8. C# Large Files MD5 C# 获取大文件MD5

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. GitHub中文社区

    今天在打开GitHub的时候,使用了bing.com搜索,输入GitHub进行搜索链接,排名第一的为GitHub中文社区,点击去发现这个社区还可以,我们看看GitHub中文社区有哪些好的地方 GitH ...

  10. ASP.NET Core appsettings.json 文件

    ASP.NET Core appsettings.json 文件 在本节中,我们将讨论 ASP.NET Core 项目中appsettings.json文件的重要性. 在以前的 ASP.NET 版本中 ...