poj 1050 To the Max & uva 108
来源:互联网 发布:大连理工大学网络教育 编辑:程序博客网 时间:2024/05/01 21:52
原题:
To the Max
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 45039 Accepted: 23848
Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.
As an example, the maximal sub-rectangle of the array:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:
9 2
-4 1
-1 8
and has a sum of 15.
Input
The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].
Output
Output the sum of the maximal sub-rectangle.
Sample Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1
8 0 -2
Sample Output
15
Source
Greater New York 2001
大意:
经典的最大子矩阵和问题,给你一个n*n的矩阵,问你和最大的子矩阵是多少?
#include <bits/stdc++.h>using namespace std;int table[102][102];int sum[102][102];int tmp[102];int n;int SubSeq(int a[])//最大子段和{ int ans = a[1]; int s = a[1]; for(int i = 2 ;i <= n ;i++) { if( s > 0) s += a[i]; else s = a[i]; ans = max(ans , s); } return ans;}int solve(){ memset(sum , 0 ,sizeof(sum)); int ans = INT_MIN; for(int i = n ;i >= 1; i--) { for(int j = n ;j >= 1 ;j--) sum[i][j] = sum[i+1][j] + table[i][j]; } for(int i = n ;i >= 1 ;i--) { for(int j = n+1 ;j > i ;j--) { for(int k = 1 ;k <= n ;k++) tmp[k] = sum[i][k] - sum[j][k]; ans = max(ans , SubSeq(tmp)); } } return ans;}int main(){ ios::sync_with_stdio(false); while(cin>>n) { for(int i = 1 ;i <= n ;i++) { for(int j = 1 ;j <=n ;j++) cin>>table[i][j]; } cout<<solve()<<endl; } return 0;}
解答:
本来想在小白书上随便找个贪心的题目练练手,结果找了一道做过的题目,权当是复习了。
最大子矩阵问题也不知道算不算是动态规划问题,思路参考最大子段和问题。可以这样想,给你一个n行n列的矩阵,现在问你固定列数为n时候的最大子矩阵是多少?。可以把这个n行n列的矩阵压缩成一个一维数组,也就是把列数全部相加,然后对这个数组计算最大子段和就是行数和列数分别为n的时候的最大矩阵和值,那么设置两个下标i,j代表第i行和第j行之间的矩阵,枚举i和j的值,把它们的对应列加一起算最大子段和,每次更新答案即可。
- poj 1050 To the Max & uva 108
- 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
- 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
- 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
- Office Web Apps安装部署
- Spring核心组件
- eerTcirtemmyS.101
- 排序笔记_4(自上而下、自底而上的归并排序)
- C#中的增删改查
- poj 1050 To the Max & uva 108
- 犀牛书随手记-06
- 学习笔记--数论大杂烩 (看心情更新)
- 2186: [Sdoi2008]沙拉公主的困惑 线性筛素数+欧拉函数+乘法逆元
- 2016年的希望
- HDU 4185 Oil Skimming(二分图最大匹配)
- 04环信聊天界面 - 接收方cell的布局
- Java匿名内部类浅析
- C 语言笔记