POJ 3368 Frequent values (RMQ)

 

 

Frequent values
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11540   Accepted: 4206

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

Source

 
 
 
 
 

 

 

这题需要一个区间内,出现次数最多的数出现的次数。

 

先离散化处理下,然后转化成RMQ问题。

 

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <iostream>
 5 using namespace std;
 6 
 7 const int MAXN = 100010;
 8 
 9 int dp[MAXN][20];
10 int mm[MAXN];
11 
12 void initRMQ(int n,int b[])
13 {
14     mm[0] = -1;
15     for(int i = 1;i <= n;i++)
16     {
17         mm[i] = ((i&(i-1)) == 0)?mm[i-1]+1:mm[i-1];
18         dp[i][0] = b[i];
19     }
20     for(int j = 1;j <= mm[n];j++)
21         for(int i = 1;i + (1<<j) - 1 <= n;i++)
22             dp[i][j] = max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
23 }
24 int rmq(int x,int y)
25 {
26     int k = mm[y-x+1];
27     return max(dp[x][k],dp[y-(1<<k)+1][k]);
28 }
29 
30 int a[MAXN];
31 int hash[MAXN];
32 int L[MAXN],R[MAXN];
33 int b[MAXN];
34 
35 int main()
36 {
37     int n,m;
38     while(scanf("%d",&n) == 1 && n)
39     {
40         scanf("%d",&m);
41         for(int i = 1;i <= n;i++)
42             scanf("%d",&a[i]);
43         int k = 1;
44         int l = 1;
45         memset(b,0,sizeof(b));
46         for(int i = 1;i <= n;i++)
47         {
48             if(i > 1 && a[i] != a[i-1])
49             {
50                 for(int j = l;j < i;j++)
51                 {
52                     L[j] = l;
53                     R[j] = i-1;
54                 }
55                 b[k] = i - l;
56                 l = i;
57                 k ++;
58             }
59             hash[i] = k;
60         }
61         for(int j = l;j <= n;j++)
62         {
63             L[j] = l;
64             R[j] = n;
65         }
66         b[k] = n-l+1;
67         initRMQ(k,b);
68 
69         int u,v;
70         while(m--)
71         {
72             scanf("%d%d",&u,&v);
73             int t1 = hash[u];
74             int t2 = hash[v];
75             if(t1 == t2)
76             {
77                 printf("%d\n",v-u+1);
78                 continue;
79             }
80             int ans = max(R[u]-u+1,v-L[v]+1);
81             t1++;
82             t2--;
83             if(t1 <= t2)ans = max(ans,rmq(t1,t2));
84             printf("%d\n",ans);
85         }
86     }
87     return 0;
88 }

 

 

posted on 2013-07-31 00:38  kuangbin  阅读(659)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: