poj3264 - Balanced Lineup(RMQ_ST)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 45243 | Accepted: 21240 | |
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来做
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #define maxx 50005 5 using namespace std; 6 int maxsum[maxx][18],minsum[maxx][18],n,Q; 7 int _max(int a,int b) 8 { 9 return a>b?a:b; 10 } 11 int _min(int a,int b) 12 { 13 return a<b?a:b; 14 } 15 int main() 16 { 17 18 while(~scanf("%d %d\n",&n,&Q)) 19 { 20 for(int i=1;i<=n;i++) 21 { 22 scanf("%d\n",&maxsum[i][0]); 23 minsum[i][0]=maxsum[i][0]; 24 } 25 for(int j=1;j<=18;j++)// 预处理 26 for(int i=1;i<=n;i++) //j 循环在 i 循环外 27 { 28 if(i+(1<<j)-1<=n) // 注意左右区间 29 { 30 maxsum[i][j]=_max(maxsum[i][j-1],maxsum[i+(1<<(j-1))][j-1]); 31 minsum[i][j]=_min(minsum[i][j-1],minsum[i+(1<<(j-1))][j-1]); 32 } 33 } 34 while(Q--) 35 {int a,b; 36 scanf("%d %d\n",&a,&b); 37 int k=(int)((log((double)(b-a+1)))/log(2.0)); //注意要包含端点并转换成double 38 int maxl=_max(maxsum[a][k],maxsum[b-(1<<k)+1][k]);//不然有些测试要出问题 比如poj 39 int minl=_min(minsum[a][k],minsum[b-(1<<k)+1][k]); 40 printf("%d\n",maxl-minl); 41 } 42 } 43 return 0; 44 }
转载于:https://www.cnblogs.com/lwhinlearning/p/5677179.html
poj3264 - Balanced Lineup(RMQ_ST)相关推荐
- POJ3264——Balanced Lineup(线段树)
本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中.取一段区间.然后在区间中找出最大的数和最小的数字.求这两个数字的差. 分析:按区间取值,非常明显使 ...
- POJ3264 Balanced Lineup【线段树】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 63040 Accepted: 29405 ...
- 竞赛最好用的平衡树-Size Balanced Tree(SBT)【建议收藏】
大家好. 前段时间我更新过AVL.红黑树以及搜索二叉树的简单讲解.今天我们还是围绕着平衡树的话题,来讲解一个很牛逼的平衡树结构.这种结构是我国的一位大牛:陈启峰.在2006年,他还在读高中阶段,所发明 ...
- 【POJ 3274】Gold Balanced Lineup (stl map )设计hash表,处理碰撞
题目链接 题目链接 http://poj.org/problem?id=3274 题意 输入每头牛的特征的10进制,若i~j头牛中每个数位的特征相等则满足要求,求所有满足要求的j-i的最大值. 解题思 ...
- Balanced Lineup(POJ-3264)
Problem Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in t ...
- POJ——3624 Balanced Lineup(线段树入门——区间最值问题)
原题链接:http://poj.org/problem?id=3264 每天挤奶时,农夫John的N头奶牛(1≤N≤50,000头)总是按照相同的顺序排列.一天,农夫约翰决定和几头牛组织一场极限飞盘游 ...
- Poj 3246 Balanced Lineup(线段树基础)
依旧是线段树基础题 询问区间的最大值和最小值之差,只有询问,没有插入删除.继续理解基础线段树 #include <iostream> #include <algorithm> ...
- uva live 7637 Balanced String (贪心)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- POJ 3274 Gold Balanced Lineup(哈希)
题目链接 很难想.会哈希,但是想不出.需要一个转化,本来是求某一段上的二进制上每一位的1数目相等,转化为找两段相等的,换元可推出公式.最后注意特判.. 1 #include <iostream& ...
最新文章
- centos7samba服务的搭建
- 从安全视角对机器学习的部分思考
- JavaScript常用判断函数 [转]
- python浪漫代码-python七夕浪漫表白源码
- 创业做什么好?先学习精英式创业从平庸到卓越
- 质量兴农战略规划-农业大健康·韩长赋:质量效益和竞争力
- 我的代码为什么看起来像shit?
- web----tcp三次握手
- mysql 报错1005_MysqlERROR 1005错误处理
- vmware安装黑苹果教程
- labelcontrol 多行_ios – UISegmentedControl中的两行文本
- 【时间序列】时间序列数据的缺失填补方法总结
- Linux系统编程入门学习笔记5-文件IO
- GPS原始RMC数据解析之DDMM.MMMM
- mysql grant priv_mysql怎么将grant priv的权限
- 什么oracle,什么是ORACLE?
- [历朝通俗演义-蔡东藩-前汉]第012回 戕县令刘邦发迹 杀郡守项梁举兵
- http://wenzhang.ztcztc.com/Detail.aspx?id=70537498-7FD3-8992-552B-27716F9315F8
- NetTerm 使用简介
- 【大学生Python】字典的基础使用