1. /************************************************************
  2. 题目: Frequent values(poj 3368)
  3. 链接: http://poj.org/problem?id=3368
  4. 题意: 给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之
  5. 间连续出现次数最多的次数
  6. 算法: RMQ
  7. 思路: 借助数组f[i]。表示第i位前面有f[i]个相同的数。对于
  8. 每个区间(l,r)。暴力求前面几个相同的数。然后在用RMQ
  9. 求后面区间的值。
  10. *************************************************************/
  11. #include<cstdio>
  12. #include<cstring>
  13. #include<cstdlib>
  14. #include<algorithm>
  15. #include<iostream>
  16. #include<cmath>
  17. using namespace std;
  18.  
  19. const int mx=;
  20. int dp[mx][];
  21. int a[mx],f[mx];
  22. int n,q;
  23.  
  24. void makermq()
  25. {
  26. for (int i=;i<=n;i++) dp[i][]=f[i];
  27. for (int j=;(<<j)<=n;j++)
  28. {
  29. for (int i=;i+(<<j)-<=n;i++)
  30. {
  31. dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
  32. }
  33. }
  34. }
  35.  
  36. int rmq(int u,int v)
  37. {
  38. if (u>v) return ;
  39. int k=(int)(log(v-u+)/log(2.0));
  40. return max(dp[u][k],dp[v-(<<k)+][k]);
  41. }
  42.  
  43. int main()
  44. {
  45. while (~scanf("%d",&n)&&n)
  46. {
  47. scanf("%d",&q);
  48. for (int i=;i<=n;i++) scanf("%d",&a[i]);
  49. f[]=;
  50. for (int i=;i<=n;i++)
  51. {
  52. if (a[i]==a[i-]) f[i]=f[i-]+;
  53. else f[i]=;
  54. }
  55. makermq();
  56.  
  57. while (q--)
  58. {
  59. int l,r;
  60. scanf("%d%d",&l,&r);
  61. int ans=;
  62. for (l=l+;l<=r;l++)
  63. {
  64. if (a[l]!=a[l-]) break;
  65. ans++;
  66. }
  67. ans=max(ans,rmq(l,r));
  68. printf("%d\n",ans);
  69. }
  70. }
  71. }

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

  1. POJ 3368 Frequent values RMQ ST算法/线段树

                                                         Frequent values Time Limit: 2000MS   Memory Lim ...

  2. POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)

    题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...

  3. POJ 3368 Frequent values RMQ 训练指南 好题

    #include<cstdio> #include<cstring> ; const int inf=0x3f3f3f3f; inline int max(int x,int ...

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

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

  5. POJ 3368 Frequent values (基础RMQ)

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

  6. poj 3368 Frequent values(段树)

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

  7. poj 3368 Frequent values(RMQ)

    题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...

  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. [RMQ] [线段树] POJ 3368 Frequent Values

    一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同 ...

随机推荐

  1. yii2 使用composer安装

    composer global require "fxp/composer-asset-plugin:~1.0.0" composer create-project --prefe ...

  2. SQL 2000/2005/2008 收缩日志方法

    一般情况下,SQL数据库的收缩并不能很大程度上减小数据库大小,其主要作用是收缩日志大小,应当定期进行此操作以免数据库日志过大. 方法一:清空日志.1.设置数据库模式为简单模式:打开SQL企业管理器,在 ...

  3. 21副GIF动图让你了解各种数学概念

    baidu 21副GIF动图让你了解各种数学概念

  4. 通过List<String>动态传递参数给 sqlcommand.Parameters

    通过List<String>动态传递参数 private void GetallChecked_TreeNote(TreeNodeCollection aNodes, ref int To ...

  5. MVC4.0网站发布和部署到IIS7.0上的方法

    最近在研究MVC4,使用vs2010,开发的站点在发布和部署到iis7上的过程中遇到了很多问题,现在将解决的过程记录下来,以便日后参考,整个过程主要以截图形式呈现 vs2010的安装和mvc4的安装不 ...

  6. Unity5版本的AssetBundle打包方案之打包Scene场景

    using UnityEngine; using System.Collections; using UnityEditor; /// <summary> /// 脚本位置:Editor文 ...

  7. [ActionScript 3.0] AS3.0 下雨及涟漪效果

    帧代码: stage.frameRate = 80; function init(x1:Number,y1:Number) { var mc:MovieClip=new MovieClip(); ad ...

  8. tar打包排除某个目录

    tar zcvf fd.tar.gz * --exclude=file1 --exclude=dir1 注意: 1.--exclude=file1 而不是 --exclude file1 2.要排除一 ...

  9. day6-2面向对象

    概述: 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 注:Java和C#来 ...

  10. Chap6: question 46 - 48

    46. 求 1+2+ … +n. 要求:不用乘除法.for.while.if.else.switch.case 以及条件判断语句(A?B:C). a. 利用构造函数求解 #include <io ...