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.
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.
* 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);
}
}
}