URAL1146——DP——Maximum Sum

Description

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 problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle. A sub-rectangle is any contiguous sub-array of size 1 × 1 or greater located within the whole array.

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-hand corner and has the 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 white-space (newlines and spaces). These N 2 integers make up the array in row-major order (i.e., all numbers on the first row, left-to-right, then all numbers on 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

The output is the sum of the maximal sub-rectangle.

Sample Input

input output
4
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
15

大意:问你最大的矩阵和,坑死我了WA了n发。。原来做的方法错了。没有考虑当答案为负值的时候

dp[i][j][k]表示i行j列从k个元素开始往前的和,三重循环保证所有情况都遍历到

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[150][150][150];
int a[150][150];
const int inf = 0x3f3f3f3f;
int main()
{
    int n;
    while(~scanf("%d",&n)){
        for(int i = 1; i <= n ; i++)
            for(int j = 1; j <= n ;j++)
                scanf("%d",&a[i][j]);
        memset(dp,0,sizeof(dp));
        int ans = -inf;
        for(int i = 1; i <= n ; i++){
            for(int j = 1; j <= n ;j++){
                int sum = 0;
                for(int k = j; k >= 1; k--){
                    sum += a[i][k];
                    dp[i][j][k] = max(sum + dp[i-1][j][k],sum);
                    ans = max(ans,dp[i][j][k]);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

错误代码也来一发....祭奠我8个WA一个CE..

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int a[150][150];
    int n,m;
    scanf("%d",&n);
    for(int i = 1; i <= n ;i++){
        for(int j = 1; j <= n ;j++){
         scanf("%d",&m);
         a[i][j] = a[i-1][j] + m;
        }
    }

    int max1 = 0;
    int sum;
    for(int i = 1; i <= n ;i++){
        for(int j = i+1 ; j<= n ;j++){
            int sum = 0;
            for(int k = 1; k <= n;k++){
              int temp = a[j][k] - a[i][k];
               sum += temp;
              if(sum < 0) sum = 0;
              if(sum  > max1) max1 = sum;
            }
        }
    }
    printf("%d",max1);
   return 0;
}

  

时间: 2024-12-27 08:50:08

URAL1146——DP——Maximum Sum的相关文章

URAL1146 &amp; POJ1050 Maximum Sum (最大连续子序列和)

Description 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 problem the sub-rectangle with the largest sum is

POJ2479 Maximum sum[DP|最大子段和]

Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39599   Accepted: 12370 Description Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: Your task is to calculate d(A). Input The input consists o

【UVA】108 - Maximum Sum(DP矩阵)

n^3的复杂度计算最小子矩阵,用了最大连续和的DP算法. 14273282 108 Maximum Sum Accepted C++ 0.013 2014-09-27  #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int INF = 1 << 30; const int maxn = 127 + 3; int mat[maxn][maxn]

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

Poj 2479 Maximum sum【双向DP/最大连续和】

Maximum sum 题意:给定一个长度为N的数组,求两个连续的子序列,使得两个连续子序列的和最大. 分析:乍一看,跟最大连续和有点类似,但是,又有区别,因为对于这个题,考虑第i项两个连续子序列的最大和,不能仅仅由前i-1项递推得出,第i项两个连续子序列的最大和,与前i项和i以后的之间是存在关系的,因此这个题目是一个双向dp. 假如给定的序列为a0, a1, a2, a3, a4, ...... ,an,那么,对于任意第i项,我以第i个元素为分界点,用一个数组项pMax [i]来保存  区间

Codeforces 75D Big Maximum Sum 最大子段和 dp

题目链接:点击打开链接 题意: 第一行 n m n个vector 下面n行 第一个数字u表示vector 的大小,然后后面u个数字给出这个vector 最后一行m个数字 表示把上面的vector拼接起来 得到一个大序列,求这个大序列的最大子段和 先预处理出每个vector的最大子段和,左起连续最大,右起连续最大,所有数的和 然后dp 一下.. #include <cstdio> #include <iostream> #include <algorithm> #incl

POJ 2479 Maximum sum(双向DP)

Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36100   Accepted: 11213 Description Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: Your task is to calculate d(A). Input The input consists o

1481:Maximum sum

1481:Maximum sum 总时间限制:  1000ms 内存限制:  65536kB 描述 Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: t1 t2 d(A) = max{ ∑ai + ∑aj | 1 <= s1 <= t1 < s2 <= t2 <= n } i=s1 j=s2 Your task is to calculate d(A). 输入

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[