题目: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中设置Animation 动画效果

    在 Android 中, Animation 动画效果的实现可以通过两种方式进行实现,一种是 tweened animation 渐变动画,另一种是 frame by frame animation ...

  2. jquery插件dataTables添加序号列

    官网方法实例: $(document).ready(function() {     var t = $('#example').DataTable({         "columnDef ...

  3. 2140: 稳定婚姻 - BZOJ

    Description 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的姗姗和男友谈恋爱半年就 ...

  4. c++ g++3.4.5 g++4.8.2 由编译器引起的编译异常

    #include <memory> #include <string> #include <iostream> class Student { public: St ...

  5. sgu 138

    自己猜测了一下  按比赛次数 从大到小排  然后类似于模拟 先排胜的场次 当只剩一场 将它定义为败 #include <cstdio> #include <cstdlib> # ...

  6. jQuery1.9.1源码分析--Ajax模块

    //Serialize an array of form elements or a set of //key/values into a query string // 将数组形式的表单元素或者哈希 ...

  7. Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...

  8. poj 2485 Highways(最小生成树,基础,最大边权)

    题目 //听说听木看懂之后,数据很水,我看看能不能水过 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stri ...

  9. POJ 2948 Martian Mining(DP)

    题目链接 题意 : n×m的矩阵,每个格子中有两种矿石,第一种矿石的的收集站在最北,第二种矿石的收集站在最西,需要在格子上安装南向北的或东向西的传送带,但是每个格子中只能装一种传送带,求最多能采多少矿 ...

  10. SDUT2087离散事件模拟-银行管理

    呃,这个题,我只想仰天长啸:无语死我了,还动用了繁和帅锅给我改,妹的,做题一定要仔细仔细再仔细啊,这种小错误都犯真是该打. 题目描述 现在银行已经很普遍,每个人总会去银行办理业务,一个好的银行是要考虑 ...