(简单) POJ 3264 Balanced Lineup,RMQ。

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 will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have 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 ≤ height ≤ 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 group.

  题目就是求RMQ,水题。

代码如下:

// ━━━━━━神兽出没━━━━━━
//      ┏┓       ┏┓
//     ┏┛┻━━━━━━━┛┻┓
//     ┃           ┃
//     ┃     ━     ┃
//     ████━████   ┃
//     ┃           ┃
//     ┃    ┻      ┃
//     ┃           ┃
//     ┗━┓       ┏━┛
//       ┃       ┃
//       ┃       ┃
//       ┃       ┗━━━┓
//       ┃           ┣┓
//       ┃           ┏┛
//       ┗┓┓┏━━━━━┳┓┏┛
//        ┃┫┫     ┃┫┫
//        ┗┻┛     ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━

// Author        : WhyWhy
// Created Time  : 2015年07月17日 星期五 16时52分31秒
// 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 namespace std;

const int MaxN=50004;

int dp1[MaxN][20],dp2[MaxN][20];
int logN[MaxN];

void init(int N,int num[])
{
    logN[0]=-1;

    for(int i=1;i<=N;++i)
    {
        dp1[i][0]=num[i];
        dp2[i][0]=num[i];
        logN[i]=logN[i-1]+((i&(i-1))==0);
    }

    for(int j=1;j<=logN[N];++j)
        for(int i=1;i+(1<<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]);
        }
}

int RMQ(int x,int y)
{
    int k=logN[y-x+1];

    return max(dp1[x][k],dp1[y-(1<<k)+1][k])-min(dp2[x][k],dp2[y-(1<<k)+1][k]);
}

int num[MaxN];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int N,Q;
    int a,b;

    while(~scanf("%d %d",&N,&Q))
    {
        for(int i=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;
}

时间: 2024-10-11 04:02:27

(简单) POJ 3264 Balanced Lineup,RMQ。的相关文章

poj 3264 Balanced Lineup RMQ线段树实现

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36613   Accepted: 17141 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

POJ - 3264 Balanced Lineup (RMQ问题求区间最值)

RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就是说,RMQ问题是指求区间最值的问题. Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description For the daily

POJ 3264 Balanced Lineup(RMQ/线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 40312   Accepted: 18936 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

POJ 3264 Balanced Lineup[RMQ入门题]

题目链接:http://poj.org/problem?id=3264 题目大意:n个数,求区间[ L,R ]的最大最小值之差: 题目分析: RQM:dp[ i ][ j ], i开始长度为2^j的长度的区间最值: O(nlog n)的预处理区间值,O(1)的查询: 代码: //author: ACsorry //result: accept #include<iostream> #include<cstdio> #include<cstring> #include&l

POJ 3264 Balanced Lineup(RMQ)

这道题为裸的RMQ.具体RMQ的意思为区间最值查询,他是先预处理出来每一段的最值,然后查询的时候直接O(1)的复杂度得出结果.其实还是个dp.用Rmin[i][j]表示从i开始长度为2^j这个区间的最值.至于为什么是2^j,因为计算机当中是二进制,移位比较方便.查询的时候也是将一个区间分成两部分,其中这两部分有可能重叠. 这个题意:给定区间,让求区间最大值最小值的差值. #include <cstdio> #include <iostream> #include <cstri

POJ 3264 Balanced Lineup(RMQ详解)

RMQ:(区间最值问题) 本质上是动态规划,用d(i, j) 表示 从 i 开始的长度为 2^j 的一段元素的最小值,则可以用递推的方法计算d(i, j) : d(i, j) = min{ d(i, j-1), d(i + 2^(j-1), j-1)} 由于2^j <= n 因此 d数组中元素个数不超过nlogn, 因此总时间复杂度为O(nlogn); #include <iostream> #include <cstring> #include <cstdlib>

poj 3264 Balanced Lineup

题目链接:http://poj.org/problem?id=3264 题目大意:就是给你一串数,问你最大数和最小数的差值....... 思路:最基本的线段树,只需要建树和查询,修改都省啦,但是查询要写两个,一个查询最大值,一个查询最小值......然后就能AC掉.....但是话说poj把它分类到RMQ中.... code: #include<cstdio> #include<cmath> #include<algorithm> #include<iostream

[POJ] 3264 Balanced Lineup [ST算法]

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

POJ 3264 Balanced Lineup ST表

链接:http://poj.org/problem?id=3264 题意:给一串数字,多次询问,求区间最大值和区间最小值的差. 思路:RMQ问题,可以用O(N^2)的预处理,然后每次O(1)的查询,可以用线段树,O(N)的建树,O(logN)的查询,可以用ST表记录,O(NlogN)的预处理,O(1)的查询. 实际上ST表的预处理过程也是一个DP的过程dp[i][j]表示从第i位开始连续2^j位的区间最值. 预处理:dp[i][j]=min(dp[i][j],dp[i+2^j][j]),查询:q