Frequent values

TimeLimit:3000Ms

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 Specification

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 Specification

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

题意:给出一个非降序排列的数组A1,A2。A3,……,An,对于一系列询问(i,j),输出Ai,A(i+1),……,Aj中出现次数最多的值出现的次数。

分析:由于整个数组是非降序的,全部相等元素会聚集在一起,这样就能够把这个数组进行游标编码。比方-1,1,1,2,2,2,4就能够编码成(-1,1),(1,2),(2,3),(4,1),当中(a,b)表示有b个连续的a。

用value[i]和count[i]分别表示第i段的数值和出现次数,num[p]、left[p]、right[p]分别表示位置p所在段的编号和左右端点位置,则每次查询时的结果为下面三部分的最大值:从L到L所在段的结束处的元素个数(即right[L]-L+1)、从R所在段的開始处到R处的元素个数(即R-left[R]+1)、中间第num[L]+1段到第num[R]-1段的count的最大值。这样问题就差点儿转化为了RMQ问题。

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int n, tot, Q;
int dp[N][20];
int num[N], cnt[N], Left[N], Right[N];
void RMQ_Init()
{
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= tot; i++)
dp[i][0] = cnt[i];
for(int j = 1; (1<<j) <= n; j++)
for(int i = 1; i + (1<<j) - 1 <= tot; i++)
dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
}
int RMQ(int L, int R)
{
if(L > R)
return 0;
int k = 0;
while((1<<(k+1)) <= R - L + 1) k++;
return max(dp[L][k], dp[R-(1<<k)+1][k]);
}
int main()
{
int v, last_v, i;
while(~scanf("%d",&n))
{
if(n == 0) break;
scanf("%d",&Q);
tot = 0;
memset(Left, 0, sizeof(Left));
memset(Right, 0, sizeof(Right));
memset(cnt, 0, sizeof(cnt));
for(i = 1; i <= n; i++)
{
scanf("%d",&v);
if(i == 1)
{
++tot;
last_v = v;
Left[tot] = 1;
}
if(last_v == v)
{
num[i] = tot;
cnt[tot]++;
Right[tot]++;
}
else
{
num[i] = ++tot;
cnt[tot]++;
Left[tot] = Right[tot] = i;
last_v = v;
}
}
RMQ_Init();
int L, R;
for(int i = 0; i < Q; i++)
{
scanf("%d%d",&L,&R);
if(num[L] == num[R])
printf("%d\n", R - L + 1);
else
{
int tmp1 = Right[num[L]] - L + 1;
int tmp2 = R - Left[num[R]] + 1;
int tmp3 = RMQ(num[L] + 1, num[R] - 1);
printf("%d\n",max(tmp1, max(tmp2, tmp3)));
}
}
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

UVA 11235 Frequent values(RMQ)的更多相关文章

  1. UVA-11235 Frequent values (RMQ)

    题目大意:在一个长度为n的不降序列中,有m次询问,每次询问(i,j)表示在区间(i,j)中找出出现次数最多的元素的出现次数. 题目分析:因为序列有序,可以将序列分段,并且记录每段的元素个数.每一个元素 ...

  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(RMQ)

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

  4. RMQ算法 以及UVA 11235 Frequent Values(RMQ)

    RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8)  内的最大值.数据量小 ...

  5. UVA 11235 Frequent values 线段树/RMQ

    vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释**************** ...

  6. UVA - 11235 Frequent values

    2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...

  7. [POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]

    2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...

  8. poj 1806 Frequent values(RMQ 统计次数) 详细讲解

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1806 题目大意:给你一个非降序排列的整数数组,你的任务是对于一系列的询问,(i,j),回答序列中出现次 ...

  9. UVa 11235 Frequent values (RMQ && 区间出现最多次的数的次数)

    题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间“缩小”,例如1 1 2 3 3 3就可以变成2 ...

随机推荐

  1. Android——与查询联系人相关的3张表

  2. 大数据实时处理-基于Spark的大数据实时处理及应用技术培训

    随着互联网.移动互联网和物联网的发展,我们已经切实地迎来了一个大数据 的时代.大数据是指无法在一定时间内用常规软件工具对其内容进行抓取.管理和处理的数据集合,对大数据的分析已经成为一个非常重要且紧迫的 ...

  3. .net三步配置错误页面,让你的站点远离不和谐的页面

    假设你的站点出现一堆让人看不懂的报错,那么你就不是一个合格的程序猿.也不是一个合格的站长. 以下的方面能够帮助你的站点远离让人头大的页面. 第一步:配置web.config 打开web.config, ...

  4. hdu 4277

    题目链接 #include <cstdio> //by-ACMer_xiaohao #include <cstring> #include <map> using ...

  5. __NSAutoreleaseNoPool(): ... utoreleased with no pool in place - just leaking

    __NSAutoreleaseNoPool(): ... utoreleased with no pool in place - just leaking 我的平台 mac os 10.6 Xcode ...

  6. information_schema模式表介绍 processlist

    在mysql里,我们一般通过show (full)processlist查看当前连接情况,处理各种数据库问题.现在在information_schema模式下,5.5以后增加了processlist表 ...

  7. 不显示系统错误对话框SetErrorMode(要学会搜索)

    关闭程序时报dde server window错误有人碰到过吗,用的别人的一个OCX控件,把这个控件去掉就不会报这个错误 //不显示系统错误对话框 SetErrorMode(SEM_NOGPFAULT ...

  8. 摘要算法CRC8、CRC16、CRC32,MD2 、MD4、MD5,SHA1、SHA256、SHA384、SHA512,RIPEMD、PANAMA、TIGER、ADLER32

    1.CRC8.CRC16.CRC32 CRC(Cyclic Redundancy Check,循环冗余校验)算法出现时间较长,应用也十分广泛,尤其是通讯领域,现在应用最多的就是 CRC32 算法,它产 ...

  9. SilkTest高级进阶系列10 – bitmap工具bitview

    SilkTest 提供了一个bitmap的工具,它可以帮助我们捕捉,比较bitmap文件.该bitmap工具的位置是~/SilkTest/bitview.exe 运行该工具后,你会发现它提供了捕捉控件 ...

  10. LINUX编程学习笔记(十四) 创建进程与 父子进程内存空间

    1什么是进程:进程是一个执行中的程序 执行的程序: 代码->资源->CPU 进程有很多数据维护:进程状态/进程属性 所有进程属性采用的一个树形结构体维护 ps  -a//所有进程 ps - ...