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 ...
随机推荐
- python 备份脚本
import osimport timesource= r"out_res.txt"target_dir= r"F:\python\Doc"target=tar ...
- iOS 导航条的影响
如果是push出来的控制器,self.view的(0,0)点从状态栏下面开始: 如果有present出来的控制器,self.view的(0,0)点包含状态栏:
- C# 如何从List集合当中取出子集合
今天项目要求随机从数据库中随机取出若干条数据,放到首页.那么要如何随机取出这个子集合呢?本人向到的方法如下: 1.假设数据量很少,如我数据库中只有10条数据,而我要求随机取出8条.对于这种低数据量,大 ...
- gdb 命令使用
1.gdb -x command.txt 每次重复输入命令很麻烦,可以使用上面的命令,把命令输入进command.txt里面,然后直接就可以执行gdb. 2.list 2.1 list functio ...
- 155Min Stack
题目地址:155Min Stack 最近为了提高数据结构和算法能力,保证每天一到leetcode的题目.从easy开始,感觉这道题目还蛮好,记录一下. 题目大意:就是维护一个栈,获得栈中元素的的最小值 ...
- 小技巧-Try Catch
与多线程,业务逻辑等比较复杂的功能打交道时,免不了对部分有可能产生不可预期的代码进行异常捕获. 这种异常可能不处理,比如: try { } catch {} 但是如果一旦发生异常,在程序调试的时候,发 ...
- std中map
在map中需要对位置a和b值进行交换,代码如下: auto val1 = tmpMap.at(a); auto val2 = tmpMap.at(b); tmpMap.insert(std::make ...
- hdu 3529 Bomberman - Just Search! 重复覆盖
题目链接 依然是重复覆盖的模板.... #include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) #d ...
- poj 3740 Easy Finding 精确匹配
题目链接 dlx的第一题, 真是坎坷..... #include <iostream> #include <vector> #include <cstdio> #i ...
- Linux cd命令
1: cd 不加任何参数,它会自动跳到用户的家目录中去! 2: ~ 表示用户的家目录 3: cd ~userNmae/ 这样可以进入指定用户的家目录中去! 4: cd - 跳到上一次所在的目 ...