[51nod 1051 最大子矩阵和]前缀和+dp

[51nod 1051 最大子矩阵和]前缀和+dp

分类:前缀和 dp 水题

1. 题目链接

[51nod 1051 最大子矩阵和]

2. 题意描述

一个 MN 的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值。
例如: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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值