Balanced Lineup
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 49061 | Accepted: 22975 | |
Case Time Limit: 2000MS |
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
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
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0 题目大意:给出一个数字序列,对于每次提问,输出区间(i,j)内的最大值数与最小值数的差。ST算法
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 50010
#define ll long long
using namespace std;
int f[N][],fi[N][],a[N];
int n,m;
void RMQ()
{
for (int i=;i<=n;i++) f[i][]=a[i],fi[i][]=a[i];
for (int i=;i<=int(log(n)/log());i++)
for (int j=;j<=n+-(<<i);j++)
{
f[j][i]=max(f[j][i-],f[j+(<<i-)][i-]);//区间最大值
fi[j][i]=min(fi[j][i-],fi[j+(<<i-)][i-]);//区间最小值
} }
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
for (int i=;i<=n;i++) scanf("%d",&a[i]);
RMQ();
for (int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
int k;
k=(int)(log(y-x+)/log());
int ans1=max(f[x][k],f[y-(<<k)+][k]),//两个区间存在重叠
ans2=min(fi[x][k],fi[y-(<<k)+][k]);
printf("%d\n",ans1-ans2);
}
} return ;
}
ST
模板题。
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 ...
- 哈希-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 ...
随机推荐
- JavaEE自定义标签:标签类的创建、tld配置文件的创建(位置、如何创建)、Web-XML配置、JSP应用
1.标签 以类似于html标签的方式实现的java代码的封装. 第一:形成了开发标签的技术标准---自定义标签的技术标准. 第二:java标准标签库(sun之前自己开发的一系列的标签的集合)jstl, ...
- 用高德地图API 通过详细地址获得经纬度
http://cloud.sinyway.com/Service/amap.html http://restapi.amap.com/v3/geocode/geo?key=xxxxxxxxxxxxxx ...
- [Javascript] IIFE
Javascript modules are a design pattern that allow you to encapsulate your code into smaller self ma ...
- PHP发送邮件类库PHPMailer的简单使用
最近需要用到发送邮件的功能,原本是用PHP自带的mail()函数发送的.php mail()这个方法非常简单.方便.易用,但是除了网易邮箱.QQ邮箱.GMAIL邮箱等常用的邮箱可以收到之外,经测试HO ...
- android79 Fragment生命周期
切换成01时依次调用onCreate,onStart,onResume方法,切换到03的时候01依次onPause,onStop,onDestroy,03依次onCreate,onStart,onRe ...
- 【转】C++:在程序中获取全球唯一标识号(GUID或UUID)
Windows:使用CoCreateGuid函数(GUID) #include <objbase.h> #include <stdio.h> #define GUID_LEN ...
- java与javax有什么区别?
java 是java j2sdk 中的类库,也就是Java Development kit . 它提供也一些基础的东西,如io库.桌面程序的类库,如awt.集合库(如Collection.List.M ...
- SQL多行拼接为一行
使用简单T-SQL,拼接一列多行为一行.按SQL SERVER的说法叫做自拼接(PS:区分自连接) 还有一种方法是for xml path的方式,感觉不实用. declare @Result varc ...
- Call to undefined function curl_init()解决方法
今天在使用php中的 curl 扩展时 在开启
- [分词] C#SegList分词辅助类,帮助类 (转载)
点击下载 SegList.rar 主要功能如下最新的SegList分词辅助类,帮助类看下面代码吧 /// <summary> /// 类说明:SegList /// 编 码 人:苏飞 // ...