• HDU 5443 The Water Problem


    The Water Problem

    Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 153    Accepted Submission(s): 123


    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 a1,a2,a3,...,anrepresenting 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 al and ar.
     
    Input
    First you are given an integer T(T10) indicating the number of test cases. For each test case, there is a number n(0n1000) on a line representing the number of water sources. n integers follow, respectively a1,a2,a3,...,an, and each integer is in {1,...,106}. On the next line, there is a number q(0q1000) representing the number of queries. After that, there will be q lines with two integers l and r(1lrn) 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

     区间最值问题 rmq和线段树都可以解决

    /* ***********************************************
    Author        :pk28
    Created Time  :2015/9/13 19:47:01
    File Name     :4.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 1000+10
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    
    bool cmp(int a,int b){
        return a>b;
    }
    int t,n,m;
    int a[1100];
    int dp[maxn][20];
    
    void rmq_init(){
        //cle(dp);
        for(int i=0;i<n;i++)
            dp[i][0]=a[i];
        for(int j=1;(1<<j)<=n;j++)
            for(int i=0;i+(1<<j)-1<n;i++){
                dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
            }
    }
    int rmq(int l,int r){
        int k=0;
        while(1<<(k+1)<=r+1-l)k++;
        return max(dp[l][k],dp[r-(1<<k)+1][k]);
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        //freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        cin>>t;
        while(t--){
            int b;
            scanf("%d",&n);
            for(int i=0;i<n;i++){
                scanf("%d",&a[i]);
            }
            rmq_init();
            int l,r;
            scanf("%d",&m);
            for(int i=1;i<=m;i++){
                scanf("%d %d",&l,&r);
                printf("%d
    ",rmq(l-1,r-1));
            }
        }
        return 0;
    }
  • 相关阅读:
    hmac模块和hashlib模块
    logging模块
    sys模块
    datetime模块
    time模块
    例题:100节楼梯,0-49节,分数等于节数。50节(包括50节)以后每节10分。输入节数,得出分数。这个题如果按照讲页来做是错误的,所以再写一遍,请大家指导
    C# .ToString() 格式化
    例题:判断平年还是闰年。理解使用异常语句try catch finally 和datetime 时间类
    SQL Server第一堂课:创建数据库,创建表,以及表中最基本的增,删,改
    例题:输入学生的各项资料,然后根据学生的分数,重新排序。重新复习结构体,集合,数组,for循环,冒泡排序,水平符的使用。
  • 原文地址:https://www.cnblogs.com/pk28/p/4805483.html
Copyright © 2020-2023  润新知