2019 牛客多校第二场 H Second Large Rectangle
题目链接:https://ac.nowcoder.com/acm/contest/882/H
题目大意
给定一个 n * m 的 01 矩阵,求其中第二大的子矩阵,子矩阵元素必须全部为 1。输出其大小。
分析1(前缀和,O(NM2))
代码如下
- #include <bits/stdc++.h>
- using namespace std;
- #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- #define Rep(i,n) for (int i = 0; i < (n); ++i)
- #define For(i,s,t) for (int i = (s); i <= (t); ++i)
- #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
- #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
- #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
- #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
- #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
- #define pr(x) cout << #x << " = " << x << " "
- #define prln(x) cout << #x << " = " << x << endl
- #define LOWBIT(x) ((x)&(-x))
- #define ALL(x) x.begin(),x.end()
- #define INS(x) inserter(x,x.begin())
- #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
- #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // ?? x ?????? c
- #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
- #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
- #define ms0(a) memset(a,0,sizeof(a))
- #define msI(a) memset(a,inf,sizeof(a))
- #define msM(a) memset(a,-1,sizeof(a))
- #define MP make_pair
- #define PB push_back
- #define ft first
- #define sd second
- template<typename T1, typename T2>
- istream &operator>>(istream &in, pair<T1, T2> &p) {
- in >> p.first >> p.second;
- return in;
- }
- template<typename T>
- istream &operator>>(istream &in, vector<T> &v) {
- for (auto &x: v)
- in >> x;
- return in;
- }
- template<typename T>
- ostream &operator<<(ostream &out, vector<T> &v) {
- Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
- return out;
- }
- template<typename T1, typename T2>
- ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
- out << "[" << p.first << ", " << p.second << "]" << "\n";
- return out;
- }
- inline int gc(){
- static const int BUF = 1e7;
- static char buf[BUF], *bg = buf + BUF, *ed = bg;
- if(bg == ed) fread(bg = buf, , BUF, stdin);
- return *bg++;
- }
- inline int ri(){
- int x = , f = , c = gc();
- for(; c<||c>; f = c=='-'?-:f, c=gc());
- for(; c>&&c<; x = x* + c - , c=gc());
- return x*f;
- }
- template<class T>
- inline string toString(T x) {
- ostringstream sout;
- sout << x;
- return sout.str();
- }
- inline int toInt(string s) {
- int v;
- istringstream sin(s);
- sin >> v;
- return v;
- }
- //min <= aim <= max
- template<typename T>
- inline bool BETWEEN(const T aim, const T min, const T max) {
- return min <= aim && aim <= max;
- }
- typedef long long LL;
- typedef unsigned long long uLL;
- typedef pair< double, double > PDD;
- typedef pair< int, int > PII;
- typedef pair< int, PII > PIPII;
- typedef pair< string, int > PSI;
- typedef pair< int, PSI > PIPSI;
- typedef set< int > SI;
- typedef set< PII > SPII;
- typedef vector< int > VI;
- typedef vector< double > VD;
- typedef vector< VI > VVI;
- typedef vector< SI > VSI;
- typedef vector< PII > VPII;
- typedef map< int, int > MII;
- typedef map< int, string > MIS;
- typedef map< int, PII > MIPII;
- typedef map< PII, int > MPIII;
- typedef map< string, int > MSI;
- typedef map< string, string > MSS;
- typedef map< PII, string > MPIIS;
- typedef map< PII, PII > MPIIPII;
- typedef multimap< int, int > MMII;
- typedef multimap< string, int > MMSI;
- //typedef unordered_map< int, int > uMII;
- typedef pair< LL, LL > PLL;
- typedef vector< LL > VL;
- typedef vector< VL > VVL;
- typedef priority_queue< int > PQIMax;
- typedef priority_queue< int, VI, greater< int > > PQIMin;
- const double EPS = 1e-;
- const LL inf = 0x7fffffff;
- const LL infLL = 0x7fffffffffffffffLL;
- const LL mod = 1e9 + ;
- const int maxN = 1e3 + ;
- const LL ONE = ;
- const LL evenBits = 0xaaaaaaaaaaaaaaaa;
- const LL oddBits = 0x5555555555555555;
- int N, M, preSumY[maxN][maxN];
- int Fmax, Smax;
- int main(){
- //freopen("MyOutput.txt","w",stdout);
- //freopen("input.txt","r",stdin);
- //INIT();
- scanf("%d%d", &N, &M);
- For(i, , N) {
- For(j, , M) {
- scanf("%1d", &preSumY[i][j]);
- preSumY[i][j] += preSumY[i - ][j] * preSumY[i][j];
- }
- }
- For(x, , N) {
- For(y, , M) {
- // 枚举以(x, y)为右下角所能形成的所有矩形
- int len = preSumY[x][y], width = ;
- while(len) {
- int area = len * width;
- if(area > Fmax) {
- Smax = Fmax;
- Fmax = area;
- }
- else if(area > Smax) Smax = area;
- len = min(len, preSumY[x][y - width]);
- ++width;
- }
- }
- }
- printf("%d\n", Smax);
- return ;
- }
分析2(单调栈,O(NM))
代码如下
- #include <bits/stdc++.h>
- using namespace std;
- #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- #define Rep(i,n) for (int i = 0; i < (n); ++i)
- #define For(i,s,t) for (int i = (s); i <= (t); ++i)
- #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
- #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
- #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
- #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
- #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
- #define pr(x) cout << #x << " = " << x << " "
- #define prln(x) cout << #x << " = " << x << endl
- #define LOWBIT(x) ((x)&(-x))
- #define ALL(x) x.begin(),x.end()
- #define INS(x) inserter(x,x.begin())
- #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
- #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // ?? x ?????? c
- #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
- #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
- #define ms0(a) memset(a,0,sizeof(a))
- #define msI(a) memset(a,inf,sizeof(a))
- #define msM(a) memset(a,-1,sizeof(a))
- #define MP make_pair
- #define PB push_back
- #define ft first
- #define sd second
- template<typename T1, typename T2>
- istream &operator>>(istream &in, pair<T1, T2> &p) {
- in >> p.first >> p.second;
- return in;
- }
- template<typename T>
- istream &operator>>(istream &in, vector<T> &v) {
- for (auto &x: v)
- in >> x;
- return in;
- }
- template<typename T>
- ostream &operator<<(ostream &out, vector<T> &v) {
- Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
- return out;
- }
- template<typename T1, typename T2>
- ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
- out << "[" << p.first << ", " << p.second << "]" << "\n";
- return out;
- }
- inline int gc(){
- static const int BUF = 1e7;
- static char buf[BUF], *bg = buf + BUF, *ed = bg;
- if(bg == ed) fread(bg = buf, , BUF, stdin);
- return *bg++;
- }
- inline int ri(){
- int x = , f = , c = gc();
- for(; c<||c>; f = c=='-'?-:f, c=gc());
- for(; c>&&c<; x = x* + c - , c=gc());
- return x*f;
- }
- template<class T>
- inline string toString(T x) {
- ostringstream sout;
- sout << x;
- return sout.str();
- }
- inline int toInt(string s) {
- int v;
- istringstream sin(s);
- sin >> v;
- return v;
- }
- //min <= aim <= max
- template<typename T>
- inline bool BETWEEN(const T aim, const T min, const T max) {
- return min <= aim && aim <= max;
- }
- typedef long long LL;
- typedef unsigned long long uLL;
- typedef pair< double, double > PDD;
- typedef pair< int, int > PII;
- typedef pair< int, PII > PIPII;
- typedef pair< string, int > PSI;
- typedef pair< int, PSI > PIPSI;
- typedef set< int > SI;
- typedef set< PII > SPII;
- typedef vector< int > VI;
- typedef vector< double > VD;
- typedef vector< VI > VVI;
- typedef vector< SI > VSI;
- typedef vector< PII > VPII;
- typedef map< int, int > MII;
- typedef map< int, string > MIS;
- typedef map< int, PII > MIPII;
- typedef map< PII, int > MPIII;
- typedef map< string, int > MSI;
- typedef map< string, string > MSS;
- typedef map< PII, string > MPIIS;
- typedef map< PII, PII > MPIIPII;
- typedef multimap< int, int > MMII;
- typedef multimap< string, int > MMSI;
- //typedef unordered_map< int, int > uMII;
- typedef pair< LL, LL > PLL;
- typedef vector< LL > VL;
- typedef vector< VL > VVL;
- typedef priority_queue< int > PQIMax;
- typedef priority_queue< int, VI, greater< int > > PQIMin;
- const double EPS = 1e-;
- const LL inf = 0x7fffffff;
- const LL infLL = 0x7fffffffffffffffLL;
- const LL mod = 1e9 + ;
- const int maxN = 1e3 + ;
- const LL ONE = ;
- const LL evenBits = 0xaaaaaaaaaaaaaaaa;
- const LL oddBits = 0x5555555555555555;
- int N, M, board[maxN][maxN];
- int Fmax, Smax;
- stack< PII > st;
- void update(int area) {
- if(area < ) return;
- if(area > Fmax) {
- Smax = Fmax;
- Fmax = area;
- }
- else if(area > Smax) Smax = area;
- }
- int main(){
- //freopen("MyOutput.txt","w",stdout);
- //freopen("input.txt","r",stdin);
- //INIT();
- scanf("%d%d", &N, &M);
- For(i, , N) {
- while(!st.empty()) st.pop();
- board[i][] = -; // 哨兵
- For(j, , M) {
- scanf("%1d", &board[i][j]);
- if(board[i][j]) board[i][j] += board[i - ][j];
- }
- board[i][M + ] = -; // 哨兵
- For(j, , M + ) {
- while(!st.empty() && st.top().ft > board[i][j]) {
- PII tmp = st.top(); st.pop();
- int w = j - tmp.sd + tmp.sd - st.top().sd - ;
- update(tmp.ft * w);
- update(tmp.ft * (w - ));
- update((tmp.ft - ) * w);
- }
- st.push(PII(board[i][j], j));
- }
- }
- printf("%d\n", Smax);
- return ;
- }
2019 牛客多校第二场 H Second Large Rectangle的更多相关文章
- 牛客多校第二场H Second Large Rectangle 单调栈or悬线法
Second Large Rectangle 题意 给出n*m的01矩阵,问由1组成的第二大的矩阵的大小是多少? 分析 单调栈(or 悬线法)入门题 单调栈 预处理出每一个点的最大高度,然后单调栈每一 ...
- 2019牛客多校第二场H题(悬线法)
把以前的题补补,用悬线求面积第二大的子矩形.我们先求出最大子矩阵的面积,并记录其行三个方向上的悬线长度.然后排除这个矩形,记得还得特判少一行或者少一列的情况 #include <bits/std ...
- 2019牛客多校第二场 A Eddy Walker(概率推公式)
2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...
- 2019牛客多校第二场H-Second Large Rectangle
Second Large Rectangle 题目传送门 解题思路 先求出每个点上的高,再利用单调栈分别求出每个点左右两边第一个高小于自己的位置,从而而得出最后一个大于等于自己的位置,进而求出自己的位 ...
- 2019 牛客暑期多校 第二场 H Second Large Rectangle (单调栈)
题目:https://ac.nowcoder.com/acm/contest/882/H 题意:一个大的01矩阵,然后现在要求第二大的全一矩阵是多少 思路:在这里我们首先学习一下另一个东西,怎么求直方 ...
- 2019年牛客多校第二场 H题Second Large Rectangle
题目链接 传送门 题意 求在\(n\times m\)的\(01\)子矩阵中找出面积第二大的内部全是\(1\)的子矩阵的面积大小. 思路 处理出每个位置往左连续有多少个\(1\),然后对每一列跑单调栈 ...
- [2019牛客多校第二场][G. Polygons]
题目链接:https://ac.nowcoder.com/acm/contest/882/G 题目大意:有\(n\)条直线将平面分成若干个区域,要求处理\(m\)次询问:求第\(q\)大的区域面积.保 ...
- 2019牛客多校第二场D-Kth Minimum Clique
Kth Minimum Clique 题目传送门 解题思路 我们可以从没有点开始,把点一个一个放进去,先把放入一个点的情况都存进按照权值排序的优先队列,每次在新出队的集合里增加一个新的点,为了避免重复 ...
- 2019牛客多校第二场F-Partition problem(搜索+剪枝)
Partition problem 题目传送门 解题思路 假设当前两队的对抗值为s,如果把红队中的一个人a分配到白队,s+= a对红队中所有人的对抗值,s-= a对白队中所有人的对抗值.所以我们可以先 ...
随机推荐
- NOIP模拟测试17
T1:入阵曲 题目大意:给定一个N*M的矩形,问一共有多少个子矩形,使得矩形内所有书的和为k的倍数. 60%:N,M<=80 枚举矩形的左上角和右下角,用二维前缀和求出数字之和. 时间复杂度$O ...
- 排序+并查集——cf1213F
/* 有向边(pi,pi+1),形成链后进行dfs,求出dfs序 一个联通块内的元素必须是同一个字符,如果最后的联通块个数<k,说明不行 */ #include<bits/stdc++.h ...
- js 读取本地文件(必须通过input控件才能实现) 及 下载文件
js 操作 文件的实现原理: 1.js是不能直接操作(读写)文件的,html的 input[type="file"] 控件是可以读取文件数据(获取文件数据流)的.js可以获取这个 ...
- (转)OpenFire源码学习之六:用户注册
转:http://blog.csdn.net/huwenfeng_2011/article/details/43413509 用户注册 注册流程: 1.客户端进行握手给服务端发送连接消息: <s ...
- 关于swiper动态更改,无法更新的悖论
关于swiper动态更改,无法更新的悖论 以前都觉得swiper的使用很简单,那是因为使用swiper时都是写的数据,按照官网上介绍直接初始化swiper,随便丢一个地方初始化就ok了,但是在很多需求 ...
- CSS:CSS 媒体类型
ylbtech-CSS:CSS 媒体类型 1.返回顶部 1. CSS 媒体类型 媒体类型允许你指定文件将如何在不同媒体呈现.该文件可以以不同的方式显示在屏幕上,在纸张上,或听觉浏览器等等. 媒体类型 ...
- docker 镜像中心搭建
1.由于国外镜像很慢,所以用了网易蜂巢的镜像docker pull hub.c.163.com/library/registry:2.5.2本地的存储空间挂载到容器内部,持久保存镜像中心文件docke ...
- 使用linkedhashmap实现LRU(最近最少使用缓存算法)
import java.util.LinkedHashMap; import java.util.Map; public class LRUCache<K, V> extends Link ...
- Codeforces 1119E Pavel and Triangles (贪心)
Codeforces Global Round 2 题目链接: E. Pavel and Triangles Pavel has several sticks with lengths equal t ...
- kuangbin专题十三-基础计算几何
链接:https://cn.vjudge.net/contest/68968 POJ 2318 TOYS 题意:m个玩具落在n+1个区间,给你玩具的坐标,问每个区间有多少玩具. 思路:叉积的简单应用, ...