Cod sursa(job #918683)

Utilizator claudiumihailClaudiu Mihail claudiumihail Data 19 martie 2013 03:37:13
Problema Balans Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 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 ) ;
    //fstream fout("balans.out", fstream::out);
    freopen ( "balans.out" , "w" , stdout ) ;
    
    double flMaxBalans = 0.0;

    scanf ( "%ld %ld %ld %ld" , &N , &M , &R, &C) ;
    //cout << N << " " << M << " " << R << " " << C << endl;
    
    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)
        {
            cout << mat[i][j] << " ";
        }
        cout << endl;
    }*/
    
    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);
                    }*/
                    flMaxBalans = max(flMaxBalans, (double)sum / (r*c));
                }
            }
        }
    }
    
    printf("%.3lf", ( long ) ( flMaxBalans * 1000 ) /  1000.0 ) ;
    
    return 0;
}