方法一

  • 黑白棋盘拥有性质:(r + c) % 2的值决定颜色
  • 因此把某色全部反转,直接求另一色的最大矩形即可,单调栈的经典问题
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int maxn = 2005;
int n, m, a[maxn][maxn], h[maxn][maxn];
int ans1, ans2; void solve(int c) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
h[i][j] = (a[i][j] == c) ? h[i][j - 1] + 1 : 0;
}
}
int L[n + 5], R[n + 5];
for (int j = 1; j <= m; j++) {
vector<int> st;
for (int i = 1; i <= n; i++) {
while (st.size() && h[st.back()][j] >= h[i][j]) st.pop_back();
L[i] = st.size() ? st.back() + 1 : 1;
st.emplace_back(i);
}
st.clear();
for (int i = n; i; i--) {
while (st.size() && h[st.back()][j] >= h[i][j]) st.pop_back();
R[i] = st.size() ? st.back() - 1 : n;
st.emplace_back(i);
}
for (int i = 1; i <= n; i++) {
int S = h[i][j] * (R[i] - L[i] + 1);
int t = min(h[i][j], R[i] - L[i] + 1);
ans1 = max(ans1, t * t);
ans2 = max(ans2, S);
}
}
} int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
if ((i + j) & 1)
a[i][j] ^= 1;
}
solve(0), solve(1);
return !printf("%d\n%d\n", ans1, ans2);
}

方法二

  • 悬线法:对每个点求出它合法的矩阵向左向右向上分别能多长,然后更新答案。其中求时肯定不能暴力,要从上一个递推过来
  • 如果高度能从上面递增过来,左右线也要跟上面取min
  • 其实我觉得和方法一思想挺像的……
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 2005;
int n, m, a[maxn][maxn];
int h[maxn][maxn], l[maxn][maxn], r[maxn][maxn];
int ans1, ans2; int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf("%d", &a[i][j]); for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (i > 1 && a[i][j] != a[i][j - 1]) {
l[i][j] = l[i][j - 1] + 1;
} else l[i][j] = 1;
}
for (int j = m; j; --j) {
if (j < m && a[i][j] != a[i][j + 1]) {
r[i][j] = r[i][j + 1] + 1;
} else r[i][j] = 1;
}
for (int j = 1; j <= m; j++) {
if (i > 1 && a[i][j] != a[i - 1][j]) {
h[i][j] = h[i - 1][j] + 1;
l[i][j] = min(l[i][j], l[i - 1][j]);
r[i][j] = min(r[i][j], r[i - 1][j]);
} else h[i][j] = 1; int Len = r[i][j] + l[i][j] - 1;
int t = min(Len, h[i][j]);
ans1 = max(ans1, t * t);
ans2 = max(ans2, Len * h[i][j]);
}
}
return !printf("%d\n%d\n", ans1, ans2);
}

BZOJ1057(单调栈 or 悬线法)的更多相关文章

  1. 牛客多校第二场H Second Large Rectangle 单调栈or悬线法

    Second Large Rectangle 题意 给出n*m的01矩阵,问由1组成的第二大的矩阵的大小是多少? 分析 单调栈(or 悬线法)入门题 单调栈 预处理出每一个点的最大高度,然后单调栈每一 ...

  2. bzoj 3039: 玉蟾宫 单调栈或者悬线法求最大子矩阵和

    3039: 玉蟾宫 Time Limit: 2 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 有一天,小猫rainbow ...

  3. 洛谷P4147 玉蟾宫 单调栈/悬线法

    正解:单调栈/悬线法 解题报告: ummm这题我当初做的时候一点思路也没有只会暴力出奇迹:D(啊听说暴力好像能水过去呢,,, 然后当初是看的题解,然后学了下悬线法 然后就忘了:D 然后我现在看发现看不 ...

  4. P1169 [ZJOI2007]棋盘制作 悬线法or单调栈

    思路:悬线法\(or\)单调栈 提交:2次 错因:正方形面积取错了\(QwQ\) 题解: 悬线法 讲解:王知昆\(dalao\)的\(PPT\) 详见代码: #include<cstdio> ...

  5. 2019牛客多校第八场A All-one Matrices 悬线法,单调栈待补

    All-one Matrices 题意 对于一个n,m的01矩阵,问有多少个极大矩阵. 分析 对于悬线法来说,其过程就是枚举极大矩阵的过程,那如何计数呢?对于一个点来说,若其左右边界包含了上一个点的悬 ...

  6. 2018.10.19 bzoj1057: [ZJOI2007]棋盘制作(悬线法)

    传送门 悬线法板题. 如果只求最大矩形面积那么跟玉蟾宫是一道题. 现在要求最大正方形面积. 所以每次更新最大矩形面积时用矩形宽的平方更新一下正方形答案就行了. 代码: #include<bits ...

  7. bzoj1057: [ZJOI2007]棋盘制作(悬线法)

    题目要求纵横坐标和奇偶性不同的点取值不同,于是我们把纵横坐标和奇偶性为1的点和0的点分别取反,就变成经典的最大全1子矩阵问题了,用悬线法解决. #include<iostream> #in ...

  8. 【UVALive】3029 City Game(悬线法)

    题目 传送门:QWQ 分析 以前见到过差不多的这题. xhk说是单调栈水题,但我又不会单调栈,于是当时就放下了. 这么久过去了我还是不会用单调栈做这题,用的是悬线法. 非常好写 代码 #include ...

  9. P1169 [ZJOI2007]棋盘制作[悬线法/二维dp]

    题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8 \times 88×8大小的黑白相间的方阵,对应八八六十四卦,黑白 ...

随机推荐

  1. Mysql远程登陆错误:ERROR 2003

    不能远程登陆Mysql,错误:ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.0.114' (10060).原因是电脑防火墙 ...

  2. centos7搭建mysql-5.7.22主从复制

    mysql7.7.22主从复制 本项目是根据真实环境搭建编写出文档,文档中的目录也是根据自己公司环境所创建.公司原来是一台服务器搭建的数据库(5.7.22),由于业务的扩展需要搭建一台从服务器,减轻主 ...

  3. Java常用类Date、Calendar、SimpleDateFormat详解

    Date类 java.util 包提供了 Date 类来封装当前的日期和时间,Date 类提供两个构造函数来实例化 Date 对象 第一个构造函数使用当前日期和时间来初始化对象   Date( ) 第 ...

  4. selenium中类名不能与方法名相同

    不要将selenium中的类名命名成需要用到的方法名,不然会报错!

  5. 事件驱动模式--Reactor

    原文:https://www.cnblogs.com/harvyxu/p/7498763.html 1 Reactor模型 Reactor模式是处理并发I/O比较常见的一种模式,用于同步I/O,中心思 ...

  6. ECMAScript Obejct 属性操作API

    Object 创建对象 我们有很多种方法创建一个对象,Object.create, Object.assign Object.create //字面量 var o = {}; // 等同于 var o ...

  7. linux进程学习笔记

    学习了linux下的进程,觉得应该整理一下,忘得差不多了,顺便回顾一下. 学而时习之,不亦说乎~~ 进程笔记 ,什么是进程? The Single UNIX Specification, Versio ...

  8. POJ-3680:Intervals (费用流)

    You are given N weighted open intervals. The ith interval covers (ai, bi) and weighs wi. Your task i ...

  9. 1080 Graduate Admission (30)(30 分)

    It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applicat ...

  10. 【Lintcode】112.Remove Duplicates from Sorted List

    题目: Given a sorted linked list, delete all duplicates such that each element appear only once. Examp ...