题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081

To The Max

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8839    Accepted Submission(s):
4281

Problem 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
 
题目大意:在一个数的矩阵里面找一个和最大矩阵。
题目思路:化二维为一维,转化成1003即可。先求一列一列的和,这样就转化成了一维了。举个例子,我先计算第一行得到dp[0]=0dp[1]=-2,dp[2]=-7,dp[3]=0;这些就是所谓的和,然后在用1003的方法做。那么第二行,就会更新,dp[0]=9,dp[1]=0,dp[2]=-13,dp[3]=2;这些依旧是和,这也就是转换成了一维数组,用1003的方法解决,以此类推~~~
 
详见代码。
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int dp[];
int num[][]; int main ()
{
int n;
while (~scanf("%d",&n))
{
for (int i=; i<n; i++)
for (int j=; j<n; j++)
scanf("%d",&num[i][j]);
int ans=-;
for (int i=; i<n; i++)
{
memset(dp,,sizeof(dp));
for (int j=i; j<n; j++)
{
int Max=-;
for (int k=; k<n; k++)
{
dp[k]+=num[j][k]; //先计算出所有的dp和
}
for (int k=; k<n; k++) //1003的做法,代码类似
{
if (Max+dp[k]<dp[k])
Max=dp[k];
else
Max=Max+dp[k];
if (ans<Max) //不断更新最大值
{
ans=Max;
//cout<<j<<" "<<k<<endl;
}
}
}
}
printf ("%d\n",ans);
}
return ;
}

hdu 1081 To The Max(dp+化二维为一维)的更多相关文章

  1. URAL 1146 Maximum Sum & HDU 1081 To The Max (DP)

    点我看题目 题意 : 给你一个n*n的矩阵,让你找一个子矩阵要求和最大. 思路 : 这个题都看了好多天了,一直不会做,今天娅楠美女给讲了,要转化成一维的,也就是说每一列存的是前几列的和,也就是说 0 ...

  2. HDU 1081 To The Max【dp,思维】

    HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...

  3. hdu 1081 To The Max(二维压缩的最大连续序列)(最大矩阵和)

    Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...

  4. HDU 1081 To The Max (dp)

    题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...

  5. Max Sum Plus Plus HDU - 1024 基础dp 二维变一维的过程,有点难想

    /* dp[i][j]=max(dp[i][j-1]+a[j],max(dp[i-1][k])+a[j]) (0<k<j) dp[i][j-1]+a[j]表示的是前j-1分成i组,第j个必 ...

  6. HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  7. 经典DP 二维换一维

    HDU 1024  Max Sum Plus Plus // dp[i][j] = max(dp[i][j-1], dp[i-1][t]) + num[j] // pre[j-1] 存放dp[i-1] ...

  8. dp - 最大子矩阵和 - HDU 1081 To The Max

    To The Max Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1081 Mean: 求N*N数字矩阵的最大子矩阵和. ana ...

  9. Hdu 1081 To The Max

    To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. 语音信号处理之动态时间规整(DTW)(转)

    这学期有<语音信号处理>这门课,快考试了,所以也要了解了解相关的知识点.呵呵,平时没怎么听课,现在只能抱佛脚了.顺便也总结总结,好让自己的知识架构清晰点,也和大家分享下.下面总结的是第一个 ...

  2. 【week6】用户数

    小组名称:nice! 小组成员:李权 于淼 杨柳 刘芳芳 项目内容:约跑app alpha发布48小时以后用户数如何,是否达到预期目标,为什么,是否需要改进,如何改进(或理性估算). 首先我们的app ...

  3. vector中数据进行去重和排序

    , , , , , , ,}; std::vector<int> vec(a, a+sizeof(a)/sizeof(int) ); std::sort(vec.begin(), vec. ...

  4. linux tomcat shutdown.sh 不能正常关闭

    一般造成这种原因是因为项目中有非守护线程的存在 基本原理为启动tomcat时记录启动tomcat的进程id(pid),关闭时强制杀死该进程 1.找到tomcat下bin/catalina.sh文件,v ...

  5. 【ADO.NET】SqlBulkCopy批量添加DataTable

    使用事务和SqlBulkCopy批量插入数据 SqlBulkCopy是.NET Framework 2.0新增的类,位于命名空间System.Data.SqlClient下,主要提供把其他数据源的数据 ...

  6. 【Python】Python time mktime()方法

    描述 Python time mktime() 函数执行与gmtime(), localtime()相反的操作,它接收struct_time对象作为参数,返回用秒数来表示时间的浮点数. 如果输入的值不 ...

  7. Python基础教程系列目录,最全的Python入门系列教程!

    Python是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. 在现在的工作及开发当中,Python的使用越来越广泛,为了方便大家的学习,Linux大学 特推出了 <Python基 ...

  8. (二)Redis字符串String操作

    String全部命令如下: set key value # 设置一个key的value值 get key # 获取key的value值 mset key1 value1 key2 value2 ... ...

  9. BZOJ 1342: [Baltic2007]Sound静音问题 | 单调队列维护的好题

    题目: 给n个数字,一段合法区间[l,l+m-1]要求max-min<=c 输出所有合法区间的左端点,如果没有输出NONE 题解: 单调队列同时维护最大值和最小值 #include<cst ...

  10. BZOJ1034 [ZJOI2008]泡泡堂BNB 【贪心】

    1034: [ZJOI2008]泡泡堂BNB Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 3531  Solved: 1798 [Submit][ ...