UVa11235 Frequent values[RMQ]

本文介绍了一种处理区间查询问题的有效算法,特别适用于不下降序列的数据结构。通过使用游程编码和倍增技术(RMQ),文章详细解释了如何快速确定指定区间内出现频率最高的数值及其出现次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 2 f1; :::; ng) separated by spaces. You can assume that for each i 2 f1; : : : ; n 1g: 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.
Note: A naive algorithm may not run in time!
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

题意:给出区间,求这个区间里出现次数最多的数字出现的次数,给出的数列为不下降序列。
分析:由于数列不下降,则这个数列可以被缩小表示(即游程编码,只保存某段的数字和该数字出现的次数),然后存下该段的左端点和右端点,在查询时对左右端点单独处理,中间一段用倍增(即RMQ)处理即可。
第一次写倍增,调了有那么久,希望下次把每个变量的意义弄清楚,不要写错导致调试花费大量的时间。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define clr(x) memset(x,0,sizeof(x))
using namespace std;
const int logy=17;
const int  maxn=1e5+5;
int val[maxn],count[maxn],f[maxn*2*2][20],a[maxn],l,r,n,q,cnt,num[maxn],lef[maxn],rig[maxn];
int poww(int bel,int upp)
{
    int tmp=bel;
    for(int i=1;i<upp;i++)tmp*=bel;
    if(!upp)return 1;
    return tmp;
}
void init()
{
    cnt=0;clr(val);clr(count);clr(f);
    scanf("%d",&q);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
        if(a[i]==a[i-1])
            count[cnt]++;
        else {
            val[++cnt]=a[i];count[cnt]++;
        }
    int j=1;
    for(int i=1;i<=cnt;i++){
        int tmp=j;
        for(j;j<tmp+count[i];j++)num[j]=i;
        f[i][0]=i;lef[i]=tmp;rig[i]=tmp+count[i]-1;
    }
    for(int j=1;j<=logy;j++)
        for(int i=1;i<=cnt;i++){
            if(count[f[i][j-1]]<count[f[i+poww(2,j-1)][j-1]])
                f[i][j]=f[i+poww(2,j-1)][j-1];
            else
                f[i][j]=f[i][j-1];
        }
}
void calc()
{
    int maxn2=min(r,rig[num[l]])-l+1;
    maxn2=max(maxn2,r-max(lef[num[r]],l)+1);
    if(num[r]-num[l]-1>0){
            int logg=floor(log(num[r]-num[l]-1)/log(2));
            maxn2=max(maxn2,count[f[num[l]+1][logg]]);
            maxn2=max(maxn2,count[f[num[r]-poww(2,logg)][logg]]);
    }
    printf("%d\n",maxn2);
}
void ans()
{
    for(int i=1;i<=q;i++){
        scanf("%d %d",&l,&r);
        calc();
    }
}
int main()
{
    freopen("UVa11235.in","r",stdin);
    freopen("UVa11235.out","w",stdout);
    for(scanf("%d",&n);n;scanf("%d",&n)){
        init();
        ans();
    }
    return 0;
}

然后呢,我发现f数组的f[i][0]被改了,这是不可能出现的,根据经验(呃),肯定是横向数字定小了,数组开大一次A,后来发现……我居然把横向大小定的17(手动再见),世界上还有比我更zz的人吗(再见

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值