poj2019 Cornfields

13 篇文章 0 订阅

Cornfields
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 7110 Accepted: 3465

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


可以用二维RMQ,也可以直接暴力,暴力有点小思维,因为直接暴力,无论初始记录每个点对应的解还是查询时暴力都会t,其实只需要对于每个点,记录他此行b长度内的最值,然后查询时O(b)遍历一遍列就好了

#include<stdio.h>
int main()
{
	int n,a[300][300],maxa[300][300],mina[300][300],b,t,k,i,j,x,y,max,min;
	scanf("%d %d %d",&n,&b,&t);
	for(i=0;i<n;i++)
	for(j=0;j<n;j++)
	scanf("%d",&a[i][j]);
	
	for(k=0;k<n;k++)
	for(i=0;i<n;i++)
	{	
		max=min=a[k][i];
		for(j=i;j<i+b&&j<n;j++)
		{
			if(a[k][j]>max)
			max=a[k][j];
			if(a[k][j]<min)
			min=a[k][j];
		}
		maxa[k][i]=max;
		mina[k][i]=min;
	}
	while(t--)
	{
		scanf("%d %d",&x,&y);
		x--,y--;
		max=min=a[x][y];
		for(i=x;i<x+b;i++)
		{
			if(maxa[i][y]>max)
			max=maxa[i][y];
			if(mina[i][y]<min)
			min=mina[i][y];
		}
		printf("%d\n",max-min);
	}
	return 0;
 } 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值