1 /************************************************************2 title: Frequent values (POJ 3368)3 Links:http://poj.org/problem?id=33684 Test Instructions: Give the n number and Q query (L,R), for each query to find (L,R)5 the maximum number of consecutive occurrences6 algorithm: RMQ7 idea: With the help of array f[i]. Indicates that F[i] is the same number in front of the first bit. For8 each interval (l,r). The violence begged for several of the same numbers before. And then use the RMQ9 the value of the trailing interval. Ten *************************************************************/ One#include <cstdio> A#include <cstring> -#include <cstdlib> -#include <algorithm> the#include <iostream> -#include <cmath> - using namespacestd; - + Const intmx=100005; - intdp[mx][ -]; + intA[MX],F[MX]; A intn,q; at - voidMAKERMQ () - { - for(intI=1; i<=n;i++) dp[i][0]=F[i]; - for(intj=1;(1<<J) <=n;j++) - { in for(intI=1; i+ (1<<J)-1<=n;i++) - { toDp[i][j]=max (dp[i][j-1],dp[i+ (1<< (J-1))][j-1]); + } - } the } * $ intRmqintUintv)Panax Notoginseng { - if(U>V)return 0; the intK= (int) (Log (v-u+1)/log (2.0)); + returnMax (dp[u][k],dp[v-(1<<K) +1][k]); A } the + intMain () - { $ while(~SCANF ("%d", &n) &&N) $ { -scanf"%d",&q); - for(intI=1; i<=n;i++) scanf ("%d",&a[i]); thef[1]=1; - for(intI=2; i<=n;i++)Wuyi { the if(a[i]==a[i-1]) f[i]=f[i-1]+1; - Elsef[i]=1; Wu } - makermq (); About $ while(q--) - { - intL,r; -scanf"%d%d",&l,&R); A intans=1; + for(l=l+1; l<=r;l++) the { - if(a[l]!=a[l-1]) Break; $ans++; the } theans=Max (ANS,RMQ (l,r)); theprintf"%d\n", ans); the } - } in}
POJ 3368 Frequent values (RMQ)