poj 3368 Frequent values(RMQ)

 1 /************************************************************
 2 题目:    Frequent values(poj 3368)
 3 链接:    http://poj.org/problem?id=3368
 4 题意:    给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之
 5           间连续出现次数最多的次数
 6 算法:    RMQ
 7 思路:    借助数组f[i]。表示第i位前面有f[i]个相同的数。对于
 8           每个区间(l,r)。暴力求前面几个相同的数。然后在用RMQ
 9           求后面区间的值。
10 *************************************************************/
11 #include<cstdio>
12 #include<cstring>
13 #include<cstdlib>
14 #include<algorithm>
15 #include<iostream>
16 #include<cmath>
17 using namespace std;
18
19 const int mx=100005;
20 int dp[mx][30];
21 int a[mx],f[mx];
22 int n,q;
23
24 void makermq()
25 {
26     for (int i=1;i<=n;i++) dp[i][0]=f[i];
27     for (int j=1;(1<<j)<=n;j++)
28     {
29         for (int i=1;i+(1<<j)-1<=n;i++)
30         {
31             dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
32         }
33     }
34 }
35
36 int rmq(int u,int v)
37 {
38     if (u>v) return 0;
39     int k=(int)(log(v-u+1)/log(2.0));
40     return max(dp[u][k],dp[v-(1<<k)+1][k]);
41 }
42
43 int main()
44 {
45     while (~scanf("%d",&n)&&n)
46     {
47         scanf("%d",&q);
48         for (int i=1;i<=n;i++) scanf("%d",&a[i]);
49         f[1]=1;
50         for (int i=2;i<=n;i++)
51         {
52             if (a[i]==a[i-1]) f[i]=f[i-1]+1;
53             else f[i]=1;
54         }
55         makermq();
56
57         while (q--)
58         {
59             int l,r;
60             scanf("%d%d",&l,&r);
61             int ans=1;
62             for (l=l+1;l<=r;l++)
63             {
64                 if (a[l]!=a[l-1]) break;
65                 ans++;
66             }
67             ans=max(ans,rmq(l,r));
68             printf("%d\n",ans);
69         }
70     }
71 }
时间: 2024-08-05 17:59:09

poj 3368 Frequent values(RMQ)的相关文章

POJ 3368 Frequent values RMQ ST算法/线段树

                                                     Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15229   Accepted: 5550 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In

POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)

题目链接:http://poj.org/problem?id=3368 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, deter

HDU 1806 &amp;&amp; POJ 3368 Frequent values (RMQ)

既然是非降序,那么相等的点一定都聚集在了一块,然后将相等的点分成一段.然后记录每一段的长度,最右端与最左端,然后记录原数列上每个位置上属于哪一段的标号.然后对于每个询问都可以分成3部分,分别计算每一部分,然后对这三部分取最大值. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include

POJ 3368 Frequent values RMQ 训练指南 好题

1 #include<cstdio> 2 #include<cstring> 3 4 const int maxn=1e5+5; 5 const int inf=0x3f3f3f3f; 6 7 inline int max(int x,int y) 8 { 9 return x>y?x:y; 10 } 11 12 int a[maxn]; 13 int left[maxn]; 14 int right[maxn]; 15 int num[maxn]; 16 int dp[ma

POJ 3368 Frequent values

Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13051 Accepted: 4792 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 consisti

poj 3368 Frequent values 解题报告

题目链接:http://poj.org/problem?id=3368 题目意思:给出一段 n 个数的序列你,对于区间 [l, r] 的询问,找出 出现频率最高的数的次数.考虑到序列中的数是非递减的,也就是相同的数会连续不间断地在一起,于是就才有了代码中这个部分来预判了: if (s > t)        printf("%d\n", ans); 这个人写RMQ 写得不错:http://dongxicheng.org/structure/lca-rmq/ 这题就是套模板的,纪念

poj 3368 Frequent values(线段树解法)

题目链接:http://poj.org/problem?id=3368 题目大意:给你一段不下降的序列,求给定区间里出现次数最多的那个数字的次数. 思路:首先看到这题时,第一感觉线段树,但是仔细一看问题来啦,用线段数我怎么才能计算出某段区间里出现的那个数,因为出现最多的那个数可能不是在他它的左儿子上也不是在它的右儿子上,可能在当他们合并成一个区间时就出现啦,但是这儿我们需要注意的就是,题目给的是一段不下降的序列,那么突破口就出来啦,因为如果出现相同的数字,那么它们一定是连续的.所以我们只需要在普

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 consi

POJ 3368 Frequent values(RMQ)

Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15134   Accepted: 5519 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 cons