【POJ 3368】Frequent values(RMQ)
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
题意:
给出N个数和Q次询问区间[L,R],对于每个询问,找到区间内连续出现最多次的数,输出该次数。
题解:
首先将每个数的连续出现次数存入数组f[i],对于所询问区间内,将最左子区间的连续且相同数进行特殊处理,单独计算该数在此区间的次数,然后计算此区间内除该数外的rmq,两者取最值即可。
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const int MAX=;
int dp[MAX][],mm[MAX],f[MAX];
void initrmq(int n,int b[])
{
mm[]=-;
for(int i=;i<=n;i++)
{
mm[i]=((i&(i-))==)?mm[i-]+:mm[i-];
dp[i][]=f[i];
}
for(int j=;j<=mm[n];j++)
for(int i=;i+(<<j)-<=n;i++)
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
}
int rmq(int x,int y)
{
int k=mm[y-x+];
return max(dp[x][k],dp[y-(<<k)+][k]);
}
int main()
{
int n,q,i;
while(scanf("%d",&n)&&n)
{
scanf("%d",&q);
int b[MAX];
for(i=;i<=n;i++)
{
scanf("%d",&b[i]);
if(i==)
{
f[i]=;
continue;
}
if(b[i]==b[i-])
f[i]=f[i-]+;
else f[i]=;
}
initrmq(n,f);
for(i=;i<=q;i++)
{
int l,r;
scanf("%d%d",&l,&r);
int tl=l;
while(tl<=r&&b[tl]==b[tl-])tl++;
printf("%d\n",max(tl-l,rmq(tl,r)));
}
}
return ;
}
【POJ 3368】Frequent values(RMQ)的更多相关文章
- UVA 11235 Frequent values(RMQ)
Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...
- BZOJ 2296【POJ Challenge】随机种子(构造)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2296 [题目大意] 给出一个数x,求一个10的16次以内的数使得其被x整除并且数字包含 ...
- 【POJ 1273】Drainage Ditches(网络流)
一直不明白为什么我的耗时几百毫秒,明明差不多的程序啊,我改来改去还是几百毫秒....一个小时后:明白了,原来把最大值0x3f(77)取0x3f3f3f3f就把时间缩短为16ms了.可是为什么原来那样没 ...
- UVA-11235 Frequent values (RMQ)
题目大意:在一个长度为n的不降序列中,有m次询问,每次询问(i,j)表示在区间(i,j)中找出出现次数最多的元素的出现次数. 题目分析:因为序列有序,可以将序列分段,并且记录每段的元素个数.每一个元素 ...
- 【POJ - 3984】迷宫问题(dfs)
-->迷宫问题 Descriptions: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 ...
- poj 3368 Frequent values(RMQ)
题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...
- POJ 3368:Frequent values(线段树区间合并)
题目大意,给出一段非降序列,求一些区间中出现频率最高的数的出现次数. 分析: 显然,区间中一个数多次出现必然是连续的,也就是最长的连续相等的一段. 用线段树解决,维护三个信息:一个区间最长连续的区间的 ...
- 【POJ 2251】Dungeon Master(bfs)
BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...
- 【POJ - 1321】棋盘问题 (dfs)
棋盘问题 Descriptions: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘 ...
随机推荐
- iview table数据排序不正确
在使用iview的table组件时,要做排序 代码: sortable: true,sortMethod: (a, b, type) => { if (type === 'desc') { re ...
- Java的Final和C#的Const,Readonly比较分析(转载)
Java里面没有readonly关键字,预留了const的关键字,目前还没有实际用途,在Java中,跟这两个关键字比较接近的是final; C#中,两者都存在并可用.两者修饰的全局变量或局部变量都不能 ...
- EF多实体对应单表
1.EF多实体对应单表 适用场景:单数据库表,表数据有较长用字段,有不常用或者大数据字段. 2.建表语句 CREATE TABLE [Chapter2].[Photograph]( ,) primar ...
- hook的函数传入类
简单记录 比如要hook一个app包中一个类的private void c(dmp dmp1),其中dmp是个类,这种的处理的方式如下: 用cydiasubstrate hook框架 1.先通过hoo ...
- 在IDEA中设置方法自动注释(带参数和返回值)
第一部分 设置 打开设置面板 新建 在线模板 新建自动添加规则,注意 这里触发的字符 不能随便写 必须为 * Template text 区域 看上去有点乱,但是是为了显示时的对齐,该区域的内容如下( ...
- Markdown学习使用
本文记录Markdown的基础应用. 一.基础知识 Markdown 是一种标记语言 文件后缀名:.md 编辑工具:VSCode(visual studio code) VSCode中预览模式快捷键: ...
- LINUX中块设备文件和字符设备文件的本质区别
在LINUX系统文件类型分类的文章中我们提到了 块设备 和 字符设备文件,那么什么是块设备 字符设备文件呢?他们之间有什么本质上的区别呢? 设备文件分为 Block(块) Device Driver ...
- January 22 2017 Week 4 Sunday
Dare and the world always yields. 大胆挑战,世界总会让步. Try it if you dare. If you want to change, if you wan ...
- 经典优秀屏幕截图录像工具FastStone Capture推荐
FastStone Capture (FSCapture) 是经典好用的屏幕截图软件,还具有图像编辑和屏幕录制两大功能. FastStone Capture 主要功能 包括:1.截图功能(可以捕捉:活 ...
- sparsity and density
转:http://searchdatamanagement.techtarget.com/definition/sparsity-and-density 查了一下定义,以免会写错 Sparsity a ...