标签:dp
#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <climits> #include <cstring> #include <cmath> #include <map> #include <set> using namespace std; int n; int a[105][105]; int dp[105][105]; int sum[105][105]; int main(){ while(scanf("%d",&n)!=EOF){ for(int i = 1;i <= n;i++){ for(int j = 1;j <= n;j++){ scanf("%d",&a[i][j]); } } memset(dp,0,sizeof(dp)); for(int j = 1;j <= n;j++){ // j列的前缀和 for(int i = 1;i <= n;i++){ sum[j][i] = sum[j][i-1] + a[i][j]; } } for(int i = 1;i <= n;i++){ for(int j = i;j <= n;j++){ int right = 0; for(int k = 1;k <= n;k++){ if(right >= 0){ right += sum[k][j] - sum[k][i-1]; } else{ right = sum[k][j] - sum[k][i-1]; } dp[i][j] = max(dp[i][j],right); } } } int ans = -INT_MAX; for(int i = 1;i <= n;i++){ for(int j = i;j <= n;j++){ if(dp[i][j] > ans){ ans = dp[i][j]; } } } cout << ans << endl; } return 0; }
标签:dp
原文地址:http://blog.csdn.net/qq_24667639/article/details/45154385