POJ 3368 Frequent values(RMQ/线段树区间合并)

题目地址:点击打开链接


题意:给定一个数组,其中的元素满足非递减顺序,要求对于一对起点和终点,回答出其中某个元素重复出现的最大次数。


思路:

由于原数列已经排好序,所以相同的数字一定是在一起的。我们可以O(n)扫描一遍,算出每一个位置上的数字num是从左起第几个num,记为num[i]

    如样例,扫描后如下,括号内是num数组值

    -1(1)  -1(2)  1(1) 1(2) 1(3) 1(4) 3(1) 10(1) 10(2) 10(3)

    这样似乎直接求num数组区间[l,r]内的最大值就能得到答案。实际上这样无法得到正确的结果,因为如果查询区间左端点上num值不等于1,就会得到错误的答案。如对样例求[5,7]会得到4。

    出现这个错误的原因上面已经说了,因为num数组是对区间[1,n]的计数。

    解决方法也很简单,在O(n)倒着扫一遍,对于每个位置上的数字num,求出最右侧的数字为num的位置,记为R[i],如下

     -1(2)  -1(2)  1(6) 1(6) 1(6) 1(6) 3(7) 10(10) 10(10) 10(10)

    这样查询[l,r]时,只要R[l] < r,就使用RMQ查询[ R[l]+1 , r ]区间内的最大值,这样可以保证查询区间的左端点num值一定是1。而对于[l , R[l] ]这段区间,很显然R[l] - l + 1是唯一一个可能的答案。

    当R[l] >= r时,显然[l, r]整个区间都是同一个数字。答案为r-l+1.



这个解法的关键是numl数组记录该位置以左该数的数量,numr数组记录最右和自己相同数到哪。RMQ直接对numl查询(l , r)时左端点的区间可能不

完整,此时就可以用numr数组直接得到在(l , r)中与左端点相同的数数量,然后剩下的范围RMQ查询,取较大值。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = 1e5+5;
int dp[maxn][25], numl[maxn], numr[maxn], a[maxn], n, m;

void initRMQ()
{
    for(int j = 0; j < 25; j++)
        for(int i = 1; i <= n; i++)
        {
            if(j == 0) { dp[i][j] = numl[i]; continue; }
            int len = 1<<j;
            if(i+len-1 <= n)
                dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
        }
}

int RMQ(int l, int r)
{
    int k = (int)log2(r-l+1);
    return max(dp[l][k], dp[r-(1<<k)+1][k]);
}

int main(void)
{
    while(cin >> n, n)
    {
        cin >> m;
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        numl[1] = 1;
        for(int i = 2; i <= n; i++)
            numl[i] = a[i]==a[i-1] ? numl[i-1]+1 : 1;
        numr[n] = n;
        for(int i = n-1; i >= 1; i--)
            numr[i] = a[i]==a[i+1] ? numr[i+1] : i;

        initRMQ();
        while(m--)
        {
            int l, r;
            scanf("%d%d", &l, &r);
            if(numr[l] >= r) printf("%d\n", r-l+1);
            else printf("%d\n", max(numr[l]-l+1, RMQ(numr[l]+1, r)));
        }
    }
    return 0;
}

Frequent values
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18434 Accepted: 6652

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

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

Source


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值