POJ 3368 Frequent values(RMQ)

Frequent values

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15134   Accepted: 5519

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

题意:给出一个长度为n的不下降序列,然后m次查询,每次输入两个数l,r,输出区间[l,r]中连续的数中最长的数目是多少。

思路:将输入的不下降序列进行预处理,找出其中的相邻的数字的相邻的数目,存储在p数组中,然后作为RMQ算法中dp的初值,每次输入l,r时,输出该区间的最大值就可以了。

ps:不过之中有一个需要注意的地方,就是每次询问[l,r]时,要先将这个区间分成两个部分,前半部分求出与num[pt] == num[l]的个数,直到num[pt]!=num[l],当然pt<=r然后对[pt,r]进行RMQ求解,这样做的原因是排除在l之前还有数与num[l]相等。这样的话可能会影响结果。然后将RMQ求解得到的结果与pt-l的值进行取最大值就可以了。

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

using namespace std;

const int N = 200010;
int maxx[N][20];
int num[N];
int p[N];
int n,m;

void play() {
    int l = floor(log10(double(n))/log10(double(2)));
    for(int j=1; j<=l; j++) {
        for(int i=1; i<=n+1-(1<<j); i++) {
            maxx[i][j] = max(maxx[i][j-1],maxx[i+(1<<(j-1))][j-1]);
        }
    }
}

int RMQ(int xx,int yy) {
    if(xx>yy) {
        return 0;
    }
    int pp = floor(log10(double(yy-xx))/log10(double(2)));
    return max(maxx[xx][pp],maxx[yy-(1<<pp)+1][pp]);
}

int main() {
    while(scanf("%d",&n)!=EOF) {
        if(n == 0) {
            break;
        }
        scanf("%d",&m);
        for(int i=1; i<=n; i++) {
            scanf("%d",&num[i]);
            if(i == 1) {
                p[i] = 1;
            } else {
                if(num[i] == num[i-1]) {
                    p[i] = p[i-1] + 1;
                } else {
                    p[i] = 1;
                }
            }
        }
        for(int i=1; i<=n; i++) {
            maxx[i][0] = p[i];
        }
        play();
        int x,y;
        while(m--) {
            scanf("%d%d",&x,&y);
            int pt = x;
            while(pt<=y && num[pt] == num[pt-1]) {
                pt++;
            }
            int ans = RMQ(pt,y);
            ans = max(ans,pt-x);
            printf("%d\n",ans);
        }

    }
    return 0;
}

版权声明:本文为博主原创文章,如有特殊需要请与博主联系 QQ : 793977586。

时间: 2024-10-23 23:44:42

POJ 3368 Frequent values(RMQ)的相关文章

POJ 3368 Frequent values(RMQ)

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 t

【POJ 3368】 Frequent values(RMQ)

[POJ 3368] Frequent values(RMQ) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15813   Accepted: 5749 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given seve

poj 3368 Frequent values(线段树解法)

题目链接:http://poj.org/problem?id=3368 题目大意:给你一段不下降的序列,求给定区间里出现次数最多的那个数字的次数. 思路:首先看到这题时,第一感觉线段树,但是仔细一看问题来啦,用线段数我怎么才能计算出某段区间里出现的那个数,因为出现最多的那个数可能不是在他它的左儿子上也不是在它的右儿子上,可能在当他们合并成一个区间时就出现啦,但是这儿我们需要注意的就是,题目给的是一段不下降的序列,那么突破口就出来啦,因为如果出现相同的数字,那么它们一定是连续的.所以我们只需要在普

UVA 11235 Frequent values(RMQ)

Frequent values TimeLimit:3000Ms 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 f

poj 3368 Frequent values(线段树)

Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13516   Accepted: 4971 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 cons

POJ 3368 Frequent values (基础RMQ)

Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14742   Accepted: 5354 Description You are given a sequence of n integersa1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consi

UVA-11235 Frequent values (RMQ)

题目大意:在一个长度为n的不降序列中,有m次询问,每次询问(i,j)表示在区间(i,j)中找出出现次数最多的元素的出现次数. 题目分析:因为序列有序,可以将序列分段,并且记录每段的元素个数.每一个元素所属的段num(i).每一个元素所属段的左端点l(i)及右端点r(i).那么对于每次询问: ans(i,j)=max(max(r(i)-i+1,j-l(j)+1),rmq(num(i)+1,num(j)-1)). ans(i,j)=r-j+1 (如果i与j属于同一段) 代码如下: # include

poj 1806 Frequent values(RMQ 统计次数) 详细讲解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1806 题目大意:给你一个非降序排列的整数数组,你的任务是对于一系列的询问,(i,j),回答序列中出现次数追的的数的个数: 如下图所示: AC代码: 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<queue> 6

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

[题目链接] http://poj.org/problem?id=3368 [题目大意] 有一个有序序列,要求区间查询出现次数最多的数 [题解] 维护每个区间左端点和右端点,以及左右的长度,还有区间的答案 每次线段合并的时候,对区间数据进行合并即可. [代码] #include <cstdio> #include <algorithm> using namespace std; const int N=100010; struct data{int a,b,l,r,val;}T[N