这道题使用逐行的histogram算法即可求解。
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int histogram(vector<int> &L){
vector<int> tmp(L.size());
L[0] = tmp[0];
int Max = L[0];
//histogram algorithm start
for(int i = 1; i < L.size(); i++){
tmp[i] = L[i];
for(int j = i - 1; j >= 0; j--){
if(tmp[j] > tmp[i]){
Max = max(tmp[j] * (i - j), Max);
tmp[j] = tmp[i];
}else break;
}
}
for(int i = 0; i < L.size(); i++)
Max = max(tmp[i] * ((int)(L.size()) - i), Max);
return Max;
}
int fuck(vector<vector<int> > &mat){
int ans = 0;
vector<int> tmp(mat[0].size());
for(auto &i : mat){ //转成直方图的形式
for(int j = 0; j < i.size(); j++){
tmp[j] += i[j];
if(!i[j]) tmp[j] = 0;
}
ans = max(histogram(tmp), ans);
//--------debug--------
cout << "本行ans:" << ans << "\ttmp[]=";
for(auto i : tmp) cout << i << ' ';
cout << endl;
//---------------------
}
return ans;
}
int main(void){
int cnt_h, cnt_w;
cin >> cnt_h >> cnt_w;
vector<vector<int> > rect(cnt_h);
for(int i = 0; i < cnt_h; i++){
for(int j = 0; j < cnt_w; j++){
int n;
cin >> n;
rect[i].push_back(n);
}
}
cout << fuck(rect) << endl;
return 0;
}