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. Struts2 (上)

    Struts2简介 Struts2框架的作用 Struts2是一个基于MVC设计模式的Web应用框架 它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controlle ...

  2. 撩课-Java每天5道面试题第13天

    撩课Java+系统架构点击开始学习 96.JDBC操作数据库的步骤 ? .加载数据库驱动 .创建并获取数据库链接 .创建jdbc statement对象 .设置sql语句 .设置sql语句中的参数(使 ...

  3. 使用ViewPager实现android软件使用向导的功能

    现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了. 先看下效果图: 首先需要一个布局文件,是FlameLayout组成的,里面包 ...

  4. Linux rescue

    适用场景: 当误操作修改系统启动文件/etc/fstab, /etc/rc.d/rc.sysinit时,就会造成系统启动时读取磁盘或初始化环境失败,导致linux无法正常启动,此时就可以借助Linux ...

  5. CentOS 7 与老版本CentOS防火墙配置的区别

    一.CentOS 7 以下版本防火墙的配置: 1.开放80,22,8080 端口/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT/sbin/ipt ...

  6. Git提交空文件夹的技巧

    这个只能说是技巧不能说是方法,原理是在每个空文件夹新建一个.gitignore文件,然后提交. 快捷命令: find . -type d -empty -exec touch {}/.gitignor ...

  7. MFC小程序------01 代码管理器

    1.代码入库: 2.代码查找: 3.查看全部代码: 4.程序设置: 自己学习MFC写的一个小程序,当中还有很多功能还待完好,比方数据库的导入功能还没有写,但导出功能是能够用的,查找算法也不是非常好,还 ...

  8. IT程序猿们,我该做什么选择呢

    这个时刻,我想我遇到人生小拐点了,程序猿到了30岁,到达了一个分界线了,现在的我该何去何从呢? 先谈下简单的情况吧: 来这个公司2年了,之前因为身体的原因,不想那么累,于是选择了一份维护的工作,就来了 ...

  9. 图片未完成加载显示loading

    <html> <title>js图片未加载完显示loading效果</title> <body> <style> img{float:lef ...

  10. Lucene新版本号对ConjunctionScorer的优化

    Lucene 4.0版本号的DocIdSetIterator中没有cost方法,而4.7.0则有这种方法,表示遍历整个DocIdSet的代价,对于DocsEnum就是其长度了,对于Scorer就能够是 ...