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 ...
随机推荐
- 计算机内存碎片(中)——外部碎片化(内存 & 文件系统 & 数据库系统通杀)
本文原创为freas_1990,转载请标明出处:http://blog.csdn.net/freas_1990/article/details/17252221 外部碎片化 当计算机内存被划分成很细碎 ...
- 重操JS旧业第一弹:Script与JS加载
不管js被包装成什么样子,最终交给浏览器执行的js都是原生的,都离不开原生js的原理. Script标签纸html中用来加载js的标签,我们知道js可以是来自外部,本地,或者内部一段代码,在这里只讨论 ...
- express for node 路由route几种实现方式的思考
1.路由实现方式和顺序 express框架创建的模板app,js中默认代码 var express = require('express'); var routes = require('./rout ...
- 编译Boost 详细步骤 适用 VC6 VS2003 VS2005 VS2008 VS2010
vs2008编译boost [一.Boost库的介绍] Boost库是一个经过千锤百炼.可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一.Boost库由C++标准委员会库 ...
- sublime搜索和替换--正则
Search and Replace Sublime Text features two main types of search: Search - Single File Search - Mul ...
- 12306 Android客户端的libcheckcode.so解密及修复
源:http://blog.csdn.net/justfwd/article/details/45219895 这篇文章纯粹属于安全分析研究,请勿用于非法用途.如有侵犯到厂家,请告知作者删除 123 ...
- MFC 对话框中动态创建N级菜单以及响应事件
创建一个基于对话框的工程,工程名为CreateMenu 为该对话框增加一个文件菜单项和测试菜单项,如下图所示 测试菜单项至少要有一个子菜单项 在对话框属性中关联该菜单 在resource.h中增加 ...
- 使用Xcode无法发布程序(Archive按钮一直为灰色不可点击)
问题现象:想在Xcode中把代码编译发布成ipa程序,但“Product”->“Archive”按钮一直不可使用. 解决办法:目前的运行配置是使用模拟器,改成“iOS Device”即可 ...
- TP-LINK无线路由器WR340G+ 54M支持WDS - 东莞市泰讯电子科技有限公司
TP-LINK无线路由器WR340G+ 54M支持WDS - 东莞市泰讯电子科技有限公司 TP-LINK无线路由器WR340G+ 54M支持WDS 品牌 TP-LINK无线路由器 型号 WR340 ...
- 深度学习系列之CNN核心内容
导读 怎么样来理解近期异常火热的深度学习网络?深度学习有什么亮点呢?答案事实上非常简答.今年十月份有幸參加了深圳高交会的中科院院士论坛.IEEE fellow汤晓欧做了一场精彩的报告,这个问题被汤大神 ...