• H


    Problem F: Frequent values

    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 Specification

    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 Specification

    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问题
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <string>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <list>
    13 #include <iomanip>
    14 #include <cstdlib>
    15 #include <sstream>
    16 using namespace std;
    17 typedef long long LL;
    18 const int INF=0x5fffffff;
    19 const double EXP=1e-6;
    20 const int MS=100005;
    21 
    22 int dp[MS][20];
    23 int a[MS],cnt[MS];
    24 int num[MS],l[MS],r[MS];
    25 int n,q;
    26 
    27 void RMQ_init()
    28 {
    29     for(int i=0;i<n;i++)
    30         dp[i][0]=cnt[i];
    31     for(int j=1;(1<<j)<=n;j++)
    32     {
    33         for(int i=0;i+(1<<j)-1<n;i++)
    34             dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
    35     }
    36 }
    37 
    38 int RMQ(int l,int r)
    39 {
    40     int k=0;
    41     while(1<<(k+1)<=r-l+1)
    42         k++;
    43     return max(dp[l][k],dp[r-(1<<k)+1][k]);
    44 }
    45 
    46 
    47 
    48 int main()
    49 {
    50     while(scanf("%d%d",&n,&q)==2&&n)
    51     {
    52         for(int i=0;i<n;i++)
    53             scanf("%d",&a[i]);
    54         a[n]=a[n-1]+1;
    55         int start=0;
    56         int id=0;
    57         for(int i=0;i<=n;i++)
    58         {
    59             if(i>0&&a[i]>a[i-1])
    60             {
    61                 for(int j=start;j<i;j++)
    62                     r[j]=i-1;
    63                 cnt[id]=i-start;
    64                 id++;
    65                 start=i;
    66             }
    67             l[i]=start;
    68             num[i]=id;
    69         }
    70         n=id;
    71         RMQ_init();
    72         int x,y;
    73         while(q--)
    74         {
    75             scanf("%d%d",&x,&y);
    76             x--;
    77             y--;
    78             int ans=0;
    79             if(num[x]==num[y])
    80             {
    81                 printf("%d
    ",y-x+1);
    82                 continue;
    83             }
    84             ans=max(r[x]-x+1,y-l[y]+1);
    85             if(num[x]+1<num[y])
    86                 ans=max(ans,RMQ(num[x]+1,num[y]-1));
    87             printf("%d
    ",ans);
    88         }
    89 
    90     }
    91     return 0;
    92 }
    
    
    

  • 相关阅读:
    如何提高技术素养
    spoolsv.exe 无法启动
    太阳高度角和方位角的计算
    树莓派 3 alsa 声卡驱动
    PHP 7 Xdebug 深深的坑
    java 线性规划 和lingo 比较
    Python Microsoft Visual C++ Compiler Package for Python 2.7
    Node debug
    angular 调试 js (分 karms protractor / test e2e unit )
    hbase scan 的例子
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4345394.html
Copyright © 2020-2023  润新知