zoukankan      html  css  js  c++  java
  • 【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)));
            }
        }
    }
    



  • 相关阅读:
    C# 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母
    .net简单录音和播放音频文件代码
    一个简单的小例子让你明白c#中的委托-终于懂了!
    你是高级程序员?那就来挑战一下!
    .NET中的三种Timer的区别和用法
    C#中判断空字符串的3种方法性能分析
    解决statusStrip控件上的项目不能靠右对齐的问题
    C#的WebBrowser操作frame如此简单
    Python学习---Python安装与基础1205
    Java学习---IKAnalyzer中文分词器V2012_FF使用手册
  • 原文地址:https://www.cnblogs.com/Torrance/p/5410563.html
Copyright © 2011-2022 走看看