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 ...
随机推荐
- webdriver学习笔记
该篇文章记录本人在学习及使用webdriver做自动化测试时遇到的各种问题及解决方式,问题比较杂乱.问题的解决方式来源五花八门,如有疑问请随时指正一遍改正. 1.WebDriver入门 //webdr ...
- L1-009. N个数求和
https://www.patest.cn/contests/gplt/L1-009 原来写的找了好久还是有一个测试点没过, 虽说是道水题,但是今天一遍就过了还是挺高兴的. 送你机组数据 52/5 4 ...
- 使用JavaMail发送邮件
一.邮件的相关概念 邮件协议.主要包括: SMTP协议:Simple Mail Transfer Protocol,即简单邮件传输协议,用于发送电子邮件 POP3协议:Post Office Prot ...
- [No00009F]CMD在执行命令时的中断快捷键
有两种: Ctrl+C:完全中断.类似于C语言在while循环中的Break: Ctrl+Break(Pause键):单步中断.类似于C语言在while循环中的Continue:
- Life
24小时热水.24小时无线WiFi.24小时空调:有人陪你,不孤单,不寂寞,不瞎想,不停的传播正能量
- mysql 文件导入方法总结
数据导入3三种方法 一.phpMyAdmin 限制大小:2M 1.创建数据库 2.导入.sql或.sql.zip文件 大数据导入方法一:http://jingyan.baidu.com/article ...
- openwrt下部署adbyby去广告大师 免luci 带自启动,自动开启透明代理
最近朋友送了个360老路由器 C301,于是乎就掉进了智能路由器的坑, 玩智能路由器第一件事一定是去广告, 要么怎么对得起智能路由器- -! 路由器去广告当然首推广告屏蔽大师 www.adbyby.c ...
- Android开发之扫描二维码开发
原贴地址:http://www.cnblogs.com/Fndroid/p/5540688.html 二维码其实有很多种,但是我们常见的微信使用的是一种叫做QRCode的二维码,像下面这样的,可以放心 ...
- 踢出非法Linux用户
非法添加用户及非法进去的远程操作用户! 01.非法用户闯入系统 最简单的办法就是用 w 命令来检查. 如果确认有非法用户出现在系统内,可以立即 kill 用户相关进程. kill -9 `lsof ...
- 【WPF】新复制wpf项目报错
错误提示:Program does not contain a static 'Main' method suitable for an entry point 1.App.xaml 文件属性:生成操 ...