zoukankan      html  css  js  c++  java
  • POJ 3264 Balanced Lineup (ST算法)

    题目链接:POJ 3264

    Problem Description

    For the daily milking, Farmer John's (N) cows ((1 le N le 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 le Q le 200,000)) potential groups of cows and their (heights (1 le height le 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
    

    Source

    USACO 2007 January Silver

    Solution

    题意

    给定 (n) 个数,(q) 个询问。每个询问包含 (l)(r),求区间 ([l, r]) 内的最大值减最小值的差值。

    题解

    ST算法

    ST 算法模板题。

    Code

    #include <cstdio>
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    const int maxn = 1e5 + 10;
    
    int a[maxn];
    int f[maxn][50], g[maxn][50];
    int n, q;
    
    void st_prework() {
        for(int i = 1; i <= n; ++i) {
            f[i][0] = a[i];
            g[i][0] = a[i];
        }
        int t = log(n) / log(2) + 1;
        for(int j = 1; j < t; ++j) {
            for(int i = 1; i <= n - (1 << j) + 1; ++i) {
                f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
                g[i][j] = min(g[i][j - 1], g[i + (1 << (j - 1))][j - 1]);
            }
        }
    }
    
    int st_query(int l, int r) {
        int k = log(r - l + 1) / log(2);
        return max(f[l][k], f[r - (1 << k) + 1][k]) - min(g[l][k], g[r - (1 << k) + 1][k]);
    }
    
    int main() {
        scanf("%d%d", &n, &q);
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &a[i]);
        }
        st_prework();
        for(int i = 0; i < q; ++i) {
            int l, r;
            scanf("%d%d", &l, &r);
            printf("%d
    ", st_query(l, r));
        }
        return 0;
    }
    
  • 相关阅读:
    汇编语言
    离散数学:每条边的权重均不相同的带权图有唯一最小生成树
    android源码如何起步与阅读方法
    linux内核——会话、进程组、线程组
    ubuntu系统——增加磁盘空间
    Android系统源代码——所需工具
    android源码相关网站
    git——分布式版本控制系统
    linux内核——进程,轻量级进程,线程,线程组
    Android系统源代码学习步骤
  • 原文地址:https://www.cnblogs.com/wulitaotao/p/11461019.html
Copyright © 2011-2022 走看看