/*
translation:
给出一个矩阵,要求求出一个子矩阵,问这个子矩阵最大的和是多少?
solution:
降维,前缀和
note:
* 将原来的矩阵复制成4份后再组合成一个大矩阵就方便处理了。然后枚举矩阵的范围,复杂度为75*75*75*75
枚举出来后,求出每一列上下界之间的和(利用前缀和可以常数时间内求出)。降维成1维后就是求最大子序列的
和的问题了。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 80;
int mat[maxn * 4][maxn * 4], n;
int sum[maxn * 4];
int main()
{
//freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
mat[i+n][j] = mat[i][j+n] = mat[i+n][j+n] = mat[i][j];
}
}
//get sumy
int ans = -2147483645;
for(int x = 0; x < n; x++) {
for(int y = 0; y < n; y++) {
memset(sum, 0, sizeof(sum));
for(int i = x; i < x + n; i++) {
int res = 0;
for(int j = y; j < n + y; j++) {
sum[j] += mat[i][j];
res += sum[j];
if(res > ans) {
ans = res;
}
}
}
}
}
printf("%d\n", ans);
}
return 0;
}
uva10827(*前缀和,降维)
最新推荐文章于 2022-08-04 20:30:47 发布