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. 10.3 noip模拟试题

    希望[题目描述]网页浏览器者有后退与前进按钮,一种实现这两个功能的方式是用两个栈,“前进栈”.“后退栈”.这里你需要实现以下几个功能:BACK: 如果“后退栈”为空则忽略此命令. 否则将当前两面压入“ ...

  2. webservice 发布到外网的时候

    在web.config的<system.web></system.web>中间加入如下配置节内容<webServices>          <protoco ...

  3. css3购物网站商品文字提示实例

    css3购物网站商品文字提示实例先来看效果图:<ignore_js_op> 当鼠标划过图片时,有着泰迪熊黑色长方形的背景就会出现.来看HTML5+CSS3代码: <!DOCTYPE ...

  4. PHP 开启报错机制

    屏蔽PHP错误提示 方法一:在有可能出错的函数前加@,然后or die("") 如: @mysql_connect(...) or die("Database Conne ...

  5. HTML5+移动APP(2)

    原理: html 页面负责内容: ui 负责页面样式: js 负责调用原生app方法. html5: html5这部分负责页面,也就是app中你看到的东西,大概的架构和内容 ui: mui 介绍:和H ...

  6. sql常用的日期函数与应用

    --本周第一天 ),getdate()) --or ,) --本周第一天 ,) --上月第一天 ),,,) --上月最后一天 ),,,)),)+' 23:59:59' --本月第一天 ,getdate ...

  7. sql查询过程中 update,insert,delete可视化收影响行数

    insert into test_tb output inserted.id,inserted.data values('c'),('d') delete from test_tb output de ...

  8. Deep Learning学习随记(二)Vectorized、PCA和Whitening

    接着上次的记,前面看了稀疏自编码.按照讲义,接下来是Vectorized, 翻译成向量化?暂且这么认为吧. Vectorized: 这节是老师教我们编程技巧了,这个向量化的意思说白了就是利用已经被优化 ...

  9. 第17章课后题(高级Perl技巧)

    17.1 写一个程序,从文件中读取一组字符串(每行一个),然后让用户键入模式以便进行字符串匹配. 对于每个模式,程序应该说明文件里共有多少字符串匹配成功,分别是哪些字符串. 对于所键入的每个新模式,不 ...

  10. splice从数组中删除指定定数据

    /*从数组中删除指定定数据var somearray = ["mon", "tue", "wed", "thur"]so ...