# POJ2019Cornfields 题解

Cornfields
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6364 Accepted: 3131
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

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

5

#include<stdio.h>
int n,b,k,i,j,x,y,max,min,a[300][300],f[300][300];
int main(){
scanf("%d%d%d",&n,&b,&k);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
while(k--){
scanf("%d%d",&x,&y);
if(!f[x][y]){
max=0;
min=10000000;
for(i=1;i<=b;i++)
for(j=1;j<=b;j++){
if(a[i+x-1][j+y-1]<min)min=a[i+x-1][j+y-1];
if(a[i+x-1][j+y-1]>max)max=a[i+x-1][j+y-1];
}
f[x][y]=max-min;//取最大最小值，用f数组存起来
}
printf("%d\n",f[x][y]);//输出处理后的f数组
}
return 0;
}

• 本文已收录于以下专栏：

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

Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6608   Acc...

## POJ 2019 Cornfields(简单二维RMQ)

POJ2019 Cornfields(简单二维RMQ) 分析:基本的二维RMQ查询. 类似于二维树状数组问题,二维RMQ问题就是求一个矩阵N*M中的一个小块矩阵内的最值问题.其中dmin[i][j][...

## poj 2019 Cornfields（二维RMQ）

Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5310   Acc...

## POJ 2019 Cornfields（我的水题之路——多case？？TLE？？）

Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4034   Acc...

## POJ 2019 Cornfields（二维RMQ）

Cornfields Time Limit:1000MS    Memory Limit:30000KB    64bit IO Format:%I64d & %I64u Desc...

## POJ--2019--Cornfields【二维RMQ】

#include #include #include #include #include #include #include #include #include #include #include #...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)