poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050
思路分析:
该题目为经典的最大子矩阵和问题,属于线性dp问题;最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而最大子矩阵为二维问题,
可以考虑将二维问题转换为一维问题,即变为最大子段和问题即可求解;
先考虑暴力解法,暴力解法需要枚举子矩阵的左上角元素的坐标与子矩阵的右下角坐标即可枚举所有的子矩阵;对于每个子矩阵,考虑压缩子矩阵的每一列
元素,即求每一列的元素的和,这样子矩阵就转换为一维的情况,再使用最大子段和问题,即可决定出在子矩阵的列决定的情况下,求出使子矩阵和最大的
行的选择;最后需要枚举子矩阵所有列的情况即可;
代码如下:
#include <iostream>
using namespace std; const int MAX_N = + ;
int sum[MAX_N];
int matrix[MAX_N][MAX_N]; int main()
{
int n, ans; while (scanf("%d", &n) != EOF)
{
for (int i = ; i < n; ++i)
for (int j = ; j < n; ++j)
scanf("%d", &matrix[i][j]); ans = INT_MIN;
for (int top = ; top < n; ++top)
{
for (int buttom = top; buttom < n; ++buttom)
{
int t_sum = , t_ans = INT_MIN; for (int i = ; i < n; ++i)
{
sum[i] = ;
for (int k = top; k <= buttom; ++k)
sum[i] += matrix[k][i];
if (t_sum >= )
t_sum += sum[i];
else
t_sum = sum[i]; if (t_sum > t_ans)
t_ans = t_sum;
}
if (t_ans > ans)
ans = t_ans;
}
}
printf("%d\n", ans);
}
return ;
}
poj 1050 To the Max(线性dp)的更多相关文章
- 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 #include<cstdio> #include<cstring> #include<iostr ...
- POJ 1050 To the Max 枚举+dp
大致题意: 求最大子矩阵和 分析: 一开始想复杂了,推出了一个状态方程:d[i][j]=max(d[i][j-1]+-,d[i-1][j]+-).写着写着发现上式省略的部分记录起来很麻烦. 后来发现n ...
- [poj]1050 To the Max dp
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 最大子矩阵和 经典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 暴力,基础知识 难度:0
http://poj.org/problem?id=1050 设sum[i][j]为从(1,1)到(i,j)的矩形中所有数字之和 首先处理出sum[i][j],此时左上角为(x1,y1),右下角为(x ...
随机推荐
- wing 5.0 注册机
输入License id 进入下一页获得request key ,输入request key 后点击生成,即可生成激活码,亲测可用 下载链接 密码:adwj
- Oracle集合操作函数:union、intersect、minus
[转]Oracle集合操作函数:union.intersect.minus 集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINU ...
- google base库之simplethread
// This is the base SimpleThread. You can derive from it and implement the // virtual Run method, or ...
- (转) ios学习之 关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系
刚接触iOS开发的人难免会对苹果的各种证书.配置文件等不甚了解,可能你按照网上的教程一步一步的成功申请了真机调试,但是还是对其中的缘由一知半解.这篇文章就对Certificate.Provisioni ...
- Android ActionBar详解(三)--->ActionBar的Home导航功能
FirstActivity如下: package cc.testsimpleactionbar2; import android.os.Bundle; import android.app.Activ ...
- 用while判断输入的数字是否回文数
/* Name:用while判断输入的数字是否回文数 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月18日 04:29:07 Description:用 ...
- 为什么要for循环以及for循环的流程
/* Name:为什么需要循环以及for循环流程 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月10日 03:16:55 Description:求1 ...
- HDU 2167 Pebbles
题目大意:有个N*N( 3<=N<=15 )方阵, 可从中若干个数, 使其总和最大.取数要求, 当某一个数被选, 其周围8个数都不能选. 题解:记s数组为合法状态,即没有相邻的数字同时被选 ...
- [每日一题] 11gOCP 1z0-052 :2013-09-17 DRA--Data Recovery Advisor.............................B31
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/11818529 正确答案:AD 数据库恢复顾问(DRA)是一个诊断和修复数据库问题的工具.共 ...
- MySQL性能调优的方法
第一种方法 1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快.因此,在创建表的时候,为了获得更好的 性能,我们可以将表中字 ...