hdu 1081 & poj 1050 To The Max(最大和的子矩阵)
转载请注明出处:http://blog.csdn.net/u012860063
Description
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
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
Source
题意:给你一个N*N的矩阵,求当中和最大的子矩阵的值!
代码例如以下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int i, j, k, t;
int a, sum, max, N, m[147][147];
while(~scanf("%d",&N))
{
memset(m,0,sizeof(m));
for(i = 1; i <= N; i++)
{
for(j = 1; j <= N; j++)
{
scanf("%d",&a);
m[i][j]+=m[i][j-1]+a;//表示第i行前j个数之和
}
}
max = -128;
for(i = 1; i <= N; i++)//起始列
{
for(j = i; j <= N; j++)//终止列
{
sum = 0;
for(k = 1; k <= N; k++)//对每一行进行搜索
{
if(sum < 0)
sum = 0;
sum+=m[k][j]-m[k][i-1];
//m[k][j]-m[k][i-1]表示第k行第i列之间的数
if(sum > max)
max = sum;
}
}
}
printf("%d\n",max);
}
return 0;
}
hdu 1081 & poj 1050 To The Max(最大和的子矩阵)的更多相关文章
- 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 -- 动态规划
题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...
- 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问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
- [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题)
To the Max Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 38573Accepted: 20350 Descriptio ...
随机推荐
- Welcome-to-Swift-15反初始化(Deinitialization)
在一个类的实例被释放之前,反初始化函数被立即调用.用关键字deinit来标示反初始化函数,类似于初始化函数用init来标示.反初始化函数只适用于类类型. 反初始化原理 Swift会自动释放不再需要的实 ...
- [codevs2185]最长公共上升子序列
[codevs2185]最长公共上升子序列 试题描述 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们要研究最长公共上升子序 ...
- [luoguP2766] 最长递增子序列问题(最大流)
传送门 题解来自网络流24题: [问题分析] 第一问时LIS,动态规划求解,第二问和第三问用网络最大流解决. [建模方法] 首先动态规划求出F[i],表示以第i位为开头的最长上升序列的长度,求出最长上 ...
- Windows cmd 生成目录结构 dir /b,tree /f,xcopy
>dir *.sh *.ksh *.java /s/b > list.txt >tree /f > list.txt >xcopy C:\folder\from_fold ...
- linux 时间模块 三
LINUX的时钟中断中涉及至二个全局变量一个是xtime,另一个则是jiffies.有一个与时间有关的时钟:实时时钟(RTC),这是一个硬件时钟,用来持久存放系统时间,系统关闭后靠主板上的微型电池保持 ...
- file中的一些常用方法
1.exists();判断文件(目录)是否存在 2.mkdir();创建一级目录:mkdirs()创建多级目录 3.delete();删除文件(目录) 4.isDirectory();判断是否是一个目 ...
- dedecms--会员信息导出excel表格
1:在dede/templets下面的member_main.htm,在全选按钮那里添加一个导出excel按钮:代码如下: <a href="toexcel.php" cla ...
- centos 7安装postgresql10.3
最新版本安装请移步:阿里云服务器 centos 7 安装postgresql 11 一.Postgresql简介 官方网站:https://www.postgresql.org/ 简介参考zhihu文 ...
- AC日记——[SCOI2010]游戏 bzoj 1854
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4938 Solved: 1948[Submit][Status] ...
- js-判断移动端用户是横屏放的还是竖屏放的
在一些移动端页面中,页面的样式我们就支持竖屏播放,这时我们就需要判断是横屏还是竖屏了,不同的方向显示冉的样式. //判断手机横屏竖屏 var html = ""; window.a ...