#include<cstdio> #include<cstring> #include<iostream> #define db(a) cout << #a << " = " << a << endl #define db2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl #define db3(a, b, c) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << endl using namespace std; int main() { #ifdef dennisbot freopen("in.in", "r", stdin); //freopen("ou.out", "w", stdout); #endif int m ,n; scanf("%d", &m); while (m --) { scanf("%d", &n); int matriz[n][n], fila[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &matriz[i][j]); } } /*for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j != 0) cout << " "; cout << matriz[i][j]; } cout << endl; }*/ int sum = 0, sum_min = 0,total = 0, var, max = -1000, min = 1000; for (int i = 0; i < n; i++) { memset(fila, 0, sizeof fila); for (int ii = i; ii < i + n; ii++) { sum = sum_min = total = 0; min = 1000; for (int j = 0; j < n; j++) { fila[j] += matriz[ii % n][j]; total += fila[j]; if (sum >= 0) { sum += fila[j]; } else sum = fila[j]; if (sum_min <= 0) { sum_min += fila[j]; } else sum_min = fila[j]; if (sum > max) max = sum; if (sum_min < min) min = sum_min; } max = std::max(max, total - min); } } printf("%d\n", max); } return 0; }
martes, 17 de abril de 2012
10827 - Maximum sum on a torus, uva
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario