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

  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

  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   这题由于序列非降,所以就可以把整个数组进行游程编码(RLE Run Length Encoding)。
  还是很简单的~~~
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int mm[maxn],Max[maxn][];
int MAXL[maxn],MAXR[maxn],cnt=;
int ID[maxn],tot[maxn],a[maxn];
void Init(){
memset(tot,,sizeof(tot));
cnt=;
}
int main(){
#ifndef ONLINE_JUDGE
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
#endif int n,Q;
while(~scanf("%d",&n)&&n){
scanf("%d",&Q);
Init();
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
a[]=a[n+]=-;
for(int i=;i<=n;i++){
if(a[i]==a[i-]){
MAXL[i]=MAXL[i-];
ID[i]=ID[i-];
}
else{
MAXL[i]=i;
ID[i]=++cnt;
}
tot[ID[i]]++;
}
//MAXL[i]指与a[i]相等的1~i-1中最靠左的位置
//ID[i]指当前位置被分到第几块
//tot[i]指第i块的长度
for(int i=n;i>=;i--){
if(a[i]==a[i+])
MAXR[i]=MAXR[i+];
else
MAXR[i]=i;
}
//MAXR[i]指与a[i]相等的i+1~n中最靠右的位置
mm[]=-;
for(int i=;i<=cnt;i++){
mm[i]=(i&(i-))?mm[i-]:mm[i-]+;
Max[i][]=tot[i];
}
for(int k=;k<=mm[cnt];k++)
for(int i=;i+(<<k)-<=cnt;i++)
Max[i][k]=max(Max[i][k-],Max[i+(<<(k-))][k-]);
//对块处理出稀疏表
int a,b,ans;
while(Q--){
scanf("%d%d",&a,&b);
if(a>b)swap(a,b);
if(ID[a]==ID[b]){
printf("%d\n",b-a+);
continue;
}
ans=-;
ans=max(MAXR[a]-a+,b-MAXL[b]+);
a=ID[MAXR[a]+];
b=ID[MAXL[b]-];
if(a<=b)
ans=max(ans,max(Max[a][mm[b-a+]],Max[b-(<<mm[b-a+])+][mm[b-a+]]));
printf("%d\n",ans);
}
}
return ;
}

数据结构(RMQ):UVAoj 11235 Frequent values的更多相关文章

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

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

  2. UVA 11235 Frequent values(RMQ)

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

  3. UVA - 11235 Frequent values

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

  4. [POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]

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

  5. UVA 11235 Frequent values 线段树/RMQ

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

  6. UVA 11235 Frequent Values ---RMQ

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

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

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

  8. 11235 - Frequent values

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

  9. POJ 3368 & UVA 11235 - Frequent values

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

随机推荐

  1. 《3D数学基础:图形与游戏开发》勘误

    最近读这本书,读到四元素的乘法时,自己推导了一下公式,然后懵了,多方查阅,确定是书籍中的笔误(我读的是中文翻译版): 具体在Page147: 上图所示,在第二处明显与第一处的公式不一样. 在维基百科上 ...

  2. js获取图片高度

    js获取图片高度时经常会获取的图片高度为0,原因是图片未加载完毕.第一次加载时,显示0(火狐等部分浏览器显示24).待加载完毕后,再刷新,显示图片高度258. var oImg = document. ...

  3. 正则表达式匹配(node.js)

    参考文档如下: http://blog.csdn.net/huiguixian/article/details/6131048 http://www.91xueke.com/2013/04/05/30 ...

  4. C# 泛型2

    我们在编写程序时,经常遇到两个模块的功能非常相似,只是一个是处理int数据,另一个是处理string数据,或者其他自定义的数据类型,但我们没有办法,只能分别写多个方法处理每个数据类型,因为方法的参数类 ...

  5. 使用highlight.js高亮你的代码

    在逛别人的博客的时候,看见别人的代码的例子使用了高亮的语法,无论是java,js还是php等等语言,都会自动的对关键字进行高亮. 于是在前几天自己写了一个博客,遇到code时,自然就想到了别人网站如何 ...

  6. 改进《完美让IE兼容input placeholder属性的jquery实现》的不完美

    <完美让IE兼容input placeholder属性的jquery实现>中的代码在IE9以下浏览器中会出错,原因是因为ie9以下的浏览器对input的标签的解释不同. 例如对以下文本框的 ...

  7. WHU 1572 Cyy and Fzz (AC自动机 dp )

    题意: 给出n个串,求任意长度为m的字符串包含串的个数的期望.(n<=8,m<=14,给定串的长度不超过12). Solution: 首先可以想到应该用概率DP,我们需要至少3维,dp[i ...

  8. 【POJ2887】【块状链表】Big String

    Description You are given a string and supposed to do some string manipulations. Input The first lin ...

  9. css3基础教程十三征服CSS3选择器

    :enabled选择器 在Web的表单中,有些表单元素有可用(“:enabled”)和不可用(“:disabled”)状态,比如输入框,密码框,复选框等.在默认情况之下,这些表单元素都处在可用状态.那 ...

  10. 下拉框——把一个select框中选中内容移到另一个select框中遇到的问题

    在使用jQuery实现把一个select框中选中内容移到另一个select框中功能时遇到了一个问题,就是点击按钮时内容可以到另一个select框中,但是到了另一个select框中的内容却很快闪退回原来 ...