Frequent values(倍增RMQ)
来源:互联网 发布:手机玩电音的软件 编辑:程序博客网 时间:2024/05/18 22:50
Frequent values
Description:
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
题目大意:
给出n个数和m个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数。
思路:
RMQ算法
预处理一个数组:
if(num[i]==num[i-1])
a[i]=a[i-1]+1;
else
a[i]=1;
对于每个询问(l,r),分为两个部分,前半部分求与l之前相同的数的个数直到t,后半部分从t开始直接用RMQ求解最大值就行了。
最后结果为max(前半部分,后半部分)。
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;const int maxn=100010;int n,m,num[maxn],a[maxn],f[maxn][20];int init(){ int f=1,p=0;char c=getchar(); while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){p=p*10+c-'0';c=getchar();} return f*p;}void prepare(){ memset(f,-1,sizeof(-1)); for(int i=1;i<=n;i++) f[i][0]=a[i]; int k=log((double)(n+1))/log(2.0); for(int j=1;j<=k;j++) for(int i=1;i+(1<<j)-1<=n;i++) f[i][j]=max(f[i][j-1],f[i+(1<<j-1)][j-1]);}int rmq(int l,int r){ if(l>r) return 0; int k=log((double)(r-l+1))/log(2.0); return max(f[l][k],f[r-(1<<k)+1][k]);}int main(){ int l,r; while(cin>>n&&n) { m=init(); for(int i=1;i<=n;i++) { num[i]=init(); if(i==1) { a[i]=1; continue; } if(num[i]==num[i-1]) a[i]=a[i-1]+1; else a[i]=1; } prepare(); for(int i=1;i<=m;i++) { l=init();r=init(); int t=l; while(t<=r&&num[t]==num[t-1]) t++; int ans=rmq(t,r); ans=max(ans,t-l); printf("%d\n",ans); } } return 0;}
- Frequent values(倍增RMQ)
- 11235 - Frequent values (RMQ)
- poj3368 Frequent values(RMQ)
- poj3368 Frequent values(rmq)
- poj3368 Frequent values(RMQ)
- poj_3368 Frequent values(RMQ)
- POJ3368 Frequent values (RMQ)
- UVa - 11235 - Frequent values(RMQ)
- poj 3368 Frequent values(RMQ问题)
- uva 11235 - Frequent values(RMQ问题)
- UVA 11235 Frequent values(RMQ)
- poj 3368(RMQ)Frequent values
- UVA - 11235 - Frequent values (RMQ)
- POJ 3368 Frequent values(RMQ)
- UVA 11235 Frequent values(RMQ)
- hdu 1806 Frequent values(RMQ)
- poj 3368 Frequent values(RMQ)
- 【POJ 3368】 Frequent values(RMQ)
- 栈的应用之后缀表达式的求解
- java多线程之基础分析
- 外部中断使用指南(寄存器设置)
- NMTUI
- SVN服务器搭建和使用(二)
- Frequent values(倍增RMQ)
- 大数据认知(一)之为什么使用Hadoop生态系统处理大数据而不是高性能关系型数据库
- m序列
- SVN服务器搭建和使用(三)
- Reverse Integer
- struts2原文件struts2-blank.war学习
- 【BSOJ1724】【bzoj3032】TYVJ P1924 【NOIP2013模拟】七夕祭
- 文章标题 欧几里得求两数的最大公约数,最小公倍数。
- 新的征程