Cornfields
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5959 | Accepted: 2934 |
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
题意:给定一个N*N的矩阵,每次询问以xy为左上角的边长为B的矩阵内,最大值和最小值的差。
思路:如果数据不水的话,最优的做法应该就是单调队列了,复杂度是N^2预处理,对于求最大值,先求出一个矩阵表示每个点到它右边长度为B的1*B的矩阵中的最大值,然后再竖着做一遍,这样就能求出对于每个店,其B*B的矩阵中的最大值。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node
{
int num,pos;
};
int T,t,n,m,len;
int num[260][260],temp[260][260],maxn[260][260],minn[260][260];
node qu[1000010],A;
int main()
{
int i,j,k,head,tail,x,y;
scanf("%d%d%d",&n,&len,&m);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&num[i][j]);
for(i=1;i<=n;i++)
{
head=1;tail=0;
for(j=n;j>=1;j--)
{
A.num=num[i][j];
A.pos=j;
while(head<=tail && qu[tail].num<=A.num)
tail--;
qu[++tail]=A;
while(head<tail && qu[head].pos>=j+len)
head++;
temp[i][j]=qu[head].num;
}
}
for(j=1;j<=n;j++)
{
head=1;tail=0;
for(i=n;i>=1;i--)
{
A.num=temp[i][j];
A.pos=i;
while(head<=tail && qu[tail].num<=A.num)
tail--;
qu[++tail]=A;
while(head<tail && qu[head].pos>=i+len)
head++;
maxn[i][j]=qu[head].num;
}
}
for(i=1;i<=n;i++)
{
head=1;tail=0;
for(j=n;j>=1;j--)
{
A.num=num[i][j];
A.pos=j;
while(head<=tail && qu[tail].num>=A.num)
tail--;
qu[++tail]=A;
while(head<tail && qu[head].pos>=j+len)
head++;
temp[i][j]=qu[head].num;
}
}
for(j=1;j<=n;j++)
{
head=1;tail=0;
for(i=n;i>=1;i--)
{
A.num=temp[i][j];
A.pos=i;
while(head<=tail && qu[tail].num>=A.num)
tail--;
qu[++tail]=A;
while(head<tail && qu[head].pos>=i+len)
head++;
minn[i][j]=qu[head].num;
}
}
while(m--)
{
scanf("%d%d",&x,&y);
printf("%d\n",maxn[x][y]-minn[x][y]);
}
}