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. LOJ P3953 逛公园 NOIP dp 最短路 拓扑排序

    https://www.luogu.org/problemnew/show/P3953 开o2过了不开o2re一个点...写法如题 顺便一提这道题在我校oj是a不了的因为我校土豆服务器速度奇慢1s时限 ...

  2. 【贪心】Codeforces Round #480 (Div. 2) C. Posterized

    题意:让你对[0,255]这个序列任意划分成一些不重叠的子段,每个子段的大小不超过K.给你n个不超过255的数,让你将每个数替换成它所在子段的任意一个元素,使得最终这个n个数的序列的字典序最小. p[ ...

  3. 转 TCP/IP的三次握手与四次挥手详解

    TCP((Transmission Control Protocol)传输控制协议,是一个面向连接的协议.在运用此协议进行数据传输前都会进行连接的建立工作(三次握手):当数据传输完毕,连接的双方都会通 ...

  4. 【洛谷】3953:逛公园【反向最短路】【记忆化搜索(DP)统计方案】

    P3953 逛公园 题目描述 策策同学特别喜欢逛公园.公园可以看成一张N个点M条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,N号点是公园的出口,每条边有一个非负权值, 代表策策经过这条 ...

  5. Educational Codeforces Round 13 D. Iterated Linear Function 水题

    D. Iterated Linear Function 题目连接: http://www.codeforces.com/contest/678/problem/D Description Consid ...

  6. [Visual Studio] SOA服务框架搭建

    1.服务框架搭建 2.服务模板创建 3.Nuget引用 4.客户端调用 任务点: 1.分析SOA 2.修改SOA架构名称以及关键字 3.使用Nuget添加引用 4.选择服务模板进行创建 5.尝试调用 ...

  7. 0056 Spring MVC如何接收浏览器传递来的请求参数--request--形参--实体类封装

    浏览器总会向服务器传递一些参数,那么Spring MVC如何接收这些参数? 先写个简单的html,向服务器传递一些书籍信息,如下: <!DOCTYPE html> <html> ...

  8. 华为S5300系列交换机V100R006SPH019升级补丁

    S5300_V100R006SPH019.pat 附件: 链接:https://pan.baidu.com/s/1M1S5amGGViUieSp8lJ9psw  密码:sexx

  9. linux下TP5安装好Workerman 报错:Class 'think\worker\Server' not found

    今天把功能放到服务器,本地测试正常,上传到服务器上报错Class 'think\worker\Server' not found 首先想到的是Windows和Linux下大小写的问题,查看了代码,并没 ...

  10. Android4.2.2启动动画前播放视频

    首先声明測试平台为瑞芯微的rk3168,Android4.2.2,Android版本号非常重要,由于Android4.0和Android4.2.2的代码有些地方就有差别,并不通用! 首先接到任务不知怎 ...