zoukankan      html  css  js  c++  java
  • Frequent values(倍增RMQ)

    Frequent values

    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
    题目大意:
    给出n个数和m个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数。
    思路:
    RMQ算法
    预处理一个数组:
    if(num[i]==num[i-1])
    a[i]=a[i-1]+1;
    else
    a[i]=1;
    对于每个询问(l,r),分为两个部分,前半部分求与l之前相同的数的个数直到t,后半部分从t开始直接用RMQ求解最大值就行了。
    最后结果为max(前半部分,后半部分)。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    using namespace std;
    const int maxn=100010;
    int n,m,num[maxn],a[maxn],f[maxn][20];
    int init()
    {
        int f=1,p=0;char c=getchar();
        while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){p=p*10+c-'0';c=getchar();}
        return f*p;
    }
    void prepare()
    {
        memset(f,-1,sizeof(-1));
        for(int i=1;i<=n;i++)
        f[i][0]=a[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++)
          f[i][j]=max(f[i][j-1],f[i+(1<<j-1)][j-1]);
    }
    int rmq(int l,int r)
    {
        if(l>r) return 0;
        int k=log((double)(r-l+1))/log(2.0);
        return max(f[l][k],f[r-(1<<k)+1][k]);
    }
    int main()
    {
        int l,r;
        while(cin>>n&&n)
        {
            m=init();
            for(int i=1;i<=n;i++)
            {
                num[i]=init();
                if(i==1)
                {
                    a[i]=1;
                    continue;
                }
                if(num[i]==num[i-1])
                a[i]=a[i-1]+1;
                else a[i]=1;
            }
            prepare();
            for(int i=1;i<=m;i++)
            {
                l=init();r=init();
                int t=l;
                while(t<=r&&num[t]==num[t-1])
                t++;
                int ans=rmq(t,r);
                ans=max(ans,t-l);
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    HAVING用法详解
    spring的定时任务
    js 将json字符串转换为json对象的方法解析
    Struts2 SSH整合框架返回json时,要注意懒加载问题
    Struts2 本是非单例的,与Spring集成就默认为单例
    Android 判断当前联网的类型 wifi、移动数据流量
    Android 动画的重复播放
    Android 监测手机联网状态 wifi、移动数据流量、无联网状态
    Android 设置界面的圆角选项
    Android 用Activity的onTouchEvent来监听滑动手势
  • 原文地址:https://www.cnblogs.com/cax1165/p/6070955.html
Copyright © 2011-2022 走看看