理想的正方形

省队选拔赛河南

 时间限制: 1 s
 空间限制: 256000 KB
 题目等级 : 大师 Master
 
 
 
题目描述 Description

有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小。

输入描述 Input Description

第一行为3个整数,分别表示a,b,n的值

第二行至第a+1行每行为b个非负整数,表示矩阵中相应位置上的数。每行相邻两数之间用一空格分隔。

输出描述 Output Description

仅一个整数,为a*b矩阵中所有“n*n正方形区域中的最大整数和最小整数的差值”的最小值。

样例输入 Sample Input

5 4 2

1 2 5 6

0 17 16 0

16 17 2 1

2 10 2 1

1 2 2 2

样例输出 Sample Output

1

数据范围及提示 Data Size & Hint

(1)矩阵中的所有数都不超过1,000,000,000

(2)20%的数据2<=a,b<=100,n<=a,n<=b,n<=10

(3)100%的数据2<=a,b<=1500,n<=a,n<=b,n<=100

/*
嗯,脑补二维RMQ fail,挂成暴力分。
一维是维护一个区间,二维是维护一个矩阵。
由于这个题是小正方形,三维数组即可。二维RMQ略麻烦...
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib> #define x2 x1+n-1-(1<<m)+1
#define y2 y1+n-1-(1<<m)+1 using namespace std;
void read(int &x)
{
int f=;x=;char c=getchar();
while(!isdigit(c)){if(c=='-')f=-;c=getchar();}
while(isdigit(c)){x=(x<<)+(x<<)+c-'';c=getchar();}
x*=f;
} void out(int x)
{
if(!x){putchar('');return;}
if(x<){x=~x+;putchar('-');}
char c[]= {};
while(x)c[++c[]]=x%+,x/=;
while(c[])putchar(c[c[]--]);
}
const int inf=1e3+;
int i,j,k,m;
int g[inf][inf][];
int f[inf][inf][];
int x,y,n;
int ans=0x7fffffff; int query(int x1,int y1)
{
int maxn=,minn=0x7fffffff;
maxn=max(f[x1][y1][m],f[x2][y2][m]);
maxn=max(maxn,f[x1][y2][m]);
maxn=max(maxn,f[x2][y1][m]);
minn=min(g[x1][y1][m],g[x2][y2][m]);
minn=min(minn,g[x1][y2][m]);
minn=min(minn,g[x2][y1][m]);
return maxn-minn;
} int main()
{
read(y);read(x);
read(n);m=log(n)/log();
for(i=; i<=y; i++)
for(j=; j<=x; j++)
{
read(f[j][i][]);
g[j][i][]=f[j][i][];
}
for(k=; k<=; k++)
for(i=; i+(<<k)-<=x; i++)
for(j=; j+(<<k)-<=y; j++)
{
f[i][j][k]=max(f[i][j][k-],f[i+(<<(k-))][j+(<<(k-))][k-]);
f[i][j][k]=max(f[i][j][k],f[i+(<<(k-))][j][k-]);
f[i][j][k]=max(f[i][j][k],f[i][j+(<<(k-))][k-]);
g[i][j][k]=min(g[i][j][k-],g[i+(<<(k-))][j+(<<(k-))][k-]);
g[i][j][k]=min(g[i][j][k],g[i+(<<(k-))][j][k-]);
g[i][j][k]=min(g[i][j][k],g[i][j+(<<(k-))][k-]);
}
for(i=; i+n-<=x; i++)
for(j=; j+n-<=y; j++)
ans=min(ans,query(i,j));
out(ans);
return ;
}

理想的正方形 HAOI2007(二维RMQ)的更多相关文章

  1. 【洛谷2216】[HAOI2007] 理想的正方形(二维RMQ)

    点此看题面 大致题意: 求出一个矩阵中所有\(n*n\)正方形中极差的最小值. 另一种做法 听说这题可以用单调队列去做,但是我写了一个二维\(RMQ\). 二维\(RMQ\) \(RMQ\)相信大家都 ...

  2. P2216 [HAOI2007]理想的正方形(二维RMQ)

    题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入输出格式 输入格式: 第一行为3个整数,分别表示a,b,n的值 第二行至 ...

  3. [HAOI2007]理想的正方形 BZOJ1047 二维RMQ

    题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入输出格式 输入格式: 第一行为3个整数,分别表示a,b,n的值 第二行至 ...

  4. 【洛谷 P2216】 [HAOI2007]理想的正方形(二维ST表)

    题目链接 做出二维\(ST\)表,然后\(O(n^2)\)扫一遍就好了. #include <cstdio> #include <cstring> #include <a ...

  5. [luoguP2216] [HAOI2007]理想的正方形(二维单调队列)

    传送门 1.先弄个单调队列求出每一行的区间为n的最大值最小值. 2.然后再搞个单调队列求1所求出的结果的区间为n的最大值最小值 3.最后扫一遍就行 懒得画图,自己体会吧. ——代码 #include ...

  6. 【bzoj1047】[HAOI2007]理想的正方形 二维RMQ

    题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入 第一行为3个整数,分别表示a,b,n的值第二行至第a+1行每行为b个非 ...

  7. 【LightOJ 1081】Square Queries(二维RMQ降维)

    Little Tommy is playing a game. The game is played on a 2D N x N grid. There is an integer in each c ...

  8. hdu2888 二维RMQ

    Check Corners Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. hduacm 2888 ----二维rmq

    http://acm.hdu.edu.cn/showproblem.php?pid=2888 模板题  直接用二维rmq 读入数据时比较坑爹  cin 会超时 #include <cstdio& ...

随机推荐

  1. keil mdk uvision使用技巧

    语法检测&代码提示 中文友好: tab 可以选中一大块代码,一起缩进 快速注释 先选中你要注释的代码区,然后右键,选择Advanced,Comment Selection 就可以了 查找替换 ...

  2. BNUOJ 3278 Candies

    Candies Time Limit: 1500ms Memory Limit: 131072KB This problem will be judged on PKU. Original ID: 3 ...

  3. Codeforces Round #304 (Div. 2)-D. Soldier and Number Game,素因子打表,超时哭晕~~

    D. Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  4. [luoguP1042] 乒乓球(模拟)

    传送门 终于过了这sb题了. 当初我连这道题都A不了(╯▔皿▔)╯ 代码 #include <cstdio> #include <iostream> #define N 100 ...

  5. 【天道酬勤】 腾讯、百度、网易游戏、华为Offer及笔经面经(转)

    应届生上泡了两年,一直都是下资料,下笔试题,面试题.一直都在感谢那些默默付出的人.写这个帖子花了我两 个夜晚的时间,不是为了炫耀,只是为了能给那些“迷惘”的学弟学妹,一点点建议而已.大家何必那么认真, ...

  6. Minimum Depth of Binary Tree(二叉树DFS)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  7. lambda简单记录

    lambda表达式对集合的一些操作,持续记录一下新的用法 List<Integer> list = new ArrayList<>(); list.add(1); list.a ...

  8. JSP的过滤器

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/writing-filters.html: Servlet和JSP过滤器都是Java类,可以在Servle ...

  9. [python]pycharm画图插件matplotlib、numpy、scipy的下载与安装

    最近在用pycharm学习python语言,不得不感叹python语言的强大与人性化! 但对于使用pycharm画图(较复杂的图)就要用到几个插件了,即matplotlib.numpy和scipy!但 ...

  10. 经常使用的android设计模式

    一般来说,经常使用的android设计模式有下面8种:单例.工厂.观察者.代理.命令.适配器.合成.訪问者.   单例模式:目的是为了让系统中仅仅有一个调用对象,缺点是单例使其它程序过分依赖它,并且不 ...