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,水题。

代码如下:

  1. // ━━━━━━神兽出没━━━━━━
  2. // ┏┓ ┏┓
  3. // ┏┛┻━━━━━━━┛┻┓
  4. // ┃ ┃
  5. // ┃ ━ ┃
  6. // ████━████ ┃
  7. // ┃ ┃
  8. // ┃ ┻ ┃
  9. // ┃ ┃
  10. // ┗━┓ ┏━┛
  11. // ┃ ┃
  12. // ┃ ┃
  13. // ┃ ┗━━━┓
  14. // ┃ ┣┓
  15. // ┃ ┏┛
  16. // ┗┓┓┏━━━━━┳┓┏┛
  17. // ┃┫┫ ┃┫┫
  18. // ┗┻┛ ┗┻┛
  19. //
  20. // ━━━━━━感觉萌萌哒━━━━━━
  21.  
  22. // Author : WhyWhy
  23. // Created Time : 2015年07月17日 星期五 16时52分31秒
  24. // File Name : 3264.cpp
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <iostream>
  29. #include <algorithm>
  30. #include <vector>
  31. #include <queue>
  32. #include <set>
  33. #include <map>
  34. #include <string>
  35. #include <math.h>
  36. #include <stdlib.h>
  37. #include <time.h>
  38.  
  39. using namespace std;
  40.  
  41. const int MaxN=;
  42.  
  43. int dp1[MaxN][],dp2[MaxN][];
  44. int logN[MaxN];
  45.  
  46. void init(int N,int num[])
  47. {
  48. logN[]=-;
  49.  
  50. for(int i=;i<=N;++i)
  51. {
  52. dp1[i][]=num[i];
  53. dp2[i][]=num[i];
  54. logN[i]=logN[i-]+((i&(i-))==);
  55. }
  56.  
  57. for(int j=;j<=logN[N];++j)
  58. for(int i=;i+(<<j)-<=N;++i)
  59. {
  60. dp1[i][j]=max(dp1[i][j-],dp1[i+(<<(j-))][j-]);
  61. dp2[i][j]=min(dp2[i][j-],dp2[i+(<<(j-))][j-]);
  62. }
  63. }
  64.  
  65. int RMQ(int x,int y)
  66. {
  67. int k=logN[y-x+];
  68.  
  69. return max(dp1[x][k],dp1[y-(<<k)+][k])-min(dp2[x][k],dp2[y-(<<k)+][k]);
  70. }
  71.  
  72. int num[MaxN];
  73.  
  74. int main()
  75. {
  76. //freopen("in.txt","r",stdin);
  77. //freopen("out.txt","w",stdout);
  78.  
  79. int N,Q;
  80. int a,b;
  81.  
  82. while(~scanf("%d %d",&N,&Q))
  83. {
  84. for(int i=;i<=N;++i)
  85. scanf("%d",&num[i]);
  86.  
  87. init(N,num);
  88.  
  89. while(Q--)
  90. {
  91. scanf("%d %d",&a,&b);
  92.  
  93. printf("%d\n",RMQ(a,b));
  94. }
  95. }
  96.  
  97. return ;
  98. }

(简单) 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. Json与Gson讲解

    1 json的含义: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言.易于 ...

  2. JPA 系列教程6-单向多对多

    JPA中的@ManyToMany @ManyToMany注释表示模型类是多对多关系的一端. @JoinTable 描述了多对多关系的数据表关系. name 属性指定中间表名称 joinColumns ...

  3. android 中ImageButton按下改变背景图片的效果

    最近在做一个app的登陆界面,才发现原来认为很简单的UI效果,其实背后却蕴含的知识很多,积累一个算一个吧. 实现方法有两种:一种是添加代码,一种是配置xml文件. 方法一:代码添加 ImageButt ...

  4. 关于SVN更新注意

    SVN更新时其实大部分都可以直接更新但是只有在 当然为0就没有事但是如果不是0的话就要注意了表示你修改的和别人修改了相同的文件先点开 次文件然后看看那些不一样主要看如果是这样就直接点更新就好了,如果有 ...

  5. IOS小工具以及精彩的博客

    IOS小工具以及精彩的博客 工具 Log Guru是一个收集Log的小工具, 可以在 Mac 上查看 iOS 设备的实时系统日志. 现在可以直接高亮显示在 FIR.im 上安装 app 失败的原因.后 ...

  6. linux中服务器定时程序设定

    服务器不重启的情况下定时自动重启apache及mysql服务,其实也大同小异.具体步骤如下:  一.每天的12点及16点重启apache及mysql服务 [root@www bin]# cd /opt ...

  7. ViewPager滑动标签-PagerSlidingTabStrip的使用

    有篇博客写的已经非常详细,所以不再写了.主要在于导入这个Library,导入Library看自己的笔记 博客地址:http://doc.okbase.net/HarryWeasley/archive/ ...

  8. discuz 添加板块失败解决办法

    最近把服务器环境升了下级,发现discuz后台添加栏目添加不了了,数据库没变,源代码没变,就突然添加不了了.刚开始添加1个板块成功了,再添加就怎么也添不进去了.只是页面刷新了一下,啥提示没有. 经过一 ...

  9. Attrib命令,可以让文件夹彻底的隐藏起来

    Attrib命令,可以让文件夹彻底的隐藏起来,就算是在文件夹选项中设置了显示隐藏文件夹,也无法显示出来的.只能通过路径访问的方式打开文件夹.如上图,就是attrib命令的隐藏文件夹和显示文件夹的两条命 ...

  10. string字符串转C风格字符串 进而转换为数字

    要求如题 头文件stdlib.h中有一个函数atof() 可以将字符串转化为双精度浮点数(double) double atof(const char *nptr); 此字符串为C风格字符串,因此需要 ...