Cod sursa(job #2854439)

Utilizator Cazacu2006RazvanRazvan Cazacu Cazacu2006Razvan Data 21 februarie 2022 13:30:09
Problema Teren Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <fstream>

using namespace std;
ifstream fin("teren.in");
ofstream fout("teren.out");
int m,n,x,a[301][301],sum[301][301],amax;
int main()
{
    ///sume partiale pe coloane
    fin>>n>>m>>x;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            fin>>a[i][j];
            //sum[i][j]=a[i][j]+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
        }
    }
    for(int j=1;j<=m;j++)
    {
        for(int i=1;i<=n;i++)
        {
            sum[i][j]=a[i][j]+sum[i-1][j];

        }
    }


    for(int l1=1;l1<=n;l1++)
    {
        for(int l2=l1;l2<=n;l2++)
        {
            int st=1,s=0;
            for(int dr=1;dr<=m;dr++)
            {
                s+=sum[l2][dr]-sum[l1-1][dr];
                while(s>x && st<=dr)
                {
                    st++;
                    s-=(sum[l2][st-1]-sum[l2][st-2]);
                }
                if(s<=x)
                {
                    int l=(l2-l1+1)*(dr-st+1);
                    if(l>amax)
                    {
                        amax=l;
                    }
                }
            }
        }
    }
    fout<<amax;
    return 0;
}