POJ 1050 To the Max -- 动态规划
题目地址:http://poj.org/problem?id=1050
Description
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
(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
Sample Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1 8 0 -2
Sample Output
15
时间复杂度为O(N^2*M^2)
#include <stdio.h>
#include <limits.h> //PS[i][j]等于以(1, 1), (1, j), (i, 1), (i, j)为顶点的矩形区域的元素之和
void Preproccess(int matrix[101][101], int PS[101][101], int N){
int i, j;
for (i=0; i<=N; ++i){
PS[0][i] = 0;
PS[i][0] = 0;
}
for (i=1; i<=N; ++i){
for (j=1; j<=N; ++j){
PS[i][j] = PS[i-1][j] + PS[i][j-1] - PS[i-1][j-1] + matrix[i][j];
}
}
} int main(void){
int N;
int matrix[101][101];
int PS[101][101];
int i, j;
int i_min, i_max;
int j_min, j_max;
int max, tmp; while (scanf ("%d", &N) != EOF){
for (i=1; i<=N; ++i)
for (j=1; j<=N; ++j)
scanf ("%d", &matrix[i][j]);
Preproccess(matrix, PS, N);
max = INT_MIN;
/*以(i_min, j_min), (i_max, j_min), (i_min, j_max), (i_max, j_max)为顶点的矩形区域的元素之和,
等于PS[i_max][j_max] - PS[i_min-1][j_max] - PS[i_max][j_min-1] + PS[i_min-1][j_min-1]
*/
for (i_min=1; i_min<=N; ++i_min){
for (i_max=i_min; i_max<=N; ++i_max){
for (j_min=1; j_min<=N; ++j_min){
for (j_max=j_min; j_max<=N; ++j_max){
tmp = PS[i_max][j_max] - PS[i_min-1][j_max] - PS[i_max][j_min-1] + PS[i_min-1][j_min-1];
if (tmp > max)
max = tmp;
}
}
}
}
printf ("%d\n", max);
} return 0;
}
时间复杂度为O(N*M*min(N, M))
#include <stdio.h>
#include <limits.h> //PS[i][j]等于以(1, 1), (1, j), (i, 1), (i, j)为顶点的矩形区域的元素之和
void Preproccess(int matrix[101][101], int PS[101][101], int N){
int i, j;
for (i=0; i<=N; ++i){
PS[0][i] = 0;
PS[i][0] = 0;
}
for (i=1; i<=N; ++i){
for (j=1; j<=N; ++j){
PS[i][j] = PS[i-1][j] + PS[i][j-1] - PS[i-1][j-1] + matrix[i][j];
}
}
} //BC(PS, a, c, i)表示在第a行和第c行之间的第i列的所有元素的和,可以通过“部分和”PS[i][j]在O(1)时间内计算出来。
int BC(int PS[101][101], int a, int c, int i){
return PS[c][i] - PS[a-1][i] - PS[c][i-1] + PS[a-1][i-1];
} int MaxSum (int matrix[101][101], int PS[101][101], int N){
int max = INT_MIN;
int a, c, i;
int Start, All;
for (a=1; a<=N; ++a){
for (c=a; c<=N; ++c){
Start = BC(PS, a, c, N);
All = BC(PS, a, c, N);
for (i=N-1; i>=1; --i){
if (Start < 0)
Start = 0;
Start += BC(PS, a, c, i);
if (Start > All)
All = Start;
}
if (All > max)
max = All;
}
}
return max;
} int main(void){
int N;
int matrix[101][101];
int PS[101][101];
int i, j; while (scanf ("%d", &N) != EOF){
for (i=1; i<=N; ++i)
for (j=1; j<=N; ++j)
scanf ("%d", &matrix[i][j]);
Preproccess(matrix, PS, N);
printf ("%d\n", MaxSum (matrix, PS, N));
} return 0;
}
HDOJ上相似的题目:http://acm.hdu.edu.cn/showproblem.php?pid=1559
九度OJ上相似的题目:http://ac.jobdu.com/problem.php?pid=1492
参考资料:编程之美
POJ 1050 To the Max -- 动态规划的更多相关文章
- [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- 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 暴力,基础知识 难度:0
http://poj.org/problem?id=1050 设sum[i][j]为从(1,1)到(i,j)的矩形中所有数字之和 首先处理出sum[i][j],此时左上角为(x1,y1),右下角为(x ...
- poj 1050 To the Max (简单dp)
题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...
- 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 Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 38573Accepted: 20350 Descriptio ...
- poj 1050 To the Max
To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45906 Accepted: 24276 Desc ...
随机推荐
- ECSHOP在线手册布局参考图--登录/注册页 user_passport.dwt
A.会员登录框 1,设置方法 自带模块 2,代码相关 user_passport.dwt 中 <div class="usBox_1 f_l"> <div ...
- zabbix邮件报警脚本(Python)
#!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys mail_ ...
- .NET加密配置文件connectionStrings节点
具体的可以参考petshop源码程序. 加密前: <connectionStrings> <add name="SQLProfileConnString" con ...
- hdu 5432 Pyramid Split 二分
Pyramid Split Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/conte ...
- android学习日记02--Activity简介
一.Activity活动 学习Android,第一个都会接触Activity滴,Activity表示一个用户界面,是Android应用程序的入口,可以同时有多个界面,但只会显示栈顶的界面. Activ ...
- [009]C---关于输出文本的打印问题
现在有这样一个问题: 针对一个long类型的变量,我们想把它打印成为32位显示. #include "stdio.h" int main() { long i =0xa; prin ...
- DOS 命令 attrib:修改文件属性
有时候系统中病毒之后会把自己的所有文件的属性改为隐藏只读格式,如果手动修改的话太麻烦并且浪费时间,不如用 Attrib 命令试试. 步骤: 1:开始->运行,在里面输入 cmd,打开命令行界面: ...
- Android 断点续传
断点续传指的是在下载或上传时,将下载或上传任务(一个文件或一个压缩包)人为的划分为几个部分,每一个部分采用一个线程进行上传或下载,如果碰到网络故障,可以从已经上传或下载的部分开始继续上传下载未完成的部 ...
- oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by
select语句学习 . 创建表 create table user(user varchar2(20), id int); . 查看执行某条命令花费的时间 set timing on: . 查看表的 ...
- iOS之文本属性Attributes的使用
1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFontOfSize:_fontS ...