题目链接:http://poj.org/problem?id=3264

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.

题意描述:n个数排成一排形成数组,Q个询问,每个询问给出两个数a和b,求出数组中[a,b]里最大值和最小值的差。

算法分析:可以用线段树做,在这里介绍RMQ算法。

RMQ:预处理O(nlogn),然后每次查询O(1)。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#define inf 0x7fffffff
using namespace std;
const int maxn=+; int n,q;
int an[maxn],dmax[maxn][],dmin[maxn][]; void RMQ_init()
{
for (int i= ;i<=n ;i++) dmax[i][]=dmin[i][]=an[i];
for (int j= ;(<<j)<=n ;j++)
{
for (int i= ;i+(<<j)-<=n ;i++)
{
dmax[i][j]=max(dmax[i][j-],dmax[i+(<<(j-))][j-]);
dmin[i][j]=min(dmin[i][j-],dmin[i+(<<(j-))][j-]);
}
}
} int RMQ(int L,int R)
{
int k=;
while ((<<(k+))<=R-L+) k++;
int maxnum=max(dmax[L][k],dmax[R-(<<k)+][k]);
int minnum=min(dmin[L][k],dmin[R-(<<k)+][k]);
return maxnum-minnum;
} int main()
{
while (scanf("%d%d",&n,&q)!=EOF)
{
for (int i= ;i<=n ;i++) scanf("%d",&an[i]);
RMQ_init();
int a,b;
for (int i= ;i<q ;i++)
{
scanf("%d%d",&a,&b);
printf("%d\n",RMQ(a,b));
}
}
return ;
}

poj 3264 Balanced Lineup 区间极值RMQ的更多相关文章

  1. POJ 3264 Balanced Lineup 线段树RMQ

    http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...

  2. POJ 3264 Balanced Lineup 区间最值

    POJ3264 比较裸的区间最值问题.用线段树或者ST表都可以.此处我们用ST表解决. ST表建表方法采用动态规划的方法, ST[I][J]表示数组从第I位到第 I+2^J-1 位的最值,用二分的思想 ...

  3. Poj 3264 Balanced Lineup RMQ模板

    题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...

  4. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

  5. POJ - 3264 Balanced Lineup (RMQ问题求区间最值)

    RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就 ...

  6. poj 3264 Balanced Lineup (RMQ)

    /******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...

  7. POJ 3264 Balanced Lineup(RMQ)

    点我看题目 题意 :N头奶牛,Q次询问,然后给你每一头奶牛的身高,每一次询问都给你两个数,x y,代表着从x位置上的奶牛到y位置上的奶牛身高最高的和最矮的相差多少. 思路 : 刚好符合RMQ的那个求区 ...

  8. poj 3264 Balanced Lineup(RMQ裸题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 43168   Accepted: 20276 ...

  9. POJ 3264 Balanced Lineup 【线段树/区间最值差】

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 62103 Accepted: 29005 Cas ...

随机推荐

  1. Ado.net利用反射执行SQL得到实体

    public Model.orderParent GetTraceIDforID(string orderid) { string sql = string.Format(" select ...

  2. [译]MongoDb生产环境注意事项

    译注: 本文是翻译MongoDB Manuel中的MongoDB Production Notes一节内容.这节内容重点关注生产环境中影响性能和可靠性的各种注意事项,值得正在部署MongoDB的工作者 ...

  3. zabbix2.4 安装配置

    首先从www.zabbix.com下载rpm包: 接下来我要配置一台zabbix server,自己监控自己即使服务端又是客户端,zabbix web gui和zabbix数据库都放在同一台主机上,除 ...

  4. 配置Nginx服务

    一,安装之前准备1.nginx依赖: gcc openssl-devel pcre-devel zlib-devel    安装依赖:yum install gcc openssl-devel pcr ...

  5. php循环创建目录

    代码取自thinkphp中: function mk_dir($dir, $mod = 0777) { if(!is_dir($dir) || mkdir($dir, $mod)) { if(!mk_ ...

  6. 开源web终端ssh解决方案-gateone简介

    好久都没来写博客,最近忙啥去了呢? 一是忙于saltstack的二次开发,二是云计算的学习研究中,所以就一直没写东西,今天给大家介绍个工具. 1. 首先来说一下为什么要 web ssh? 许多人不是说 ...

  7. JDBC基础二

    1.配置文件:dbinfo.properties driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/test user ...

  8. linux设备驱动层次

    USB 采用树形拓扑结构,主机侧和设备侧的USB 控制器分别称为主机控制器(HostController)和USB 设备控制器(UDC),每条总线上只有一个主机控制器,负责协调主机和设备间的通信,而设 ...

  9. Requests:Python HTTP Module学习笔记(二)(转)

    在上一篇日志中对Requests做了一个整体的介绍,接来下再介绍一些高级的用法,主要资料还是翻译自官网的文档,如有错漏,欢迎指正. 参考资料:http://docs.python-requests.o ...

  10. 《大话设计模式》ruby版代码:策略模式

    需求: 商场收银软件,根据客户购买物品的单价和数量,计算费用,会有促销活动,打八折,满三百减一百之类的. 一,使用工厂模式. # -*- encoding: utf-8 -*- #现金收费抽象类 cl ...