poj 3368 rmq ***

题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 #include<map>
 8 using namespace std;
 9 #define MOD 1000000007
10 const int INF=0x3f3f3f3f;
11 const double eps=1e-5;
12 typedef long long ll;
13 #define cl(a) memset(a,0,sizeof(a))
14 #define ts printf("*****\n");
15 int n,m,tt;
16 const int MAXN = 100005;
17 int dp[MAXN][20];
18 int mm[MAXN];
19 //初始化RMQ, b数组下标从1开始,从0开始简单修改
20 void initRMQ(int n,int b[])
21 {
22 mm[0] = -1;
23 for(int i = 1; i <= n;i++)
24 {
25 mm[i] = ((i&(i-1)) == 0)?mm[i-1]+1:mm[i-1];
26 dp[i][0] = b[i];
27 }
28 for(int j = 1; j <= mm[n];j++)
29 for(int i = 1;i + (1<<j) -1 <= n;i++)
30 dp[i][j] = max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
31 }
32 //查询最大值
33 int rmq(int x,int y)
34 {
35 int k = mm[y-x+1];
36 return max(dp[x][k],dp[y-(1<<k)+1][k]);
37 }
38 int a[MAXN],f[MAXN];
39 int main()
40 {
41     int i,j,k;
42     #ifndef ONLINE_JUDGE
43     freopen("1.in","r",stdin);
44     #endif
45     while(scanf("%d",&n)!=EOF)
46     {
47         if(n==0)  break;
48         scanf("%d",&m);
49         for(i=1;i<=n;i++)
50         {
51             scanf("%d",&a[i]);
52             f[1]=1;
53             if(i!=1)
54             {
55                 if(a[i]==a[i-1])
56                 {
57                     f[i]=f[i-1]+1;
58                 }
59                 else
60                     f[i]=1;
61             }
62         }
63         /*for(i=1;i<=n;i++)
64         {
65             printf("%d ",f[i]);
66         }
67         printf("\n");*/
68         initRMQ(n,f);
69         while(m--)
70         {
71             int uu,v;
72             scanf("%d%d",&uu,&v);
73             int u=uu;
74             while(u<=v&&a[u]==a[u-1])
75             {
76                 u++;
77             }
78             int ans=rmq(u,v);
79             ans=max(u-uu,ans);
80             printf("%d\n",ans);
81         }
82     }
83 }
时间: 2024-08-09 06:58:57

poj 3368 rmq ***的相关文章

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).暴力求前面几个

【POJ 3368】 Frequent values(RMQ)

[POJ 3368] Frequent values(RMQ) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15813   Accepted: 5749 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given seve

poj 3368(RMQ简单应用)

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

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 3264 RMQ Spare Table算法

今天下午大帝讲的,我以前也不懂,所以也就跟着学学了,把中间的那个状态转移方程学错了好几次,于是就wa了 好几发. #include<iostream> #include<cstdio> #include<algorithm> #define maxn 200010 using namespace std; int a[maxn],m,n,b[maxn],fl[maxn][50],fr[maxn][50]; void solve() { b[1]=0;//其实就是用来计算

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

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