Cod sursa(job #1160873)

Utilizator j.loves_rockJessica Joanne Patrascu j.loves_rock Data 30 martie 2014 21:17:19
Problema Balans Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <algorithm>
#include <cstdio>

#define MAX_SIZE 305

using namespace std;

static long mat[MAX_SIZE][MAX_SIZE];

int main()
{
    int N, M, R, C;
    freopen ( "balans.in" , "r" , stdin ) ;
    freopen ( "balans.out" , "w" , stdout ) ;

    double flMaxBalans = 0.0;

    scanf ( "%ld %ld %ld %ld" , &N , &M , &R, &C) ;


    if (R == 0)
    {
        R = 1;
    }
    if (C == 0)
    {
        C = 1;
    }

    for (int i=1; i<=N; ++i)
    {
        for (int j=1; j<=M; ++j)
        {
            scanf("%ld", &mat[i][j]);
            mat[i+N][j] = mat[i][j];
            mat[i][j+M] = mat[i][j];
            mat[i+N][j+M] = mat[i][j];
        }
    }

    for (int i=1; i<=2*N; ++i)
    {
        for (int j=1; j<=2*M; ++j)
        {
            mat[i][j] = mat[i][j] + mat[i-1][j] + mat[i][j-1] - mat[i-1][j-1];
        }
    }

    for (int r=R; r<=N; ++r)
    {
        for (int c=C; c<=M; ++c)
        {
            for (int i=N+1; i<=2*N; ++i)
            {
                for (int j=M+1; j<=2*M; ++j)
                {
                    long sum = mat[i][j] - mat[i-r][j] - mat[i][j-c] + mat[i-r][j-c];

                    if (sum > flMaxBalans * r * c)
                    {
                        flMaxBalans = (double)sum / (r*c);
                    }
                }
            }
        }
    }

    printf("%.3lf", ( long ) ( flMaxBalans * 1000 ) /  1000.0 ) ;

    return 0;
}