• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
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;
}

 

posted on 2016-07-30 23:48  dwtfukgv  阅读(324)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3