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 ...
随机推荐
- HTML新手推荐
对于前端的学习要先了解一下浏览器和html的发展史其次看看这篇文章:https://kb.cnblogs.com/page/129756/#chapter1我读到这句话时候感觉到了科技这个东西有很多时 ...
- BZOJ4513: [Sdoi2016]储能表(数位dp)
题意 题目链接 Sol 一点思路都没有,只会暴力,没想到标算是数位dp??Orz 首先答案可以分成两部分来统计 设 \[ f_{i,j}= \begin{aligned} i\oplus j & ...
- <pre> <textarea> <code>标签区别
这篇文章里面放的大都是我自己写程序的时候遇到的一些小问题,其实都是自己没有掌握的点,别人看起来应该很简单啦,但写下来能提醒自己,也能鼓励一下自己,这条路也不好走哇. <pre> <t ...
- Monitorix:一款面向Linux的轻型系统和网络监测工具
Monitorix是一款功能非常强大的免费开源轻型工具,目的在于监测Linux中的系统和网络资源.它可以定期收集系统和网络数据,并使用自己的Web界面,通过图形显示相关信息.Monitorix让用户可 ...
- Android 显示html标签或者带图片
Android中显示html文件要用Html.fromHtml(...)处理过的返回值,返回值可以成为setText()的参数. 只显示带文本的html可以用下面的方法处理html文件. public ...
- volley6--CacheDispatcher从缓存中获取数据
源码: /* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, V ...
- 代码整洁之道读书笔记(Ch4-Ch7)
这几章从注释.程序格式.对象与数据结构的规范以及错误处理四个方面介绍了如何使代码变得简洁易懂.不同于上次摘抄的方法,这一次我会结合第一次个人作业的代码进行分析. 第四章 注释 这一章告诉我们,好的注 ...
- Spring MVC基本配置和实践(一)
一.Spring MVC介绍 1. Spring MVC是什么? The Spring Web MVC framework和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从S ...
- 关于serialVersionUID与序列化"
java序列化trick and trap 厂内经常出现序列化对象版本不匹配问题,于是发本文说明一些序列化的注意点 调用MQ.memcached.rpc等等涉及到远程通讯的都会经过序列化,虽然客户端透 ...
- 产品从生到死的N宗罪
写在前面 昨天晚上做了一个梦,大概就是跟CTO,PM在说着什么..现在回想起好像就是说产品怎么怎么的..:索性就吐槽下这几个项目生与死的N宗罪吧.. 特别提示: 本文为全方位吐槽型,前方多处具有针对性 ...