Cornfields
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3842 | Accepted: 1823 |
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
分析:这题很明显是二维RMQ问题,细看会发现他的区间大小是固定的,所以能用两次优先队列来搞。。。不过我还是用了ST算法,有点牛刀小用的感觉。。。
代码:
#include<cstdio>
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define N 255
int s[N][N];
int f[N][8][N][8],g[N][8][N][8];
int log_2(int n)
{
int ret=0;
while((1<<(ret+1))<n)++ret;
return ret;
}
void ST(int n)
{
int i,j,u,v;
int logn=log_2(n);
for(j=0; j<=logn; ++j)
for(v=0; v<=logn; ++v)
for(i=0; i+(1<<j)-1<n; ++i)
for(u=0; u+(1<<v)-1<n; ++u)
if(j==0&&v==0)f[i][j][u][v]=s[i][u],g[i][j][u][v]=s[i][u];
else if(v==0)
{
f[i][j][u][v]=min(f[i][j-1][u][v],f[i+(1<<(j-1))][j-1][u][v]);
g[i][j][u][v]=max(g[i][j-1][u][v],g[i+(1<<(j-1))][j-1][u][v]);
}
else
{
f[i][j][u][v]=min(f[i][j][u][v-1],f[i][j][u+(1<<(v-1))][v-1]);
g[i][j][u][v]=max(g[i][j][u][v-1],g[i][j][u+(1<<(v-1))][v-1]);
}
}
int get(int i,int j,int u,int v)
{
int k=log_2(u-i+1);
int t=log_2(v-j+1);
int a=min(f[i][k][j][t],f[i][k][v-(1<<t)+1][t]);
int b=min(f[u-(1<<k)+1][k][j][t],f[u-(1<<k)+1][k][v-(1<<t)+1][t]);
int c=max(g[i][k][j][t],g[i][k][v-(1<<t)+1][t]);
int d=max(g[u-(1<<k)+1][k][j][t],g[u-(1<<k)+1][k][v-(1<<t)+1][t]);
return max(c,d)-min(a,b);
}
void in(int &a)
{
char c,f;
while(((f=getchar())<'0'||f>'9')&&f!='-');
c=(f=='-')?getchar():f;
for(a=0; c>='0'&&c<='9'; c=getchar())a=a*10+c-'0';
if(f=='-')a=-a;
}
void out(int x)
{
if(x>9)out(x/10);
putchar(x%10+48);
}
int main()
{
int i,j,n,b,q,x,y;
in(n),in(b),in(q);
for(i=0; i<n; ++i)
for(j=0; j<n; ++j)in(s[i][j]);
ST(n);
while(q--)
{
in(x),in(y);
out(get(x-1,y-1,x+b-2,y+b-2)),puts("");
}
return 0;
}