poj 3368 Frequent values(RMQ)
/************************************************************
题目: 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)的更多相关文章
- POJ 3368 Frequent values RMQ ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
- POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)
题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...
- POJ 3368 Frequent values RMQ 训练指南 好题
#include<cstdio> #include<cstring> ; const int inf=0x3f3f3f3f; inline int max(int x,int ...
- POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】
传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS Memory Limit: 65536K Total S ...
- POJ 3368 Frequent values (基础RMQ)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14742 Accepted: 5354 ...
- poj 3368 Frequent values(段树)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13516 Accepted: 4971 ...
- poj 3368 Frequent values(RMQ)
题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...
- (简单) POJ 3368 Frequent values,RMQ。
Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...
- [RMQ] [线段树] POJ 3368 Frequent Values
一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同 ...
随机推荐
- nova-scheduler start flow
- myeclipse的debug模式中breakpoint窗口怎么调出来
myeclipse的debug模式中breakpoint窗口怎么调出来? 解决办法: window-->show view-->breakpoints. 如下:
- MemcacheQ 安装与使用
MemcacheQ 是一个基于 MemcacheDB 的消息队列服务器.官网地址:http://memcachedb.org/memcacheq/ 特点: 1.简单易用. 2.处理速度快. 3.可创建 ...
- 关于iscroll阻止浏览器默认动作
使用iscroll时,移动端遇到需要长按复制功能,但是iscroll屏蔽了浏览器默认事件,所以实现不了. 解决方案: myScroll = new IScroll('#wrapper',{ preve ...
- ConfigParser.MissingSectionHeaderError: File contains no section headers.
今天使用ConfigParser解析一个ini文件,报出如下错误: config.read(logFile) File "C:\Python26\lib\ConfigParser.py&qu ...
- 网络换行符替换为word换行符
在替换的页面上,查找里输入:^l,在替换里输入:^p,然后点击替换即可.
- 浅谈session/cookie
Session 和Cookie是常用的Web跟踪技术.Cookie保存在客户端,而Session则保存在服务器端,二者结合使用来跟踪用户的会话状态,是http协议的一种扩展技术.之所以说是一种扩展技术 ...
- strlen和sizeof
1.sizeof操作符的结果类型是size_t,它在头文件中typedef为unsigned int类型.该类型保证能容纳实现所建立的最大对象的字节大小. 2.sizeof是算符,strlen是函数. ...
- gsons
java 处理 json格式字符串,目前只使用过Google的Gson库. pom: <dependency> <groupId>com.google.code.gson< ...
- Rserve, java调用R源文件
Rserve安装和加载: install.packages("Rserve") library("Rserve") Rserve() java调用: REn ...