zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    贷款计算公式
    P2P行业专业术语(最全)
    p2p投资理财入门篇(新手必备)
    2015年p2p网络借贷平台的发展现状
    MyEclipse中SVN的常见的使用方法
    linux下打开、关闭tomcat,实时查看tomcat运行日志
    Spring 向页面传值以及接受页面传过来的参数的方式
    Spring自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法的实例
    PowerDesigner概述(系统分析与建模)以及如何用PowerDesigner快速的创建出这个数据库
    MySQL 8.x 函数和操作符,官方网址:https://dev.mysql.com/doc/refman/8.0/en/functions.html
  • 原文地址:https://www.cnblogs.com/pk28/p/4805483.html
Copyright © 2011-2022 走看看