POJ 3368 Frequent values (基础RMQ)
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
题目链接:
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));
}
}
}
POJ 3368 Frequent values (基础RMQ)的更多相关文章
- poj 3368 Frequent values(RMQ)
题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...
- (简单) POJ 3368 Frequent values,RMQ。
Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...
- POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】
传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS Memory Limit: 65536K Total S ...
- poj 3368 Frequent values(RMQ)
/************************************************************ 题目: Frequent values(poj 3368) 链接: http ...
- POJ 3368 Frequent values RMQ ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
- poj 3368 Frequent values(段树)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13516 Accepted: 4971 ...
- POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)
题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...
- [RMQ] [线段树] POJ 3368 Frequent Values
一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同 ...
- poj 3368 Frequent values(经典)【RMQ】
<题目链接> 题目大意: 给你一个长度为n的序列,这个序列每个数都有一个值,接下来进行q次询问,问在指定区间内出现次数最多的数出现了几次. 解题分析: 因为该序列是非降序的,所以该序列中的 ...
随机推荐
- (Nginx和PHP下)URL重写,TP实现URL重写
UrlRewrite就是我们通常说的地址重写,用户得到的全部都是经过处理后的URL地址. 优点 一:提高安全性,可以有效的避免一些参数名.ID等完全暴露在用户面前,如果用户随便乱输的话,不符合规则的话 ...
- java多线程技术之条件变量
上一篇讲述了并发包下的Lock,Lock可以更好的解决线程同步问题,使之更面向对象,并且ReadWriteLock在处理同步时更强大,那么同样,线程间仅仅互斥是不够的,还需要通信,本篇的内容是基于上篇 ...
- hashmap的遍历方法
How to iterate over the entries of a Map? What is the order of iteration - if you are just using Map ...
- 可变参数模拟printf()函数实现一个my_print()函数以及调用可变参数需注意的陷阱
入栈规则 可变参数函数的实现与函数调用的栈帧结构是密切相关的.所以在我们实现可变参数之前,先得搞清楚 栈是怎样传参的. 正常情况下,C的函数参数入栈遵照__stdcall规则, 它是从右到左的,即函数 ...
- 重温PHP面向对象的三大特性
PHP面向对象的三大特性:封装性.继承性.多态性. 1. 封装性: 也称为信息隐藏,就是将一个类的使用和实现分开,只保留部分接口和方法与外部联系,或者说只公开了一些供开发人员使用的方法. 于是开发人员 ...
- Redis在Windows+linux平台下的安装配置(转)
window平台Redis安装 下载地址: http://code.google.com/p/servicestack/wiki/RedisWindowsDownload Redis文件夹有以下几个文 ...
- Android intent action大全
android.intent.action.ALL_APPSandroid.intent.action.ANSWERandroid.intent.action.ATTACH_DATAandroid.i ...
- VPP电源控制(VPP Power)-- 由DC-DC变换集成电路MC34063组成
http://www.willar.com/article/article_view.asp?id=463 由DC-DC变换集成电路MC34063组成,34063 广泛应用于于DC-DC的电源转换电路 ...
- 浅析Windows系统调用——2种切换到内核模式的方法
http://shayi1983.blog.51cto.com/4681835/1710861/
- [Winform]使用winform制作远程桌面管理工具
摘要 突然在园子里看到一篇远程连接的文章,觉得挺好玩的,就自己尝试能不能自己制作一个可以管理多台远程连接的工具,说做就做.当然这样的管理工具已经很多,纯粹是为了好玩,采用winform做的. 资料 首 ...