题目:http://poj.org/problem?id=3368

题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数。。

大白书上的 例题。。算是RMQ变形了,

对 原数组重新分段,并标记相同的个数为 该段的数值,然后RMQ...

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <algorithm>
  6. using namespace std;
  7. const int maxn = ;
  8. const int maxm = ;
  9.  
  10. int d_max[maxn][maxm],a[maxn];
  11. int n,t;
  12. int val[maxn],cnt[maxn],num[maxn],l[maxn],r[maxn];
  13.  
  14. void RMQ_init()
  15. {
  16. int i,j;
  17. memset(d_max,,sizeof(d_max));
  18. for(i = ; i <= t; i++)
  19. {
  20. d_max[i][] = cnt[i];
  21. }
  22. for(j = ; (<<j) <= t; j++)
  23. for(i = ; i + j - <= t; i++)
  24. {
  25. d_max[i][j] = max(d_max[i][j-], d_max[i + (<<(j-))][j-]);
  26. }
  27. }
  28.  
  29. int RMQ_max(int l, int r)
  30. {
  31. if(l>r)
  32. return ;
  33.  
  34. int k = ;
  35. while((<<(k+)) <= r-l+)
  36. k++;
  37. return max(d_max[l][k], d_max[r-(<<k)+][k]);
  38. }
  39. int main()
  40. {
  41. int i, j, le, rig;
  42. int q, ans, k, maxs;
  43. while(~scanf("%d",&n) && n)
  44. {
  45. scanf("%d",&q);
  46. for(i = ; i <= n; i++)
  47. {
  48. scanf("%d",&a[i]);
  49. }
  50. i = ;
  51. t = ;
  52. while(i <= n)
  53. {
  54. j = i;
  55. ans = ;
  56. while(a[j] == a[i] && j <=n)
  57. {
  58. j++;
  59. ans++;
  60. }
  61. for(k = i; k < j; k++)
  62. {
  63. num[k] = t; //位置k的编号
  64. l[k] = i; //位置k的最左端编号
  65. r[k] = j-; //位置k的最右端编号
  66. }
  67. val[t] = a[i]; //第i段的数值
  68. cnt[t] = ans; //第i段的出现次数
  69. t++; i = j;
  70. }
  71. RMQ_init();
  72. while(q--)
  73. {
  74. scanf("%d%d",&le, &rig);
  75. if(num[le] == num[rig])
  76. maxs = rig - le +;
  77. else
  78. {
  79. maxs = max(r[le] - le + , rig - l[rig] + );
  80. maxs = max(maxs, RMQ_max(num[le]+, num[rig]-));
  81. }
  82. printf("%d\n",maxs);
  83. }
  84. }
  85. return ;
  86. }

poj 3368 Frequent values(RMQ)的更多相关文章

  1. poj 3368 Frequent values(段树)

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13516   Accepted: 4971 ...

  2. POJ 3368 Frequent values (基础RMQ)

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14742   Accepted: 5354 ...

  3. UVA 11235 Frequent values(RMQ)

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

  4. UVA-11235 Frequent values (RMQ)

    题目大意:在一个长度为n的不降序列中,有m次询问,每次询问(i,j)表示在区间(i,j)中找出出现次数最多的元素的出现次数. 题目分析:因为序列有序,可以将序列分段,并且记录每段的元素个数.每一个元素 ...

  5. 【POJ 3368】Frequent values(RMQ)

    Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...

  6. poj 1806 Frequent values(RMQ 统计次数) 详细讲解

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1806 题目大意:给你一个非降序排列的整数数组,你的任务是对于一系列的询问,(i,j),回答序列中出现次 ...

  7. POJ 3368 Frequent values(线段树区间合并)

    [题目链接] http://poj.org/problem?id=3368 [题目大意] 有一个有序序列,要求区间查询出现次数最多的数 [题解] 维护每个区间左端点和右端点,以及左右的长度,还有区间的 ...

  8. (简单) POJ 3368 Frequent values,RMQ。

    Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...

  9. POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】

    传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. 【经验】Angularjs 中使用 layDate 日期控件

    layDate 控件地址:http://laydate.layui.com/ 前情:原来系统中使用的日期控件是UI bootstrap(地址:https://angular-ui.github.io/ ...

  2. Java 集合类(一)

    今天我们先讲一下Collection: Collection和Collections的区别: java.util.Collection是一种java集合接口,它提供了对集合对象的基本操作通用接口方法, ...

  3. BZOJ 1854: [Scoi2010]游戏 无向图判环

    题目链接: 题目 1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MB 问题描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装 ...

  4. HDU 5637 Transform 单源最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5637 题意: http://bestcoder.hdu.edu.cn/contests/contes ...

  5. 【转载】struct和typedef struct彻底明白了

    分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int a; }Stu; 于是在声明变量的时候就可 ...

  6. iOS开发之数据存取3-CoreData自定义数据类型

    当系统提供的类型不能达到我们的使用要求时,比如我想在CoreData中存储UIColor,该怎么办呢? 这时候就要用到CoreData中非常强大的一个存储类型了:Transformable 下面将通过 ...

  7. 转载一个不错的Scrapy学习博客笔记

    背景: 最近在学习网络爬虫Scrapy,官网是 http://scrapy.org 官方描述:Scrapy is a fast high-level screen scraping and web c ...

  8. Ignore files which are already versioned

    If you accidentally added some files which should have been ignored, how do you get them out of vers ...

  9. 浏览器后退按钮导致jquery动态添加的select option值丢失的解决方法

    监控浏览器返回功能 判断浏览器返回功能 禁用浏览器的后退按钮 JS禁止浏览器后退键 http://volunteer521.iteye.com/blog/830522/ 浏览器返回功能 判断上一页面来 ...

  10. 利用vim阅读源代码一个好用的工具

    阅读源代码时常常遇到找变量,函数定义的问题.vim为我们提供了一个好用的工具,ctags. 安装 ctags. 在 libvirt的源代码根目录运行 ctags -R . vim -t virConn ...