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

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

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

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

同理求出最小值

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4.  
  5. using namespace std;
  6.  
  7. #define N 1001
  8.  
  9. int num[N][N];
  10.  
  11. int mx1[N][N],mx2[N][N];
  12. int mi1[N][N],mi2[N][N];
  13.  
  14. int q[N],pos[N],h,t;
  15.  
  16. void read(int &x)
  17. {
  18. x=; int f=; char c=getchar();
  19. while(!isdigit(c)) { if(c=='-') f=-; c=getchar(); }
  20. while(isdigit(c)) { x=x*+c-''; c=getchar(); }
  21. x*=f;
  22. }
  23.  
  24. int main()
  25. {
  26. #ifndef ONLINE_JUDGE
  27. freopen("data.in","r",stdin);
  28. freopen("xxy.out","w",stdout);
  29. #endif
  30. int a,b,n;
  31. read(a); read(b); read(n);
  32. for(int i=;i<=a;++i)
  33. for(int j=;j<=b;++j)
  34. read(num[i][j]);
  35. for(int i=;i<=a;++i)
  36. {
  37. h=t=;
  38. for(int j=;j<=b;++j)
  39. {
  40. while(h<t && j-pos[h]+>n) h++;
  41. while(h<t && num[i][j]>q[t-]) t--;
  42. q[t]=num[i][j];
  43. pos[t++]=j;
  44. if(j>=n) mx1[i][j]=q[h];
  45. }
  46. }
  47. for(int j=n;j<=b;++j)
  48. {
  49. h=t=;
  50. for(int i=;i<=a;++i)
  51. {
  52. while(h<t && i-pos[h]+>n) h++;
  53. while(h<t && mx1[i][j]>q[t-]) t--;
  54. q[t]=mx1[i][j];
  55. pos[t++]=i;
  56. if(i>=n) mx2[i][j]=q[h];
  57. }
  58. }
  59. for(int i=;i<=a;++i)
  60. {
  61. h=t=;
  62. for(int j=;j<=b;++j)
  63. {
  64. while(h<t && j-pos[h]+>n) h++;
  65. while(h<t && num[i][j]<q[t-]) t--;
  66. q[t]=num[i][j];
  67. pos[t++]=j;
  68. if(j>=n) mi1[i][j]=q[h];
  69. }
  70. }
  71. for(int j=n;j<=b;++j)
  72. {
  73. h=t=;
  74. for(int i=;i<=a;++i)
  75. {
  76. while(h<t && i-pos[h]+>n) h++;
  77. while(h<t && mi1[i][j]<q[t-]) t--;
  78. q[t]=mi1[i][j];
  79. pos[t++]=i;
  80. if(i>=n) mi2[i][j]=q[h];
  81. }
  82. }
  83. int ans=2e9;
  84. for(int i=n;i<=a;++i)
  85. for(int j=n;j<=b;++j)
  86. ans=min(ans,mx2[i][j]-mi2[i][j]);
  87. printf("%d",ans);
  88. }

AC

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4.  
  5. using namespace std;
  6.  
  7. #define N 1001
  8.  
  9. int num[N][N];
  10.  
  11. int mx[N][N];
  12. int mi[N][N];
  13.  
  14. int q[N],pos[N],h,t;
  15.  
  16. void read(int &x)
  17. {
  18. x=; int f=; char c=getchar();
  19. while(!isdigit(c)) { if(c=='-') f=-; c=getchar(); }
  20. while(isdigit(c)) { x=x*+c-''; c=getchar(); }
  21. x*=f;
  22. }
  23.  
  24. int main()
  25. {
  26. freopen("data.in","r",stdin);
  27. freopen("std.out","w",stdout);
  28. int a,b,n;
  29. read(a); read(b); read(n);
  30. for(int i=;i<=a;++i)
  31. for(int j=;j<=b;++j)
  32. read(num[i][j]);
  33. int ans=1e9;
  34. for(int i=;i+n-<=a;++i)
  35. for(int j=;j+n-<=b;++j)
  36. {
  37. int I=i+n-;
  38. int J=j+n-;
  39. int p=1e9,q=-1e9;
  40. for(int k=i;k<=I;++k)
  41. for(int l=j;l<=J;++l)
  42. p=min(p,num[k][l]),q=max(q,num[k][l]);
  43. ans=min(ans,q-p);
  44. }
  45. printf("%d",ans);
  46. }

force

  1. #include<cmath>
  2. #include<cstdlib>
  3. #include<cstdio>
  4. #include<ctime>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. freopen("data.in","w",stdout);
  11. srand(time()+);
  12. int a=,b=;
  13. int n=rand()%+;
  14. printf("%d %d %d\n",a,b,n);
  15. for(int i=;i<=a;++i)
  16. {
  17. for(int j=;j<=b;++j) printf("%d ",rand());
  18. printf("\n");
  19. }
  20. }

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. Unity关于方法事件生命周期官方文档

    http://docs.unity3d.com/Manual/ExecutionOrder.html 一.组件运行的基本顺序 下图中创建类的顺序为A,B,C,A1,二运行的结果为A1,B,C,A. 可 ...

  2. bower配置私服nexus3

    内容来自 https://help.sonatype.com/repomanager3/bower-repositories#BowerRepositories-BrowsingBowerReposi ...

  3. 关于OBS获取显示器黑屏的解决办法

    近来看到许多人说OBS获取显示器源的时候黑屏,下面介绍下相关处理办法. 第一种,先尝试把OBS程序的兼容性设置成Win 7和管理员身份,具体操作: 设置成这样,如果能够获取到显示器,那么问题解决,否则 ...

  4. 腾讯 深圳 25928-PHP开发工程师(深圳)

    工作地点:深圳 职位类别:技术类 招聘人数:1人 工作职责: 负责游戏研运体系流程工具及相关产品的开发.维护: 参与支撑系统的需求分析.设计.编码: 承担外包开发成员技术导师,解决技术难点,把控开发质 ...

  5. 在ubuntu下运行python脚本

    转自http://www.cnblogs.com/hester/p/5575658.html 1. 运行方式一 新建test.py文件: 1 touch test.py 然后vim test.py打开 ...

  6. d3 数学方法(伪随机数生成器 )

    一.正态(高斯)分布(normal (Gaussian) distribution)的随机数 /* var nomarlRandmo = d3.random.normal(); console.log ...

  7. webpack打包使用

    md创建文件夹 dir是遍历 如何安装node环境 首先下载安装包;安装好以后 使用cmd命令 建立一个新的文件夹(一定是英文的),然后cmd 命令中 cd 进入所安装的盘的地址. 在该文件夹下面 使 ...

  8. ci test

    下载ci 版本 3.1.9 下载地址 https://www.codeigniter.com/ 怎么查看CI的版本信息?想看某个项目中使用的CI具体是哪个版本,怎么查看?system\core\cod ...

  9. sessionStorage & string typeof

    sessionStorage & string typeof

  10. MT【109】线面角最大时为二面角平面角

    解答:如图设C到$\alpha$面的距离为$d_1,C_1$到虚线距离为$d_2$ 所求距离$d=d_1+d_2=|AC|sin\theta+|CC_1|cos\theta=4\sqrt{2}sin\ ...