【BZOJ1047 || HAOI2007】理想的正方形

【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;
}

单调队列很好写的赶脚。。。


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值