<题目链接>

题目大意:

给你一个长度为n的序列,这个序列每个数都有一个值,接下来进行q次询问,问在指定区间内出现次数最多的数出现了几次。

解题分析:

因为该序列是非降序的,所以该序列中的所有相等的数都是连续的,因此,我们可以先用dp预处理一下,dp[i]表示以第i个数结尾的最大连续相等个数,于是,本题目就变成了,在指定区间内,dp[i]的最大的值是多少。

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; const int M =1e5+;
int st[M][],dp[M],val[M];
int n,q;
void RMQ_init(){
for(int i=;i<=n;i++) //初始化
st[i][]=dp[i];
int k=log((double)(n+))/log(2.0);
for(int j=;j<=k;j++) //用倍增预处理ST表
for(int i=;i+(<<j)-<=n;i++)
st[i][j]=max(st[i][j-],st[i+(<<(j-))][j-]); //倍增递推式
}
int RMQ(int l,int r){
if(l>r)return ;
int k=log((double)(r-l+))/log(2.0);
return max(st[l][k],st[r-(<<k)+][k]);
}
int main(){
while(scanf("%d",&n)!=EOF,n){
scanf("%d",&q);
dp[]=;
for(int i=;i<=n;i++){
scanf("%d",&val[i]);
if(i>)dp[i]=(val[i]==val[i-]?dp[i-]+:); //dp[i]表示以第i个数结尾的最大连续相等个数
}
RMQ_init();
while(q--){
int l,r;
scanf("%d%d",&l,&r);
int tmp=l; //因为dp[i]表示以第i个数结尾的最大连续相等个数,而这里规定了区间的左下标,所以要对该区间与第一个值相同数的个数进行特殊处理,不能直接套用原来预先得到dp[l]
while(tmp<=r&&val[tmp]==val[tmp-])tmp++;
printf("%d\n",max(RMQ(tmp,r),tmp-l));
}
}
return ;
}

2018-10-19

poj 3368 Frequent values(经典)【RMQ】的更多相关文章

  1. POJ 3368 Frequent values (基础RMQ)

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

  2. poj 3368 Frequent values(RMQ)

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

  3. (简单) POJ 3368 Frequent values,RMQ。

    Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...

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

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

  5. poj 3368 Frequent values(RMQ)

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

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

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

  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. [RMQ] [线段树] POJ 3368 Frequent Values

    一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同 ...

随机推荐

  1. swift 学习- 21 -- 类型转换

    // 类型转换 可以判断实例的类型, 也可以将实例看做其父类的或者子类的实例 // 类型转换在 Swift 中使用 is 和 as 操作符实现, 这两个操作符提供了一种简单达意的方式去检查值的类型 或 ...

  2. 关于《common-net》的ftp上传

    1:jar的maven的引用: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...

  3. Confluence 6 导入模板的备注

    创建你自己的模板组件(template bundles).你可以使用插件(add-on,也可以被称 plugin)来创建模板组件然后将这些模板组件上传到你的 Confluence 站点中.你可以从你的 ...

  4. Jquery如何获取iframe里面body的html呢?

    如果是自己网页的话,可以这样,$("iframe").contents().find("body").html();意思是,获取iframe里面页面body的内 ...

  5. Bootstrap补充

    一.一个小知识点 1.截取长屏的操作 2.设置默认格式 3.md,sm, xs 4.空格和没有空格的选择器 二.响应式介绍 - 响应式布局是什么? 同一个网页在不同的终端上呈现不同的布局等 - 响应式 ...

  6. linux三剑客

    grep grep       "oldboy"     test.txt   过滤掉文件中oldboy的字符串 -v                               ...

  7. windows下bat批处理执行sql语句__Mysql

    直接上代码: @ECHO OFF SET dbhost=主机名(例如:127.0.0.1)SET dbuser=用户名(例如:root)SET dbpasswd=用户密码(例如:root)SET db ...

  8. oracle数据库无法连接 The Network Adapter could not establish

    Caused by: java.sql.SQLException: Io 异常: The Network Adapter could not establish the connection 这个错误 ...

  9. c++ 链表基础功能实现

    #include<stack> struct ListNode { int m_nValue; ListNode* m_pNext; }; ListNode* CreateListNode ...

  10. What is base..ctor(); in C#?

    I am disassembling some C# applications and I am trying to reconstruct the source code. I am disasse ...