POJ_3264_Balanced Lineup
2018-06-17 21:46:53来源:未知 阅读 ()
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 57259 | Accepted: 26831 | |
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
- 区间最值RMQ
- ST打表
1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <climits> 7 #include <cmath> 8 #include <vector> 9 #include <queue> 10 #include <stack> 11 #include <set> 12 #include <map> 13 using namespace std; 14 typedef long long LL ; 15 typedef unsigned long long ULL ; 16 const int maxn = 5e5 + 10 ; 17 const int inf = 0x3f3f3f3f ; 18 const int npos = -1 ; 19 const int mod = 1e9 + 7 ; 20 const int mxx = 100 + 5 ; 21 const double eps = 1e-6 ; 22 const double PI = acos(-1.0) ; 23 24 int n, m, k, A, B, u, v; 25 int a[maxn], mx[maxn][30], mi[maxn][30], fac[30]; 26 int main(){ 27 // freopen("in.txt","r",stdin); 28 // freopen("out.txt","w",stdout); 29 for(int i=0;i<30;i++) 30 fac[i]=(1<<i); 31 while(~scanf("%d %d",&n,&m)){ 32 for(int i=1;i<=n;i++){ 33 scanf("%d",&mx[i][0]); 34 mi[i][0]=mx[i][0]; 35 } 36 int k=(int)(log((double)n)/log(2.0)); 37 for(int j=1;j<=k;j++) 38 for(int i=1;i+fac[j]-1<=n;i++){ 39 mx[i][j]=max(mx[i][j-1],mx[i+fac[j-1]][j-1]); 40 mi[i][j]=min(mi[i][j-1],mi[i+fac[j-1]][j-1]); 41 } 42 while(m--){ 43 scanf("%d %d",&u,&v); 44 k=(int)(log((double)(v-u+1))/log(2.0)); 45 A=max(mx[u][k],mx[v-fac[k]+1][k]); 46 B=min(mi[u][k],mi[v-fac[k]+1][k]); 47 printf("%d\n",A-B); 48 } 49 } 50 return 0; 51 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- POJ-3278 2020-04-01
- Asteroids!_poj2225 2020-02-09
- poj-1753题题解思路 2020-01-26
- POJ1852 2019-11-11
- POJ2431 优先队列+贪心 - biaobiao88 2019-11-03
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash