牛客多校第三场 F Planting Trees

题意:

求矩阵内最大值减最小值大于k的最大子矩阵的面积

题解:

矩阵压缩的技巧

因为对于我们有用的信息只有这个矩阵内的最大值和最小值

所以我们可以将一个长度为i*j的子矩阵给压缩成一个1*i的序列

那么压缩成一维就是求区间内最大值减最小值大于k的最长长度了,这个问题用两个单调队列维护即可

代码:

  1. /**
  2. *        ┏┓    ┏┓
  3. *        ┏┛┗━━━━━━━┛┗━━━┓
  4. *        ┃       ┃  
  5. *        ┃   ━    ┃
  6. *        ┃ >   < ┃
  7. *        ┃       ┃
  8. *        ┃... ⌒ ...  ┃
  9. *        ┃       ┃
  10. *        ┗━┓   ┏━┛
  11. *          ┃   ┃ Code is far away from bug with the animal protecting          
  12. *          ┃   ┃ 神兽保佑,代码无bug
  13. *          ┃   ┃           
  14. *          ┃   ┃       
  15. *          ┃   ┃
  16. *          ┃   ┃           
  17. *          ┃   ┗━━━┓
  18. *          ┃       ┣┓
  19. *          ┃       ┏┛
  20. *          ┗┓┓┏━┳┓┏┛
  21. *           ┃┫┫ ┃┫┫
  22. *           ┗┻┛ ┗┻┛
  23. */
  24. // warm heart, wagging tail,and a smile just for you!
  25. //
  26. // _ooOoo_
  27. // o8888888o
  28. // 88" . "88
  29. // (| -_- |)
  30. // O\ = /O
  31. // ____/`---'\____
  32. // .' \| |// `.
  33. // / \||| : |||// \
  34. // / _||||| -:- |||||- \
  35. // | | \ - /// | |
  36. // | \_| ''\---/'' | |
  37. // \ .-\__ `-` ___/-. /
  38. // ___`. .' /--.--\ `. . __
  39. // ."" '< `.___\_<|>_/___.' >'"".
  40. // | | : `- \`.;`\ _ /`;.`/ - ` : | |
  41. // \ \ `-. \_ __\ /__ _/ .-` / /
  42. // ======`-.____`-.___\_____/___.-`____.-'======
  43. // `=---='
  44. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  45. // 佛祖保佑 永无BUG
  46. #include <set>
  47. #include <map>
  48. #include <stack>
  49. #include <cmath>
  50. #include <queue>
  51. #include <cstdio>
  52. #include <string>
  53. #include <vector>
  54. #include <cstring>
  55. #include <iostream>
  56. #include <algorithm>
  57. using namespace std;
  58. typedef long long LL;
  59. typedef pair<int, int> pii;
  60. typedef unsigned long long uLL;
  61. #define ls rt<<1
  62. #define rs rt<<1|1
  63. #define lson l,mid,rt<<1
  64. #define rson mid+1,r,rt<<1|1
  65. #define bug printf("*********\n")
  66. #define FIN freopen("input.txt","r",stdin);
  67. #define FON freopen("output.txt","w+",stdout);
  68. #define IO ios::sync_with_stdio(false),cin.tie(0)
  69. #define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
  70. #define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
  71. #define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
  72. const int maxn = 3e5 + 5;
  73. const int INF = 0x3f3f3f3f;
  74. const int mod = 1e9 + 7;
  75. const double Pi = acos(-1);
  76. LL gcd(LL a, LL b) {
  77. return b ? gcd(b, a % b) : a;
  78. }
  79. LL lcm(LL a, LL b) {
  80. return a / gcd(a, b) * b;
  81. }
  82. double dpow(double a, LL b) {
  83. double ans = 1.0;
  84. while(b) {
  85. if(b % 2)ans = ans * a;
  86. a = a * a;
  87. b /= 2;
  88. } return ans;
  89. }
  90. LL quick_pow(LL x, LL y) {
  91. LL ans = 1;
  92. while(y) {
  93. if(y & 1) {
  94. ans = ans * x % mod;
  95. } x = x * x % mod;
  96. y >>= 1;
  97. } return ans;
  98. }
  99. int a[505][505];
  100. int qmax[505], qmin[505];
  101. int Max[505], Min[505];
  102. int main() {
  103. #ifndef ONLINE_JUDGE
  104. FIN
  105. #endif
  106. int T;
  107. scanf("%d", &T);
  108. while(T--) {
  109. int n, K;
  110. scanf("%d%d", &n, &K);
  111. for(int i = 1; i <= n; i++) {
  112. for(int j = 1; j <= n; j++) {
  113. scanf("%d", &a[i][j]);
  114. }
  115. }
  116. LL res = 1;
  117. for(int i = 1; i <= n; i++) {
  118. for(int j = 1; j <= n; j++) {
  119. Max[j] = -INF;
  120. Min[j] = INF;
  121. }
  122. for(int j = i; j <= n; j++) {
  123. for(int k = 1; k <= n; k++) {
  124. Max[k] = max(Max[k], a[j][k]);
  125. Min[k] = min(Min[k], a[j][k]);
  126. }
  127. int l = 1, hmax = 0, hmin = 0, tmax = 1, tmin = 1;
  128. for(int r = 1; r <= n; r++) {
  129. while(tmax <= hmax && Max[r] >= Max[qmax[hmax]]) hmax--;
  130. while(tmin <= hmin && Min[r] <= Min[qmin[hmin]]) hmin--;
  131. qmax[++hmax] = r;
  132. qmin[++hmin] = r;
  133. while(l <= r && ( Max[qmax[tmax]] - Min[qmin[tmin]] > K) ) {
  134. l++;
  135. if(qmax[tmax] < l)tmax++;
  136. if(qmin[tmin] < l)tmin++;
  137. }
  138. res = max(res, 1LL * (r - l + 1) * (j - i + 1));
  139. }
  140. }
  141. }
  142. printf("%lld\n", res);
  143. }
  144. return 0;
  145. }

