2019牛客暑期多校训练营(第二场)

H Second Large Rectangle

前天打比赛唯一的收获就是单调栈,然后这个题还没做出来,主要时没想到把它搞成直方图模拟,一直在推右下角作为最大矩形的dp,搞成直方图就很简单了,和poj那个入门单调栈题一模一样,只不过这个是有i层,还有一个点时每次找到最大的要对长-1还有宽-1,那么才能保证不丢失次大值。

好菜啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int MAXN=1000+5;
 4 typedef long long ll;
 5 int G[MAXN][MAXN];
 6 
 7 
 8 
 9 int main(){
10     int m,n;cin>>m>>n;
11     for(int i=0;i<m;i++) 
12     {
13         char s[MAXN];
14         scanf("%s",s);
15         for(int j=0;j<n;j++)
16         {
17             
18             if(i==0) 
19             {
20                 G[i][j]=s[j]-'0';
21                 continue;
22             }
23             if(s[j]=='1') 
24                 G[i][j]=G[i-1][j]+1;
25             else G[i][j]=0;
26         }
27     }
28     priority_queue <int>  D;
29     for(int i=0;i<m;i++)
30     {
31         stack<int>S;S.push(0);
32         int num[MAXN];
33         memset(num,0,sizeof(num));num[0]=1;
34         for(int j=1;j<=n;j++)
35         {
36             int tmp=1;G[i][n]=-1;
37             while(!S.empty()&&G[i][j]<=G[i][S.top()])
38             {
39                 tmp+=num[S.top()];
40                 int sq=(num[S.top()]+(j-S.top()-1))*G[i][S.top()];
41                 int sa=(num[S.top()]+(j-S.top()-1)-1)*G[i][S.top()];
42                 int sb=(num[S.top()]+(j-S.top()-1))*(G[i][S.top()]-1);
43                 S.pop();
44                 if(sq>0) D.push(sq);
45                 if(sa>0) D.push(sa);
46                 if(sb>0) D.push(sb); 
47             }
48             S.push(j);
49             num[j]=tmp;
50         }
51     }
52     if(D.size()<2)
53     {
54         cout <<0<<endl;
55     }else
56     {
57         D.pop();
58         //D.erase(it);
59         cout <<D.top();    
60     }
61     return 0;
62 }

 

 

 

 

posted @ 2019-07-20 23:35  Chuhanjing  阅读(370)  评论(0编辑  收藏  举报