POJ 3368 Frequent values (RMQ)

Source: Internet
Author: User

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&LT;&LT;J) <=n;j++) -     { in          for(intI=1; i+ (1&LT;&LT;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&GT;V)return 0; the     intK= (int) (Log (v-u+1)/log (2.0)); +     returnMax (dp[u][k],dp[v-(1&LT;&LT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.