poj 3368 Frequent values (RMQ或线段树)
来源:互联网 发布:怎么在淘宝买东西便宜 编辑:程序博客网 时间:2024/06/05 18:30
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 integersa1 , ... , 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 qlines 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 102 31 105 100
Sample Output
143
求一段数字区间出现次数最多的数字的频率,数列单调不减。对数列转化后可以用RMQ或者线段树求解。
RMQ经常用来查询区间最值问题,对于任意区间,RMQ算法可以方便的在O(1)时间查询出最值。
以最大值为例,考虑递推方程:f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
f[i][j]:表示区间[i,i+2^j-1],j=0,1,2,在区间内即可。
上面式子即表示:大区间[i,i+2^j-1]区间=小区间[i,i+2^(j-1)-1]U[i+2^(j-1),i+2^(j-1)+2^(j-1)-1],
等式两边明显相等(相同),故成立。
对于该题,查询区间[l,r],对于区间左端点处和区间外面相等的数字单独处理即可。
RMQ解法:
#include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<string.h>using namespace std;#define LL long long int#define rep(i,a,n) for(int i=a;i<n;++i)#define per(i,a,n) for(int i=n-1;i>=a;--i#define N 100005const int inf=1e9+10;int a[N],num[N],rt[N];int dp[N][20];void rmq(int n){ int i,j; rep(i,1,n+1) dp[i][0]=num[i]; for(j=1;j<20;++j){ for(i=1;i+(1<<(j-1))<=n;++i){ dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); } }}int myquery(int l,int r){ if(l>r) return 0; int k,t1,t2; k=log(r-l+1.0)/log(2.0); t1=dp[l][k]; t2=dp[r-(1<<k)+1][k]; return max(t1,t2);}int main(){ //freopen("in.txt","r",stdin); int n,q; while(scanf("%d",&n),n){ scanf("%d",&q); a[0]=inf; rep(i,1,n+1){ scanf("%d",&a[i]); if(i==1){ num[i]=1; } else if(a[i]==a[i-1]){ num[i]=num[i-1]+1; } else{ num[i]=1; } } per(i,1,n+1){ if(a[i]==a[i+1]){ rt[i]=rt[i+1]; } else{ rt[i]=i; } } rmq(n); int l,r,t,tmp,ans; while(q--){ scanf("%d%d",&l,&r); if(rt[l-1]==rt[l]) t=min(r,rt[l])+1; else t=l; tmp=myquery(t,r); ans=max(t-l,tmp); cout<<ans<<endl; } } return 0;}
- 【POJ】3368-Frequent values(RMQ或线段树)
- poj 3368 Frequent values (RMQ或线段树)
- POJ 3368 Frequent values RMQ / 线段树
- POJ 3368 Frequent values 线段树 || RMQ
- POJ 3368 Frequent values(线段树/RMQ)
- POJ 3368 Frequent values(RMQ/线段树区间合并)
- poj 3368 Frequent values //RMQ
- POJ 3368 Frequent Values(RMQ)
- [POJ 3368]Frequent values(RMQ)
- Frequent values - POJ 3368 RMQ
- POJ--3368--Frequent values【RMQ】
- POJ 3368 Frequent values (RMQ)
- POJ 3368 Frequent values(RMQ)
- POJ 3368 Frequent values (RMQ)
- POJ 3368 Frequent Values(RMQ)
- POJ 3368 Frequent values RMQ
- POJ 3368 Frequent values (RMQ)
- POJ 3368 Frequent values RMQ
- navicat for mysql的下载、安装与基本使用
- 多线程知识3-线程间通信
- Spark集群启动python shell错误: Could not resolve hostname localhost: Temporary failure
- 相关事件
- poj 1753 Flip Game
- poj 3368 Frequent values (RMQ或线段树)
- 多种CSMA策略的比较
- lua源码的研究入门之makefile
- iOS 常用的第三方库
- Label--自定义可调整内边距的Label
- 《GeekBand》c++面向对象高级编程02
- 谨以此文献给所有对目前工作不满意的宝宝们!
- BZOJ 1853: [Scoi2010]幸运数字
- 将x6cms移植到Codeigniter 3.0框架