动态规划 最大子矩阵

题目:动态规划求解最大子矩阵问题


动态规划基本类型


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
const double pi = acos(-1.0);
///////////////////////////////

LL st[504][504];
LL temp[504];
LL sum[504][504];
int m,n;
long long maxarr(){
	long long res=0;
	long long te=0;
	for(int i=1;i<=m;i++){
		te+=temp[i];
		if(te<0) te=0;
		if(te>res) res=te;
	}
	
	return res;
}




///////////////////////////////
int main(int argc, char**argv) {
	ios_base::sync_with_stdio(0); cin.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	////////////////////////////
	

	cin>>m>>n;
	
	
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>st[i][j];
		}
	}
	
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			sum[i][j]=st[i][j]+sum[i-1][j];
			//cout<<sum[i][j]<<" ";
		}
	}
	
	
	long long ans=0;
	
	for(int i=1;i<=n;i++){
		for(int j=i;j<=n;j++){
			memset(temp,0,sizeof(temp));
			for(int k=1;k<=m;k++){
				temp[k]=sum[j][k]-sum[i-1][k];
				//cout<<temp[k];
			}
			LL tp=maxarr();
			//cout<<tp<<endl;
			if(tp>ans) ans=tp;
		}
	}
	cout<<ans<<endl;
	
	
	////////////////////////////
	//system("pause");
	return 0;

}
//END


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值