HDU 5443 The Water Problem (水题,暴力)

题意:给定 n 个数,然后有 q 个询问,问你每个区间的最大值。

析:数据很小,直接暴力即可,不会超时,也可以用RMQ算法。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn];
int main(){
    int T;
    cin >> T;
    while(T--){
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i)  scanf("%d", &a[i]);
        scanf("%d", &m);
        while(m--){
            int l, r;
            scanf("%d %d", &l, &r);
            int ans = -1;
            for(int i = l; i <= r; ++i)
                ans = max(ans, a[i]);
            printf("%d\n", ans);
        }
    }
    return 0;
}

  

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int d[maxn][20];

void rmq_init(){
    for(int j = 1; (1<<j) <= n; ++j)
        for(int i = 1; i + (1<<j) - 1 <= n; ++i)
            d[i][j] = max(d[i][j-1], d[i+(1<<(j-1))][j-1]);
}

int rmq_query(int l, int r){
    //int k = log(r-l+1)/log(2.0);
    int k = 0;
    while((1<<(k+1)) <= r-l+1)  ++k;
    return max(d[l][k], d[r-(1<<k)+1][k]);
}

int main(){
    int T;  cin >> T;
    while(T--){
        scanf("%d", &n);
        memset(d, 0, sizeof(d));
        for(int i = 0; i < n; ++i)
            scanf("%d", &d[i+1][0]);
        rmq_init();
        scanf("%d", &m);
        while(m--){
            int l, r;
            scanf("%d %d", &l, &r);
            printf("%d\n", rmq_query(l, r));
        }
    }
    return 0;
}
时间: 2024-08-10 09:39:31

HDU 5443 The Water Problem (水题,暴力)的相关文章

hdu 5443 The Water Problem(长春网络赛——暴力)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1084    Accepted Submission(s): 863 Problem Description In Land waterless

hdu 5443 The Water Problem 线段树

The Water Problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5443 Description In Land waterless, water is a very limited resource. People always fight for the biggest source of water. Given a sequence of wat

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 figh

ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)

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,...,an representing the size of the water source. Given a set of queries eac

HDU 6182 A Math Problem 水题

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6182 题目描述: 输入N, 输出满足k^k <= N 的 k的个数 解题思路: 水水更健康 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #include <cstring> #include <iterator> #in

HDU 5832 A water problem(某水题)

HDU 5832 A water problem(某水题) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Two planets named Haha and Xixi in the universe and they were created with the universe beginning. There is

hdu 1999 不可摸数 水题。

不可摸数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7966    Accepted Submission(s): 2024 Problem Description s(n)是正整数n的真因子之和,即小于n且整除n的因子和.例如s(12)=1+2+3+4+6=16.如果任何数m,s(m)都不等于n,则称n为不可摸数. Input 包

sdut 2841 Bit Problem (水题)

题目 贴这个题是因为看题解有更简单的方法, 我做的时候是直接算的, 也很简单. 贴一下题解吧: 如果一个整数不等于 0,那么该整数的二进制表示中至少有一位是 1. 这个题结果可以直接输出 x - (x&(x-1)); 因为x-1 之后二进制下,就是最右边的1变成了0, 最右边的1的 右边所有的0变成了1, 不影响最左边. 我的代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4

HDU Senior&#39;s Gun (水题)

题意:给n把枪,m个怪兽,每把枪可消灭1怪兽,并获得能量=枪的攻击力-怪兽的防御力.求如何射杀能获得最多能量?(不必杀光) 思路:用最大攻击力的枪杀防御力最小的怪兽明显可获得最大能量.如果每把枪都去射杀刚好1点能量都拿不到的怪物,那简直等于把枪全丢掉. 1 //#pragma comment(linker,"/STACK:102400000,102400000") 2 #include <iostream> 3 #include <stdio.h> 4 #inc