1. 程式人生 > >[HAOI2007]理想的正方形 BZOJ1047 二維RMQ

[HAOI2007]理想的正方形 BZOJ1047 二維RMQ

tdi deque 一行 brush tor 行為 har pre 空格

題目描述

有一個a*b的整數組成的矩陣,現請你從中找出一個n*n的正方形區域,使得該區域所有數中的最大值和最小值的差最小。

輸入輸出格式

輸入格式:

第一行為3個整數,分別表示a,b,n的值

第二行至第a+1行每行為b個非負整數,表示矩陣中相應位置上的數。每行相鄰兩數之間用一空格分隔。

輸出格式:

僅一個整數,為a*b矩陣中所有“n*n正方形區域中的最大整數和最小整數的差值”的最小值。

輸入輸出樣例

輸入樣例#1: 復制
5 4 2
1 2 5 6
0 17 16 0
16 17 2 1
2 10 2 1
1 2 2 2
輸出樣例#1: 復制
1

說明

問題規模

(1)矩陣中的所有數都不超過1,000,000,000

(2)20%的數據2<=a,b<=100,n<=a,n<=b,n<=10

(3)100%的數據2<=a,b<=1000,n<=a,n<=b,n<=100

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 700005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == ‘-‘) f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}


ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }



/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/
int a, b, n;
int Log;
int maxx[1103][1103];
int minn[1103][1103];
int mx[1101][1101];

int query(int x, int y) {
	int Max = -inf, Min = inf;
	Max = max(maxx[x][y], max(maxx[x + n - (1 << Log)][y + n - (1 << Log)], max(maxx[x + n - (1 << Log)][y], maxx[x][y + n - (1 << Log)])));
	Min = min(minn[x][y], min(minn[x + n - (1 << Log)][y + n - (1 << Log)], min(minn[x + n - (1 << Log)][y], minn[x][y + n - (1 << Log)])));
	return Max - Min;
}

int main()
{
	//	ios::sync_with_stdio(0);
	a = rd(); b = rd(); n = rd();
	for (int i = 1; i <= a; i++) {
		for (int j = 1; j <= b; j++) {
			mx[i][j] = rd();
			maxx[i][j] = minn[i][j] = mx[i][j];
		}
	}
	for (Log = 0; (1 << (Log + 1) <= n); Log++);
	for (int k = 0; k < Log; k++) {
		for (int i = 1; i + (1 << k) <= a; i++) {
			for (int j = 1; j + (1 << k) <= b; j++) {
				maxx[i][j] = max(maxx[i][j], max(maxx[i + (1 << (k))][j + (1 << (k))], max(maxx[i][j + (1 << k)], maxx[i + (1 << k)][j])));
				minn[i][j] = min(minn[i][j], min(minn[i + (1 << k)][j + (1 << k)], min(minn[i + (1 << k)][j], minn[i][j + (1 << k)])));
			}
		}
	}
	ll ans = 9999999999;
	for (int i = 1; i <= a - n + 1; i++) {
		for (int j = 1; j <= b - n + 1; j++) {
			ans = min(ans, 1ll * query(i, j));
		}
	}
	printf("%d\n", ans);
	return 0;
}

[HAOI2007]理想的正方形 BZOJ1047 二維RMQ