POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)
传送门:
http://poj.org/problem?id=1050
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 52306 | Accepted: 27646 |
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
Source
#include <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
#include<queue>
#include<vector>
using namespace std;
#define max_v 105
#define INF 99999999
int dp1[max_v][max_v];//起始i列 终止j列的max
int dp2[max_v];//最大子段和的dp,代表以第i个数结尾的最大子合和值
int a[max_v][max_v];
int f(int j1,int j2,int i)//j1列到j2列,i行上数字的和
{
int sum=;
for(int j=j1;j<=j2;j++)
{
sum+=a[i][j];
}
return sum;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&a[i][j]);
memset(dp1,,sizeof(dp1));
dp1[][]=a[][];
int result=-INF;
for(int i=;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
memset(dp2,,sizeof(dp2));
dp2[]=f(i,j,);
int maxv=dp2[];
for(int k=;k<=n;k++)
{
int x=;
if(dp2[k-]>)
x=dp2[k-];
dp2[k]=x+f(i,j,k);
maxv=max(maxv,dp2[k]);
}
dp1[i][j]=maxv;
result=max(result,dp1[i][j]);
}
}
printf("%d\n",result);
}
return ;
}
POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)的更多相关文章
- poj 1050 To the Max(最大子矩阵之和)
http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here 也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...
- POJ 1050 To the Max (最大子矩阵和)
题目链接 题意:给定N*N的矩阵,求该矩阵中和最大的子矩阵的和. 题解:把二维转化成一维,算下就好了. #include <cstdio> #include <cstring> ...
- 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
To the Max Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
- [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(dp)
题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...
- POJ 1050 To the Max 二维最大子段和
To the MaxTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 52281 Accepted: 27633Description ...
- [poj]1050 To the Max dp
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
随机推荐
- UGUI——重写Image类实现进度条
目的: 游戏中经常会用到进度条,但是美术给的图片用filled一拉伸就很难看,如下图 第一种模式是九宫格模式,第二种是filled.而我们需要的是两种可结合的. 如何实现: 新建一个类,继承image ...
- MIME格式说明,电子邮件格式(转载)
邮件格式说明 Mutiple Internet Mail Extensions Refer to Internet Official Protocol Standards RFC 822 1 概述 网 ...
- springcloud 实战 网关zuul使用中遇到的相关问题
springcloud 实战 网关zuul使用中遇到的相关问题 1.网关zuul使用时,跨域问题在网关中配置pre过滤器: response.setHeader("Access-Contr ...
- python保存字典和读取字典pickle
import pickle import numpy as np def save_obj(obj, name): with open(name + '.pkl', 'wb') as f: pickl ...
- 【python基础】之str类字符串
str类字符串是不可变对象 1.创建字符串 s1 = str() #创建一个空字符串 s2 = str("hello") #创建字符串"hello" 2.处理字 ...
- MySQL数据库(11)----使用子查询实现多表查询
子查询指的是用括号括起来,并嵌入另一条语句里的那条 SELECT 语句.下面有一个示例,它实现的是找出与考试类别('T')相对应的所有考试事件行的 ID,然后利用它们来查找那些考试的成绩: SELEC ...
- Creating dynamic/configurable parameterized queries in Entity Framework
https://dillieodigital.wordpress.com/2013/05/09/creating-dynamicconfigurable-parameterized-queries-i ...
- (转)informatica 面试题大全
1 What is the difference between a data warehouse and a data mart? Ø Dataware house: It is a collect ...
- 如何清除SQL 的登录名
复制路径 找到SqlStudio.bin 文件删除即可 SQL Server 2016 版本: C:\Users\%username%\AppData\Roaming\Microsoft\SQL Se ...
- [翻译] CHAnimation
CHAnimation https://github.com/cyndibaby905/CHAnimation How it looks CHAnimation is a project used t ...