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

时间:2023-03-09 05:14:19
[POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]

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。所以,序列分布共有以下几种情况: [POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]              
     [1] ans=max(Right[i]-L+1,R-Left[j]+1)
[POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]     [2] ans=R-L+1 [POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]     [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 ;
}