2007/2008 ACM International Collegiate Programming Contest 
University of Ulm Local Contest

Problem F: Frequent values

You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers ai , ... , aj.

Input Specification

The input consists of several test cases. Each test case starts with a line containing two integers n and q(1 ≤ n, q ≤ 100000). The next line contains n integers a1 , ... , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the query.

The last test case is followed by a line containing a single 0.

Output Specification

For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.

Sample Input

10 3
-1 -1 1 1 1 1 3 10 10 10
2 3
1 10
5 10
0

Sample Output

1
4
3 题解:因为序列a1,a2,a3...an是不下降的,所以相同的数字肯定连续集中在一段。维护d[i][0]为相同数字从左到右分别从1到n开始编号,例如样例中的d[i][0]从左到右为 1,2,1,2,3,4,1,1,2,3。这样就转化成了RMQ问题。因为没有中途修改a[i]的值,所以可以用Sparse-Table算法,d[i][j]表示从i开始长度为2^i的一段元素的最大值。维护数组Left[i],Right[i]分别为相同数字连续一段最左端的编号和最右端的编号,例如样例中的Left[i]分别为:1,1,3,3,3,3,7,8,8,8。所以,序列分布共有以下几种情况:               
     [1] ans=max(Right[i]-L+1,R-Left[j]+1)
     [2] ans=R-L+1      [3] ans=max(max(Right[i]-L+1,R-Left[j]+1),RMQ(k)) 代码:
 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdbool.h> #define rep(i,a,b) for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define sqr(x) (x*x)
#define LL long long int i,j,n,m,maxn,q,
a[],d[][],Left[],Right[]; void pre()
{
clr(d,);
clr(Left,);
clr(Right,);
clr(a,);
} int max(int a,int b)
{
if(a>b) return a;
return b;
} int RMQ_init()
{
int i,j; for(j=;(<<j)<=n;j++)
for(i=;i+(<<j)-<=n;i++)
d[i][j]=max(d[i][j-],d[i+(<<(j-))][j-]); return ; } int RMQ(int L,int R)
{
int k;
k=; while(<<(k+)<=R-L+) k++; maxn=max(d[L][k],d[R-(<<k)+][k]);; return ; } int Num_init()
{
int i; a[]=;
rep(i,,n)
if(a[i]!=a[i-]) Left[i]=i;
else Left[i]=Left[i-]; a[n+]=;
red(i,n,)
if(a[i]!=a[i+]) Right[i]=i;
else Right[i]=Right[i+]; return ;
} int main()
{
int i,x,y; while(true) {
pre();
scanf("%d",&n); if(n==) exit();
scanf("%d",&q);
a[]=;
rep(i,,n) {
scanf("%d",&a[i]);
if(a[i]!=a[i-]) d[i][]=;
else d[i][]=d[i-][]+; } Num_init();
RMQ_init(); while(q--) {
scanf("%d%d",&x,&y);
if(Left[x]==Left[y]) printf("%d\n",y-x+);
else {
if(Right[x]+==Left[y]) printf("%d\n",max(Right[x]-x+,y-Left[y]+));
else {
RMQ(Right[x]+,Left[y]-);
printf("%d\n",max(max(Right[x]-x+,y-Left[y]+),maxn));
}
}
}
} return ;
}

 

[POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]的更多相关文章

  1. POJ 3368 & UVA 11235 - Frequent values

    题目链接:http://poj.org/problem?id=3368 RMQ应用题. 解题思路参考:http://blog.csdn.net/libin56842/article/details/4 ...

  2. RMQ算法 以及UVA 11235 Frequent Values(RMQ)

    RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8)  内的最大值.数据量小 ...

  3. UVA - 11235 Frequent values

    2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...

  4. UVA 11235 Frequent values(RMQ)

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

  5. UVA 11235 Frequent values 线段树/RMQ

    vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释**************** ...

  6. UVa 11235 Frequent values (RMQ && 区间出现最多次的数的次数)

    题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间“缩小”,例如1 1 2 3 3 3就可以变成2 ...

  7. UVA 11235 Frequent Values ---RMQ

    大白书上的例题,具体讲解见大白书,最好用用一个Log数组直接求k,这样就是纯O(1)了 #include <iostream> #include <cstdio> #inclu ...

  8. 数据结构(RMQ):UVAoj 11235 Frequent values

    Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. I ...

  9. 11235 - Frequent values

    <算法竞赛入门经典-训练指南>P198 记录一下区间的左右边界就可以了 #include <iostream> #include <stack> #include ...

随机推荐

  1. document.body.scrollTop vs document.documentElement.scrollTop

    window.addEventListener("scroll", function () { if (document.body.scrollTop >= window.i ...

  2. 磁珠(FB)的选用

    1. 磁珠(FB)的单位是欧姆,而不是亨特,这一点要特别注意.因为磁珠的单位是按照它在某一频率 产生的阻抗来标称的,阻抗的单位也是欧姆.磁珠的 DATASHEET上一般会提供频率和阻抗的特性曲线图,一 ...

  3. 直接修改别人jar包里面的class文件 工具:jclasslib

    出于某些原因 需要把别人jar包里面的class修改一下信息 配置文件*.properties MANIFEST.MF 这些东西可以直接用记事本打开修改 然后替换掉就OK.. 在网上游荡了半天,没有找 ...

  4. Spring注解 @Resource和@Autowired

    @Resource和@Autowired两者都是做bean的注入使用.其实@Resource并不是Spring的注解,他的包是javax.annotation.Resource 需要导入.但是Spri ...

  5. bzoj3174 [Tjoi2013]拯救小矮人

    Description 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀上,知道最顶端的小矮人伸直胳膊可以碰到陷阱口.对于每一个小矮人, ...

  6. 符号表实现(Symbol Table Implementations)

    符号表的实现有很多方式,下面介绍其中的几种. 乱序(未排序)数组实现 这种情况,不需要改变数组,操作就在这个数组上执行.在最坏的情况下插入,搜索,删除时间复杂度为O(n). 有序(已排序)数组实现 这 ...

  7. Android——自定义Actionbar左侧覆盖不全的解决方案

    今天遇到一个很蛋疼的问题,就是在自定义Actionbar的时候,setCustomView中,自定义的view怎么也覆盖不了整个视图,左侧一直留有一个空白,看下图: 所写的部分代码如下: protec ...

  8. oracle 双机热备,oracle dataguard 和oracle rac的区别和联系(转)

    Data Guard 是Oracle的远程复制技术,它有物理和逻辑之分,但是总的来说,它需要在异地有一套独立的系统,这是两套硬件配置可以不同的系统,但是这两套系统的软件结构保持一致,包括软件的版本,目 ...

  9. oracle之substr函数

    substr(字符串,截取开始位置,截取长度) //返回截取的字 substr(,) //返回结果为 'H' *从字符串第一个字符开始截取长度为1的字符串 substr(,) //返回结果为 'H' ...

  10. 什么是系统平均负载(Load average)

    一.什么是系统平均负载(Load average)? 在Linux系统中,uptime.w.top等命令都会有系统平均负载load average的输出,那么什么是系统平均负载呢? Load Aver ...