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 ...
随机推荐
- Encog
http://www.heatonresearch.com/encog/ https://www.mql5.com/zh/articles/252
- Qt postEvent
Qt3中可以直接向线程发送消息 QThread::postEventQ4中已不支持为了模拟向线程发送消息,可以通过QObject::moveToThread后,然后再向这个QObject发送消息 ob ...
- XML学习总结
什么是XML?XML指可扩展标记语言(EXtendsible Markup Language) XML的设计宗旨是传输数据,而不是显示数据. XML标签没有被预定义(html是预定义),XML里面您需 ...
- linux xampp常见问题
一.常见问题 1.安装xampp4linux后,只能本机(http://localhost)访问,局域网内其他机器无法访问 解答:在/opt/lampp/etc中修改httpd.conf,将Liste ...
- IIS Express start introduction and applicationHost modification
1. First you need create a web project in VS 2. When you finish your project, click start then IIS E ...
- 【BZOJ】【2132】圈地计划
网络流/最小割 Orz Hzwer 这类大概是最小割建模中的经典应用吧…… 黑白染色,然后反转黑色的技巧感觉很巧妙!这个转化太神奇了…… /****************************** ...
- 【Asp.Net MVC】Avoid Mass Assignment in ASP.NET MVC
Mass Assignment Vulnerability in ASP.NET MVC: http://freshbrewedcode.com/joshbush/2012/03/05/mass-as ...
- c#中HttpWebRequest使用Proxy实现指定IP的域名请求
原文:http://www.cnblogs.com/greenerycn/archive/2010/04/11/httpwebreques_host_modify_By_set_proxy.html ...
- POJ 2568/ZOJ 1965 Decode the Tree
题意:在树中,每次删去节点值最小的叶子结点. 每删去一个点,就给出与这相连的点的值,直到最后只剩下一个根结点,给这N-1个数,重新建立这个树. 思路: 给出的节点号按次序存入到数组a中,将未给出的数存 ...
- **json_encode:让Json更懂中文(JSON_UNESCAPED_UNICODE)
我们知道, 用PHP的json_encode来处理中文的时候, 中文都会被编码, 变成不可读的, 类似”\u***”的格式, 还会在一定程度上增加传输的数据量. 代码如下: <?php echo ...