Frequent values

  You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers ai , ... , aj.

Input

  The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a1 , ... , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the
query.

The last test case is followed by a line containing a single 0.

Output

  For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.

Sample Input

  1. 10 3
  2. -1 -1 1 1 1 1 3 10 10 10
  3. 2 3
  4. 1 10
  5. 5 10
  6. 0

Sample Output

  1. 1
  2. 4
  3. 3
  4.  
  5.   这题由于序列非降,所以就可以把整个数组进行游程编码(RLE Run Length Encoding)。
      还是很简单的~~~
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. using namespace std;
  5. const int maxn=;
  6. int mm[maxn],Max[maxn][];
  7. int MAXL[maxn],MAXR[maxn],cnt=;
  8. int ID[maxn],tot[maxn],a[maxn];
  9. void Init(){
  10. memset(tot,,sizeof(tot));
  11. cnt=;
  12. }
  13. int main(){
  14. #ifndef ONLINE_JUDGE
  15. //freopen(".in","r",stdin);
  16. //freopen(".out","w",stdout);
  17. #endif
  18.  
  19. int n,Q;
  20. while(~scanf("%d",&n)&&n){
  21. scanf("%d",&Q);
  22. Init();
  23. for(int i=;i<=n;i++)
  24. scanf("%d",&a[i]);
  25. a[]=a[n+]=-;
  26. for(int i=;i<=n;i++){
  27. if(a[i]==a[i-]){
  28. MAXL[i]=MAXL[i-];
  29. ID[i]=ID[i-];
  30. }
  31. else{
  32. MAXL[i]=i;
  33. ID[i]=++cnt;
  34. }
  35. tot[ID[i]]++;
  36. }
  37. //MAXL[i]指与a[i]相等的1~i-1中最靠左的位置
  38. //ID[i]指当前位置被分到第几块
  39. //tot[i]指第i块的长度
  40. for(int i=n;i>=;i--){
  41. if(a[i]==a[i+])
  42. MAXR[i]=MAXR[i+];
  43. else
  44. MAXR[i]=i;
  45. }
  46. //MAXR[i]指与a[i]相等的i+1~n中最靠右的位置
  47. mm[]=-;
  48. for(int i=;i<=cnt;i++){
  49. mm[i]=(i&(i-))?mm[i-]:mm[i-]+;
  50. Max[i][]=tot[i];
  51. }
  52. for(int k=;k<=mm[cnt];k++)
  53. for(int i=;i+(<<k)-<=cnt;i++)
  54. Max[i][k]=max(Max[i][k-],Max[i+(<<(k-))][k-]);
  55. //对块处理出稀疏表
  56. int a,b,ans;
  57. while(Q--){
  58. scanf("%d%d",&a,&b);
  59. if(a>b)swap(a,b);
  60. if(ID[a]==ID[b]){
  61. printf("%d\n",b-a+);
  62. continue;
  63. }
  64. ans=-;
  65. ans=max(MAXR[a]-a+,b-MAXL[b]+);
  66. a=ID[MAXR[a]+];
  67. b=ID[MAXL[b]-];
  68. if(a<=b)
  69. ans=max(ans,max(Max[a][mm[b-a+]],Max[b-(<<mm[b-a+])+][mm[b-a+]]));
  70. printf("%d\n",ans);
  71. }
  72. }
  73. return ;
  74. }

数据结构(RMQ):UVAoj 11235 Frequent values的更多相关文章

  1. RMQ算法 以及UVA 11235 Frequent Values(RMQ)

    RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8)  内的最大值.数据量小 ...

  2. UVA 11235 Frequent values(RMQ)

    Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...

  3. UVA - 11235 Frequent values

    2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...

  4. [POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]

    2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...

  5. UVA 11235 Frequent values 线段树/RMQ

    vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释**************** ...

  6. UVA 11235 Frequent Values ---RMQ

    大白书上的例题,具体讲解见大白书,最好用用一个Log数组直接求k,这样就是纯O(1)了 #include <iostream> #include <cstdio> #inclu ...

  7. UVa 11235 Frequent values (RMQ && 区间出现最多次的数的次数)

    题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间“缩小”,例如1 1 2 3 3 3就可以变成2 ...

  8. 11235 - Frequent values

    <算法竞赛入门经典-训练指南>P198 记录一下区间的左右边界就可以了 #include <iostream> #include <stack> #include ...

  9. POJ 3368 & UVA 11235 - Frequent values

    题目链接:http://poj.org/problem?id=3368 RMQ应用题. 解题思路参考:http://blog.csdn.net/libin56842/article/details/4 ...

随机推荐

  1. jQuery作用

    jquery是前端里面比较总要的,是很强大的一个选择器. 表单: 1.$(":input") 查找所有的input元素 2.$("text")    匹配所有的 ...

  2. div宽度设置无效问题解决

    问题描述: 要设置两个div在同一行显示,都加入了display:inline样式,但是其中一个div的宽度设置无效,在浏览器显示它的宽度始终是1003px. 解决办法: 方法1/给div加入样式:f ...

  3. SQL Server Management Studio 使用作业实现数据库备份

    1.数数据库备份脚本: 数据库备份:DECLARE @BcpFile VARCHAR(30),@SQLBACKUP VARCHAR(1000),@BcpFullFile VARCHAR(100) SE ...

  4. datazen 修改instanceid db_encryption_key

    切换到Datazen.Enterprise.Server.3.0.3327.exe 所在的目录 运行如下命令: Datazen.Enterprise.Server.3.0.3327.exe DATAZ ...

  5. jquery ui 插件------------------------->sortable

    <!doctype html><html lang="en"><head>  <meta charset="utf-8" ...

  6. 动软代码生成器三层用于winform

    DBUtility项目中的DbHelperSQL.cs (找自己对应的数据库类型) 修改前20行中的数据库连接字符串获取方式为: //数据库连接字符串(web.config来配置),多数据库可使用Db ...

  7. 第17章课后题(高级Perl技巧)

    17.1 写一个程序,从文件中读取一组字符串(每行一个),然后让用户键入模式以便进行字符串匹配. 对于每个模式,程序应该说明文件里共有多少字符串匹配成功,分别是哪些字符串. 对于所键入的每个新模式,不 ...

  8. 安装freebsd9 出现 mountroot>怎么办

    之前手欠把linux分区给删了想重装freebsd 重新进入的时候mbr提示grub信息 用PE把MBR删掉 之后再用freebsd光盘启动出现mountroot> 就用mountroot> ...

  9. paramiko SSH 模块简单应用。

    目的:需要ssh链接到Linux主机,执行telnet 命令,抓回显匹配制定内容. ssh --->执行telnet到本地端口--->执行类似 ls 的命令.匹配命令执行后的特定回显字段. ...

  10. spring mvc easyui tree 异步加载树

    使用spring mvc 注解 异步加载一棵树 jsp: <ul id="orgInfoTree"></ul> $(function(){ loadOrgT ...