昨天写的博客删了,占坑失败,还是先把RMQ玩的6一点再去搞后面的东西。废话少说,题解题姐姐_(:з」∠)_ 
 
 
Frequent values
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20960   Accepted: 7403

Description

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

10 3
-1 -1 1 1 1 1 3 10 10 10
2 3
1 10
5 10
0

Sample Output

1
4
3

Source

 
 
这个题就是找数列区间中出现次数最多的数出现的次数,将数据处理一下,记录一下出现的次数,然后用RMQ区间查询最大值就可以得出结果。
这个处理很重要,首先是记录出现的次数,然后区间查询的时候,有可能将本来出现次数最多的拆开成不是最多的了,所以要处理一下。
举个例子。查询的区间为5---10,就是1 1 3 10 10 10,数据处理之后为3 4 1 1 2 3(因为前面的1是连续的,就会影响结果),这样找出来的最大值是4,但是正确的结果应该是3。
所以在RMQ之前,先判断要查询的左区间是否为前面连续的一部分,如果是的话,将区间右移,直到找到第一个第一次出现的数开始,这里就是找到处理数据之前数字3的位置,从3开始到10进行RMQ,然后将查询出来的最大值和一开始跳过去的连续的数的个数进行比较,这里就是将查询出来的结果3和一开始1出现的次数2进行比较,发现是3大,所以结果就是3。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=1e5+;
int f[maxn],num[maxn],mm[maxn][];
int n;
void ST(){
for(int i=;i<=n;i++)
mm[i][]=num[i];
int k=floor(log((double)n+)/log(2.0));//最多能走2的多少次方
for(int j=;j<=k;j++){
for(int i=;i+(<<j)-<=n;i++)
mm[i][j]=max(mm[i][j-],mm[i+(<<(j-))][j-]);
}
}
int RMQ(int l,int r){
if(l>r)return ;
int k=floor(log((double)(r-l+))/log(2.0));
return max(mm[l][k],mm[r-(<<k)+][k]);
}
int main(){
int q,a,b;
while(~scanf("%d",&n)&&n){
scanf("%d",&q);
for(int i=;i<=n;i++){
scanf("%d",&f[i]);
if(i==){
num[i]=;continue;
}
if(f[i]==f[i-])
num[i]=num[i-]+;
else
num[i]=;
}
ST();
while(q--){
scanf("%d%d",&a,&b);
int t=a;
while(t<=b&&f[t]==f[t-])t++;//处理一下连续的数对查询的影响,找到第一次出现的数的位置
int cnt=RMQ(t,b);
int ans=max(t-a,cnt);//将查询出来的最大值和开头跳过去的数的次数比较一下
printf("%d\n",ans);
}
}
return ;
}

总之,RMQ好像很厉害的样子,好多东西变形一下就可以用RMQ写,溜了,在写HDU的3183,写好之后写题解。

好菜啊,难受。

POJ 3368.Frequent values-处理数据+RMQ(ST)的更多相关文章

  1. poj 3368 Frequent values(经典)【RMQ】

    <题目链接> 题目大意: 给你一个长度为n的序列,这个序列每个数都有一个值,接下来进行q次询问,问在指定区间内出现次数最多的数出现了几次. 解题分析: 因为该序列是非降序的,所以该序列中的 ...

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

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

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

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

  4. POJ 3368 Frequent values (基础RMQ)

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

  5. poj 3368 Frequent values(RMQ)

    /************************************************************ 题目: Frequent values(poj 3368) 链接: http ...

  6. POJ 3368 Frequent values 线段树与RMQ解法

    题意:给出n个数的非递减序列,进行q次查询.每次查询给出两个数a,b,求出第a个数到第b个数之间数字的最大频数. 如序列:-1 -1 1 1 1 1 2 2 3 第2个数到第5个数之间出现次数最多的是 ...

  7. poj 3368 Frequent values(段树)

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

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

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

  9. poj 3368 Frequent values(RMQ)

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

随机推荐

  1. Ubuntux下简单设置vim

    我自己在vim下的设置,基本写简单脚本用的,在~/.vimrc作出如下设置 syntax on "高亮 set nu "行号显示 set smartindent "基于a ...

  2. 利用wget 和 curl 监控网站是否正常

    监控网站URL是否正常最常见的方法莫过于wget和curl命令了,这两个命令都是非常强大,参数也非常多,下面列举几个常用的参数. wget  常用命令参数:--spider              ...

  3. 小谈python里 列表 的几种常用用法

    在python中列表的常用方法主要包括增加,删除,查看和修改.下面以举例子的方法具体说明,首先我们创建两个列表,列表是用[ ]表示的,里面的元素用逗号隔开. a=[‘hello’,78,15.6,‘你 ...

  4. Linux学习-延伸正则表达式

    grep 默认仅支持基础正则表达式,如果要使用延伸型正则 表达式,你可以使用 grep -E , 不过更建议直接使用 egrep !直接区分指令比较好记忆!其 实 egrep 与 grep -E 是类 ...

  5. linux学习-CentOS 7 环境下大量建置账号的方法

    一些账号相关的检查工具 pwck pwck 这个指令在检查 /etc/passwd 这个账号配置文件内的信息,与实际的家目录是否存在等信息, 还可以比对 /etc/passwd /etc/shadow ...

  6. 再谈H2的MVStore与MVMap

    对H2的[MVStore]: http://www.cnblogs.com/simoncook/p/5188105.html 这篇文章的补充. 概述 我们通常用的map,比如HashMap Linke ...

  7. webdriver高级应用- 测试HTML5语言实现的视频播放器

    能够获取HTML5语言实现的视频播放器,视频文件的地址.时长,控制播放器进行播放或暂停播放等操作. #encoding=utf-8 import unittest from selenium impo ...

  8. Python面试题(练习二)

    1.用Python实现一个二分查找的函数. data = [1, 3, 6, 7, 9, 12, 14, 16, 17, 18, 20, 21, 22, 23, 30, 32, 33, 35] def ...

  9. day01_01.了解php

    1.了解PHP 第一个程序 echo 'hello world'; 和python的区别,python是 print (hello world) 并且python结尾没有;2.X版本不需要加括号,但是 ...

  10. [git 学习篇] 提交文件

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743256916071d ...