POJ - 2019(RMQ二维)

本文深入探讨了二维RMQ(Range Maximum Query)算法的应用场景,特别是在处理大规模数据集中的最大值和最小值查询问题上。通过一个具体的问题背景——农场主FJ寻找最平坦土地种植玉米,介绍了如何使用二维RMQ算法高效解决查询问题。文章详细解释了算法的实现过程,包括数据预处理和查询操作,并提供了一段C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FJ has decided to grow his own corn hybrid in order to help the cows make the best possible milk. To that end, he's looking to build the cornfield on the flattest piece of land he can find. 

FJ has, at great expense, surveyed his square farm of N x N hectares (1 <= N <= 250). Each hectare has an integer elevation (0 <= elevation <= 250) associated with it. 

FJ will present your program with the elevations and a set of K (1 <= K <= 100,000) queries of the form "in this B x B submatrix, what is the maximum and minimum elevation?". The integer B (1 <= B <= N) is the size of one edge of the square cornfield and is a constant for every inquiry. Help FJ find the best place to put his cornfield. 

Input

* Line 1: Three space-separated integers: N, B, and K. 

* Lines 2..N+1: Each line contains N space-separated integers. Line 2 represents row 1; line 3 represents row 2, etc. The first integer on each line represents column 1; the second integer represents column 2; etc. 

* Lines N+2..N+K+1: Each line contains two space-separated integers representing a query. The first integer is the top row of the query; the second integer is the left column of the query. The integers are in the range 1..N-B+1. 

Output

* Lines 1..K: A single integer per line representing the difference between the max and the min in each query. 

Sample Input

5 3 1
5 1 2 6 3
1 3 5 2 7
7 2 4 6 1
9 9 8 6 5
0 6 9 3 9
1 2

Sample Output

5

思路很简单,就直接二维RMQ,dp[k][j][i]第k行的rmq区间维护。

我竟然wa了8次。。。

死都没看到求mi时dpi【】【】【】少了个i。。。。。。。

好不容易碰到自己想出来的题。。wa在这种弱智的地方。。醉了。。。。

加油。。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f3f
int dp[255][255][32];
int dpi[255][255][32];
int a[255][255];
void RMQ(int n)
{
    for(int k=1;k<=n;k++)
    for(int i=1;i<=n;i++)
        dpi[k][i][0]=dp[k][i][0]=a[k][i];
    for(int k=1;k<=n;k++)
    for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i+(1<<j)-1<=n;i++)
        {
            dp[k][i][j]=max(dp[k][i][j-1],dp[k][i+(1<<j-1)][j-1]);
            dpi[k][i][j]=min(dpi[k][i][j-1],dpi[k][i+(1<<j-1)][j-1]);
        }
}
int main()
{
    int n,b,k,x,y;
    while(~scanf("%d%d%d",&n,&b,&k))
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                scanf("%d",&a[i][j]);
        int len=0;
        while(1<<len+1<=b)
                len++;
        RMQ(n);
        while(k--)
        {
            scanf("%d%d",&x,&y);
            int mi,ma;
            mi=inf;
            ma=-inf;
        // cout<<ma<<"++"<<mi<<"--"<<endl;
            for(int i=x;i<x+b/*&&i<=n*/;i++)
            {
       //     if(f>n)f=n;
                ma=max(max(dp[i][y][len],dp[i][(y+b-1)-(1<<len)+1][len]),ma);
                mi=min(min(dpi[i][y][len],dpi[i][(y+b-1)-(1<<len)+1][len]),mi);
            }
      //  cout<<ma<<"++"<<mi<<endl;*/
            cout<<ma-mi<<endl;
        }
    }
    return 0;
}

人一我百,人百我万.夕林山寸,寻梦指尖!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值