UVA 11235 Frequent values(RMQ)
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)的更多相关文章
- UVA-11235 Frequent values (RMQ)
题目大意:在一个长度为n的不降序列中,有m次询问,每次询问(i,j)表示在区间(i,j)中找出出现次数最多的元素的出现次数. 题目分析:因为序列有序,可以将序列分段,并且记录每段的元素个数.每一个元素 ...
- 【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(RMQ)
题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...
- RMQ算法 以及UVA 11235 Frequent Values(RMQ)
RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8) 内的最大值.数据量小 ...
- UVA 11235 Frequent values 线段树/RMQ
vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释**************** ...
- UVA - 11235 Frequent values
2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...
- [POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]
2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...
- poj 1806 Frequent values(RMQ 统计次数) 详细讲解
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1806 题目大意:给你一个非降序排列的整数数组,你的任务是对于一系列的询问,(i,j),回答序列中出现次 ...
- UVa 11235 Frequent values (RMQ && 区间出现最多次的数的次数)
题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间“缩小”,例如1 1 2 3 3 3就可以变成2 ...
随机推荐
- dataStage 7.5.1A
------------------------------ DataStage Server License ------------------------------ Serial Num ...
- 【ASP.NET Web API教程】2.1 创建支持CRUD操作的Web API
原文 [ASP.NET Web API教程]2.1 创建支持CRUD操作的Web API 2.1 Creating a Web API that Supports CRUD Operations2.1 ...
- zabbix 监控jmx 需要--enable-java
安装Javagateway如果原来已经安装zabbix,只需要再添加以下zabbix-java # tar zxvf zabbix-2.2.0.tar.gz # cd zabbix-2.2.0 # . ...
- 积累的VC编程小技巧之滚动条
1.设置滚动条的滚动大小 创建一个基于CScrollview的SDI Project(在第6步中选CScrollview) 若你已创建了,这步可以省略. 然后: 改为如 void CTestView: ...
- vc 制作图片资源dll
方法一: 使用纯WIN32 DLL方法封装纯资源第一步,通过VS2005建立WIN32 DLL 空工程第二步,设置配置属性->链接器->高级->无入口点(是/NOENTRY)设置配置 ...
- 使MYSQL能被外部访问_xeyuu_新浪博客
使MYSQL能被外部访问_xeyuu_新浪博客 使MYSQL能被外部访问 (
- 【问题解决】syntax error: unexpected end of file或-bash: ./full_build.sh: /bin/bash^M: bad interpreter: No
在阅读的过程中有不论什么问题,欢迎一起交流 邮箱:1494713801@qq.com QQ:1494713801 运行一个脚本full_build.sh 时, 一直是提示我: -bash: ./ ...
- LCD显示--Ht1621b芯片显示屏驱动
Ht1621b芯片显示屏驱动 关于HT1621b芯片的具体信息能够參考数据手冊上的内容:百度文库HT1621b中文资料 CS : 片选输入接一上拉电阻当/CS 为高电平读写HT1621的数据和命令无效 ...
- 爬虫框架: DotnetSpider
[开源 .NET 跨平台 数据采集 爬虫框架: DotnetSpider] [一] 初衷与架构设计 一 ,为什么要造轮子 有兴趣的同学可以去各大招聘网站看一下爬虫工程师的要求,大多是JAVA,PYTH ...
- SystemParametersInfo API学习(128个中文参数解释,215个实际值)
uiAction:该参数指定要查询或设置的系统级参数.其取值如下:SPI_GETACCESSTIMEOUT:检索与可访问特性相关联的超时段的信息,PvParam参数必须指向某个ACCESSTIMEOU ...