您的位置:首页 > 其它

UVA 11235 经典RMQ问题

2014-08-10 21:09 344 查看
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2176

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

A naive algorithm may not run in time!
题目大意:

          给出一个非降序的排列的整数数组a1,a2,a3...an,你的任务是对于一系列的询问(i,j),回答ai,ai+1,....aj,中出现次数最多的值所出现的次数。

解题思路:RMQ范围最小值问题(大白书P197)

          应注意到整个数组是非降序的,所有相等的元素都会聚集到一起。这样就可以把整个数组进行游程编码。比如:-1,1,1,2,2,2,4就可以编码成(-1,1),(1,2),(2,3),(4,1),其中(a,b)表示b个连续的a。用value[i]和count[j]分别表示第i段的数值和出现的次数,num[p],left[p],right[p]分别表示位置p所在段的编号和左右端点的位置,则在下图的情况,每次查询(L,R)的结果为以下三个部分的最大值:从L到L所在段的结束处的元素个数(即right[L]-L+1)、从R所在段的开始处到R处的元素个数(即R-left[R]-1)、中间第num[L]+1段到第num[R]-1段的count的最大值。

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn=110005;
const int inf=2e9;
int num[maxn],_left[maxn],_right[maxn],cnt[maxn],d[maxn][30];
int pre,n,m;
int RLE()//游程编码
{
int x,l=0,count=-1;
pre=inf;
int size;
for(int i=0; i<n; i++)
{
scanf("%d",&x);
if(x!=pre)
{
for(int j=l; j<i; j++)
_right[j]=i-1;
l=i;
_left[i]=l;
count++;
pre=x;
num[i]=count;
cnt[count]=1;
}
else
{
_left[i]=l;
num[i]=count;
cnt[count]++;
}
}
for(int i=l; i<n; i++)
_right[i]=n-1;
size=count+1;
return size;
/**
printf("\n\n");
for(int i=0;i<n;i++)
printf("%d ",_left[i]);
printf("\n");
for(int i=0;i<n;i++)
printf("%d ",num[i]);
printf("\n");
for(int i=0;i<n;i++)
printf("%d ",cnt[i]);
printf("\n");
for(int i=0;i<n;i++)
printf("%d ",_right[i]);
printf("\n");
**/
}
void RMQ_init()
{
int n=RLE();
for(int i=0;i<n;i++)
d[i][0]=cnt[i];
for(int j=1;(1<<j)<=n;j++)
for(int i=0;i+(1<<j)-1<n;i++)
d[i][j]=max(d[i][j-1],d[i+(1<<(j-1))][j-1]);
}
int RMQ_query(int l,int r)
{
if(l>r)
return 0;
int k=0;
while((1<<(k+1))<=r-l+1)k++;//如果2^(k+1)<=r-l+1,那么k还可以加1
return max(d[l][k],d[r-(1<<k)+1][k]);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==0)
break;
RMQ_init();
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
l--;
r--;
if(num[l]==num[r])
{
printf("%d\n",r-l+1);
continue;
}
int t1=_right[l]-l+1;
int t2=r-_left[r]+1;
int t3=RMQ_query(num[l]+1,num[r]-1);
printf("%d\n",max(t1,max(t2,t3)));
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: