UVA-11235 frequent values (RMQ)

Source: Internet
Author: User

The main idea: in a non-descending sequence of length n, there are M queries, each query (I,J) indicates the number of occurrences of the most frequently occurring element in the interval (i,j).

Title analysis: Because the sequence is ordered, you can segment the sequence and record the number of elements per paragraph, the segment num (i) each element belongs to, the left endpoint L (i) and the right endpoint R (i) for each element. So for each query:

Ans (i,j) =max (max (R (i)-i+1,j-l (j) +1), RMQ (num (i) +1,num (j)-1)).

Ans (i,j) =r-j+1 (if I and J belong to the same paragraph)

The code is as follows:

# include<iostream># include<cstdio># include<cstring># include<vector># include<queue ># include<list># include<set># include<map># include<string># include<cmath># include<cstdlib># include<algorithm>using namespace std;# define LL long longconst int n=1005;const int INF=1 000000000;const LL oo=0x7fffffffffffffff;const Double eps=1e-10;int n,m;int a[n*100];vector<int>v;int num[N*100 ];int l[n*100];int r[n*100];int d[n*100][20];void rmq_init () {int len=v.size (); for (int i=0;i<len;++i) d[i][0]=v[i]; for (int j=1, (1<<j) <=len;++j) for (int i=0;i+ (1&LT;&LT;J) -1<len;++i) D[i][j]=max (d[i][j-1],d[i+ (1< < (J-1))][j-1]);} void Init () {v.clear (); int cnt=1;num[0]=0; l[0]=0;for (int i=1;i<n;++i) {if (a[i]==a[i-1]) {++cnt;num[i]=num[i-1]; L[I]=L[I-1];} Else{v.push_back (CNT); num[i]=v.size (); L[i]=i;cnt=1;}} V.push_back (CNT); r[n-1]=n-1;for (int i=n-2;i>=0;--i) {if (a[i]==a[i+1]) r[i]=r[i+1];elser[i]=i;} Rmq_init ();} int QuerY (int l,int R) {if (l>r) return 0;int k=0;while ((1<< (k+1)) <=r-l+1) ++k;return Max (d[l][k],d[r-(1<<k) + 1][k]);} int solve (int l,int r) {if (Num[l]==num[r]) return R-l+1;else{return Max (max (r[l]-l+1,r-l[r]+1), query (num[l]+1,num[r]- 1));}} int main () {while (scanf ("%d", &n) &&n) {scanf ("%d", &m), for (int i=0;i<n;++i) scanf ("%d", a+i); Init () int L,r;while (m--) {scanf ("%d%d", &l,&r),--l,--r;printf ("%d\n", Solve (L,r));}} return 0;}

  

UVA-11235 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.