第二大矩阵面积--(stack)牛客多校第二场-- Second Large Rectangle

题意:

给你一幅图,问你第二大矩形面积是多少。

思路:

直接一行行跑stack求最大矩阵面积的经典算法,不断更新第二大矩形面积,注意第二大矩形可能在第一大矩形里面。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 #define mem(a,b) memset(a,b,sizeof(a))
 24 #define fo(a,b,c) for(a=b;a<=c;++a)//register int i
 25 #define fr(a,b,c) for(a=b;a>=c;--a)
 26 #define pr printf
 27 #define sc scanf
 28 void swapp(int &a,int &b);
 29 double fabss(double a);
 30 int maxx(int a,int b);
 31 int minn(int a,int b);
 32 int Del_bit_1(int n);
 33 int lowbit(int n);
 34 int abss(int a);
 35 //const long long INF=(1LL<<60);
 36 const double E=2.718281828;
 37 const double PI=acos(-1.0);
 38 const int inf=(1<<29);
 39 const double ESP=1e-9;
 40 const int mod=(int)1e9+7;
 41 const int N=(int)1003;
 42  
 43 struct node
 44 {
 45     int h,l;
 46 };
 47 stack<node> S;
 48 int n,m;
 49 int Nextl[N][N];
 50 char mp[N][N];
 51 //-----------------------------------------------------
 52  
 53 int main()
 54 {
 55     sc("%d%d",&n,&m);
 56     for(int i=1;i<=n;++i)
 57         sc("%s",mp[i]+1);
 58     for(int j=1;j<=m;++j)
 59     {
 60         int pos=n;
 61         for(int i=n;i>=1;--i)
 62         {
 63             if(mp[i][j]=='0')
 64                 pos=-1;
 65             else
 66             {
 67                 if(pos==-1)
 68                     Nextl[i][j]=1,pos=i;
 69                 else
 70                     Nextl[i][j]=pos-i+1;
 71             }
 72         }
 73     }
 74     int ans1=0,ans2=0;
 75     int ll=0,rr=0;
 76     for(int i=1;i<=n;++i)
 77     {
 78         for(int j=1;j<=m;++j)
 79         {
 80             node temp;
 81             temp.h=Nextl[i][j];
 82             temp.l=1;
 83             if(S.empty())
 84                 S.push(temp);
 85             else
 86             {
 87                 int L=0;
 88                 while(!S.empty()&&temp.h<=S.top().h)
 89                 {
 90                     L+=S.top().l;
 91                     int t=L*S.top().h;
 92                     if(t>ans1)
 93                     {
 94                         ans2=ans1;
 95                         ll=L,rr=S.top().h;
 96                         ans1=t;
 97                     }
 98                     else
 99                     {
100                         if(t>=ans2)
101                             ans2=t;
102                     }
103                     S.pop();
104                 }
105                 temp.l+=L;
106                 S.push(temp);
107             }
108         }
109         int L=0;
110         while(!S.empty())
111         {
112             L+=S.top().l;
113             int t=L*S.top().h;
114             if(t>ans1)
115             {
116                 ll=L,rr=S.top().h;
117                 ans2=ans1;
118                 ans1=t;
119             }
120             else
121             {
122                 if(t>=ans2)
123                     ans2=t;
124             }
125             S.pop();
126         }
127     }
128     pr("%d\n",maxx(ans2,maxx((ll-1)*rr,(rr-1)*ll)));
129     return 0;
130 }
131  
132 /**************************************************************************************/
133  
134 int maxx(int a,int b)
135 {
136     return a>b?a:b;
137 }
138  
139 void swapp(int &a,int &b)
140 {
141     a^=b^=a^=b;
142 }
143  
144 int lowbit(int n)
145 {
146     return n&(-n);
147 }
148  
149 int Del_bit_1(int n)
150 {
151     return n&(n-1);
152 }
153  
154 int abss(int a)
155 {
156     return a>0?a:-a;
157 }
158  
159 double fabss(double a)
160 {
161     return a>0?a:-a;
162 }
163  
164 int minn(int a,int b)
165 {
166     return a<b?a:b;
167 }

猜你喜欢

转载自www.cnblogs.com/--HPY-7m/p/11436933.html