牛客多校第三场 F Planting Trees的更多相关文章

  1. 牛客多校第三场F Planting Trees 单调栈

    Planting Trees 题意 给出一个矩阵,求最大矩阵面积满足该矩阵中任2元素的绝对值之差小于等于M T<1000) (n<500)但是题目明示单组(n*3)可过 分析 又是矩阵问题 ...

  2. 2019牛客多校第三场 F.Planting Trees

    题目链接 题目链接 题解 题面上面很明显的提示了需要严格\(O(n^3)\)的算法. 先考虑一个过不了的做法,枚举右下角的\((x,y)\),然后二分矩形面积,枚举其中一边,则复杂度是\(O(n^3 ...

  3. 2019牛客多校第三场F Planting Trees(单调队列)题解

    题意: 求最大矩阵面积,要求矩阵内数字满足\(max - min < m\) 思路: 枚举上下长度,在枚举的时候可以求出每一列的最大最小值\(cmax,cmin\),这样问题就变成了求一行数,要 ...

  4. 2019 牛客暑期多校 第三场 F Planting Trees (单调队列+尺取)

    题目:https://ac.nowcoder.com/acm/contest/883/F 题意:求一个矩阵最大面积,这个矩阵的要求是矩阵内最小值与最大值差值<=m 思路:首先我们仔细观察范围,我 ...

  5. 2019年牛客多校第三场 F题Planting Trees(单调队列)

    题目链接 传送门 题意 给你一个\(n\times n\)的矩形,要你求出一个面积最大的矩形使得这个矩形内的最大值减最小值小于等于\(M\). 思路 单调队列滚动窗口. 比赛的时候我的想法是先枚举长度 ...

  6. 2019牛客多校第八场 F题 Flowers 计算几何+线段树

    2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...

  7. 牛客多校第三场 G Removing Stones(分治+线段树)

    牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...

  8. 牛客多校第三场 A—pacm team (4维背包加路径压缩)

    链接:https://www.nowcoder.com/acm/contest/141/A 来源:牛客网 Eddy was a contestant participating , Eddy fail ...

  9. 牛客多校第五场 F take

    链接:https://www.nowcoder.com/acm/contest/143/F来源:牛客网 题目描述 Kanade has n boxes , the i-th box has p[i] ...

随机推荐

  1. C#中App.config文件配置获取

    最新的framework使用如下方法: using System.Configuration; ConfigurationManager.AppSettings["key"]; A ...

  2. UVA_414:Machined Surfaces

    Language : C++ 4.8.2 #include<stdio.h> #include<string.h> int main(void) { int n; int su ...

  3. HTML5入门指南

    1.HTML5到底是什么? HTML5是HTML最新的修订版本,2014年10月由万维网联盟(W3C)完成标准制定.目标是取代1999年所制定的HTML 4.01和XHTML 1.0标准,以期能在互联 ...

  4. spider csdn博客和quantstart文章

    spider csdn博客和quantstart文章 功能 提取csdn博客文章 提取quantstart.com 博客文章, Micheal Hall-Moore 创办的网站 特色功能就是: 想把原 ...

  5. 巨蟒python全栈开发-第11阶段 ansible_project1

    今日大纲: 1.前端页面介绍 2.发布流程 3.需求分析 4.表结构设计 5.前端页面设计 昨日内容回顾: 1.roles - tasks - handlers - files - templates ...

  6. sql select时增加常量列

    阅读更多 string sql="select a,b,'常量' as c from table" 注:单引号' ' 很重要,否则编译时会把其看成查询参数,从而提示参数未指定错误. ...

  7. Oracle函数——TO_DATE

    TO_DATE 含义:将具有固定格式的字符串类型的数据转化为相对应的Date类型数据,官网解释如下图   使用方法 TO_DATE("需要转换的字符串","日期格式&qu ...

  8. Java中的Runnable、Callable、Future、FutureTask的区别与示例

    Java中存在Runnable.Callable.Future.FutureTask这几个与线程相关的类或者接口,在Java中也是比较重要的几个概念,我们通过下面的简单示例来了解一下它们的作用于区别. ...

  9. OpenStack组件系列☞glance简介

    Glance项目提供虚拟机镜像的发现,注册,取得服务. Glance提供restful API可以查询虚拟机镜像的metadata,并且可以获得镜像. 通过Glance,虚拟机镜像可以被存储到多种存储 ...

  10. H3C 数据链路层