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. Python pip 安装包

    Python 第三方包可以使用pip 更容易地安装,和管理 pip 的下载地址 https://pypi.python.org/pypi/pip/ pip的使用以及相关文档参考 https://pip ...

  2. 7个基于Linux命令行的文件下载和网站浏览工具

    7个基于Linux命令行的文件下载和网站浏览工具 时间:2015-06-01 09:36来源:linux.cn 编辑:linux.cn 点击: 2282 次 Linux命令行是GNU/Linux中最神 ...

  3. HDU-1039-Easier Done Than Said?(Java &amp;&amp; 没用正則表達式是我的遗憾.....)

    Easier Done Than Said? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  4. 暂停和屏蔽右键网页中的Flash

    如何暂停网页中的Flash?原理很简单,就是屏蔽Flash的消息即可.屏蔽右键也可以通过此方法 直接贴代码吧,加了注释,很容易就能懂了 新建工程,加一个WebBrowser,再加两个按钮.Flash ...

  5. Delphi 模拟网站验证码(酷,把随机文字写道图片上)

    function TfrmLabelPages.PwdImg(img: Timage): string; var I,j,k: Integer; vPoint: TPoint; vLeft: Inte ...

  6. enum可以做索引

    enum可以做索引 enum可以做索引, 配上虚函数,或者函数指针,可以实现上层的统一封装和快速索引. 点击(此处)折叠或打开 MoTbl.cpp #include <stdio.h> # ...

  7. 在bmp上添加字符

    //打开位图文件,得到位图句柄   HBITMAP OpenBmpFile(HDC hDC, LPSTR lpszFileName)   {       HBITMAP hBmp = NULL;    ...

  8. Linux内核驱动开发之KGDB原理介绍及kgdboe方式配置

    接博文<Linux内核驱动开发之KGDB单步调试内核(kgdboc方式)>.上篇博文中,仅简单介绍使用串口的Kgbd的流程(kgdboc方式),本文将重点介绍KGDB调试Linux内核的原 ...

  9. [置顶] Cocos2d-x 实例源码分析之二 小实例的主框架

    这篇文章是分析第一个小实例ActionTest的源码.其实所有实例程序的结构都是一样的,只有特定方法里的代码不同,大的框架都是一样的.也就是说看完这篇文章你就可以自己开始分析其他源码了. 废话不多说, ...

  10. form不提交问题

    var confirmOrderForm=document.getElementById("confirmOrderForm"); var url="${pageCont ...