zoukankan      html  css  js  c++  java
  • 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 ", max(Query(tmp, r), tmp - l)); } } }



  • 相关阅读:
    【CentOS】CentOS7开放及查看端口
    【nginx】配置https 证书生成的方法
    【MacOs】 Royal TSX SSH 和 FTP 中文乱码
    【PHP】thinkphp3.2.5
    【TCP/IP】入门学习笔记 五
    【TCP/IP】入门学习笔记 四
    HTTP
    【PHP】高并发和大流量的解决方案(思路)
    react多级路由 重定向与404定义
    react自定义导航组件 路由参数
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6707067.html
Copyright © 2011-2022 走看看