【poj3264】Balanced Lineup
2016-05-07 09:00
417 查看
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
Line 1: Two space-separated integers, N and Q.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
Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.Sample Input
6 3 1 7 3 4 2 5 1 5 4 6 2 2
Sample Output
6 3 0
Source
USACO 2007 January Silver题解
给定一个区间,求最大值与最小值之差ST表
#include<iostream> #include<cstdio> #include<cstring> #define N 50010 using namespace std; int ff[1<<20]; int a ,rmq[20] ,rmx[20] ; int n,m; void rmq_init() { for (int i=1;i<=n;i++) { rmq[0][i] = a[i]; rmx[0][i] = a[i]; } for (int j=1;j<20;j++) for (int i=1;i+(1<<j)-1<=n;i++){ rmq[j][i] = min(rmq[j-1][i],rmq[j-1][i+(1<<j>>1)]); rmx[j][i] = max(rmx[j-1][i],rmx[j-1][i+(1<<j>>1)]); } memset(ff,-1,sizeof(ff)); for (int i=0;i<20;i++) ff[1<<i] = i; for (int i=0;i<N;i++) if (ff[i] == -1) ff[i] = ff[i-1]; } pair<int,int> rmq_find(int x,int y) { int t=ff[y-x+1]; return make_pair(min(rmq[t][x],rmq[t][y-(1<<t)+1]), max(rmx[t][x],rmx[t][y-(1<<t)+1])); } int main() { int l,r; scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%d",&a[i]); rmq_init(); for (int i=1;i<=m;i++) { scanf("%d%d",&l,&r); printf("%d\n",rmq_find(l,r).second-rmq_find(l,r).first); } }
相关文章推荐
- win7 64位系统双击桌面所有程序提示"文件没有与之关联的程序来执行"的解决方法
- JAX-RS的参考实现:《使用 Jersey 和 Apache Tomcat 构建 RESTful Web 服务》
- 匿名管道
- CSS3-04 样式 3
- 加强版DVD管理系统
- MapReduce输出格式
- SEO
- COBBLER无人值守安装-----已用真实服务器测试
- [置顶] 决策树算法预测森林植被
- GeoPython 随笔:空间数据查询——测试代码
- Leetcode 28.Implement strStr() 解题报告【C库函数strstr()模拟-字符串中子串首次出现的地址】
- Linux(centOS6.5)下SVN的安装、配置及开机启动
- C++/Php/Python/Shell 程序按行读取文件或者控制台
- NYOJ-93汉诺塔(三)
- 《safe+》离线密码更安全,一键修改最方便
- java多线程技能基础
- Android系统自带样式(@android:style/)
- POJ3233 Matrix Power Series构造矩阵解决问题
- C++3层架构的UML例图
- Linux的原子操作与同步机制