Cod sursa(job #3163298)

Utilizator apoputoaievladVlad Cristian Apoputoaie apoputoaievlad Data 31 octombrie 2023 11:00:26
Problema Teren Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <bits/stdc++.h>

using namespace std;
/**
3 5 1
0 0 0 0 1
0 1 0 0 0
0 0 0 1 0
x=8
t=1 5 2 3 1 2 4 3

*/
ifstream fin("teren.in");
ofstream fout("teren.out");
int n,m,x;
int a[304][304],t[304];
int main()
{
    int i,j,k,p1,p2,amax=0,lm,sum;
    fin>>n>>m>>x;
    for(i=1; i<=n; i++)
        for(j=1; j<=m; j++)
        {
            fin>>a[i][j];
            a[i][j]+=a[i-1][j];
        }
    for(i=1; i<=n; i++)
        for(k=i; k<=n; k++)
        {

            for(j=1; j<=m; j++)
                t[j]=a[k][j]-a[i-1][j];
            /// aflam lungimea max a unei secv de sum <= x
            lm=0;
            sum=0;
            p1=1;
            for(p2=1;p2<=m;p2++)
            {
                sum+=t[p2];
                while(sum>x)
                {
                    sum-=t[p1];
                    p1++;
                }
                lm=max(lm,p2-p1+1);
            }
            amax=max(amax,lm*(k-i+1));
        }
    fout<<amax;
    return 0;
}