51nod1051_DP求最大子矩阵

题目链接:最大子矩阵和
题目描述当所有数都为负数时输出0,意味着除了都为0的情况,总存在一个数>=0,即结果一定>=0,所以当中间结果tmp<0时,后面加它都必然使得结果更小,意味着上面的段不用,tmp置为0,从下一段重新开始寻找可能的最大值。转化为最大子段和求解。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<cstdlib>
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
using namespace std;
const int maxn = 510;
int x,y;
ll dp[maxn][maxn],num;
int main(){
    ll ans;
    while(scanf("%d%d",&x,&y) != EOF){
        mem(dp,0);
        for(int i = 1; i <= y; i++){
            for(int j = 1; j <= x; j++){
                scanf("%lld",&num);
                dp[i][j] = dp[i-1][j] + num;
            }
        }
        ans = 0;
        ll tmp;
        for(int i = 1; i <= y; i++){
            for(int j = i; j <= y; j++){
                tmp = 0;
                for(int k = 1; k <= x; k++){
                    tmp += dp[j][k] - dp[i-1][k];
                    if(tmp < 0) tmp = 0;
                    if(tmp > ans) ans = tmp;
                }
            }
        }
        printf("%lld\n",ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值