题意: 给你一组数列, 查询区间内有出现次数最多的数的频数

RMQ ,

对于一个区间, 分为两部分, 从  L 开始连续到  T  , T + 1 到  R

显然 答案为  MAX (T – L + 1 , RMQ ( T+1, R))

对于 T, 可以先预处理出位置 Pos

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100000 + 131;
int Pos[maxn];
int Num[maxn];
int d[maxn][30];
int Fun[maxn];
int n, m; void RMQ_init()
{
for(int i = 0; i < n; ++i) d[i][0] = Fun[i];
for(int j = 1; (1<<j) <= n; ++j)
for(int i = 0; i + (1<<j) - 1 < n; ++i)
d[i][j] = max(d[i][j-1], d[i+(1<<(j-1))][j-1]);
} int RMQ(int L, int R)
{
int k = 0;
while((1<<(k+1)) <= R-L+1) k++;
return max(d[L][k], d[R-(1<<k)+1][k]);
} int main()
{
while(scanf("%d",&n) != EOF)
{
if(n == 0) break;
scanf("%d",&m);
for(int i = 0; i < n; ++i) scanf("%d",&Num[i]);
for(int i = 0; i < n; ++i)
{
if(i == 0) Fun[i] = 1;
else
{
if(Num[i] == Num[i-1]) Fun[i] = Fun[i-1] + 1;
else Fun[i] = 1;
}
}
//for(int i = 0; i < n; ++i) cout << Fun[i];
//cout <<endl; ////////////////////////
int Now = Num[n-1], pos = n-1;
for(int i = n-1; i >= 0; --i)
{
if(Num[i] == Now)
Pos[i] = pos;
else
{
Now = Num[i];
Pos[i] = i;
pos = i;
}
}
/*for(int i = 0; i < n; ++i) cout << Pos[i];
cout <<endl;*/
//////////////////////////
RMQ_init();
//cout << RMQ(2,5) <<endl;
int l, r;
for(int i = 0; i < m; ++i)
{
scanf("%d%d",&l,&r);
l--, r--;
int t = Pos[l];
//cout << t << endl;
if(t >= r)
{
printf("%d\n",r-l+1);
continue;
}
else
printf("%d\n", max(t-l+1,RMQ(t+1,r)));
}
}
}

POJ 3368的更多相关文章

  1. poj 3368 Frequent values(RMQ)

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

  2. POJ 3368 RMQ-ST

    一直感觉RMQ水,没自己写过,今天写了一道题,算是完全独立写的,这感觉好久没有了... 一直以来,都是为了亚洲赛学算法,出现了几个问题: 1.学的其实只是怎么用算法,对算法的正确性没有好好理解,或者说 ...

  3. poj 3368 Frequent values(RMQ)

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

  4. POJ 3368/RMQ/线段数

    题目链接 /* 给出一段序列,询问[L,R]区间内最大相同数的个数. 用一个很巧妙地方法,转化成求区间内的最大值的问题. RMQ维护区间最大值. MAX处理: */ for(int i=1;i< ...

  5. POJ 3368 & UVA 11235 - Frequent values

    题目链接:http://poj.org/problem?id=3368 RMQ应用题. 解题思路参考:http://blog.csdn.net/libin56842/article/details/4 ...

  6. poj 3368 Frequent values -Sparse-Table

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16537   Accepted: 5981 Description You ...

  7. poj 3368(RMQ模板)

    题目链接:http://poj.org/problem?id=3368 题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数. 求解RMQ问题的算法有:搜索(比较 ...

  8. POJ 3368 Frequent values (基础RMQ)

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

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

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

  10. POJ 3368 Frequent values(线段树区间合并)

    [题目链接] http://poj.org/problem?id=3368 [题目大意] 有一个有序序列,要求区间查询出现次数最多的数 [题解] 维护每个区间左端点和右端点,以及左右的长度,还有区间的 ...

随机推荐

  1. spingBoot整合mybatis+generator+pageHelper

    spingBoot整合mybatis+generator+pageHelper 环境/版本一览: 开发工具:Intellij IDEA 2018.1.4 springboot: 2.0.4.RELEA ...

  2. HTTP method constants

    HTTP method constants ngx.HTTP_GET ngx.HTTP_HEAD ngx.HTTP_PUT ngx.HTTP_POST ngx.HTTP_DELETE ngx.HTTP ...

  3. ios 不支持-,-时间。

    var date = "2000-08-29";  console.log(new Date(date).getTime());解决办法 date .replace(/-/g, ' ...

  4. ASP.NET Web API 2 使用 DelegatingHandler(委托处理程序)实现签名认证

    Ø  前言 在前一篇ASP.NET Web API 2 使用 AuthorizationFilter(授权过滤器)实现 Basic 认证文章中实现了采用 Basic 认证的方式,但是这种方式存在安全隐 ...

  5. 大规模数据导入和导出(mysql)

    测试数据2.5G,共有数据9427567条.用的mysql的large服务器的配置.load 一次需要大概10分钟左右.建的表用的是MYISAM,调整了几个session的参数值 SET SESSIO ...

  6. Commons Lang 介绍

    https://commons.apache.org/proper/commons-lang/ https://commons.apache.org/proper/commons-lang/javad ...

  7. hashMap源码解析(四)

    ---恢复内容开始--- 在上文中讲到了putval这个方法,这里继续: final V putVal(int hash, K key, V value, boolean onlyIfAbsent, ...

  8. ms sql 导出单个表数据

    然后都是下一步操作

  9. sublime text 3 3103 注册码

    —– BEGIN LICENSE —– Ryan Clark Single User License EA7E-812479 2158A7DE B690A7A3 8EC04710 006A5EEB 3 ...

  10. org.apache.flume.conf.ConfigurationException: Channel c1 not in active set.

    1 错误详细信息 WARN conf.FlumeConfiguration: Could not configure sink k1 due to: Channel c1 not in active ...