[HAOI2007]理想的正方形 BZOJ1047 二维RMQ
题目描述
有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小。
输入输出格式
输入格式:第一行为3个整数,分别表示a,b,n的值
第二行至第a+1行每行为b个非负整数,表示矩阵中相应位置上的数。每行相邻两数之间用一空格分隔。
输出格式:仅一个整数,为a*b矩阵中所有“n*n正方形区域中的最大整数和最小整数的差值”的最小值。
输入输出样例
说明
问题规模
(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; }
EPFL - Fighting