POJ3264 Balanced Lineup
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 44720 | Accepted: 20995 | |
| 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
Source
ST算法求区间内最值
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int mxn=;
int n,Q;
int h[mxn];
int fmx[mxn][],fmi[mxn][];//f[i][j]表示从i开始到i*(1<<j)范围内的目标值
int ST(int a,int b){//ST算法,倍增求区间最值
int i,j;
for(i=;i<=n;i++){//自身
fmx[i][]=fmi[i][]=h[i];
}
int k=(int)(log(n*1.0)/log(2.0));//k=以2为底n的对数
for(i=;i<=k;i++){//第一层为次数
for(j=;j<=n;j++){//第二层为范围
fmx[j][i]=fmx[j][i-];
if(j+(<<(i-)) <=n)
fmx[j][i]=max(fmx[j][i],fmx[j+(<<(i-))][i-]);
fmi[j][i]=fmi[j][i-];
if(j+(<<(i-)) <=n)
fmi[j][i]=min(fmi[j][i],fmi[j+(<<(i-))][i-]);
}
}
return ;
}
int ansmx(int a,int b){//查找最大值
int k=(int)(log(b-a+1.0)/log(2.0));
return max(fmx[a][k],fmx[b-(<<k)+][k]);
}
int ansmi(int a,int b){//查找最小值
int k=(int)(log(b-a+1.0)/log(2.0));
return min(fmi[a][k],fmi[b-(<<k)+][k]);
}
int main(){
scanf("%d%d",&n,&Q);
int i,j;
for(i=;i<=n;i++){
scanf("%d",&h[i]);
}
int a,b;
ST(,n);
for(i=;i<=Q;i++){
scanf("%d%d",&a,&b);
printf("%d\n",ansmx(a,b)-ansmi(a,b));//输出询问区间内最大值和最小值的差
}
return ;
}
POJ3264 Balanced Lineup的更多相关文章
- poj3264 - Balanced Lineup(RMQ_ST)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 45243 Accepted: 21240 ...
- POJ3264 Balanced Lineup 【线段树】+【单点更新】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32778 Accepted: 15425 ...
- poj3264 Balanced Lineup(树状数组)
题目传送门 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 64655 Accepted: ...
- POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值
题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...
- poj3264 balanced lineup【线段树】
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...
- kuangbin专题七 POJ3264 Balanced Lineup (线段树最大最小)
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...
- POJ-3264 Balanced Lineup(区间最值,线段树,RMQ)
http://poj.org/problem?id=3264 Time Limit: 5000MS Memory Limit: 65536K Description For the daily ...
- POJ3264 Balanced Lineup [RMQ模板]
题意:有n头牛,输入他们的身高,求某区间身高的极值的差(max-min), 用RMQ模板,同时构造求极大值和极小值的两个数组. //poj3264 #include <iostream> ...
- [POJ3264]Balanced Lineup(RMQ, ST算法)
题目链接:http://poj.org/problem?id=3264 典型RMQ,这道题被我鞭尸了三遍也是醉了…这回用新学的st算法. st算法本身是一个区间dp,利用的性质就是相邻两个区间的最值的 ...
随机推荐
- Linux搭建python环境中cx_Oracle模块安装遇到的问题与解决方法
安装或使用cx_Oracle时,需要用到Oracel的链接库,如libclntsh.so.11.1,否则会有各种各样的错误信息. 安装Oracle Instant Client就可得到这个链接库,避免 ...
- Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- C++哈弗曼编码
// haffman.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using name ...
- 储存与更新 access_token
做微信的项目,一开始就是 access_token 的申请,微信文档上写的比较清楚: 1.为了保密appsecrect,第三方需要一个access_token获取和刷新的中控服务器.而其他业务逻辑服务 ...
- [CareerCup] 3.3 Set of Stacks 多个栈
3.3 Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in ...
- [CareerCup] 11.7 Tower of People in Circus 马戏团的人塔
11.7 A circus is designing a tower routine consisting of people standing atop one another's shoulder ...
- 20145215《Java程序设计》第9周学习总结
20145215<Java程序设计>第九周学习总结 教材学习内容总结 整合数据库 JDBC入门 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操 ...
- HoloLens开发手记 - Unity之Gestures手势识别
手势识别是HoloLens交互的重要输入方法之一.HoloLens提供了底层API和高层API,可以满足不同的手势定制需求.底层API能够获取手的位置和速度信息,高层API则借助手势识别器来识别预设的 ...
- replace 替换全部的正确姿势
本文同步自我的个人博客:http://www.52cik.com/2015/11/06/replace-all.html 关于字符串替换问题,其实是个很简单的问题,但却也不那么简单,至少对于很多新手而 ...
- css清楚浮动的几种常用方法
请先看博客:http://www.jb51.net/css/173023.html