poj 3368 Frequent values(RMQ)
题目:http://poj.org/problem?id=3368
题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数。。
大白书上的 例题。。算是RMQ变形了,
对 原数组重新分段,并标记相同的个数为 该段的数值,然后RMQ...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = ;
const int maxm = ; int d_max[maxn][maxm],a[maxn];
int n,t;
int val[maxn],cnt[maxn],num[maxn],l[maxn],r[maxn]; void RMQ_init()
{
int i,j;
memset(d_max,,sizeof(d_max));
for(i = ; i <= t; i++)
{
d_max[i][] = cnt[i];
}
for(j = ; (<<j) <= t; j++)
for(i = ; i + j - <= t; i++)
{
d_max[i][j] = max(d_max[i][j-], d_max[i + (<<(j-))][j-]);
}
} int RMQ_max(int l, int r)
{
if(l>r)
return ; int k = ;
while((<<(k+)) <= r-l+)
k++;
return max(d_max[l][k], d_max[r-(<<k)+][k]);
}
int main()
{
int i, j, le, rig;
int q, ans, k, maxs;
while(~scanf("%d",&n) && n)
{
scanf("%d",&q);
for(i = ; i <= n; i++)
{
scanf("%d",&a[i]);
}
i = ;
t = ;
while(i <= n)
{
j = i;
ans = ;
while(a[j] == a[i] && j <=n)
{
j++;
ans++;
}
for(k = i; k < j; k++)
{
num[k] = t; //位置k的编号
l[k] = i; //位置k的最左端编号
r[k] = j-; //位置k的最右端编号
}
val[t] = a[i]; //第i段的数值
cnt[t] = ans; //第i段的出现次数
t++; i = j;
}
RMQ_init();
while(q--)
{
scanf("%d%d",&le, &rig);
if(num[le] == num[rig])
maxs = rig - le +;
else
{
maxs = max(r[le] - le + , rig - l[rig] + );
maxs = max(maxs, RMQ_max(num[le]+, num[rig]-));
}
printf("%d\n",maxs);
}
}
return ;
}
poj 3368 Frequent values(RMQ)的更多相关文章
- poj 3368 Frequent values(段树)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13516 Accepted: 4971 ...
- POJ 3368 Frequent values (基础RMQ)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14742 Accepted: 5354 ...
- UVA 11235 Frequent values(RMQ)
Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...
- UVA-11235 Frequent values (RMQ)
题目大意:在一个长度为n的不降序列中,有m次询问,每次询问(i,j)表示在区间(i,j)中找出出现次数最多的元素的出现次数. 题目分析:因为序列有序,可以将序列分段,并且记录每段的元素个数.每一个元素 ...
- 【POJ 3368】Frequent values(RMQ)
Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...
- poj 1806 Frequent values(RMQ 统计次数) 详细讲解
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1806 题目大意:给你一个非降序排列的整数数组,你的任务是对于一系列的询问,(i,j),回答序列中出现次 ...
- POJ 3368 Frequent values(线段树区间合并)
[题目链接] http://poj.org/problem?id=3368 [题目大意] 有一个有序序列,要求区间查询出现次数最多的数 [题解] 维护每个区间左端点和右端点,以及左右的长度,还有区间的 ...
- (简单) POJ 3368 Frequent values,RMQ。
Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...
- POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】
传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS Memory Limit: 65536K Total S ...
随机推荐
- asp.net中的mysql传参数MySqlParameter
注意在asp.net中传参 string sql="select name,id from user where id=@id"; //@idm不需要引号 MySqlParamet ...
- Android Bluetooth Stream Non-blocking Communication Tutorial
This is a tutorial for Android to do non-blocking bluetooth socket communication. I am using 32feet ...
- FMS (端口问题)如何穿透防火墙
转自http://www.cnblogs.com/zhchongyao/archive/2010/01/22/1653803.html 先是管理端口,就是fms2_console文件连接到server ...
- 【CodeForces】【148D】Bag of mice
概率DP kuangbin总结中的第9题 啊……题目给的数据只有白鼠和黑鼠的数量,所以我们只能在这个上面做(gao)文(D)章(P)了…… 明显可以用两种老鼠的数量来作为状态= = 我的WA做法: 令 ...
- [spring]启动时报错:NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I
Spring V4.1以后的版本在不支持Servlet3.0的应用服务器上跑时会报如下错误: NoSuchMethodError: javax.servlet.http.HttpServletResp ...
- thinpad E43系列WIN8装WIN7系统
昨晚WIN8系统下装WIN7 折腾了好久,故此总结一下写一篇U盘装WIN7 : 先简述一下思路:BOSS设置启动项------->U盘启动--------> 进入PE删除所有分区----- ...
- JsRender系列demo-对null 和boolen类型数据的探讨
废话不说了,直接上代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <he ...
- JsRender系列demo(9)自定义函数
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- C#中dynamic的正确用法 以及 typeof(DynamicSample).GetMethod("Add");
dynamic是FrameWork4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性.比如,即使你 ...
- hadoop集群基本配置
最近在学习hadoop.网上具体过程很多,我就说说简单过程和注意问题. 环境:宿主机(windows64),虚拟机(centos64). 准备软件: 1.Vmware——虚拟机 2.centos镜像文 ...