POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)
id=3368
Description
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
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
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
Source
PS:
RMQ介绍+模板:http://blog.csdn.net/u012860063/article/details/40752197
代码例如以下:
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; const int maxn = 100017;
int num[maxn], f[maxn], MAX[maxn][20];
int n;
int max(int a,int b)
{
return a>b ? a:b;
}
int rmq_max(int l,int r)
{
if(l > r)
return 0;
int k = log((double)(r-l+1))/log(2.0);
return max(MAX[l][k],MAX[r-(1<<k)+1][k]);
}
void init()
{
for(int i = 1; i <= n; i++)
{
MAX[i][0] = f[i];
}
int k = log((double)(n+1))/log(2.0);
for(int i = 1; i <= k; i++)
{
for(int j = 1; j+(1<<i)-1 <= n; j++)
{
MAX[j][i] = max(MAX[j][i-1],MAX[j+(1<<(i-1))][i-1]);
}
}
}
int main()
{
int a, b, q;
while(scanf("%d",&n) && n)
{
scanf("%d",&q);
for(int i = 1; i <= n; i++)
{
scanf("%d",&num[i]);
}
sort(num+1,num+n+1);
for(int i = 1; i <= n; i++)
{
if(i == 1)
{
f[i] = 1;
continue;
}
if(num[i] == num[i-1])
{
f[i] = f[i-1]+1;
}
else
{
f[i] = 1;
} } init(); for(int i = 1; i <= q; i++)
{
scanf("%d%d",&a,&b);
int t = a;
while(t<=b && num[t]==num[t-1])
{
t++;
}
int cnt = rmq_max(t,b);
int ans = max(t-a,cnt);
printf("%d\n",ans);
}
}
return 0;
}
/*
10 3
-1 -1 1 2 1 1 1 10 10 10
2 3
1 10
5 10
*/
POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)的更多相关文章
- poj 3368 Frequent values(RMQ)
/************************************************************ 题目: Frequent values(poj 3368) 链接: http ...
- POJ 3368 Frequent values RMQ ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
- POJ 3368 Frequent values RMQ 训练指南 好题
#include<cstdio> #include<cstring> ; const int inf=0x3f3f3f3f; inline int max(int x,int ...
- POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】
传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS Memory Limit: 65536K Total S ...
- POJ 3368 Frequent values (基础RMQ)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14742 Accepted: 5354 ...
- (简单) 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变形了, 对 原数组重 ...
- POJ 3368 Frequent values 线段树与RMQ解法
题意:给出n个数的非递减序列,进行q次查询.每次查询给出两个数a,b,求出第a个数到第b个数之间数字的最大频数. 如序列:-1 -1 1 1 1 1 2 2 3 第2个数到第5个数之间出现次数最多的是 ...
- poj 3368 Frequent values -Sparse-Table
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16537 Accepted: 5981 Description You ...
随机推荐
- PHP动态函数处理
public class Student{ public function speek($name){ echo 'my name is '.$name; } } $method='speek'; $ ...
- ES6之用let,const和用var来声明变量的区别
var(掌握) 不区分变量和常量 用var声明的变量都是变量,都是可变的,我们可以随便对它进行运算操作.这样当多个人进行同一个项目时,区分变量和常量会越来越难,一不小心就会把设计为常量的数据更改了 ...
- ubuntu12.04开启Framebuffer
一.framebuffer概述 Framebuffer在Linux中是作为设备来实现的,它是对图形硬件的一种抽象,代表着显卡中的帧缓冲区(Framebuffer).通过Framebuffer设备,上层 ...
- 论wpf的设备无关性 - 简书
原文:论wpf的设备无关性 - 简书 WPF从发布之日起,一直将“分辨率无关(resolution independence)”作为其亮点,声称使用WPF制作的用户界面在轻巧的Ultra-Mobile ...
- THINKPHP实现搜索分页保留搜索条件
使用tp自带的分页类时,里面自带了POST查询条件保留机制,但是之针对于普通的map一维数组,如果包含like,gt等等比较复杂的查询条件则力不从心了. 带入查询条件 如果是POST方式查询,如何确保 ...
- 2015 Multi-University Training Contest 4 hdu 5334 Virtual Participation
Virtual Participation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- BZOJ——1602: [Usaco2008 Oct]牧场行走 || 洛谷—— P2912 [USACO08OCT]牧场散步Pasture Walking
http://www.lydsy.com/JudgeOnline/problem.php?id=1602 || https://www.luogu.org/problem/show?pid=2912 ...
- cogs 2752. [济南集训 2017] 数列运算
2752. [济南集训 2017] 数列运算 ★★☆ 输入文件:sequenceQBXT.in 输出文件:sequenceQBXT.out 简单对比时间限制:1 s 内存限制:512 ...
- spring boot基础
1.ANT下面典型的项目层次结构.(1) src存放文件.(2) class存放编译后的文件.(3) lib存放第三方JAR包.(4) dist存放打包,发布以后的代码. 2.Source Folde ...
- urlEncoder和urlDecoder的作用和使用
1.URLEncoder.encode(String s, String enc) 使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式 URLD ...