POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】
传送门:http://poj.org/problem?id=3368
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 23016 | Accepted: 8060 |
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
题意概括:
给出一串长度为 N 的非递减序列,和 M 次查询, 每次查询区间 【L,R】的出现频率最高的数的频率。
解题思路:
很妙的一种做法啊!
因为他是非递减数列 ,所以基于把每一个连续的相同的数作为同一块处理。
那么我们可以把查询区间分成两部分来处理。
第一部分:边界块,区间 起点 L 所处的块 和 区间 R 所处的块。
如果两者所处的块相同则说明,该区间在一个块内,区间内的数都相同,那么我们所求的最大频率就是当前区间长度了
如果两者所处的块不相同,那么说明除了这两个块还有其他的块,那么剩下的那些块就是接下来讨论的第二部分。
第二部分:很显然剩余的块都是连续的,完整的。那么RMQ维护的区间最大值妥妥的,没毛病(这里的最大值就是预处理的每一块的数出现的频率啦)。
AC code:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 1e5+; int num[MAXN];
int hh[MAXN];
int L[MAXN], R[MAXN];
int b[MAXN];
int dpmax[MAXN][];
int mm[MAXN]; void init_RMQ(int N, int a[])
{
mm[] = -;
for(int i = ; i <= N; i++){
mm[i] = ((i&(i-)) == )?mm[i-]+:mm[i-];
dpmax[i][] = b[i];
}
for(int ilen = ; ilen <= mm[N]; ilen++){
for(int i = ; i+(<<ilen)- <= N; i++)
dpmax[i][ilen] = max(dpmax[i][ilen-], dpmax[i+(<<(ilen-))][ilen-]);
}
} int get_RMQ(int LL, int RR)
{
int k = mm[RR-LL+];
return max(dpmax[LL][k], dpmax[RR-(<<k)+][k]);
} int main()
{
int N, M;
while(~scanf("%d", &N) && N){
scanf("%d", &M);
for(int i = ; i <= N; i++){
scanf("%d", &num[i]);
} int k = , ll = ;
memset(b, ,sizeof(b)); for(int i = ; i <= N; i++){
if(i > && num[i] != num[i-]){
for(int j = ll; j < i; j++){
L[j] = ll;
R[j] = i-;
}
b[k] = i-ll;
ll = i;
k++;
}
hh[i] = k;
} for(int j = ll; j <= N; j++){
L[j] = ll;
R[j] = N;
}
b[k] = N-ll+;
init_RMQ(k, b); int U, V;
while(M--){
scanf("%d%d", &U, &V);
int st = hh[U], ed = hh[V];
if(st == ed){
printf("%d\n", V-U+);
continue;
}
int ans = max(R[U]-U+, V-L[V]+);
st++, ed--;
if(st <= ed) ans = max(ans, get_RMQ(st, ed));
printf("%d\n", ans);
}
}
return ;
}
POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】的更多相关文章
- poj 3368 Frequent values(经典)【RMQ】
<题目链接> 题目大意: 给你一个长度为n的序列,这个序列每个数都有一个值,接下来进行q次询问,问在指定区间内出现次数最多的数出现了几次. 解题分析: 因为该序列是非降序的,所以该序列中的 ...
- POJ 3368 Frequent values RMQ ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
- POJ 3368 Frequent values (基础RMQ)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14742 Accepted: 5354 ...
- poj 3368 Frequent values(RMQ)
/************************************************************ 题目: Frequent values(poj 3368) 链接: http ...
- POJ 3368.Frequent values-处理数据+RMQ(ST)
昨天写的博客删了,占坑失败,还是先把RMQ玩的6一点再去搞后面的东西.废话少说,题解题姐姐_(:з」∠)_ Frequent values Time Limit: 2000MS Memo ...
- Poj 3368 Frequent values
/* 线段树区间合并 维护几个信息 到时候乱搞一下就好了 开始T了 有一种情况可以不用递归 直接算出来 */ #include<iostream> #include<cstdio&g ...
- poj 3368 Frequent values(段树)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13516 Accepted: 4971 ...
- POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)
题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...
- poj 3368 Frequent values(RMQ)
题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...
随机推荐
- 禅道和JIRA大对比
转自:https://blog.csdn.net/qq_40543535/article/details/78182636?locationNum=9&fps=1 禅道和JIRA大对比 置顶 ...
- SQL 工具系列二
1.RedGate 工具 SQL Prompt 脚步智能提示工具 脚步提示工具,轻松写入,编辑和探索SQL: SQL Prompt能根据数据库的对象名称,语法和用户编写的代码片段自动进行检索,智能的为 ...
- golang学习之win7下go web之revel安装
接着上回记录的win7下go环境搭建,go的开发,现在除了sublime外,LiteIDE比较推荐,下载链接 下载安装后直接打开,需要配置下go环境(本机使用的是window 386版本),如下: 打 ...
- Java 集合类常用方法
Collection中的contains()方法和remove()方法. boolean contains(Object o);该方法是用来判断集合中是否包含某个元素,若包含,返回true,不包含返回 ...
- Python之异常设计(一)
一 定义 异常分为两类:一类是自动触发异常如除零错误:另一类是通过raise触发. 二 为什么要使用异常 当程序运行时,如果检测到程序错误,Python就会引发异常,我们可以在程序中使用try语句捕获 ...
- PAT 1082. Read Number in Chinese
#include <cstdio> #include <cstdlib> #include <string> #include <vector> #in ...
- 深入理解jQuery插件开发总结(三)
容器:一个即时执行函数 根本上来说,每个插件的代码是被包含在一个即时执行的函数当中,如下: (function(arg1, arg2) { // 代码 })(arg1, arg2); 即时执行函数,顾 ...
- Java基础知识错误分析
答案:A,C 解析: 题目2: 答案:B 解析: 题目3: 答案:A 解析: 题目4: 答案:D 解析: 题目5: 答案:C 题目六: 答案:C 解析:
- RocketMQ读书笔记2——生产者
[生产者的不同写入策略] 生产者向消息队列里写入数据,不同的业务需要生产者采用不同的写入策略: 同步发送.异步发送.延迟发送.发送事务消息等. [DefaultMQProduce示例] public ...
- web安全-XSS
了解XSS的定义 跨站脚本攻击(Cross Site Scripting),为了不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻 ...