http://www.lydsy.com/JudgeOnline/problem.php?id=1047

先用单调队列求出每横着n个最大值

再在里面用单调队列求出每竖着n个的最大值

这样一个位置就代表了一个n*n矩阵的最大值

同理求出最小值

#include<cstdio>
#include<iostream>
#include<algorithm> using namespace std; #define N 1001 int num[N][N]; int mx1[N][N],mx2[N][N];
int mi1[N][N],mi2[N][N]; int q[N],pos[N],h,t; void read(int &x)
{
x=; int f=; char c=getchar();
while(!isdigit(c)) { if(c=='-') f=-; c=getchar(); }
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
x*=f;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("data.in","r",stdin);
freopen("xxy.out","w",stdout);
#endif
int a,b,n;
read(a); read(b); read(n);
for(int i=;i<=a;++i)
for(int j=;j<=b;++j)
read(num[i][j]);
for(int i=;i<=a;++i)
{
h=t=;
for(int j=;j<=b;++j)
{
while(h<t && j-pos[h]+>n) h++;
while(h<t && num[i][j]>q[t-]) t--;
q[t]=num[i][j];
pos[t++]=j;
if(j>=n) mx1[i][j]=q[h];
}
}
for(int j=n;j<=b;++j)
{
h=t=;
for(int i=;i<=a;++i)
{
while(h<t && i-pos[h]+>n) h++;
while(h<t && mx1[i][j]>q[t-]) t--;
q[t]=mx1[i][j];
pos[t++]=i;
if(i>=n) mx2[i][j]=q[h];
}
}
for(int i=;i<=a;++i)
{
h=t=;
for(int j=;j<=b;++j)
{
while(h<t && j-pos[h]+>n) h++;
while(h<t && num[i][j]<q[t-]) t--;
q[t]=num[i][j];
pos[t++]=j;
if(j>=n) mi1[i][j]=q[h];
}
}
for(int j=n;j<=b;++j)
{
h=t=;
for(int i=;i<=a;++i)
{
while(h<t && i-pos[h]+>n) h++;
while(h<t && mi1[i][j]<q[t-]) t--;
q[t]=mi1[i][j];
pos[t++]=i;
if(i>=n) mi2[i][j]=q[h];
}
}
int ans=2e9;
for(int i=n;i<=a;++i)
for(int j=n;j<=b;++j)
ans=min(ans,mx2[i][j]-mi2[i][j]);
printf("%d",ans);
}

AC

#include<cstdio>
#include<iostream>
#include<algorithm> using namespace std; #define N 1001 int num[N][N]; int mx[N][N];
int mi[N][N]; int q[N],pos[N],h,t; void read(int &x)
{
x=; int f=; char c=getchar();
while(!isdigit(c)) { if(c=='-') f=-; c=getchar(); }
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
x*=f;
} int main()
{
freopen("data.in","r",stdin);
freopen("std.out","w",stdout);
int a,b,n;
read(a); read(b); read(n);
for(int i=;i<=a;++i)
for(int j=;j<=b;++j)
read(num[i][j]);
int ans=1e9;
for(int i=;i+n-<=a;++i)
for(int j=;j+n-<=b;++j)
{
int I=i+n-;
int J=j+n-;
int p=1e9,q=-1e9;
for(int k=i;k<=I;++k)
for(int l=j;l<=J;++l)
p=min(p,num[k][l]),q=max(q,num[k][l]);
ans=min(ans,q-p);
}
printf("%d",ans);
}

force

#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<ctime> using namespace std; int main()
{
freopen("data.in","w",stdout);
srand(time()+);
int a=,b=;
int n=rand()%+;
printf("%d %d %d\n",a,b,n);
for(int i=;i<=a;++i)
{
for(int j=;j<=b;++j) printf("%d ",rand());
printf("\n");
}
}

maker

1047: [HAOI2007]理想的正方形

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 3791  Solved: 2095
[Submit][Status][Discuss]

Description

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

Input

  第一行为3个整数,分别表示a,b,n的值第二行至第a+1行每行为b个非负整数,表示矩阵中相应位置上的数。每
行相邻两数之间用一空格分隔。
100%的数据2<=a,b<=1000,n<=a,n<=b,n<=1000

Output

  仅一个整数,为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

bzoj千题计划215:bzoj1047: [HAOI2007]理想的正方形的更多相关文章

  1. BZOJ1047: [HAOI2007]理想的正方形 [单调队列]

    1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2857  Solved: 1560[Submit][St ...

  2. bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块

    http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...

  3. [bzoj1047][HAOI2007]理想的正方形_动态规划_单调队列

    理想的正方形 bzoj-1047 HAOI-2007 题目大意:有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 注释:$2\le a, ...

  4. bzoj千题计划186:bzoj1048: [HAOI2007]分割矩阵

    http://www.lydsy.com/JudgeOnline/problem.php?id=1048 #include<cmath> #include<cstdio> #i ...

  5. bzoj千题计划296:bzoj1053: [HAOI2007]反素数ant

    http://www.lydsy.com/JudgeOnline/problem.php?id=1053 求n以内约数个数最多的数 #include<cstdio> using names ...

  6. bzoj千题计划196:bzoj4826: [Hnoi2017]影魔

    http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...

  7. bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪

    http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...

  8. bzoj千题计划177:bzoj1858: [Scoi2010]序列操作

    http://www.lydsy.com/JudgeOnline/problem.php?id=1858 2018 自己写的第1题,一遍过 ^_^ 元旦快乐 #include<cstdio> ...

  9. bzoj千题计划317:bzoj4650: [Noi2016]优秀的拆分(后缀数组+差分)

    https://www.lydsy.com/JudgeOnline/problem.php?id=4650 如果能够预处理出 suf[i] 以i结尾的形式为AA的子串个数 pre[i] 以i开头的形式 ...

随机推荐

  1. 一、Django前后端交互之Ajax和跨域问题

    一.Ajax介绍 1.概述 AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术.AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Jav ...

  2. unity2D以最小的角度旋转到目标方向(y方向为角色的主方向)

    一.使用向量原理转换到目标方向 为了让角色的自身y转向目标方向,并且以最小角度旋转,要点是获得当前方向与目标方向的叉值,从而判断应该旋转的方向 float rotateSpeed; //相对目标位置运 ...

  3. 基于tensorflow使用全连接层函数实现多层神经网络并保存和读取模型

    使用之前那个格式写法到后面层数多的话会很乱,所以编写了一个函数创建层,这样看起来可读性高点也更方便整理后期修改维护 #全连接层函数 def fcn_layer( inputs, #输入数据 input ...

  4. PowerDesigner数据库设计实用技巧

    欢迎大家补充,谢谢! 1. 原始单据与实体之间的关系 可以是一对一.一对多.多对多的关系.在一般情况下,它们是一对一的关系:即一张原始单据对应且只对应一个实体.在特殊情况下,它们可能是一对多或多对一的 ...

  5. PAT甲题题解-1015. Reversible Primes (20)-素数

    先判断n是否为素数然后把n转化成d进制下再反转,转化为十进制的num判断num是否为素数 注意n为0和1时,不是素数!!!注意反转后的num也有可能为1,不是素数!!! #include <io ...

  6. PAT甲题题解-1081. Rational Sum (20)-模拟分数计算

    模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...

  7. Scrum立会报告+燃尽图(Final阶段第七次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2486 项目地址:https://coding.net/u/wuyy694 ...

  8. 20135202闫佳歆--week4 两种方式使用同一个系统调用--实验及总结

    实验四 使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 在这里我选择的是第20号系统调用,getpid. 1.使用库函数API: 代码如下: /* getpid.c */ #incl ...

  9. Docker打DB2 9.7镜像采坑相关

    概况:以centos:7.2.1511镜像为基础镜像,使用docker commit方式进行构建   步骤: 运行centos7.2.1511镜像(以特权模式运行,后续内核参数修改必需参数) dock ...

  10. react & monaco editor & vs code

    react & monaco editor & vs code monaco-editor https://microsoft.github.io/monaco-editor/inde ...