# Cod sursa(job #2638871)

Utilizator Data 30 iulie 2020 13:03:04 Balans 95 cpp-64 done Arhiva de probleme 1.39 kb
``````#include <bits/stdc++.h>

using namespace std;

int N, M, R, C;

int a[301][301];
long long sp[301][301];

double sol = -1;

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

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

for (int i = 1; i <= N; ++ i ) {
for (int j = 1; j <= M; ++ j ){
scanf("%d", &a[ i ][ j ]);
a[ i + N ][ j ] = a[ i ][ j ];
a[ i ][ j + M ] = a[ i ][ j ];
a[ i + N ][ j + M ] = a[ i ][ j ];
}
}
N *= 2, M *= 2;

for (int i = 1; i <= N; ++ i ) {
for (int j = 1; j <= M; ++ j ) {
sp[ i ][ j ] = (sp[ i - 1 ][ j ] + sp[ i ][ j - 1 ] - sp[ i - 1 ][ j - 1 ] + a[ i ][ j ]);
}
}

for (int r = R; r <= N / 2; ++ r ) {
for (int c = C; c <= M / 2; ++ c ) {

int Max = 0;

for (int i = r; i <= N; ++ i ) {
for (int j = c; j <= M; ++ j ) {
int sum = sp[ i ][ j ] - sp[ i - r ][ j ] - sp[ i ][ j - c ] + sp[ i - r ][ j - c ];

if (sum > Max) {
Max = sum;
}
}
}

double rez = (double)Max / (r * c);

if (rez > sol) {
sol = rez;
}
}
}

printf("%.3f\n", sol - 0.000499);

return 0;
}
``````