最大長方形
2 x 2マスの部分を見ると解法が自明だが、強実装で辛い
障害がマスではなく点の最大長方形問題になる
- #include<iostream>
- #include<algorithm>
- #include<vector>
- #include<queue>
- #define lol(i,n) for(int i=0;i<n;i++)
- #define mod 1000000007
- typedef long long ll;
- using namespace std;
- int dp[2010];
- int c[2010][2010];
- #include<stack>
- int BiggestRectangle(int N,int buf[]){
- buf[N]=0;
- stack<pair<int,int> >st;
- int ret=0;
- for(int i=0;i<=N;i++){
- if(st.empty())st.push(make_pair(buf[i],i));
- else if(st.top().first<buf[i])st.push(make_pair(buf[i],i));
- else if(st.top().first>buf[i]){
- int pnt=i;
- while(!st.empty()){
- int height=st.top().first;
- int index=st.top().second;
- if(height<buf[i])break;
- st.pop();
- ret=max(ret,height*(i-index+1));
- pnt=index;
- }
- st.push(make_pair(buf[i],pnt));
- }
- }
- return ret;
- }
- int main(){
- int h,w;
- cin>>h>>w;
- lol(i,h){
- lol(j,w){
- char z;cin>>z;
- if(z=='.')c[i][j]=0;
- if(z=='#')c[i][j]=1;
- }
- }
- for(int i=0;i<h-1;i++){
- for(int j=0;j<w-1;j++){
- int tmp=c[i][j]+c[i+1][j];
- tmp+=c[i][j+1]+c[i+1][j+1];
- c[i][j]=tmp%2;
- }
- }
- int ans=max(h,w);
- for(int j=0;j<w-1;j++)dp[j]=1;
- for(int i=0;i<h-1;i++){
- for(int j=0;j<w-1;j++){
- if(c[i][j]==true)dp[j]=1;
- else dp[j]++;
- }
- ans=max(ans,BiggestRectangle(w-1,dp));
- }
- cout<<ans<<endl;
- return 0;
- }
部分問題は2つセットの場合がある