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.

  题目就是求RMQ,水题。

代码如下:

// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━━━━━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ████━████ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━┛
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━━━━━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2015年07月17日 星期五 16时52分31秒
// File Name : 3264.cpp #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=; int dp1[MaxN][],dp2[MaxN][];
int logN[MaxN]; void init(int N,int num[])
{
logN[]=-; for(int i=;i<=N;++i)
{
dp1[i][]=num[i];
dp2[i][]=num[i];
logN[i]=logN[i-]+((i&(i-))==);
} for(int j=;j<=logN[N];++j)
for(int i=;i+(<<j)-<=N;++i)
{
dp1[i][j]=max(dp1[i][j-],dp1[i+(<<(j-))][j-]);
dp2[i][j]=min(dp2[i][j-],dp2[i+(<<(j-))][j-]);
}
} int RMQ(int x,int y)
{
int k=logN[y-x+]; return max(dp1[x][k],dp1[y-(<<k)+][k])-min(dp2[x][k],dp2[y-(<<k)+][k]);
} int num[MaxN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int N,Q;
int a,b; while(~scanf("%d %d",&N,&Q))
{
for(int i=;i<=N;++i)
scanf("%d",&num[i]); init(N,num); while(Q--)
{
scanf("%d %d",&a,&b); printf("%d\n",RMQ(a,b));
}
} return ;
}

(简单) POJ 3264 Balanced Lineup,RMQ。的更多相关文章

  1. Poj 3264 Balanced Lineup RMQ模板

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

  2. poj 3264 Balanced Lineup (RMQ)

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

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

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

  4. poj 3264 Balanced Lineup (RMQ算法 模板题)

    RMQ支持操作: Query(L, R):  计算Min{a[L],a[L+1], a[R]}. 预处理时间是O(nlogn), 查询只需 O(1). RMQ问题 用于求给定区间内的最大值/最小值问题 ...

  5. POJ 3264 Balanced Lineup -- RMQ或线段树

    一段区间的最值问题,用线段树或RMQ皆可.两种代码都贴上:又是空间换时间.. RMQ 解法:(8168KB 1625ms) #include <iostream> #include < ...

  6. POJ 3264 Balanced Lineup RMQ ST算法

    题意:有n头牛,编号从1到n,每头牛的身高已知.现有q次询问,每次询问给出a,b两个数.要求给出编号在a与b之间牛身高的最大值与最小值之差. 思路:标准的RMQ问题. RMQ问题是求给定区间内的最值问 ...

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

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

  8. poj 3264 Balanced Lineup(RMQ裸题)

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

  9. POJ 3264 Balanced Lineup(RMQ)

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

随机推荐

  1. ref与out之间的区别整理 摘自与望楼http://blog.csdn.net/xiaoning8201/article/details/6893154

    ref和out都是C#中的关键字,所实现的功能也差不多,都是指定一个参数按照引用传递. 对于编译后的程序而言,它们之间没有任何区别,也就是说它们只有语法区别. 总结起来,他们有如下语法区别: .ref ...

  2. skia入门

    SkBitmap bmp; bmp.setConfig(SkBitmap::kARGB_8888_Config, rect.Width(), rect.Height()); bmp.allocPixe ...

  3. 简单hash[或者是哈希思想]

    题目链接 /* 有一个长度为n的只包含小写字母的字符串s,有m次操作,每次输入2个字符 A , B表示将s中的全部字符A变成B,B变成A. char sky[30],顺序记录每个字母的映射,在sky[ ...

  4. JS-DOM元素灵活查找

    用className选择元素 封装成函数 <title>无标题文档</title> <script> /* window.onload=function () { ...

  5. JS里的CSS函数

    <title>无标题文档</title> <script> function css(obj,name,value){ if(arguments.length==2 ...

  6. maven认识

    在这里普及一下知识: ┣ maven与ant是同类,构建工具 ┣ svn与cvs,css是同类,版本控制工具 1.为什么要用Maven? 在进行软件开发的过程中,无论什么项目,采用何种技术,使用何种编 ...

  7. Linux学习 -- 系统管理

    1 进程管理 判断服务器健康状态 top [选项] 查看系统中所有进程 ps aux      BSD格式 ps -le        Linux格式 pstree [选项] -p   显示PID - ...

  8. hdu_4352_XHXJ's LIS(数位DP+状态压缩)

    题目连接:hdu_4352_XHXJ's LIS 题意:这题花大篇篇幅来介绍电子科大的一个传奇学姐,最后几句话才是题意,这题意思就是给你一个LL范围内的区间,问你在这个区间内最长递增子序列长度恰为K的 ...

  9. Android Stduio的使用(七)--Structure窗口

    1.本篇博文介绍Android查看.Java文件中所有属性和类方法的工具:Structure窗口 2.我们知道Eclipse的OutLine窗口可以查看.java文件所有的属性和方法. 2.Andro ...

  10. VLAN及Trunk实验

    VLAN及Trunk实验 一 基本VLAN试验 实验拓扑: 实验目的:掌握VLAN基本配置 掌握静态接口VLAN划分 实验需求:在交换机switch上面创建两个VLAN,vlan2和vlan3,vla ...