[51nod 1051 最大子矩阵和]前缀和+dp
分类:前缀和
dp
水题
1. 题目链接
2. 题意描述
一个
M∗N
的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值。
例如:3*3的矩阵:
-1 3 -1
2 -1 3
-3 1 2
和最大的子矩阵是:
3 -1
-1 3
1 2
3. 解题思路
首先,预处理出二维前缀和。然后枚举子矩阵的上下边界 lx , rx ,用前缀和按列求和,结果保存下来到b[]数组, 那么对b[]数据求一个最大字段和就得到上下边界为 lx , rx 时的最大矩阵和。枚举上下边界就得到答案。
4. 实现代码
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef unsigned int uint;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<LB, LB> PLB;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const long double PI = acos(-1.0);
const long double eps = 1e-4;
void debug() { cout << endl; }
template<typename T, typename ...R> void debug (T f, R ...r) { cout << "[" << f << "]"; debug (r...); }
template<typename T> inline void umax(T &a, T b) { a = max(a, b); }
template<typename T> inline void umin(T &a, T b) { a = min(a, b); }
template <typename T> inline bool scan_d (T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0; //EOF
while (c != '-' && (c < '0' || c > '9') ) if((c = getchar()) == EOF) return 0;
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template<typename T> void print(T x) {
static char s[33], *s1; s1 = s;
if (!x) *s1++ = '0';
if (x < 0) putchar('-'), x = -x;
while(x) *s1++ = (x % 10 + '0'), x /= 10;
while(s1-- != s) putchar(*s1);
}
template<typename T> void println(T x) { print(x); putchar('\n'); }
template<typename T> T randIntv(T a, T b) { return rand() % (b - a + 1) + a; } /*[a, b]*/
const int MAXN = 505;
LL n, m, A[MAXN][MAXN], dp[MAXN][MAXN], b[MAXN];
inline LL sum(int lx, int ly, int rx, int ry) {
return dp[rx][ry] + dp[lx - 1][ly - 1] - dp[lx - 1][ry] - dp[rx][ly - 1];
}
inline LL sum2(int lx, int ly, int rx, int ry) {
LL ret = 0;
for(int i = lx; i <= rx; ++i) {
for(int j = ly; j <= ry; ++j) {
ret += A[i][j];
}
}
return ret;
}
LL calc() {
LL cur_sum = 0, max_sum = 0;
for(int j = 1; j <= m; ++j) {
cur_sum += b[j];
if(cur_sum < 0) cur_sum = 0;
else umax(max_sum, cur_sum);
}
return max_sum;
}
int main() {
#ifdef ___LOCAL_WONZY___
freopen ("input.txt", "r", stdin);
#endif // ___LOCAL_WONZY___
while(scan_d(m) && scan_d(n)) {
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
scan_d(A[i][j]);
}
}
for(int i = 0; i <= n; ++i) dp[i][0] = 0;
for(int j = 0; j <= m; ++j) dp[0][j] = 0;
for(int i = 1; i <= n; ++i) {
LL lsum = 0;
for(int j = 1; j <= m; ++j) {
lsum += A[i][j];
dp[i][j] = dp[i - 1][j] + lsum;
}
}
LL maxs = 0;
for(int lx = 1; lx <= n; ++lx) {
for(int rx = lx; rx <= n; ++rx) {
for(int j = 1; j <= m; ++j) {
b[j] = sum(lx, j, rx, j);
}
umax(maxs, calc());
}
}
println(maxs);
}
#ifdef ___LOCAL_WONZY___
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // ___LOCAL_WONZY___
return 0;
}