poj 1050 To the Max
2012-06-08 16:10
295 查看
题目链接:http://poj.org/problem?id=1050
//zhaomingming 1050 Accepted 580K 63MS C++ 1356B /**思路:用 i,j表示起始行和终止行,遍历所有可能 for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) 当 i = 2; j = 4时,即在2,3,4行求解。 我们考察其中一种情况 i=2 j=4,这样就相当与选中了2 3 4三行,求那几列的组合能获得最大值,由于总是 2 3 4行,所以我们可以将这3行”捆绑”起来,变为求 4(9-4-1),11(8+2+1),-10(-6-4+0),7(7+2-2) 的最大子段和,问题成功转化为一维的情况! **/ #include <iostream> #include <cstdio> #include <string> #include <cstring> using namespace std; const int M = 1010; int a[M][M]; int n; int ans[M]; int maxx; int solve() { memset(dp,0,sizeof(dp)); maxx = -999; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(i == j) //单独处理,只有一行的情况 { for(int k = 1; k <= n; k++) ans[k] = a[i][k]; } else { for(int k = 1; k <= n; k++) { ans[k] = 0; for(int h = i; h <= j; h++) //捆绑 { ans[k] += a[h][k]; } } } //这里就是一维的,求最大连续子段和 int sum = 0,max1 = 0; for(int k = 1; k <= n; k++) { sum += ans[k]; if(sum > max1) max1 = sum; else if(sum < 0) sum = 0; } if(maxx < max1) maxx = max1; } } return 0; } int main() { cin >> n; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) cin >> a[i][j]; solve(); cout << maxx<<endl; return 0; }
相关文章推荐
- POJ 1050 To the Max DP
- POJ 1050 To the Max (最大子矩阵和)
- POJ 1050 To the Max(动态规划、最大子矩阵和)
- POJ 1050 / HDU 1081 To the Max(最大子矩阵和)
- POJ 1050 To the Max (最大子矩阵和)
- poj1050 To the Max
- [POJ 1050] To the Max DP+最大子矩阵和
- POJ 1050 To the Max (动规)
- POJ 1050 to the max(dp)
- poj 1050 To the Max(最大子矩阵之和)
- ACM学习-POJ-1050-To the Max
- POJ ---1050 To the Max
- poj-1050- To the Max
- POJ 1050 to the max最大子矩阵
- poj 1050 To the Max(动态规划处理二维最大子段和)
- POJ 1050 To the Max(DP,最大子矩阵和)
- poj 1050 To the Max_dp求最大子矩阵和
- POJ 1050 To the Max
- G - To the Max POJ - 1050 最大子矩阵和
- POJ_1050_ToTheMax