DP——To the Max

Description

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 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 x 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

HINT

大意:用一个数组表示从i行到j行的第k个元素的值的和。

#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-16 23:09:44

DP——To the Max的相关文章

dp/hdu 1003 Max Sum

题目 给出一列数,求出最大的子段和,并输出起始点和结束点 分析 设d[i]为末尾数是i的最大和,我们可以得到 d[i-1]>=0时,d[i]=d[i-1]+a[i]; d[i-1]<=0时,d[i]=a[i]; i从1到n都算过一遍后,找出最大的d[i],则此时的i为结束点.再从结束点倒着向前加,直到找到与答案相同的位置,记为起始点 Accepted Code 1 /* 2 PROBLEM:hdu1003 3 AUTHER:NicoleLam 4 MEMO:dp 5 */ 6 #include

hdu 1024 Max Sum Plus Plus(简单dp)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意:给定一个数组,求其分成m个不相交子段和的最大值. 这题有点问题其实m挺小的但题目并没有给出. dp[i][j]表示取第i 位的数共取了j段然后转移方程显然为 dp[i][j]=max(dp[i - 1][j]+a[j] , max(dp[j - 1][j - 1]~dp[i - 1][j - 1]))(大致意思是取第i位要么i-1位取了j个那么a[j]刚好能与i-1拼成一段,或者j -

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

hdu1024 Max Sum Plus Plus (Dp)

hdu1024 Max Sum Plus Plus Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem. Given a consecutive numbe

HDU 1024:Max Sum Plus Plus(DP)

http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24675    Accepted Submission(s): 8478 Problem Description Now I think you have g

HDU 1024 DP Max Sum Plus Plus

题意:本题的大致意思为给定一个数组,求其分成m个不相交子段和最大值的问题. kuangbin专题. dp[i][j]=Max(dp[i][j-1]+a[j] , max( dp[i-1][k] ) + a[j] ) 0<k<j 注意可以换成一维数组.第一维i没有影响,我们要求dp[m][n],所以i维可以省去,每次保留前j-1个数之中最大的部分. 1 #include<iostream> 2 #include<string> 3 #include<algorith

Hdu 1024 Max Sum Plus Plus (dp)

题目链接: Hdu 1024 Max Sum Plus Plus 题目描述: 给出n个数,问m段连续子序列的和相加最大是多少? 解题思路: dp[i][j]表示把前i个元素(包括第i个),分成j段的最大和.状态转移方程就是dp[i][j] = max (dp[i-1][j] + arr[j],  max( dp[k][j-1]) + arr[j]),其中0<k<i.(第i个元素是保存在第j段,还是自己单独成段) 由于1<=n<=1000,000.n*n的数组肯定会爆炸,所以要对方程

hdu1003 1024 Max Sum&amp;Max Sum Plus Plus【基础dp】

dp是竞赛中常见的问题,也是我的弱项orz,更要多加练习.看到邝巨巨的dp专题练习第一道是Max Sum Plus Plus,所以我顺便把之前做过的hdu1003 Max Sum拿出来又做了一遍 HDU 1003 Max Sum 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目描述:给一个整数序列,求其中的一段连续子序列,使它的和最大值以及这个序列的起始下标 思路:简单的dp,抓住“连续”来入手.构造pair<int,int> dp[

hdu 1024 Max Sum Plus Plus(DP)

转移方程dp[i][j]=Max(dp[i][j-1]+a[j],max(dp[i-1][k] ) + a[j] ) 0<k<j 此链接中有详解点击打开链接 #include<stdio.h> #include<algorithm> #include<iostream> using namespace std; #define MAXN 1000000 #define INF 0x7fffffff int dp[MAXN+10]; int mmax[MAXN