您的位置:首页 > 其它

【POJ】【2019】

2015-07-23 20:27 393 查看
Cornfields

Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 5805Accepted: 2868
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

USACO 2003 March Green

#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <assert.h>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;

int N,B,K;
const int maxn = 250*250+10;
int arr[maxn];
int LOG[maxn];
int dp1[maxn][20];
int dp2[maxn][20];

void initRMQ(int n)
{
	LOG[0] = -1;
	for(int i=1;i<=n;i++)
	{
		LOG[i] = ((i & (i-1) ) == 0) ? LOG[i-1] + 1 : LOG[i-1];
	}
	memset(dp1,0,sizeof(dp1));
	memset(dp2,0,sizeof(dp2));
	for(int i=1;i<=n;i++)	
	{
		dp1[i][0] = dp2[i][0] = arr[i];
	}
	for(int j=1;j<=LOG
;j++)
	{
		for(int i=1;i + (1<<j) -1 <=n;i++)
		{
			dp1[i][j] = max(dp1[i][j-1],dp1[i+(1 << (j-1))][j-1]);
			dp2[i][j] = min(dp2[i][j-1],dp2[i+(1 << (j-1))][j-1]);
		}	
	}
}

int calcmin(int l,int r)
{
	int k = LOG[r - l + 1];
	int mins = min(dp2[l][k],dp2[r - (1 << k) + 1][k]);
	return mins;
}
int calcmax(int l,int r)
{
	int k = LOG[r - l + 1];
	int maxs = max(dp1[l][k],dp1[r - (1 << k) + 1][k]);
	return maxs;
}
int main()
{
	while(scanf("%d%d%d",&N,&B,&K) != EOF)
	{
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<N;j++)
			{
				scanf("%d",&arr[i*N+j+1]);
			}

		}
		initRMQ(N*N);
	
		int ans = 0;
		while(K --)
		{
			int x,y;
			ans = 0;
			int mins = 0x3f3f3f3f;
			int maxs = -1;
			scanf("%d%d",&x,&y);
			x --; 
			y --;
			for(int i=0;i<B;i++)
			{
				int l = (x + i) * N + y + 1;
				int tempmin = calcmin(l,l+B-1);
				if(tempmin < mins) mins = tempmin;
				int tempmax = calcmax(l,l+B-1);
				if(tempmax > maxs) maxs = tempmax;
			}
			printf("%d\n",maxs-mins);
		}

	}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: