2019牛客暑期多校训练营(第二场)H Second Large Rectangle(第二大子矩阵)

代码已注释

AC代码:

语言:C++ 代码长度:2117 运行时间: 32 ms 占用内存:4572K

 1 #include<bits/stdc++.h>
 2 #define numm ch-48
 3 #define pd putchar(' ')
 4 #define pn putchar('\n')
 5 #define pb push_back
 6 #define mp make_pair
 7 #define fi first
 8 #define se second
 9 #define fi first
10 #define se second
11 #define fre1 freopen("1.txt","r",stdin)
12 #define fre2 freopen("2.txt","w",stdout)
13 using namespace std;
14 template <typename T>
15 void read(T &res) {
16     bool flag=false;char ch;
17     while(!isdigit(ch=getchar())) (ch=='-')&&(flag=true);
18     for(res=numm;isdigit(ch=getchar());res=(res<<1)+(res<<3)+numm);
19     flag&&(res=-res);
20 }
21 template <typename T>
22 void write(T x) {
23     if(x<0) putchar('-'),x=-x;
24     if(x>9) write(x/10);
25     putchar(x%10+'0');
26 }
27 const int maxn=1010;
28 typedef long long ll;
29 typedef long double ld;
30 const ll mod=1e9+7;
31 const int inf=0x3f3f3f3f;
32 int a[maxn][maxn];
33 char s[maxn];
34 int h[maxn];    ///当前行往上能延伸的最大高度
35 int main()
36 {
37     int n,m;
38     read(n),read(m);
39     for(int i=1;i<=n;i++) {
40         scanf("%s",s);
41         for(int j=0;j<m;j++)
42             if(s[j]=='1') a[i][j+1]=1;
43     }
44 
45     int max1=0,max2=0;
46     stack<int>sta;  ///单调栈,栈内储存下标
47     while(!sta.empty()) sta.pop();
48 
49     for(int i=1;i<=n;i++) {
50         for(int j=1;j<=m;j++)   ///如果为1,就累加,否则清零,因为无法往上延伸
51             h[j]=(a[i][j]?(h[j]+1):0);
52 
53         while(!sta.empty()) sta.pop();
54         h[++m]=0;   ///保证最后单调栈的计算
55         sta.push(0);
56         for(int j=1;j<=m;j++) {
57             while(!sta.empty()&&h[j]<h[sta.top()]) {
58                 
59                 int x=h[sta.top()];///第j列的高度不符合要求
60                 sta.pop();  ///pop掉,找出
61                 ///计算当前sta.top()+1(h[sta.top()+1]肯定大于等于x)的到j-1的矩阵
62                 int y=j-1-sta.top();
63                 int ans=x*y,ans1=(x-1)*y,ans2=x*(y-1);
64                 if(max2<ans)  swap(ans,max2);
65                 if(max1<max2) swap(max1,max2);
66 
67                 if(max2<ans1) swap(ans1,max2);
68                 if(max1<max2) swap(max1,max2);
69 
70                 if(max2<ans2) swap(ans2,max2);
71                 if(max1<max2) swap(max1,max2);
72             }
73             sta.push(j);
74         }
75         --m;
76     }
77     write(max2);pn;
78     return 0;
79 }
代码在这里!

猜你喜欢

转载自www.cnblogs.com/wuliking/p/11253470.html