【Description】
有一个AXB的数字矩阵,从中找出一个NXN的正方形区域使得该区域中最大值与最小值的差值最小
【Data Range】
矩阵中所有数字 <= 1000,000,000
20% 2 <= a , b <= 100, n <= a, n <= b, n <= 10
100% 2 <= a , b <= 1000, n <= a, n <= b, n <= 100
【Analysis】
因为需要知道每N个格子最大值与最小值
所以第一想法是建1000颗线段树,然后分析下复杂度发现竟然可以过!
然后就啪啪啪地打完、发现其实用单调队列随便搞一下就可以过= =
【Code】
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <climits>
#include <cstring>
#include <utility>
#include <vector>
#include <string>
#include <cstdio>
#include <bitset>
#include <ctime>
#include <cmath>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;
#define sci stack <int>
#define vci vector <int>
#define vcs vector <string>
#define vcd vector <double>
#define vci64 vector <long long>
#define seti set <int>
#define mseti multiset <int>
const int maxn = 1000 + 5;
const int maxm = 1000 + 5;
typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
template <class T> inline T Sqr(const T & x) { return x * x; }
template <class T> inline T Abs(const T & x) { return x > 0 ? x : -x; }
template <class T> inline T Min(const T & a, const T & b) { return a < b ? a : b; }
template <class T> inline T Max(const T & a, const T & b) { return a > b ? a : b; }
template <class T> inline T Ksm(const T & a, const T & b, const T & m) { T _ = 1; for (; b; b >>= 1, a = a * a % m) (b & 1) ? _ = _ * a % m : 0; return _ % m; }
template <class T> inline void Swap(T & a, T & b) { T _; _ = a; a = b; b = _; }
struct node { int data, wh; };
int a, b, n, h, ans = INT_MAX;
int mat[maxn][maxn], mit[maxn][maxn];
int getint()
{
char ch = getchar(); int result = 0, res = 1;
for (; '0' > ch || ch > '9'; ch = getchar()) ch == '-' ? res = -1 : 0;
for (; '0' <= ch && ch <= '9'; result = result * 10 + ch - '0', ch = getchar());
return result * res;
}
int bit[20], p;
void putint(int64 ans)//正数
{
if (bit[0] = p = 0, ans == 0) ++p;
else for (; ans; bit[p++] = ans % 10, ans /= 10);
for (--p; p >= 0; --p) putchar('0' + bit[p]);
putchar('\n');
}
bool cmp_(node a, node b) { return (a.data == b.data) ? a.wh < b.wh : a.data < b.data; }
bool _cmp(node a, node b) { return (a.data == b.data) ? a.wh < b.wh : a.data > b.data; }
void prepare()
{
node q[maxn], p[maxn];
int qhead = 1, qtail = n, phead = 1, ptail = n;
for (int i = 1; i <= n; ++i) q[i] = p[i] = (node) { getint(), i };
sort(q + 1, q + n + 1, cmp_), sort(p + 1, p + n + 1, _cmp);
mit[h][1] = q[1].data, mat[h][1] = p[1].data;
for (int i = 2; i <= b - n + 1; ++i)
{
int x = getint();
for (; q[qhead].wh < i; ++qhead);
for (; qhead <= qtail && q[qtail].data > x; --qtail);
q[++qtail] = (node) { x, i + n - 1 };
mit[h][i] = q[qhead].data;
for (; p[phead].wh < i; ++phead);
for (; phead <= ptail && p[ptail].data < x; --ptail);
p[++ptail] = (node) { x, i + n - 1 };
mat[h][i] = p[phead].data;
}
}
void work()
{
node q[maxn], p[maxn];
int qhead = 1, qtail = n, phead = 1, ptail = n;
for (int i = 1; i <= n; ++i) q[i] = (node) { mit[i][h], i };
for (int i = 1; i <= n; ++i) p[i] = (node) { mat[i][h], i };
sort(q + 1, q + n + 1, cmp_), sort(p + 1, p + n + 1, _cmp);
ans = Min(ans, p[1].data - q[1].data);
for (int i = 2; i <= a - n + 1; ++i)
{
int minx = mit[i + n - 1][h], maxx = mat[i + n - 1][h];
for (; q[qhead].wh < i; ++qhead);
for (; qhead <= qtail && q[qtail].data > minx; --qtail);
q[++qtail] = (node) { minx, i + n - 1 };
for (; p[phead].wh < i; ++phead);
for (; phead <= ptail && p[ptail].data < maxx; --ptail);
p[++ptail] = (node) { maxx, i + n - 1 };
ans = Min(ans, p[phead].data - q[qhead].data);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1047.in", "r", stdin);
freopen("1047.out", "w", stdout);
#endif
a = getint(), b = getint(), n = getint();
for (int i = 1; i <= a; ++i) h = i, prepare();
for (int i = 1; i <= b - n + 1; ++i) h = i, work();
putint(ans);
return 0;
}
单调队列很好写的赶脚。。。