UVA - 11235 Frequent values
University of Ulm Local Contest
Problem F: Frequent values
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
A naive algorithm may not run in time!
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; const int maxn=;
int cnt=;
struct node
{
int L,R,nb,v;
}N[maxn]; int dp[maxn+][];
int a[maxn+],kt[maxn+],n,m; int RMQ_init()
{
for(int i=;i<=cnt;i++) dp[i][]=N[i].nb;
for(int j=;(<<j)<=cnt;j++)
{
for(int i=;i+(<<j)-<=cnt;i++)
{
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
}
}
} int RMQ(int L,int R)
{
int k=;
while(R-L+>=(<<k)) k++; k--;
return max(dp[L][k],dp[R-(<<k)+][k]);
} int main()
{
while(scanf("%d",&n)&&n)
{
scanf("%d",&m);
///yu chu li
memset(N,,sizeof(N));
scanf("%d",a+);
cnt=;N[cnt].v=a[];N[cnt].L=;N[cnt].nb=;
for(int i=;i<=n;i++)
{
scanf("%d",a+i);
if(a[i]==N[cnt].v)
{
N[cnt].nb++;
}
else
{
cnt++;
N[cnt-].R=i-;
N[cnt].L=i;N[cnt].v=a[i];N[cnt].nb=;
}
if(i==n) N[cnt].R=n;
}
int pos=;
kt[]=-;
for(int i=;i<=cnt;i++)
{
int temp=N[i].nb;
while(temp--)
{
kt[pos]=i;
pos++;
}
}
RMQ_init();
while(m--)
{
int a,b,na=-,nb=-;
scanf("%d%d",&a,&b);
na=kt[a],nb=kt[b];
if(na==nb)
{
printf("%d\n",b-a+);
}
else if(na+==nb)
{
printf("%d\n",max(N[na].R-a+,b-N[nb].L+));
}
else
{
int ans1=N[na].R-a+,ans2=b-N[nb].L+,ans3=;
int L=na+,R=nb-;
ans3=RMQ(L,R);
printf("%d\n",max(ans1,max(ans2,ans3)));
}
}
}
return ;
}
UVA - 11235 Frequent values的更多相关文章
- RMQ算法 以及UVA 11235 Frequent Values(RMQ)
RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8) 内的最大值.数据量小 ...
- [POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]
2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...
- UVA 11235 Frequent values(RMQ)
Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...
- UVA 11235 Frequent values 线段树/RMQ
vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释**************** ...
- UVA 11235 Frequent Values ---RMQ
大白书上的例题,具体讲解见大白书,最好用用一个Log数组直接求k,这样就是纯O(1)了 #include <iostream> #include <cstdio> #inclu ...
- POJ 3368 & UVA 11235 - Frequent values
题目链接:http://poj.org/problem?id=3368 RMQ应用题. 解题思路参考:http://blog.csdn.net/libin56842/article/details/4 ...
- UVa 11235 Frequent values (RMQ && 区间出现最多次的数的次数)
题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间“缩小”,例如1 1 2 3 3 3就可以变成2 ...
- 数据结构(RMQ):UVAoj 11235 Frequent values
Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. I ...
- 11235 - Frequent values
<算法竞赛入门经典-训练指南>P198 记录一下区间的左右边界就可以了 #include <iostream> #include <stack> #include ...
随机推荐
- js 页面刷新location.reload和location.replace的区别小结
reload 方法,该方法强迫浏览器刷新当前页面. 语法: location.reload([bForceGet]) 参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里取当前 ...
- servlet过滤器使用
servlet的过滤器夹在用户和servlet之间,可以截获并预处理或者打断{用户到servlet的请求或servlet到用户的响应},但本身并不能处理和响应用户请求. filter类必须实现Filt ...
- [LeetCode] Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- 调用altera IP核的仿真流程—上
调用altera IP核的仿真流程—上 在学习本节内容之后,请详细阅读<基于modelsim-SE的简单仿真流程>,因为本节是基于<基于modelsim-SE的简单仿真流程>的 ...
- ROS中DDNS的使用
一.通过tool fetch更新ddns,关于此命令的使用,参考 tool fetch Scripts中添加脚本/tool fetch url="http://www.51kwl.com/? ...
- 读《单页web应用》-回顾作用域
js中没有块级作用域,只有全局作用域和函数作用域.全局变量可以在任何地方访问,局部变量只能在声明它的地方访问. var a=1; function func(){ var a=2; } console ...
- python基础补漏-05-生成器和装饰器
[1]生成器 很难用简单的语言描述生成器. 生成器:从字面上来理解,就是以某种规则为基础,不断的生成数据的工具 生成器函数: 在函数中如果出现了yield关键字,那么该函数就不再是普通函数,而是生成器 ...
- mysql在空闲8小时之后会断开连接(默认情况)
调试程序的过程发现,在mysql连接空闲一定时间(默认8小时)之后会断开连接,需要重新连接,也引发我对重连机制的思考.
- CADisplayLink 及定时器的使用
第一种: 用CADisplayLink可以实现不停重绘. 例子: CADisplayLink* gameTimer; gameTimer = [CADisplayLink displayLinkW ...
- Cordova应用程序修改启动画面或者Icon
1) 制作启动画面图片或icon ionic resources //同时生成icon和splash ionic resources --icon //只生成icon ionic resources ...