最大子矩阵和 URAL 1146 Maximum Sum

题目传送门

 1 /*
 2     最大子矩阵和:把二维降到一维,即把列压缩;然后看是否满足最大连续子序列;
 3     好像之前做过,没印象了,看来做过的题目要经常看看:)
 4 */
 5 #include <cstdio>
 6 #include <iostream>
 7 #include <cstring>
 8 #include <algorithm>
 9 using namespace std;
10
11 const int MAXN = 1e2 + 10;
12 const int INF = 0x3f3f3f3f;
13 int a[MAXN][MAXN];
14 int dp[MAXN][MAXN][MAXN];
15
16 int main(void)        //URAL 1146 Maximum Sum
17 {
18     //freopen ("D.in", "r", stdin);
19
20     int n;
21     while (scanf ("%d", &n) == 1)
22     {
23         int ans = -INF;
24         memset (dp, 0, sizeof (dp));
25         for (int i=1; i<=n; ++i)
26         {
27             for (int j=1; j<=n; ++j)
28             {
29                 scanf ("%d", &a[i][j]);
30             }
31         }
32
33         for (int i=1; i<=n; ++i)
34         {
35             for (int j=1; j<=n; ++j)
36             {
37                 int sum = 0;
38                 for (int k=j; k>=1; --k)
39                 {
40                     sum += a[i][k];
41                     dp[i][j][k] = max (sum + dp[i-1][j][k], sum);
42                     ans = max (ans, dp[i][j][k]);
43                 }
44             }
45         }
46
47         printf ("%d\n", ans);
48     }
49
50     return 0;
51 }
时间: 2024-10-14 18:59:47

最大子矩阵和 URAL 1146 Maximum Sum的相关文章

URAL 1146 Maximum Sum(最大子矩阵的和 DP)

Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最开始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4),就不知道该怎么办了.问了一下,是压缩矩阵,转换成最大字段和的问题. 压缩行或者列都是可以的. 1 int n, m, x, y, T, t; 2 int Map[1010][1010]; 3 4 int main() 5 { 6 while(~scanf("%d", &n)) 7 { 8 memset(Map, 0, siz

URAL 1146. Maximum Sum(求最大子矩阵和)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1146 1146. Maximum Sum Time limit: 0.5 second Memory limit: 64 MB Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is

ural 1146. Maximum Sum

1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. In this p

ural 1146 Maximum Sum 最大连续和

1146. Maximum Sum Time limit: 0.5 second Memory limit: 64 MB Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. In this

Ural 1146 Maximum Sum(DP)

题目地址:Ural 1146 这题是求最大子矩阵和.方法是将二维转化一维. 首先用n*n的方法来确定矩阵的列.需要先进行预处理,只对每行来说,转化成一维的前缀和,这样对列的确定只需要前后两个指针来确定,只需要用前缀和相减即可得到.前后两个指针用n*n的枚举. 确定好了哪几列,那么再确定行的时候就转化成了一维的最大连续子序列的和.再来一次O(n)的枚举就可以. 这样,总复杂就变成了O(n^3),对于n为100来说,已经足够了. 代码如下: #include <iostream> #include

Timus 1146. Maximum Sum

1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. In this p

maximum sum

uva,10684 1 #include <iostream> 2 #include <cstdio> 3 #define maxn 10005 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 int a[maxn]; 10 while(scanf("%d",&n),n) 11 { 12 for(int i=0;i<n;i++) 13 scanf("%d",&a[

UVa 108 - Maximum Sum(最大连续子序列)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=44  Maximum Sum  Background A problem that is simple to solve in one dimension is often much more difficult to solve in more th

UVA108 - Maximum Sum(最大连续和)

题意:从一个n*n的矩阵中找出和最大的子矩阵 思路:最大连续和的求解.我们可以将二维的转化为一维进行计算.sum[i][j]表示以(1, 1)和(i, j)为对角的矩阵的和.之后只要逐个枚举每个可能出现的值,保存最大值即可. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x3