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

Description

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

Input

The input consists of several test cases. Each test case starts with a line containing two integersn and
q (1 ≤ n, q ≤ 100000). The next line containsn integers
a1 , ... , an (-100000 ≤ ai ≤ 100000, for eachi ∈ {1, ..., n}) separated by spaces. You can assume that for each
i ∈ {1, ..., n-1}: ai ≤ ai+1. The followingq 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 single0.

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

Ulm Local 2007

题目链接:

id=3368">http://poj.org/problem?

id=3368



题目大意:有一串数字,查询区间中频数最大的数字的频数



题目分析:由于数字是按非递增序排列好的,我们能够先预处理出某连续数字在当前位置时出现的频数,比方例子有

dp[1]=1,val[1] = -1

dp[2]=2,val[2] = -1

dp[3]=1,val[3] = 1

dp[4]=2,val[4] = 1

dp[5]=3,val[5] = 1

dp[6]=4。val[6] = 1

。。。

则对于查询区间(l,r)。答案即为区间(l。tmp)和(tmp。r)某一数字出现的频数的较大的那个(l <= tmp <= r)

对于区间(l,tmp)直接可得出答案。对于区间(tmp, r)我们能够用RMQ求解



代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int const MAX = 100005;
int st[MAX][20], dp[MAX], val[MAX];
int n, q; void RMQ_Init()
{
for(int i = 1; i <= n; i++)
st[i][0] = dp[i];
int k = log((double)(n + 1)) / log(2.0);
for(int j = 1; j <= k; j++)
for(int i = 1; i + (1 << j) - 1 <= n; i++)
st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
} int Query(int l, int r)
{
if(l > r)
return 0;
int k = log((double)(r - l + 1)) / log(2.0);
return max(st[l][k], st[r - (1 << k) + 1][k]);
} int main()
{
while(scanf("%d", &n) != EOF && n)
{
scanf("%d", &q);
dp[1] = 1;
for(int i = 1; i <= n; i++)
{
scanf("%d", &val[i]);
if(i > 1)
dp[i] = (val[i] == val[i - 1] ? dp[i - 1] + 1 : 1);
}
RMQ_Init();
while(q--)
{
int l, r;
scanf("%d %d", &l, &r);
int tmp = l;
while(tmp <= r && val[tmp] == val[tmp - 1])
tmp ++;
printf("%d\n", max(Query(tmp, r), tmp - l));
}
}
}

field=source&key=Ulm+Local+2007">

POJ 3368 Frequent values (基础RMQ)的更多相关文章

  1. poj 3368 Frequent values(RMQ)

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

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

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

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

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

  4. poj 3368 Frequent values(RMQ)

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

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

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

  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 Description You are given a sequence of n integers a1 , a2 , .. ...

  8. [RMQ] [线段树] POJ 3368 Frequent Values

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

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

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

随机推荐

  1. SSH项目整合基本步骤

    SSH项目整合基本步骤 一.项目简介 该项目是由Spring4.Struts2 以及 Hibernate4 整合搭建的 web 项目,把Action分开编写,便于查看,使用JSTL.EL标签. 二.项 ...

  2. [xsy3343]程序锁

    题意:有两个序列,序列中数字$\in\{-1,0,1\}$ 有两个指针,初始时分别指向两个序列的开始位置,有一个初始为$0$的数$a$,重复以下过程直到两个指针都指向序列末尾后 如果一个指针指向末尾后 ...

  3. 批量将Java源代码文件的编码从GBK转为UTF-8

    主要参考: http://blog.csdn.net/liu_qiqi/article/details/38706497 使用common io批量将java编码从GBK转UTF-8 http://w ...

  4. Python168的学习笔记3

    list.extend(),可以拓展list,a=(0,1),b=(2,3) a.extend(b),a就变成(0,1,2,3) 分割字符串(除去字符串中的,\/;之类的),如果用str.split( ...

  5. Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索

    Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx ...

  6. 快速定位问题 Request无法获取参数

    比如说最近开发甲修改了iframe标签的src,开发乙在设置src的时候传入了2个参数,通过iframe标签链接到这个页面时,开发乙调试时发现没有拿到任何参数值.然后开发乙百度了一下,发现iframe ...

  7. SQLyog客户端无法连接MySQL服务器

    环境:centos下使用yum 命令安装了mysql服务 1.进入linux 通过命令service mysqld start启动mysql的服务 2.使用sqlyog 连接mysql发现连接不上,如 ...

  8. VC++ 设置控件显示文本的前景色、背景色以及字体

    在每个控件开始绘制之前,都会向其父窗口发送WM_CTLCOLOR通告消息,在该消息的处理函数中,可以设置控件显示文本的前景色.背景色以及字体.该消息处理函数还要求返回一个画刷的句柄,用于在控件具体的绘 ...

  9. Illegal instruction错误的定位---忽略编译期警告的代价

    在原计算机的linux c++程序可以正确跑起来,但是换了一台机器运行时出现致命错误,程序直接当掉,错误提示如下: Illegal instruction (core dumped) 造成改错的主要原 ...

  10. no device found for connection ‘ System eth0′

    解决办法: 1.删除/etc/udev/rules.d/70-persistent-net.rules文件,重启系统. 2.如果上面的不起作用,那么去看ifcfg-eth0文件中的HWADDR是否正确 ...