Simple POJ 3264 Balanced lineup,rmq.

Source: Internet
Author: User

Description

For the daily milking, Farmer John's n cows (1≤ n ≤50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate frisbee with some of the cows. To keep things simple, he'll take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to has fun they should not differ too much in height.

Farmer John has made a list of Q (1≤ q ≤200,000) Potential groups of cows and their heights (1≤ H Eight ≤1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the G Roup.

The problem is to ask for RMQ, water problem.

The code is as follows:

//The ━━━━━━ of gods and Beasts ━━━━━━//┏┓┏┓//┏┛┻━━━━━━━┛┻┓//┃┃//┃━┃//████━████┃//┃┃//┃┻┃//┃┃//┗━┓┏━┛//┃┃//┃┃//┃┗━━━┓//┃┣┓//┃┏┛//┗┓┓┏━━━━━┳┓┏┛//┃┫┫┃┫┫//┗┻┛┗┻┛////━━━━━━ Feel the ━━━━━━ of Meng Meng//author:whywhy//Created time:2015 July 17 Friday 16:52 31 seconds//File name:3264.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=50004;intdp1[maxn][ -],dp2[maxn][ -];intLOGN[MAXN];voidInitintNintnum[]) {logn[0]=-1;  for(intI=1; i<=n;++i) {dp1[i][0]=Num[i]; dp2[i][0]=Num[i]; Logn[i]=logn[i-1]+ ((i& (i-1))==0); }     for(intj=1; j<=logn[n];++j) for(intI=1; i+ (1&LT;&LT;J)-1<=n;++i) {dp1[i][j]=max (dp1[i][j-1],dp1[i+ (1<< (J-1))][j-1]); DP2[I][J]=min (dp2[i][j-1],dp2[i+ (1<< (J-1))][j-1]); }}intRMQ (intXinty) {    intk=logn[y-x+1]; returnMax (dp1[x][k],dp1[y-(1&LT;&LT;K) +1][k])-min (dp2[x][k],dp2[y-(1&LT;&LT;K) +1][k]);}intNUM[MAXN];intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);    intn,q; intb;  while(~SCANF ("%d%d",&n,&Q)) { for(intI=1; i<=n;++i) scanf ("%d",&Num[i]);        Init (n,num);  while(q--) {scanf ("%d%d",&a,&b); printf ("%d\n", RMQ (A, b)); }    }        return 0;}
View Code

Simple POJ 3264 Balanced lineup,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.