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 ...
随机推荐
- [css]全屏背景图片设置,django加载图片路径
<head><style type="text/css"> #bg { position:fixed; top:; left:; width:100%; h ...
- monkeyrunner之坐标或控件ID获取方法-续
在之前的文章中,介绍过控件坐标和ID的获取方法,这里,我们再介绍一个新的工具-uiautomatorviewer. Uiautomatorviewer是Android sdk自带的工具,位置在sdk/ ...
- JS--遍历对象
var person = { Name:"Frank", Age:23 } Object.keys(person).forEach(function(key){ console.l ...
- ActiveMQ笔记(7):如何清理无效的延时消息?
ActiveMQ的延时消息是一个让人又爱又恨的功能,具体使用可参考上篇ActiveMQ笔记(6):消息延时投递,在很多需要消息延时投递的业务场景十分有用,但是也有一个缺陷,在一些大访问量的场景,如果瞬 ...
- 《UNIX环境高级编程》笔记——2.标准和实现
随着UNIX各种衍生版本不断发展壮大,标准化工作就十分必要.其实干啥事都是这样,玩的人多了,必须进行标准化. 一.UNIX标准 1.1 ISO C(ANSI C) ANSI:Amerocan Nato ...
- 常用js归纳
一.获取地址栏参数 /*根据name获取URL参数*/ function getQueryString(name) { var reg = new RegExp("(^|&)&quo ...
- mybatis 一对一与一对多collection和association的使用
在mybatis如何进行一对一.一对多的多表查询呢?这里用一个简单的例子说明. 一.一对一 1.association association通常用来映射一对一的关系,例如,有个类user,对应的实体 ...
- linux 让程序在后台运行的几种可靠方法
我们经常会碰到这样的问题,用 telnet/ssh 登录了远程的 Linux 服务器,运行了一些耗时较长的任务, 结果却由于网络的不稳定导致任务中途失败.如何让命令提交后不受本地关闭终端窗口/网络断开 ...
- centos7 安装lnmp环境
准备工作 一.配置防火墙 vim /etc/sysconfig/iptables 开启80端口.3306.22端口 -A INPUT -m state --state NEW -m tcp -p tc ...
- ubuntu10.04配置XMAPP中的环境变量
1. 显示环境变量:[root@localhost ~]# echo $PATH/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/lo ...