传送门:http://poj.org/problem?id=3368

Frequent values
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 维护区间频率最大值】的更多相关文章

  1. poj 3368 Frequent values(经典)【RMQ】

    <题目链接> 题目大意: 给你一个长度为n的序列,这个序列每个数都有一个值,接下来进行q次询问,问在指定区间内出现次数最多的数出现了几次. 解题分析: 因为该序列是非降序的,所以该序列中的 ...

  2. POJ 3368 Frequent values RMQ ST算法/线段树

                                                         Frequent values Time Limit: 2000MS   Memory Lim ...

  3. POJ 3368 Frequent values (基础RMQ)

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14742   Accepted: 5354 ...

  4. poj 3368 Frequent values(RMQ)

    /************************************************************ 题目: Frequent values(poj 3368) 链接: http ...

  5. POJ 3368.Frequent values-处理数据+RMQ(ST)

    昨天写的博客删了,占坑失败,还是先把RMQ玩的6一点再去搞后面的东西.废话少说,题解题姐姐_(:з」∠)_      Frequent values Time Limit: 2000MS   Memo ...

  6. Poj 3368 Frequent values

    /* 线段树区间合并 维护几个信息 到时候乱搞一下就好了 开始T了 有一种情况可以不用递归 直接算出来 */ #include<iostream> #include<cstdio&g ...

  7. poj 3368 Frequent values(段树)

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13516   Accepted: 4971 ...

  8. POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)

    题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...

  9. poj 3368 Frequent values(RMQ)

    题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...

随机推荐

  1. [转]象棋AI算法(一)

    本文转自:http://blog.csdn.net/u012723995/article/details/47133693 参考文献:http://www.xqbase.com/computer/se ...

  2. Expression Blend实例中文教程(7) - 动画基础快速入门Animation

    通过前面文章学习,已经对Blend的开发界面,以及控件有了初步的认识.本文将讲述Blend的一个核心功能,动画设计.大家也许注意到,从开篇到现在,所有的文章都是属于快速入门,是因为这些文章,都是我曾经 ...

  3. in和not in

    当子查询返回的列的值是多个值,那么就不能使用比较运算符(> < = !=),使用关键字in 语法: select …..from …..where 表达式 in (子查询) 常用in替换等 ...

  4. spring_boot 配置

    配置pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

  5. 在grid结果集中实现全选或全不选某些特定的行

    在script的中的代码如下: function check(){ var id = gridgetselectvalue("require_id"); if(id.length& ...

  6. Hql语句转化为sql语句中文乱码问题

    刚刚学习Hql语句就出现这一的问题,百度半天终于解决了,总结一下解决的方案: 出现中文乱码最可能的原因是hibernate配置文件配置的问题 1.检查url路径是否指定字符集为UTF-8 <pr ...

  7. thinkphp注册并写入数据到数据库中

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 小白学flask之路由,反向路由,路由参数

    # -*- coding: utf-8 -*- from flask import Flask, request, url_for app = Flask(__name__) @app.route(& ...

  9. css3之弹性盒模型初探(一)

    什么是弹性盒模型? 弹性盒模型是指在父级改变大小的时候内部的自己元素也会相应的改变大小,即子集会按照父级的大小按比例自适应大小. 弹性盒模型的提出可以解决一些响应式布局的需求   如何使用弹性盒模型? ...

  10. Java中的深拷贝和浅拷贝(转载)

    深拷贝(深复制)和浅拷贝(浅复制)是两个比较通用的概念,尤其在C++语言中,若不弄懂,则会在delete的时候出问题,但是我们在这幸好用的是Java.虽然java自动管理对象的回收,但对于深拷贝(深复 ...