题目链接:https://ac.nowcoder.com/acm/contest/882/H

题目大意

  给定一个 n * m 的 01 矩阵,求其中第二大的子矩阵,子矩阵元素必须全部为 1。输出其大小。

分析1(前缀和,O(NM2))

  这题数据没那么强,所以用前缀和暴力枚举也能过。

代码如下

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5. #define Rep(i,n) for (int i = 0; i < (n); ++i)
  6. #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  7. #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  8. #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9. #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
  10. #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
  11. #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
  12.  
  13. #define pr(x) cout << #x << " = " << x << " "
  14. #define prln(x) cout << #x << " = " << x << endl
  15.  
  16. #define LOWBIT(x) ((x)&(-x))
  17.  
  18. #define ALL(x) x.begin(),x.end()
  19. #define INS(x) inserter(x,x.begin())
  20. #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
  21. #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // ?? x ?????? c
  22. #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
  23. #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
  24.  
  25. #define ms0(a) memset(a,0,sizeof(a))
  26. #define msI(a) memset(a,inf,sizeof(a))
  27. #define msM(a) memset(a,-1,sizeof(a))
  28.  
  29. #define MP make_pair
  30. #define PB push_back
  31. #define ft first
  32. #define sd second
  33.  
  34. template<typename T1, typename T2>
  35. istream &operator>>(istream &in, pair<T1, T2> &p) {
  36. in >> p.first >> p.second;
  37. return in;
  38. }
  39.  
  40. template<typename T>
  41. istream &operator>>(istream &in, vector<T> &v) {
  42. for (auto &x: v)
  43. in >> x;
  44. return in;
  45. }
  46.  
  47. template<typename T>
  48. ostream &operator<<(ostream &out, vector<T> &v) {
  49. Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
  50. return out;
  51. }
  52.  
  53. template<typename T1, typename T2>
  54. ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
  55. out << "[" << p.first << ", " << p.second << "]" << "\n";
  56. return out;
  57. }
  58.  
  59. inline int gc(){
  60. static const int BUF = 1e7;
  61. static char buf[BUF], *bg = buf + BUF, *ed = bg;
  62.  
  63. if(bg == ed) fread(bg = buf, , BUF, stdin);
  64. return *bg++;
  65. }
  66.  
  67. inline int ri(){
  68. int x = , f = , c = gc();
  69. for(; c<||c>; f = c=='-'?-:f, c=gc());
  70. for(; c>&&c<; x = x* + c - , c=gc());
  71. return x*f;
  72. }
  73.  
  74. template<class T>
  75. inline string toString(T x) {
  76. ostringstream sout;
  77. sout << x;
  78. return sout.str();
  79. }
  80.  
  81. inline int toInt(string s) {
  82. int v;
  83. istringstream sin(s);
  84. sin >> v;
  85. return v;
  86. }
  87.  
  88. //min <= aim <= max
  89. template<typename T>
  90. inline bool BETWEEN(const T aim, const T min, const T max) {
  91. return min <= aim && aim <= max;
  92. }
  93.  
  94. typedef long long LL;
  95. typedef unsigned long long uLL;
  96. typedef pair< double, double > PDD;
  97. typedef pair< int, int > PII;
  98. typedef pair< int, PII > PIPII;
  99. typedef pair< string, int > PSI;
  100. typedef pair< int, PSI > PIPSI;
  101. typedef set< int > SI;
  102. typedef set< PII > SPII;
  103. typedef vector< int > VI;
  104. typedef vector< double > VD;
  105. typedef vector< VI > VVI;
  106. typedef vector< SI > VSI;
  107. typedef vector< PII > VPII;
  108. typedef map< int, int > MII;
  109. typedef map< int, string > MIS;
  110. typedef map< int, PII > MIPII;
  111. typedef map< PII, int > MPIII;
  112. typedef map< string, int > MSI;
  113. typedef map< string, string > MSS;
  114. typedef map< PII, string > MPIIS;
  115. typedef map< PII, PII > MPIIPII;
  116. typedef multimap< int, int > MMII;
  117. typedef multimap< string, int > MMSI;
  118. //typedef unordered_map< int, int > uMII;
  119. typedef pair< LL, LL > PLL;
  120. typedef vector< LL > VL;
  121. typedef vector< VL > VVL;
  122. typedef priority_queue< int > PQIMax;
  123. typedef priority_queue< int, VI, greater< int > > PQIMin;
  124. const double EPS = 1e-;
  125. const LL inf = 0x7fffffff;
  126. const LL infLL = 0x7fffffffffffffffLL;
  127. const LL mod = 1e9 + ;
  128. const int maxN = 1e3 + ;
  129. const LL ONE = ;
  130. const LL evenBits = 0xaaaaaaaaaaaaaaaa;
  131. const LL oddBits = 0x5555555555555555;
  132.  
  133. int N, M, preSumY[maxN][maxN];
  134. int Fmax, Smax;
  135.  
  136. int main(){
  137. //freopen("MyOutput.txt","w",stdout);
  138. //freopen("input.txt","r",stdin);
  139. //INIT();
  140. scanf("%d%d", &N, &M);
  141. For(i, , N) {
  142. For(j, , M) {
  143. scanf("%1d", &preSumY[i][j]);
  144. preSumY[i][j] += preSumY[i - ][j] * preSumY[i][j];
  145. }
  146. }
  147.  
  148. For(x, , N) {
  149. For(y, , M) {
  150. // 枚举以(x, y)为右下角所能形成的所有矩形
  151. int len = preSumY[x][y], width = ;
  152.  
  153. while(len) {
  154. int area = len * width;
  155.  
  156. if(area > Fmax) {
  157. Smax = Fmax;
  158. Fmax = area;
  159. }
  160. else if(area > Smax) Smax = area;
  161.  
  162. len = min(len, preSumY[x][y - width]);
  163. ++width;
  164. }
  165. }
  166. }
  167.  
  168. printf("%d\n", Smax);
  169. return ;
  170. }

