Frequent values

Time Limit: 2000MS Memory Limit: 65536K

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

题意:

题目要求我们求出在一段区间中连续的相同的数字的最大值是多少。

思路:

看到区间求最大值的问题,可以想到是RMQ问题,然后我们可以将连续且相等的次数存进对应的数组里面,接着查询最大的次数就行了,但是需要注意的是。假如刚开始有部分在外面的时候,比如1 1 1 2 2,查询2 5,如果按照之前的思维,那么最大的就是num[3]=3,查询出来的也就会是3了,但是确实2,所以要提前将1 1 1单独提取出来进行比较判断。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100010;
int num[maxn], dp[maxn][20], mm[maxn];
void BuildRmq(int n) {mm[0] = -1;for (int i = 1; i <= n; i++) {mm[i] = mm[i - 1];if ((i & (i - 1)) == 0) mm[i]++;dp[i][0] = num[i];}for (int j = 1; j <= mm[n]; j++) {for (int i = 1; i + (1 << j) - 1 <= n; i++) {dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);}}
}
int Rmq(int l, int r) {int k = mm[r - l + 1];return max(dp[l][k], dp[r - (1 << k) + 1][k]);
}
int main() {int n, m, x, temp = -1;while (scanf("%d", &n) != EOF && n) {memset(num, 0, sizeof(num));memset(dp, 0, sizeof(dp));scanf("%d", &m);for (int i = 1; i <= n; i++) {scanf("%d", &x);if (x == temp) num[i] = num[i - 1] + 1;else {temp = x;num[i] = 1;}}BuildRmq(n);int l, r;while (m--) {scanf("%d %d", &l, &r);int temp = l;while (temp <= r && num[temp] == num[temp - 1] + 1) temp++;printf("%d\n", max(Rmq(temp, r), temp - l));}}return 0;
}

POJ 3368 Frequent values相关推荐

  1. POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】

    传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  2. POJ 3368 Frequent values (RMQ)

    题目链接:http://poj.org/problem?id=3368 题目大意: 给出一个n个数长度的串. m个询问 求出给定范围内的最大连续字符串的长度 方法: RMQ模板, 记录每一个位置的数连 ...

  3. POJ 3368 Frequent values 线段树区间合并

    题意O(-1)不用解释.. 线段树结点维护三个信息:区间内相同的数出现最多的次数maxc.区间左边第一个数出现的次数lc.区间右边第一个数出现的次数rc. 分左区间右端点和右区间左端点相同于否的情况合 ...

  4. poj 3368 Frequent values rmq

    题意:给你一串数列,然后给你一个起终位置对,问你这个段序列里出现最多的出现了几次. 思路:一段序列里,无非就是三种情况,1,一组相同的数,2两组相同,3,3组或者更多组数,这样我们队每一组数的开头结束 ...

  5. 【线段树】FREQUENT - Frequent values(luogu-SP1684 / poj 3368)

    FREQUENT - Frequent values luogu-SP1684 poj 3368 题目大意: 有一个单调不降序列,让你求出某些区间内的出现次数最多的数出现的次数(有多组数据,以0结尾) ...

  6. UVA 11235 Frequent values(RMQ)

    Frequent values TimeLimit:3000Ms You are given a sequence of n integers a1 , a2 , ... , an in non-de ...

  7. Frequent values【线段树】

    Frequent values UVA - 11235 题目传送门 题目大意:给出一个非降序的整数数组a1,a2,a3...an,你的任务是对一系列的询问(i,j),回答ai,ai+1,ai+2... ...

  8. POJ 2785 4 Values whose Sum is 0

    传送门:http://poj.org/problem?id=2785 解题思路: 从这四个数列中选择的话总有n的4次方中情况,所以全部判断一遍不可行.不过将他们对半分成AB和CD再考虑的话就可以解决了 ...

  9. hdu 1806 Frequent values 线段树

    题目链接 给一个非递减数列, n个数, m个询问, 每个询问给出区间[L, R], 求这个区间里面出现次数最多的数的次数. 非递减数列, 这是最关键的一个条件... 需要保存一个区间最左边的数, 最右 ...

  10. 【图灵杯 E也即POJ 3368】简单的RMQ

    Description 给定一个数组,其中的元素满足非递减顺序.任意给定一个区间[i,j],求其中某个元素重复出现的最大次数. Input 多组数据输入.每组数据的第一行包含两个整数n和q(1< ...

最新文章

  1. 如何在 CentOS 上启用 软件集 Software Collections(SCL)
  2. 树莓派设置音频输出音量
  3. 「独家」五面阿里P6:Java开发面试题及答案
  4. Jzoj4778 数列编辑器
  5. 中国离婚大数据:离婚/结婚比东北三省和四大直辖市霸榜
  6. 数据结构实验 寻找数组主元素(2013考研题)
  7. js获取浏览器和设备相关width(屏幕的宽度)
  8. ES6学习之路10----Symbol
  9. 热电厂sis系统服务器升中标结果,电厂SIS系统简介
  10. ORAN专题系列-19:5G O-RAN FrontHaul前传接口M Plane互操作性测试IOT规范
  11. 查看电脑ip地址是否被占用
  12. Adroid11,拍照,裁剪以及保存图片
  13. Java DateUtil 时间工具类
  14. 微信小程序开发01 双线程模型:为什么小程序不用浏览器的线程模型?
  15. uc打开html文件是空的,UC浏览器中打开不出现主页的解决方法
  16. kylin如何支持flink_Flink 在快手实时多维分析场景的应用
  17. 2015 上海邀请赛c题 calculator hdu5238
  18. 使用telnet发送附件邮件
  19. C语言指针与结构体详述
  20. 纪念相对论发表110周年

热门文章

  1. 01 JavaScript的前世今生
  2. JSP完成添加商品时的图片上传
  3. git - 1.基础
  4. 面试官:你能说一下 什么是熔断?什么是服务降级吗?
  5. linux 配置回指路由,不配置回指路由多网段网络如何互联?
  6. 算24(递归)--算法学习
  7. 299. 猜数字游戏【我亦无他唯手熟尔】
  8. pygame外星人入侵
  9. 身高预测和体脂判断,选择结构练习,C语言
  10. bzoj4484[Jsoi2015]最小表示 拓补排序+bitset