Poj 2019 Cornfields【二维RMQ-------ST算法】

博客介绍了如何使用二维RMQ和ST算法解决Poj 2019题目的矩阵查询问题。通过预处理矩阵,可以在O(n)时间内查询小矩阵内最大值和最小值的差。

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

Cornfields
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6608 Accepted: 3234

Description

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

Source


题目大意:

给你一个n*n的矩阵,让你从中圈定一个小矩阵,其大小为b*b,有Q个询问,每次询问告诉你小矩阵的左上角,求小矩阵内的最大值和最小值的差。


思路:


1、一维RMQ可以用来求线性区间最大值问题。那么我们不如将一维变成二维,将maxn【】【】变成maxn【i】【j】【len】表示在第i行中,以j为起点,j+len-1区间内的最大值。


2、然后我们求N^2LogN预处理这个问题。然后O(n)来查询区间最大值和最小的值的差即可。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
using namespace std;
int maxn[255][255][15];
int minn[255][255][15];
int n,b,q;
void ST()
{
    int len=floor(log10(double(n))/log10(double(2)));
    for(int k=1;k<=n;k++)
    {
        for(int j=1;j<=len;j++)
        {
            for(int i=1;i<=(n+1)-(1<<j);i++)
            {
                maxn[k][i][j]=max(maxn[k][i][j-1],maxn[k][i+(1<<(j-1))][j-1]);
                minn[k][i][j]=min(minn[k][i][j-1],minn[k][i+(1<<(j-1))][j-1]);
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&n,&b,&q))
    {
        memset(maxn,0,sizeof(maxn));
        memset(minn,0,sizeof(minn));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                int tmp;
                scanf("%d",&tmp);
                maxn[i][j][0]=tmp;
                minn[i][j][0]=tmp;
            }
        }
        ST();
        while(q--)
        {
            int x,y;
            int ma=-0x3f3f3f3f;
            int mi=0x3f3f3f3f;
            scanf("%d%d",&x,&y);
            int len=floor(log10(double(b))/log10(double(2)));
            for(int i=x;i<x+b;i++)
            {
                ma=max(max(maxn[i][y][len],maxn[i][(y+b-1)-(1<<len)+1][len]),ma);
                mi=min(min(minn[i][y][len],minn[i][(y+b-1)-(1<<len)+1][len]),mi);
            }
            //printf("%d %d\n",ma,mi);
            printf("%d\n",ma-mi);
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值