/************************************************************
题目: Frequent values(poj 3368)
链接: http://poj.org/problem?id=3368
题意: 给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之
间连续出现次数最多的次数
算法: RMQ
思路: 借助数组f[i]。表示第i位前面有f[i]个相同的数。对于
每个区间(l,r)。暴力求前面几个相同的数。然后在用RMQ
求后面区间的值。
*************************************************************/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std; const int mx=;
int dp[mx][];
int a[mx],f[mx];
int n,q; void makermq()
{
for (int i=;i<=n;i++) dp[i][]=f[i];
for (int j=;(<<j)<=n;j++)
{
for (int i=;i+(<<j)-<=n;i++)
{
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
}
}
} int rmq(int u,int v)
{
if (u>v) return ;
int k=(int)(log(v-u+)/log(2.0));
return max(dp[u][k],dp[v-(<<k)+][k]);
} int main()
{
while (~scanf("%d",&n)&&n)
{
scanf("%d",&q);
for (int i=;i<=n;i++) scanf("%d",&a[i]);
f[]=;
for (int i=;i<=n;i++)
{
if (a[i]==a[i-]) f[i]=f[i-]+;
else f[i]=;
}
makermq(); while (q--)
{
int l,r;
scanf("%d%d",&l,&r);
int ans=;
for (l=l+;l<=r;l++)
{
if (a[l]!=a[l-]) break;
ans++;
}
ans=max(ans,rmq(l,r));
printf("%d\n",ans);
}
}
}

poj 3368 Frequent values(RMQ)的更多相关文章

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

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

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

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

  3. POJ 3368 Frequent values RMQ 训练指南 好题

    #include<cstdio> #include<cstring> ; const int inf=0x3f3f3f3f; inline int max(int x,int ...

  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 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14742   Accepted: 5354 ...

  6. poj 3368 Frequent values(段树)

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

  7. poj 3368 Frequent values(RMQ)

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

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

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

  9. [RMQ] [线段树] POJ 3368 Frequent Values

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

随机推荐

  1. localStorage兼容ie6/7 用addBehavior 实现

    制作过程我就不说了,程序下面会占出来 define(function(){ if('localStorage' in window) return; function Storage(){ this. ...

  2. GridView使用自带分页功能时分页方式及样式PagerStyle

    // 转向地址:http://www.bubuko.com/infodetail-412562.html GridView分页,使用自带分页功能,类似下面样式: 在aspx页面中,GridView上的 ...

  3. php发送post包

    class Request{ public static function post($url, $post_data = '', $timeout = 5){//curl $ch = curl_in ...

  4. CDN的实现原理

    在描述CDN的实现原理,让我们先看传统的未加缓存服务的访问过程,以便了解CDN缓存访问方式与未加缓存访问方式的差别: 用户提交域名→浏览器对域名进行解释→得到目的主机的IP地址→根据IP地址访问发出请 ...

  5. ADF_ManagedBean的概念和管理(概念)

    20150623 Created By BaoXinjian

  6. git初始化

    git init:初始化 git status:查看当前目录下文件状态 git add -A(表示添加当前目录下所有文件)/文件名(表示只添加该一个文件) git commit -m '':提交到本地 ...

  7. 根据 MySQL 状态优化 ---- 2. 连接数

    查看 MySQL 服务器运行的各种状态值: mysql> show global status: 2. 连接数 查看设置的最大连接数: mysql> show variables like ...

  8. 无法连接远程SQL Server 数据库的原因

    1. tcp协议开启 2. 1433端口是否添加防火墙例外

  9. oracle PL/SQL管理命令语句

    一.ORACLE的启动和关闭 1.在单机环境下 要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下 su - oracle a.启动ORACLE系统 oracle>svrmgrl ...

  10. google快捷键

          使用快捷键的好处不言自明,如果能过直接访问google自然有对应的帮助文档以供参考,不过为了方便起见,还是将自己觉得有用的信息收藏到自己访问无障碍的地方吧! 一:以下快捷键适用于Windo ...