• HDU 5443 The Water Problem (ST算法)


    题目链接:HDU 5443

    Problem Description

    In Land waterless, water is a very limited resource. People always fight for the biggest source of water. Given a sequence of water sources with (a_1,a_2,a_3,...,a_n) representing the size of the water source. Given a set of queries each containing (2) integers (l) and (r), please find out the biggest water source between (a_l) and (a_r).

    Input

    First you are given an integer (T(Tle 10)) indicating the number of test cases. For each test case, there is a number (n(0le nle 1000)) on a line representing the number of water sources. (n) integers follow, respectively (a_1,a_2,a_3,...,a_n), and each integer is in ({1,...,10^6}). On the next line, there is a number (q(0le qle 1000)) representing the number of queries. After that, there will be (q) lines with two integers (l) and (r(1le lle rle n)) indicating the range of which you should find out the biggest water source.

    Output

    For each query, output an integer representing the size of the biggest water source.

    Sample Input

    3
    1
    100
    1
    1 1
    5
    1 2 3 4 5
    5
    1 2
    1 3
    2 4
    3 4
    3 5
    3
    1 999999 1
    4
    1 1
    1 2
    2 3
    3 3
    

    Sample Output

    100
    2
    3
    4
    4
    5
    1
    999999
    999999
    1
    

    Source

    2015 ACM/ICPC Asia Regional Changchun Online

    Solution

    题意

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

    思路

    ST算法

    (RMQ) 问题。ST 算法模板题。预处理时间 (O(nlogn)),查询时间 (O(1))

    Code

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 1010;
    
    int a[maxn];
    int f[maxn][11];
    int n;
    
    void st_prework() {
        for(int i = 1; i <= n; ++i) f[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]);
            }
        }
    }
    
    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]);
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);
        int T;
        cin >> T;
        while(T--) {
            cin >> n;
            for(int i = 1; i <= n; ++i) {
                cin >> a[i];
            }
            st_prework();
            int q;
            cin >> q;
            for(int i = 0; i < q; ++i) {
                int l, r;
                cin >> l >> r;
                cout << st_query(l, r) << endl;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    反射
    关于EwebEeditor 不能上传图片问题解决方法
    js去掉前后空格
    园子开张
    使用Python 爬取 京东 ,淘宝。 商品详情页的数据。(避开了反爬虫机制)
    c#对于加密的一点整合 (AES,RSA,MD5,SHA256)
    移动战略调查:应用开发者首选微软Windows
    Surface Pro打包微软精华 今晚在华开售
    Ceph学习全过程 基于N版
    k8s中部署wordpress
  • 原文地址:https://www.cnblogs.com/wulitaotao/p/11461013.html
Copyright © 2020-2023  润新知