分析2(单调栈,O(NM))

  这是对分析1的优化,每一行都可以看成是直方图的底边,然后从底边开始向上连续最多的 1 代表柱子的高度。于是问题转化为求直方图最大面积。
  而求直方图最大面积可以用到单调栈(可以求一个数的左右最近小的位置)。详细见:https://www.cnblogs.com/linkstar/p/6139668.html

代码如下

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5. #define Rep(i,n) for (int i = 0; i < (n); ++i)
  6. #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  7. #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  8. #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9. #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
  10. #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
  11. #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
  12.  
  13. #define pr(x) cout << #x << " = " << x << " "
  14. #define prln(x) cout << #x << " = " << x << endl
  15.  
  16. #define LOWBIT(x) ((x)&(-x))
  17.  
  18. #define ALL(x) x.begin(),x.end()
  19. #define INS(x) inserter(x,x.begin())
  20. #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
  21. #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // ?? x ?????? c
  22. #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
  23. #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
  24.  
  25. #define ms0(a) memset(a,0,sizeof(a))
  26. #define msI(a) memset(a,inf,sizeof(a))
  27. #define msM(a) memset(a,-1,sizeof(a))
  28.  
  29. #define MP make_pair
  30. #define PB push_back
  31. #define ft first
  32. #define sd second
  33.  
  34. template<typename T1, typename T2>
  35. istream &operator>>(istream &in, pair<T1, T2> &p) {
  36. in >> p.first >> p.second;
  37. return in;
  38. }
  39.  
  40. template<typename T>
  41. istream &operator>>(istream &in, vector<T> &v) {
  42. for (auto &x: v)
  43. in >> x;
  44. return in;
  45. }
  46.  
  47. template<typename T>
  48. ostream &operator<<(ostream &out, vector<T> &v) {
  49. Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
  50. return out;
  51. }
  52.  
  53. template<typename T1, typename T2>
  54. ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
  55. out << "[" << p.first << ", " << p.second << "]" << "\n";
  56. return out;
  57. }
  58.  
  59. inline int gc(){
  60. static const int BUF = 1e7;
  61. static char buf[BUF], *bg = buf + BUF, *ed = bg;
  62.  
  63. if(bg == ed) fread(bg = buf, , BUF, stdin);
  64. return *bg++;
  65. }
  66.  
  67. inline int ri(){
  68. int x = , f = , c = gc();
  69. for(; c<||c>; f = c=='-'?-:f, c=gc());
  70. for(; c>&&c<; x = x* + c - , c=gc());
  71. return x*f;
  72. }
  73.  
  74. template<class T>
  75. inline string toString(T x) {
  76. ostringstream sout;
  77. sout << x;
  78. return sout.str();
  79. }
  80.  
  81. inline int toInt(string s) {
  82. int v;
  83. istringstream sin(s);
  84. sin >> v;
  85. return v;
  86. }
  87.  
  88. //min <= aim <= max
  89. template<typename T>
  90. inline bool BETWEEN(const T aim, const T min, const T max) {
  91. return min <= aim && aim <= max;
  92. }
  93.  
  94. typedef long long LL;
  95. typedef unsigned long long uLL;
  96. typedef pair< double, double > PDD;
  97. typedef pair< int, int > PII;
  98. typedef pair< int, PII > PIPII;
  99. typedef pair< string, int > PSI;
  100. typedef pair< int, PSI > PIPSI;
  101. typedef set< int > SI;
  102. typedef set< PII > SPII;
  103. typedef vector< int > VI;
  104. typedef vector< double > VD;
  105. typedef vector< VI > VVI;
  106. typedef vector< SI > VSI;
  107. typedef vector< PII > VPII;
  108. typedef map< int, int > MII;
  109. typedef map< int, string > MIS;
  110. typedef map< int, PII > MIPII;
  111. typedef map< PII, int > MPIII;
  112. typedef map< string, int > MSI;
  113. typedef map< string, string > MSS;
  114. typedef map< PII, string > MPIIS;
  115. typedef map< PII, PII > MPIIPII;
  116. typedef multimap< int, int > MMII;
  117. typedef multimap< string, int > MMSI;
  118. //typedef unordered_map< int, int > uMII;
  119. typedef pair< LL, LL > PLL;
  120. typedef vector< LL > VL;
  121. typedef vector< VL > VVL;
  122. typedef priority_queue< int > PQIMax;
  123. typedef priority_queue< int, VI, greater< int > > PQIMin;
  124. const double EPS = 1e-;
  125. const LL inf = 0x7fffffff;
  126. const LL infLL = 0x7fffffffffffffffLL;
  127. const LL mod = 1e9 + ;
  128. const int maxN = 1e3 + ;
  129. const LL ONE = ;
  130. const LL evenBits = 0xaaaaaaaaaaaaaaaa;
  131. const LL oddBits = 0x5555555555555555;
  132.  
  133. int N, M, board[maxN][maxN];
  134. int Fmax, Smax;
  135. stack< PII > st;
  136.  
  137. void update(int area) {
  138. if(area < ) return;
  139. if(area > Fmax) {
  140. Smax = Fmax;
  141. Fmax = area;
  142. }
  143. else if(area > Smax) Smax = area;
  144. }
  145.  
  146. int main(){
  147. //freopen("MyOutput.txt","w",stdout);
  148. //freopen("input.txt","r",stdin);
  149. //INIT();
  150. scanf("%d%d", &N, &M);
  151. For(i, , N) {
  152. while(!st.empty()) st.pop();
  153.  
  154. board[i][] = -; // 哨兵
  155. For(j, , M) {
  156. scanf("%1d", &board[i][j]);
  157. if(board[i][j]) board[i][j] += board[i - ][j];
  158. }
  159. board[i][M + ] = -; // 哨兵
  160.  
  161. For(j, , M + ) {
  162. while(!st.empty() && st.top().ft > board[i][j]) {
  163. PII tmp = st.top(); st.pop();
  164.  
  165. int w = j - tmp.sd + tmp.sd - st.top().sd - ;
  166. update(tmp.ft * w);
  167. update(tmp.ft * (w - ));
  168. update((tmp.ft - ) * w);
  169. }
  170. st.push(PII(board[i][j], j));
  171. }
  172. }
  173.  
  174. printf("%d\n", Smax);
  175. return ;
  176. }

