UVA 11235 經典RMQ問題

life4711發表於2014-08-10

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;
}


相關文章