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 consisting of indicesi
and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integersai , ... , aj.

Input

The input consists of several test cases. Each test case starts with a line containing two integersn and
q (1 ≤ n, q ≤ 100000). The next line containsn integers
a1 , ... , an (-100000 ≤ ai ≤ 100000, for eachi ∈ {1, ..., n}) separated by spaces. You can assume that for each
i ∈ {1, ..., n-1}: ai ≤ ai+1. The followingq 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 single0.

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

Ulm Local 2007

题目链接:http://poj.org/problem?id=3368

题目大意:有一串数字,查询区间中频数最大的数字的频数

题目分析:因为数字是按非递增序排列好的,我们可以先预处理出某连续数字在当前位置时出现的频数,比如样例有

dp[1]=1,val[1] = -1

dp[2]=2,val[2] = -1

dp[3]=1,val[3] = 1

dp[4]=2,val[4] = 1

dp[5]=3,val[5] = 1

dp[6]=4,val[6] = 1

。。。

则对于查询区间(l,r),答案即为区间(l,tmp)和(tmp,r)某一数字出现的频数的较大的那个(l <= tmp <= r)

对于区间(l,tmp)直接可得出答案,对于区间(tmp, r)我们可以用RMQ求解

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int const MAX = 100005;
int st[MAX][20], dp[MAX], val[MAX];
int n, q;

void RMQ_Init()
{
    for(int i = 1; i <= n; i++)
        st[i][0] = dp[i];
    int k = log((double)(n + 1)) / log(2.0);
    for(int j = 1; j <= k; j++)
        for(int i = 1; i + (1 << j) - 1 <= n; i++)
            st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}

int Query(int l, int r)
{
    if(l > r)
        return 0;
    int k = log((double)(r - l + 1)) / log(2.0);
    return max(st[l][k], st[r - (1 << k) + 1][k]);
}

int main()
{
    while(scanf("%d", &n) != EOF && n)
    {
        scanf("%d", &q);
        dp[1] = 1;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &val[i]);
            if(i > 1)
                dp[i] = (val[i] == val[i - 1] ? dp[i - 1] + 1 : 1);
        }
        RMQ_Init();
        while(q--)
        {
            int l, r;
            scanf("%d %d", &l, &r);
            int tmp = l;
            while(tmp <= r && val[tmp] == val[tmp - 1])
                tmp ++;
            printf("%d\n", max(Query(tmp, r), tmp - l));
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-23 22:05:39

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

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 cons

(简单) 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)

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)

题意:求给定区间中出现最多的数的出现次数,原数列为非降序列: 思路:将原数列处理为当前数在连续数中的出现顺序:从后向前处理: 对于查询区间[l,r],先通过二分计算与a[r]相同的数的个数(num[r]可能大于1):剩余区间rmq求最大值:在求两种情况的最大值: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m; int num[500010],mm[5

POJ 3368 Frequent values

Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13051 Accepted: 4792 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 consisti

poj 3368 Frequent values 解题报告

题目链接:http://poj.org/problem?id=3368 题目意思:给出一段 n 个数的序列你,对于区间 [l, r] 的询问,找出 出现频率最高的数的次数.考虑到序列中的数是非递减的,也就是相同的数会连续不间断地在一起,于是就才有了代码中这个部分来预判了: if (s > t)        printf("%d\n", ans); 这个人写RMQ 写得不错:http://dongxicheng.org/structure/lca-rmq/ 这题就是套模板的,纪念

poj 3368 Frequent values(线段树解法)

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

poj 3368 Frequent values(RMQ)

1 /************************************************************ 2 题目: Frequent values(poj 3368) 3 链接: http://poj.org/problem?id=3368 4 题意: 给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之 5 间连续出现次数最多的次数 6 算法: RMQ 7 思路: 借助数组f[i].表示第i位前面有f[i]个相同的数.对于 8 每个区间(l,r).暴力求前面几个

POJ 3368 Frequent values RMQ ST算法/线段树

                                                     Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15229   Accepted: 5550 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In