2019 牛客多校第二场 H Second Large Rectangle的更多相关文章

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

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

  2. 2019牛客多校第二场H题(悬线法)

    把以前的题补补,用悬线求面积第二大的子矩形.我们先求出最大子矩阵的面积,并记录其行三个方向上的悬线长度.然后排除这个矩形,记得还得特判少一行或者少一列的情况 #include <bits/std ...

  3. 2019牛客多校第二场 A Eddy Walker(概率推公式)

    2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...

  4. 2019牛客多校第二场H-Second Large Rectangle

    Second Large Rectangle 题目传送门 解题思路 先求出每个点上的高,再利用单调栈分别求出每个点左右两边第一个高小于自己的位置,从而而得出最后一个大于等于自己的位置,进而求出自己的位 ...

  5. 2019 牛客暑期多校 第二场 H Second Large Rectangle (单调栈)

    题目:https://ac.nowcoder.com/acm/contest/882/H 题意:一个大的01矩阵,然后现在要求第二大的全一矩阵是多少 思路:在这里我们首先学习一下另一个东西,怎么求直方 ...

  6. 2019年牛客多校第二场 H题Second Large Rectangle

    题目链接 传送门 题意 求在\(n\times m\)的\(01\)子矩阵中找出面积第二大的内部全是\(1\)的子矩阵的面积大小. 思路 处理出每个位置往左连续有多少个\(1\),然后对每一列跑单调栈 ...

  7. [2019牛客多校第二场][G. Polygons]

    题目链接:https://ac.nowcoder.com/acm/contest/882/G 题目大意:有\(n\)条直线将平面分成若干个区域,要求处理\(m\)次询问:求第\(q\)大的区域面积.保 ...

  8. 2019牛客多校第二场D-Kth Minimum Clique

    Kth Minimum Clique 题目传送门 解题思路 我们可以从没有点开始,把点一个一个放进去,先把放入一个点的情况都存进按照权值排序的优先队列,每次在新出队的集合里增加一个新的点,为了避免重复 ...

  9. 2019牛客多校第二场F-Partition problem(搜索+剪枝)

    Partition problem 题目传送门 解题思路 假设当前两队的对抗值为s,如果把红队中的一个人a分配到白队,s+= a对红队中所有人的对抗值,s-= a对白队中所有人的对抗值.所以我们可以先 ...

