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 ...
随机推荐
- FastDFS分布文件系统[转]
FastDFS是为互联网应用量身定做的一套分布式文件存储系统,非常适合用来存储用户图片.视频.文档等文件.对于互联网应用,和其他分布式文件系统相比,优势非常明显.具体情况大家可以看相关的介绍文档,包括 ...
- 修改Tomcat 6 默认的ROOT
1.找到conf/server.xml. 2.找到Host节点. 3.在该节点中添加子节点<Context path="" docBase="项目名称" ...
- register_globals(全局变量注册开关)
register_globals,是php.ini文件里面的一个配置选项,接下来,我们可以通过例程来分析一下,当register_globals = on 与 register_globals = o ...
- NDK开发之调用方法
与NDK开发之访问域中介绍的一样,Java中的方法也是分为两类:实例方法和静态方法.JNI提供了访问两类方法的函数,下面我们一起来看看怎么在C中访问Java中的方法. 我们的MainActivity中 ...
- oracle在敏感操作前创建还原点
我们都知道,在vmware虚拟机中有一个拍摄快照的功能,我们可以把系统此时的状态保存下来,一方后面遇到不测事件,也好将系统还原,oracle中也有类似功能. 首先创建一张学生表: 向学生表中插入一条数 ...
- apache下配置虚拟主机案例详解
案例说明: 域名: www.tianyik.com /var/html/www blog.tianyik.com /var/html/blog bb ...
- .net+easyui系列--验证框
1.允许从 0 到 10个字符 <input id="vv" class="easyui-validatebox" data-options=" ...
- HDU-1113(map的运用)
Word Amalgamation Problem Description In millions of newspapers across the United States there is a ...
- 9张思维导图学习Javascript(转)
思维导图小tips:思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又极其有效,是一种革命性的思维工具.思维导图运用图文并重的技巧,把各级主题的关系用相互隶属与相关的层级图表现出来 ...
- Hibernate session flush
最近做项目时,用到了hibernnate,批量删除10000条数据时,删除时前台将id传到后台,用in匹配去删除,页面直接卡死. 解决方法,将传过来的10000条id分批删除,每删除五百条后,调用ge ...