题目链接: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. 【week2】燃尽图

    燃尽图(burn down chart)是在项目完成之前,对需要完成的工作的一种可视化表示.燃尽图有一个Y轴(工作)和X轴(时间).理想情况下,该图表是一个向下的曲线,随着剩余工作的完成,“烧尽”至零 ...

  2. RT-thread内核之系统时钟

    一.系统时钟 rt-thread的系统时钟模块采用全局变量rt_tick作为系统时钟节拍,该变量在系统时钟中断函数中不断加1.而系统时钟中断源和中断间隔一般由MCU硬件定时器(如stm32的嘀嗒定时器 ...

  3. BZOJ 口胡记录

    最近实在是懒的不想打代码...好像口胡也算一种训练,那就口胡把. BZOJ 2243 染色(树链剖分) 首先树链剖分,然后记录下每个区间的左右端点颜色和当前区间的颜色段.再对每个节点维护一个tag标记 ...

  4. poj1474 Video Surveillance

    题意:求多边形的内核,即:在多边形内部找到某个点,使得从这个点能不受阻碍地看到多边形的所有位置. 只要能看到所有的边,就能看到所有的位置.那么如果我们能够在多边形的内部的点x看到某条边AB,这个点x一 ...

  5. ZOJ3229:Shoot the Bullet——题解

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3229 题目大意:射命丸文要给幻想乡的居民照相,共照n天m个人,每天射命丸文 ...

  6. 分享几款常用的API/文档浏览器

    1.Dash 支持平台:Mac  iOS 官网:https://kapeli.com/dash 2.Zeal 支持平台:Linux Windows 官网:https://zealdocs.org/ G ...

  7. NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第一轮Day2题解

    上星期打的...题有点水,好多人都AK了 T1排个序贪心就好了 #include<iostream> #include<cstring> #include<cstdlib ...

  8. [IOI2007]Miners 矿工配餐

    link 其实就是一个比较简单的$IOI$题.简单$dp$就行,设$5$维$dp$即可 最后在滚动一下,判一下可行性即可. #include<iostream> #include<c ...

  9. rand、randi和randn的区别?

    1,rand 生成均匀分布的伪随机数.分布在(0~1)之间 主要语法:rand(m,n)生成m行n列的均匀分布的伪随机数 rand(m,n,'double')生成指定精度的均匀分布的伪随机数,参数还可 ...

  10. HTML学习基本代码

    HTML的代码比较繁琐,在此记下一些基本的东西,以后自己回来看看 <html> <head> <title>我的人生啊</title> <styl ...