随机推荐

  1. NOIP模拟测试17

    T1:入阵曲 题目大意:给定一个N*M的矩形,问一共有多少个子矩形,使得矩形内所有书的和为k的倍数. 60%:N,M<=80 枚举矩形的左上角和右下角,用二维前缀和求出数字之和. 时间复杂度$O ...

  2. 排序+并查集——cf1213F

    /* 有向边(pi,pi+1),形成链后进行dfs,求出dfs序 一个联通块内的元素必须是同一个字符,如果最后的联通块个数<k,说明不行 */ #include<bits/stdc++.h ...

  3. js 读取本地文件(必须通过input控件才能实现) 及 下载文件

    js 操作 文件的实现原理: 1.js是不能直接操作(读写)文件的,html的  input[type="file"] 控件是可以读取文件数据(获取文件数据流)的.js可以获取这个 ...

  4. (转)OpenFire源码学习之六:用户注册

    转:http://blog.csdn.net/huwenfeng_2011/article/details/43413509 用户注册 注册流程: 1.客户端进行握手给服务端发送连接消息: <s ...

  5. 关于swiper动态更改,无法更新的悖论

    关于swiper动态更改,无法更新的悖论 以前都觉得swiper的使用很简单,那是因为使用swiper时都是写的数据,按照官网上介绍直接初始化swiper,随便丢一个地方初始化就ok了,但是在很多需求 ...

  6. CSS:CSS 媒体类型

    ylbtech-CSS:CSS 媒体类型 1.返回顶部 1. CSS 媒体类型 媒体类型允许你指定文件将如何在不同媒体呈现.该文件可以以不同的方式显示在屏幕上,在纸张上,或听觉浏览器等等. 媒体类型 ...

  7. docker 镜像中心搭建

    1.由于国外镜像很慢,所以用了网易蜂巢的镜像docker pull hub.c.163.com/library/registry:2.5.2本地的存储空间挂载到容器内部,持久保存镜像中心文件docke ...

  8. 使用linkedhashmap实现LRU(最近最少使用缓存算法)

    import java.util.LinkedHashMap; import java.util.Map; public class LRUCache<K, V> extends Link ...

  9. Codeforces 1119E Pavel and Triangles (贪心)

    Codeforces Global Round 2 题目链接: E. Pavel and Triangles Pavel has several sticks with lengths equal t ...

  10. kuangbin专题十三-基础计算几何

    链接:https://cn.vjudge.net/contest/68968 POJ 2318 TOYS 题意:m个玩具落在n+1个区间,给你玩具的坐标,问每个区间有多少玩具. 思路:叉积的简单应用, ...