zoukankan      html  css  js  c++  java
  • poj_3264

    Balanced Lineup
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 27473   Accepted: 12898
    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 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.

    Input

    Line 1: Two space-separated integers, N and Q
    Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i 
    Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

    Output

    Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

    Sample Input

    6 3
    1
    7
    3
    4
    2
    5
    1 5
    4 6
    2 2

    Sample Output

    6
    3
    0
    #include<iostream>
    #include<cmath>
    using namespace std;
    #pragma warning(disable : 4996)
    #define MAXN 50005
    #define min(a, b)   ((a) <= (b) ? (a) : (b))
    #define max(a, b)   ((a) >= (b) ? (a) : (b))
    
    int num[MAXN];
    int f1[MAXN][100];
    int f2[MAXN][100];
    
    //sparse table算法
    void st(int n)
    { 
        int i, j, k, m;
        k = (int) (log((double)n) / log(2.0)); 
        for(i = 0; i < n; i++) 
        {
            f1[i][0] = num[i]; //递推的初值
            f2[i][0] = num[i];
        }
        for(j = 1; j <= k; j++)
        { //自底向上递推
            for(i = 0; i + (1 << j) - 1 < n; i++)
            {
                m = i + (1 << (j - 1)); //求出中间的那个值
                f1[i][j] = max(f1[i][j-1], f1[m][j-1]);
                f2[i][j] = min(f2[i][j-1], f2[m][j-1]);
            }
        }
    }
    
    //查询i和j之间的最值,注意i是从0开始的
    void rmq(int i, int j) 
    { 
        int k = (int)(log(double(j-i+1)) / log(2.0)), t1, t2; //用对2去对数的方法求出k
        t1 = max(f1[i][k], f1[j - (1<<k) + 1][k]);
        t2 = min(f2[i][k], f2[j - (1<<k) + 1][k]);
        printf("%d\n",t1 - t2);
    }
    
    int main()
    {
        int i, n, q, x, y;
        scanf("%d %d", &n, &q);
        for (i = 0; i < n; ++i)
        {
            scanf("%d", &num[i]);
        }
    
        st(n); //初始化
        while(q--)
        {
            scanf("%d %d", &x, &y);
            rmq(x - 1, y - 1);
        }
        return 0;
    }



  • 相关阅读:
    WEB功能测试说明
    年轻的采访可以学到很多东西
    金融系列11《电子现金应用》
    vs2010调用matlab2011下的.m文件
    使用VS2010调用matlab的mat格式文件
    matlab mex入门简介
    MEX文件编写和调试
    MAT文件操作
    8.Redis 数据备份与恢复
    JAVA_OPTS设置
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835007.html
Copyright © 2011-2022 走看看