poj 3368 rmq ***
题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
int n,m,tt;
const int MAXN = ;
int dp[MAXN][];
int mm[MAXN];
//初始化RMQ, b数组下标从1开始,从0开始简单修改
void initRMQ(int n,int b[])
{
mm[] = -;
for(int i = ; i <= n;i++)
{
mm[i] = ((i&(i-)) == )?mm[i-]+:mm[i-];
dp[i][] = b[i];
}
for(int j = ; j <= mm[n];j++)
for(int i = ;i + (<<j) - <= n;i++)
dp[i][j] = max(dp[i][j-],dp[i+(<<(j-))][j-]);
}
//查询最大值
int rmq(int x,int y)
{
int k = mm[y-x+];
return max(dp[x][k],dp[y-(<<k)+][k]);
}
int a[MAXN],f[MAXN];
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
scanf("%d",&m);
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
f[]=;
if(i!=)
{
if(a[i]==a[i-])
{
f[i]=f[i-]+;
}
else
f[i]=;
}
}
/*for(i=1;i<=n;i++)
{
printf("%d ",f[i]);
}
printf("\n");*/
initRMQ(n,f);
while(m--)
{
int uu,v;
scanf("%d%d",&uu,&v);
int u=uu;
while(u<=v&&a[u]==a[u-])
{
u++;
}
int ans=rmq(u,v);
ans=max(u-uu,ans);
printf("%d\n",ans);
}
}
}
poj 3368 rmq ***的更多相关文章
- POJ 3368/RMQ/线段数
题目链接 /* 给出一段序列,询问[L,R]区间内最大相同数的个数. 用一个很巧妙地方法,转化成求区间内的最大值的问题. RMQ维护区间最大值. MAX处理: */ for(int i=1;i< ...
- poj 3368(RMQ模板)
题目链接:http://poj.org/problem?id=3368 题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数. 求解RMQ问题的算法有:搜索(比较 ...
- poj 3368 Frequent values(RMQ)
/************************************************************ 题目: Frequent values(poj 3368) 链接: http ...
- POJ 3368 RMQ-ST
一直感觉RMQ水,没自己写过,今天写了一道题,算是完全独立写的,这感觉好久没有了... 一直以来,都是为了亚洲赛学算法,出现了几个问题: 1.学的其实只是怎么用算法,对算法的正确性没有好好理解,或者说 ...
- POJ 3368 & UVA 11235 - Frequent values
题目链接:http://poj.org/problem?id=3368 RMQ应用题. 解题思路参考:http://blog.csdn.net/libin56842/article/details/4 ...
- poj 3368 Frequent values(RMQ)
题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...
- POJ 3368 Frequent values (基础RMQ)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14742 Accepted: 5354 ...
- 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 求区间出现最多次数的数字的次数)
题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...
随机推荐
- Solr记录-solr文档xml
Solr添加文档(XML) 在上一章中,我们学习解释了如何向Solr中添加JSON和.CSV文件格式的数据.在本章中,将演示如何使用XML文档格式在Apache Solr索引中添加数据. 示例数据 假 ...
- CSS Counter Style试玩儿
2015年2月3日,CSS Counter Style level3成为了W3C的候选标准,是时候来一探究竟,看看强大魔力的@counter-style如何自定义list-style和counter. ...
- 【转载】ssh(安全外壳协议)
http://baike.baidu.com/subview/16184/5909252.htm?fr=aladdin
- TED_Topic9:How we're priming some kids for college — and others for prison
Alice Goffman In the United States, two institutions guide teenagers on the journey to adulthood: co ...
- 20155303 2016-2017-2 《Java程序设计》第六周学习总结
20155303 2016-2017-2 <Java程序设计>第六周学习总结 课堂笔记 高效学习法推荐 看视频学习(2h)→ 以代码为中心看课本,思考运行结果并验证(3h)→ 课后作业验证 ...
- 第9月第12天 lua_push lua_to luaL_check stack quick
1. c代码中通过lua_push 把数据压入堆栈,lua调用c函数得到数据.luaL_check是对lua_to的封装,从堆栈中获取lua代码中函数调用的数据. static int lread(l ...
- 第6月第4天 AVMutableComposition AVMutableVideoComposition
1. AVMutableComposition is a mutable subclass of AVComposition you use when you want to create a new ...
- 『Matplotlib』数据可视化专项
一.相关知识 官网介绍 matplotlib API 相关博客 matplotlib绘图基础 漂亮插图demo 使用seaborn绘制漂亮的热度图 fig, ax = plt.subplots(2,2 ...
- Longest Words
Given a dictionary, find all of the longest words in the dictionary. Example Given { "dog" ...
- 设置linux的console为串口【转】
转自:http://blog.chinaunix.net/uid-27717694-id-4074219.html 以Grub2为例:1. 修改文件/etc/default/grub #显示启动菜 ...