题目: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)的更多相关文章

  1. poj 3368 Frequent values(段树)

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

  2. POJ 3368 Frequent values (基础RMQ)

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

  3. UVA 11235 Frequent values(RMQ)

    Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...

  4. UVA-11235 Frequent values (RMQ)

    题目大意:在一个长度为n的不降序列中,有m次询问,每次询问(i,j)表示在区间(i,j)中找出出现次数最多的元素的出现次数. 题目分析:因为序列有序,可以将序列分段,并且记录每段的元素个数.每一个元素 ...

  5. 【POJ 3368】Frequent values(RMQ)

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

  6. poj 1806 Frequent values(RMQ 统计次数) 详细讲解

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1806 题目大意:给你一个非降序排列的整数数组,你的任务是对于一系列的询问,(i,j),回答序列中出现次 ...

  7. POJ 3368 Frequent values(线段树区间合并)

    [题目链接] http://poj.org/problem?id=3368 [题目大意] 有一个有序序列,要求区间查询出现次数最多的数 [题解] 维护每个区间左端点和右端点,以及左右的长度,还有区间的 ...

  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. POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】

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

随机推荐

  1. Android crop image size

    private void performCrop() { try { //call the standard crop action intent (the user device may not s ...

  2. HTML & XML 转义字符

    HTML & XML 转义字符 HTML中<, >,&等有特殊含义,(前两个字符用于链接签,&用于转义),不能直接使用.使用这三个字符时,应使用它们的转义序列,如下 ...

  3. 《Dive into Python》Chapter 2 and Chapter 3 笔记

    Example 2.1. odbchelper.py def buildConnectionString(params): """Build a connection s ...

  4. HDU 5486 Difference of Clustering 图论

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5486 题意: 给你每个元素一开始所属的集合和最后所属的集合,问有多少次集合的分离操作,并操作和不变操 ...

  5. SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件二--获取注入的bean的二种方式

    前言: spring容器以xml的形式注入bean,然后可以在类中获取,获取的形式主要有二种:第一种最简单--采用@Resource 或@Autowired关键字在加载spring文件时将bean注入 ...

  6. DispatcherServlet--Spring的前置控制器作用简介

    参考网址:http://jinnianshilongnian.iteye.com/blog/1602617 Struts有一个ActionServlet,用来完成前置控制器(分发器)的功能.其实,所有 ...

  7. 查看系统网络连接打开端口、系统进程、DOS打开文件

    问题描述: DOS查看系统网络连接打开端口.打开的服务 问题解决: (1)DOS查看系统网络连接打开端口 注: 使用    netstat 命令,可以查看系统打开的端口 (2)查看和关闭系统打开进程 ...

  8. 如何实现Windows Phone代码与Unity相互通信(事件方式)

    源地址:http://www.cnblogs.com/petto/p/3909063.html 一些废话 昨天写一篇今天写一篇.不是我闲的蛋疼,是今天一天碰到了好几个恼人的问题,浪费一天时间搞定.本文 ...

  9. Zabbix 安装及微信短信提醒

    Zabbix简介 Zabbix 近几年得到了各大互联网公司的认可,当然第一点归功与它强大的监控功能,第二点免费开源也得到了广大用户的青睐.Zabbix 能将操作系统中的绝大部分指标进行监控,比如(CP ...

  10. HDU4612 Warm up 边双连通分量&&桥&&树直径

    题目的意思很简单,给你一个已经连通的无向图,我们知道,图上不同的边连通分量之间有一定数量的桥,题目要求的就是要你再在这个图上加一条边,使得图的桥数目减到最少. 首先要做的就是找出桥,以及每个点所各自代 ...