Cod sursa(job #2031353)

Utilizator Vlad3108Tir Vlad Ioan Vlad3108 Data 3 octombrie 2017 01:34:33
Problema BMatrix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <bits/stdc++.h>
using namespace std;
/// DREPTUNGHI DE ARIE MAXIMA SUB HISTOGRAMA
int st[205];
int MaxArea(int hist[], int n){
    int Max=0,i=0,top=0;
    while(i<n){
        if(!top||hist[st[top]]<=hist[i])
            st[++top]=i++;
        else{
            int tp=st[top];
            --top;
            int Area;
            if(!top) Area=hist[tp]*i;
            else Area=hist[tp]*(i-st[top]-1);
            if(Area>Max)
                Max=Area;
        }
    }
    while(top){
        int tp=st[top];
        --top;
        int Area;
        if(!top) Area=hist[tp]*i;
        else Area=hist[tp]*(i-st[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;
}