• 【POJ 3368】Frequent values


    Frequent values
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 15880   Accepted: 5778

    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




    题目大意:求区间内重复次数最多的元素所重复的次数。

    思路:由于数据的范围很大,所以是一道很典型的RMQ,由于数组保证非降序,所以相同元素一定排列在一起,这样我们就可以将相同元素划分在一个区间内,用一个num数组储存第i个元素在新划分后的第一个小区间,left数组储存第i个小区间的左端点,right数组储存右端点,res存第i个区间有多少个元素,这样在求解时如果发现输入的l和r值在一个区间内,答案即为r-l+1,若不在一个区间内,答案即为right[num[l]]-l+1,r-left[num[r]]+1,RMQ(num[l]+1,num[r]-1))三者中的最大值。


    代码:

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <vector>
    #define N 100010
    using namespace std;
    int num[N],lef[N],rig[N],res[N],dp[N][20],cnt,n;
    int maxn(int a,int b,int c)
    {
        return max(max(a,b),c);
    }
    void RMQ_init()
    {
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=cnt; i++) dp[i][0]=res[i];
        for(int j=1; (1<<j)<=cnt; j++)
            for(int i=1; i+(1<<j-1)<=cnt; i++)
                dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
    }
    int RMQ(int L,int R)
    {
        if(L>R)
            return 0;
        int k=0;
        while((1<<(k+1))<=R-L+1) k++;
        return max(dp[L][k],dp[R-(1<<k)+1][k]);
    }
    int main()
    {
        int q,temp,before;
        while(~scanf("%d",&n)&&n)
        {
            scanf("%d",&q);
            cnt=0;
            memset(num,0,sizeof(num));
            memset(lef,0,sizeof(lef));
            memset(rig,0,sizeof(rig));
            memset(res,0,sizeof(res));
            for(int i=1; i<=n; i++)
            {
                scanf("%d",&temp);
                if(i==1)
                {
                    ++cnt;
                    lef[cnt]=1;
                    before=temp;
                }
                if(temp==before)
                {
                    num[i]=cnt;
                    res[cnt]++;
                    rig[cnt]++;
                }
                else
                {
                    num[i]=++cnt;
                    res[cnt]++;
                    lef[cnt]=rig[cnt]=i;
                    before=temp;
                }
            }
            RMQ_init();
            while(q--)
            {
                int l,r;
                scanf("%d%d",&l,&r);
                if(num[l]==num[r])
                {
                    printf("%d
    ",r-l+1);
                    continue;
                }
                printf("%d
    ",maxn(rig[num[l]]-l+1,r-lef[num[r]]+1,RMQ(num[l]+1,num[r]-1)));
            }
        }
    }
    



  • 相关阅读:
    【jQuery】用jQuery给文本框添加只读属性【readOnly】
    解决embed标签显示在div上层【转藏】
    width:100% 和 max-width:100%; 有区别吗【转藏】
    一位资深程序员的独白
    jQuery 取值、赋值的基本方法【转藏】
    js判断手机端操作系统(Andorid/IOS)
    PhpStrom 和 wamp 配置 xdebug
    php 中 ?? 和 empty 的 区别
    phpSpreadSheet 中 使用的 一些坑
    html td 限制 高度 和 宽度
  • 原文地址:https://www.cnblogs.com/Torrance/p/5410563.html
Copyright © 2020-2023  润新知