Cod sursa(job #918688)

Utilizator claudiumihailClaudiu Mihail claudiumihail Data 19 martie 2013 07:18:50
Problema Balans Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.85 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 i=1; i<=N-1; ++i)
    {
        for (int j=1; j<=M-1; ++j)
        {
            for (int r=R; r<=N; ++r)
            {
                for (int c=C; c<=M; ++c)
                {
                    long sum = mat[i+r][j+c] - mat[i+r][j] - mat[i][j+c] + mat[i][j];
                     
                    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;
}