POJ 3264 RMQ水题

题目大意就是有很多牛。告诉你每只牛的高度。然后有很多个询问。输出该区间内的最大身高差。也就是用RMQ求最大值最小值。貌似还可以用线段树。然而,我还不会线段树。。。。。T_T

可能是太多组数据了。cin和cout会TLE。换成scanf和printf就顺当的AC了。。。。啦啦啦、

RMQ还是只会用模板。。T_T

附代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#define maxn 51000
#define maxm 20
using namespace std;

int dpmax[maxn][maxm];
int dpmin[maxn][maxm];
int n, q;

int main()
{
    while(cin >> n >> q)
    {
        for (int i=1; i<=n; ++i)   // 下标从开始。初始化dpmax和dpmin数组。
        {
           // cin >> dpmax[i][0];
            scanf("%d", &dpmax[i][0]);
            dpmin[i][0] = dpmax[i][0];
        }

int end_j = log(n + 0.0) / log(2.0); // 预处理。计算每个区间的最大值和最小值。所以询问非常快。
        for (int j=1; j<=end_j; ++j)
        {
            int end_i = n + 1 - (1 << j);  // dpmax[i][j] 表示从第 i 个数开始。长度为1<<j的区间。包括第i个数。
            for (int i=1; i<=end_i; ++i)
            {
                dpmax[i][j] = max(dpmax[i][j-1], dpmax[i+(1<<(j-1))][j-1]);
                dpmin[i][j] = min(dpmin[i][j-1], dpmin[i+(1<<(j-1))][j-1]);
            }
        }

for (int i=0; i<q; ++i)
        {
            int st, ed;
            cin >> st >> ed;
            int k = log(ed - st + 1.0) / log(2.0);   //需要讨论的最大区间长度。
            int ansmax = max(dpmax[st][k], dpmax[ed-(1<<k)+1][k]);
            int ansmin = min(dpmin[st][k], dpmin[ed-(1<<k)+1][k]);
            //cout << ansmax - ansmin << endl;
            printf("%d\n", ansmax - ansmin);
        }
    }
    return 0;
}

RMQ复习链接:http://blog.csdn.net/liang5630/article/details/7917702

时间: 2024-10-01 07:18:53

POJ 3264 RMQ水题的相关文章

poj 3264 RMQ 水题

题意:找到一段数字里最大值和最小值的差 水题 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 using namespace std; 8 const int maxn=550; 9 const int INF=0x3f3f3f3f; 10 in

POJ 3264 RMQ裸题

POJ 3264 题意:n个数,问a[i]与a[j]间最大值与最小值之差. 总结:看了博客,记下了模板,但有些地方还是不太理解. #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<string> #include<cmath> #include<queue> #

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

POJ 3264 RMQ Spare Table算法

今天下午大帝讲的,我以前也不懂,所以也就跟着学学了,把中间的那个状态转移方程学错了好几次,于是就wa了 好几发. #include<iostream> #include<cstdio> #include<algorithm> #define maxn 200010 using namespace std; int a[maxn],m,n,b[maxn],fl[maxn][50],fr[maxn][50]; void solve() { b[1]=0;//其实就是用来计算

poj 1979 dfs水题

// 练练水题,夯实基础吧 #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib

poj 3264 RMQ

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 44075   Accepted: 20687 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

POJ 1000(水题)

这道题就很水啦,只要看懂题...所以我受了这个影响,再去hoj的时候想都没想就还按照这个打了,结果就像之前那篇说的那样WA. #include <stdio.h> int main() { int a,b; scanf("%d %d",&a, &b); printf("%d\n",a+b); return 0; }

poj 3264(RMQ或者线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 42929   Accepted: 20184 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

POJ 3264 -----RMQ问题

Balanced Lineup Time Limit: 5000 MS Memory Limit: 0 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decide