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 ...
随机推荐
- google calendar
1. user guide on google https://developers.google.com/google-apps/calendar/instantiate 2. google app ...
- object to primitive in javascript
例1: var a={}; alert(a); //[object Object]; 例2: var a={ toString:function(){ return 1; } } alert(a); ...
- HDAO
dx11 hdao10.1 除了dx的sample竟然搜不到什么文档.... 估计去问别人也是让我继续看代码.. ---------------------------------------- 算法 ...
- json封装与解析
#include <iostream> #include <boost/property_tree/ptree.hpp> #include <boost/property ...
- CKeditor 配置使用
CKeditor 配置使用 一.使用方法:1.在页面<head>中引入ckeditor核心文件ckeditor.js<script type="text/javascrip ...
- QQ拼音还是不行哇
QQ拼音还是不行啊,虽说没广告,但是很多词条没有,例如知乎.蒋京虎. 泰囧……
- lintcode 中等题:N Queens N皇后问题
题目: N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击.<不同行,不同列,不同对角线> 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案 ...
- *[hackerrank]Jim Beam
https://www.hackerrank.com/contests/infinitum-aug14/challenges/jim-beam 学习了线段相交的判断法.首先是叉乘,叉乘的几何意义是有向 ...
- *[topcoder]BracketExpressions
http://community.topcoder.com/stat?c=problem_statement&pm=13243 就是能否通过把字符串中的'X'替换成"()" ...
- C内存分配函数
C语言跟内存分配方式(1) 从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量,static变量.(2) 在栈上创建.在执行函数时,函数内局部变量的 ...