zoukankan      html  css  js  c++  java
  • (简单) 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
    ",RMQ(a,b));
            }
        }
        
        return 0;
    }
    View Code
  • 相关阅读:
    利用数据库复制技术 实现MSSQL数据同步更新
    育子两篇你会教育自已的小孩吗
    hdu 1046 Gridland (找规律题)
    hdu 1022 Train Problem I (栈的操作,还水了半天)
    hdu 4022 Bombing (强大的map一对多的映射)
    POJ 1702 Eva's Balance (数论,平衡三进制)
    hdu 3951 Coin Game (博弈)
    hdu 1058 Humble Numbers (DP初步)
    hdu 2084 数塔 (DP初步)
    hdu 1056 HangOver (打表水题)
  • 原文地址:https://www.cnblogs.com/whywhy/p/4655138.html
Copyright © 2011-2022 走看看