Cod sursa(job #2031343)

Utilizator Vlad3108Tir Vlad Ioan Vlad3108 Data 3 octombrie 2017 01:17:15
Problema BMatrix Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <bits/stdc++.h>
using namespace std;
/// DREPTUNGHI DE ARIE MAXIMA SUB HISTOGRAMA
int MaxArea(int hist[], int n){
    stack<int> s;
    int Max=0,i=0;
    while(i<n){
        if(s.empty()||hist[s.top()]<=hist[i])
            s.push(i++);
        else{
            int top=s.top();
            s.pop();
            int Area;
            if(s.empty()) Area=hist[top]*i;
            else Area= hist[top]*(i-s.top()-1);
            if(Area>Max)
                Max=Area;
        }
    }
    while(!s.empty()){
        int top=s.top();
        s.pop();
        int Area;
        if(s.empty()) Area=hist[top]*i;
        else Area=hist[top]*(i-s.top()-1);
        if(Area>Max) Max=Area;
    }
    return Max;
}
int Largest(char A[205][205],int l1,int c1,int l2,int c2){
    int v[205]={0};
    int Max=-1;
    for(int i=l1;i<=l2;++i){
        for(int j=c1;j<=c2;++j)
            if(A[i][j]=='0') ++v[j-c1];
            else v[j-c1]=0;
        int Sol=MaxArea(v,c2+1-c1);
        if(Sol>Max)
            Max=Sol;
    }
    return Max;
}
char A[205][205];
int main(){
    freopen("bmatrix.in","r",stdin);
    freopen("bmatrix.out","w",stdout);
    int n,m;
    scanf("%d %d\n",&n,&m);
    for(int i=1;i<=n;++i)
        fgets(A[i]+1,205,stdin);
    int Max=-1;
    /// CAZ 1 TAIETURA VERTICALA
    for(int k=0;k<=m/2;++k){
        int Sol=Largest(A,1,1,n,k)+Largest(A,1,k+1,n,m);
        if(Sol>Max)
            Max=Sol;
    }
    /// CAZ 2 TAIETURA ORIZONTALA
    for(int k=0;k<=n/2;++k){
        int Sol=Largest(A,1,1,k,m)+Largest(A,k+1,1,n,m);
        if(Sol>Max)
            Max=Sol;
    }
    printf("%d\n",Max);
    return 0;
}