Simple POJ 3368 frequent values,rmq.

Source: Internet
Author: User

Description

You are given a sequence of n integers a1, A2, ..., an in non-decreasing order. In addition to, given several queries consisting of indices I and J (1≤i≤j≤n ). For each query, determine the most frequent value among the integers ai, ..., AJ.

The question is what is the highest frequency within the range.

With rmq words, the statistics of a number and the previous frequency, and then ask the time is to find the maximum value of two parts, the first part is the same number as X, the second part is the first and x different from the part of Y, with RMQ for the second part.

Then unable to spit groove himself, Logn array opened small, the result has been WA, really enough silly.

The code is as follows:

//The ━━━━━━ of gods and Beasts ━━━━━━//┏┓┏┓//┏┛┻━━━━━━━┛┻┓//┃┃//┃━┃//████━████┃//┃┃//┃┻┃//┃┃//┗━┓┏━┛//┃┃//┃┃//┃┗━━━┓//┃┣┓//┃┏┛//┗┓┓┏━━━━━┳┓┏┛//┃┫┫┃┫┫//┗┻┛┗┻┛////━━━━━━ Feel the ━━━━━━ of Meng Meng//author:whywhy//Created time:2015 July 17 Friday 17:11 45 seconds//File name:3368.cpp#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<time.h>using namespacestd;Const intmaxn=100005;intdp[maxn][ -];intLOGN[MAXN];//!!!voidInitintNintnum[]) {logn[0]=-1;  for(intI=1; i<=n;++i) {Logn[i]=logn[i-1]+ ((i& (i-1))==0); dp[i][0]=Num[i]; }     for(intj=1; j<=logn[n];++j) for(intI=1; i+ (1&LT;&LT;J)-1<=n;++i) dp[i][j]=max (dp[i][j-1],dp[i+ (1<< (J-1))][j-1]);}intRMQ (intAintb) {    if(a>b)return 0; intk=logn[b-a+1]; returnMax (dp[a][k],dp[b-(1&LT;&LT;K) +1][k]);}intNUM[MAXN];intREM[MAXN],WEI[MAXN];intn,q;intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);        intb;  while(~SCANF ("%d", &n) &&N) {scanf ("%d",&p); rem[1]=1;  for(intI=1; i<=n;++i) scanf ("%d",&Num[i]);  for(intI=2; i<=n;++i)if(num[i]==num[i-1]) Rem[i]=rem[i-1]+1; ElseRem[i]=1; Wei[n]=o;  for(intj=m-1; i>=1;--i)if(num[i]==num[i+1]) Wei[i]=wei[i+1]; ElseWei[i]=i;        Init (N,REM);  while(q--) {scanf ("%d%d",&a,&b); printf ("%d\n", Max (RMQ (wei[a]+1, b), Min (wei[a],b)-a+1)); }    }        return 0;}
View Code

Simple 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.