19 H cattle off more school problems

 

First, assume that this problem if it is to ask the maximum value, then this idea is definitely a monotonous stack of bare title, we came across this naked topic idea is to look at the current point can be derived up the maximum height, then according to this height, then we can operate

This information can actually be processed out of the n ^ 2, since we deal with the longest-derived upward, then only need to enumerate the next on ok, do not need the last enumeration, enumeration, then we put the next question head into a simple model of a monotonous stack

 

So this monotonous stack model of how to operate it, this is a problem, we can see that what we want is the second largest value, the second largest value is from there to produce it, a monotonous current stack processing maximum, there is a possibility of a reduction of the current maximum width, this is based on the characteristics of the stack to get monotonous, we need to keep in mind, and for that matter, I was wrong several times (for such a problem, how do it? ??????? the most important is the nature of thinking, that is, for this problem is in the end how these values ​​are generated, resulting from there, Here is the code)

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <cmath>
 6 #include <bitset>
 7 #include <stack>
 8 typedef long long ll;
 9 using namespace std;
10 const int maxn=1100;
11 int n,m;
12 char s[maxn][maxn];
13 int h[maxn][maxn];
14 int ai[maxn],siz[maxn];
15 int cnt[maxn*maxn*2];
16 int tot=0;
17 stack<int> sta;
18 
19 void solve(){
20     ai[0]=0;ai[m+1]=0;
21     for(int i=1;i<=m;i++)  siz[i]=1;
22     sta.push(0);
23     for(int i=1;i<=m+1;i++){
24         if(ai[i]>ai[sta.top()]){
25             sta.push(i);
26         }else{
27             int len=0;
28             while(!sta.empty()&&ai[sta.top()]>ai[i]){
29                 len+=siz[sta.top()];
30                 cnt[++tot]=len*ai[sta.top()];
31                 if(len>1) cnt[++tot]=(len-1)*ai[sta.top()];
32                 sta.pop();
33             }
34             sta.push(i);siz[i]=len+1;
35         }
36     }
37 }
38 
39 int main(){
40     scanf("%d%d",&n,&m);
41     for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
42     for(int i=1;i<=n;i++){
43         for(int j=1;j<=m;j++){
44             if(s[i][j]=='0') h[i][j]=0;
45             else h[i][j]=h[i-1][j]+1;
46         }
47     }
48 
49     for(int i=1;i<=n;i++){
50         for(int j=1;j<=m;j++) ai[j]=h[i][j];
51         solve();
52     }
53     sort(cnt+1,cnt+tot+1);
54     if(tot<2) printf("0\n");
55     else printf("%d\n",cnt[tot-1]);
56     return 0;
57 }
View Code

Guess you like

Origin www.cnblogs.com/pandaking/p/12114592.html