[poj]1050 To the Max dp
Description
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
Output
Sample Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1 8 0 -2
Sample Output
15 和最大连续子串和类似,可以把二维的转化为一维,将i行到j行的各列数分别相加,求最大连续子串和即可。
同时可利用前缀和,sum[i][j]表示第j列前i行相加的和,i从1开始,则i-j行相加的和为sum[j][k]-sum[i-1][k]
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std; #define INF 0x3f3f3f3f
int a[][];
int t[], dp[];
int sum[][]; int n; int sovle()
{
int ans = -INF;
for (int i = ; i <= n; i++) {
for (int j = i; j <= n; j++) { int Max;
dp[] = sum[j][] - sum[i-][];
for (int k = ; k <= n; k++) {
int temp = sum[j][k]-sum[i-][k];
dp[k] = max(dp[k-]+temp, temp);
}
Max = *max_element(dp+, dp+n+);
if (Max > ans)
ans = Max;
}
}
return ans;
} int main()
{
//freopen("1.txt", "r", stdin);
scanf("%d", &n);
memset(sum, , sizeof(sum));
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++) {
scanf("%d", &a[i][j]);
sum[i][j] = sum[i-][j] + a[i][j];
}
printf("%d\n", sovle()); return ;
}
[poj]1050 To the Max dp的更多相关文章
- poj 1050 To the Max (简单dp)
题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...
- POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)
传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- poj 1050 To the Max(最大子矩阵之和)
http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here 也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...
- POJ 1050 To the Max 二维最大子段和
To the MaxTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 52281 Accepted: 27633Description ...
- poj - 1050 - To the Max(dp)
题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...
- poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
- poj 1050 To the Max 最大子矩阵和 经典dp
To the Max Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- poj 1050 To the Max(最大子矩阵之和,基础DP题)
To the Max Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 38573Accepted: 20350 Descriptio ...
- POJ 1050 To the Max 枚举+dp
大致题意: 求最大子矩阵和 分析: 一开始想复杂了,推出了一个状态方程:d[i][j]=max(d[i][j-1]+-,d[i-1][j]+-).写着写着发现上式省略的部分记录起来很麻烦. 后来发现n ...
随机推荐
- 洛谷 2261 [CQOI2007]余数求和
题目戳这里 一句话题意 求 \(\sum_{i=1}^{n} (k ~~\texttt{mod} ~~i)\) Solution 30分做法: 说实话并不知道怎么办. 60分做法: 很明显直接一遍o( ...
- BZOJ2328: [HNOI2011]赛车游戏
BZOJ2328: [HNOI2011]赛车游戏 Description 题解Here! 一开始被题面那一长串的描述吓到了,一直没敢做... 然后尝试着硬着头皮读懂题面. 然后...这不是贪心么??? ...
- gradle 跳过junitTest的方法
Web项目中不长会写JunitTest,但也会写.gradle build的时候回执行test 这项task.如果想跳过,通常有几种方法: 1.在build.gradle 文件中禁用task test ...
- Java基础之I/O流
一.数据流的基本概念 数据流是一串连续不断的数据的集合,就象水管里的水流,在水管的一端一点一点地供水,而在水管的另一端看到的是一股连续不断的水流.数据写入程序可以是一段.一段地向数据流管道中写入数据, ...
- vi中如何替换某字符成“回车”?
vi中如何替换某字符成“回车”? 在 vi 中::s/,/^M/g (you need to type CTRL-V <CR> to get a ^M here)VIM - Vi IMpr ...
- python获取本机IP地址
方法一 通常使用socket.gethostname()方法即可获取本机IP地址,但有时候获取不到(比如没有正确设置主机名称) import socket #获取计算机名称hostname=socke ...
- AnkhSVN
安装和配置 签入签出问题 1.安装和配置 ①安装.(貌似默认的安装到C:\Program Files\AnkhSVN 2下,开始菜单也没快捷?) ②源代码管理器设置:打开vs2012,工具→选项→源代 ...
- CSS那个背景图片的坐标怎么设置?怎么计算的?
background:url(images/hh.gif) no-repeat -10px 0;},作用是移动背景的位置. 背影图片的左上角相对当前元素左上角的坐标. 右为X轴正半轴, 下为Y轴正半轴 ...
- Java_正则_00_资源贴
二.参考资料 1.揭开正则表达式的神秘面纱
- Eclipse_插件_03_反编译插件_Eclipse Class Decompiler
一.插件优势 此插件比jd-eclipse更加强大,反编译之后不会像jd-eclipse一样出现注释符号. 二.插件下载地址 1.github https://github.com/cnfree/Ec ...