随手练——ZOJ-1074 To the Max(最大矩阵和)
To the Max
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1074
动态规划(O(n^3))
获得一维数组的最大子数组和:(O(n))
int MaxSubSum(int* arr, int n) {
<< , t = ;
; i < n; i++) {
t += arr[i];
if (t > max) max = t;
) {
t = ;
}
}
return max;
}
目标:
通过一维数组的最大子数组和,找到二维数组的最大矩阵和
1、形如这样的一个二维数组,将其 “压扁” 成为一维数组(数组每列元素的和):

2、首先特殊情况,假设最大子矩阵是二维数组中的粉色部分,则相对应一维数组的最大子数组和就一定也是粉色部分。

3、推广到所有情况,假设最大子矩阵和是二维数组中的,p~q行,i~j列 ;则相对应的 p~q行 的列元素和的最大子数组和就是是 i 到 j 。
代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int n, res = 0x80000000;
][];
int MaxSubSum(int *arr) {
<< , t = ;
; i < n; i++) {
t += arr[i];
if (t > max) max = t;
) {
t = ;
}
}
return max;
}
int main()
{
cin >> n;
; i < n; i++) {
; j < n; j++) {
scanf("%d", &arr[i][j]);
}
}
int* arrSum = new int[n];
; i < n; i++) {
memset(arrSum, , sizeof(int) * n);
for (int j = i; j < n; j++) {
; k < n; k++) {
arrSum[k] += arr[j][k];
}
int t = MaxSubSum(arrSum);
res = t > res ? t : res;
}
}
cout << res << endl;
;
}
随手练——ZOJ-1074 To the Max(最大矩阵和)的更多相关文章
- ZOJ 1074 To the Max
原题链接 题目大意:这是一道好题.在<算法导论>这本书里面,有一节是介绍如何求最大子序列的.这道题有点类似,区别是从数组变成了矩阵,求最大子矩阵. 解法:完全没有算法功底的人当然不知道最大 ...
- ZOJ 1074 To the Max(DP 最大子矩阵和)
To the Max Time Limit: 2 Seconds Memory Limit: 65536 KB Problem Given a two-dimensional array o ...
- 随手练——ZOJ 1093 Monkey and Banana(动态规划)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=93 一堆科学家研究猩猩的智商,给他M种长方体,每种N个. 然后,将一个 ...
- 随手练——洛谷-P1151(枚举与暴力搜索)
枚举 #include <iostream> using namespace std; int main() { ; cin >> k; ; i < ; i++) { ) ...
- SUBSTRING / CHARINDEX_函数随手练_2
SUBSTRING / CHARINDEX_函数随手练_2环境:MSSQL 2014(AdventureWorks2008R2附加到2014中的表 Location) /* Learning SQL ...
- case when then 随手练_1
CASE WHEN THEN随手练,就当做练习指法吧 --drop table tbStudent GO Create table tbStudent( studentId int identity( ...
- HDOJ 1081(ZOJ 1074) To The Max(动态规划)
Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...
- ZOJ 1074 最大子矩阵和
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- 随手练——HDU 1078 FatMouse and Cheese(记忆化搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意: 一张n*n的格子表格,每个格子里有个数,每次能够水平或竖直走k个格子,允许上下左右走,每次走的格子 ...
随机推荐
- winform窗体 小程序【移动窗体和阴影】
窗体无边框设置后无法移动,引用API 使其获得功能 移动 //窗体移动API [DllImport("user32.dll")] public static extern bool ...
- 使用"+"进行字符串拼接
本文来自:Hollis(微信号:hollischuang) 字符串,是Java中最常用的一个数据类型了.本文,也是对于Java中字符串相关知识的一个补充,主要来介绍一下字符串拼接相关的知识.本文基于j ...
- java通过url在线预览Word、excel、ppt、pdf、txt文档
java通过url在线预览Word.excel.ppt.pdf.txt文档中的内容[只获得其中的文字] 在页面上显示各种文档中的内容.在servlet中的逻辑 word: BufferedInputS ...
- POJ2318(KB13-A 计算几何)
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16222 Accepted: 7779 Description ...
- 【 js 基础 】【 源码学习 】 深浅拷贝
underscore的源码中,有很多地方用到了 Array.prototype.slice() 方法,但是并没有传参,实际上只是为了返回数组的副本,例如 underscore 中 clone 的方法: ...
- 小tip:CSS vw让overflow:auto页面滚动条出现时不跳动——张鑫旭
小tip:CSS vw让overflow:auto页面滚动条出现时不跳动 这篇文章发布于 2015年01月25日,星期日,23:08,归类于 css相关. 阅读 46274 次, 今日 91 次 by ...
- 【代码笔记】iOS-可拷贝的label
一,效果图. 二,工程图. 三,代码. ViewController.m #import "ViewController.h" #import "MKBeCopyLabe ...
- Core Animation-1:图层树
图层的树状结构 >巨妖有图层,洋葱也有图层,你懂吗?我们都有图层 -- 史莱克 Core Animation其实是一个令人误解的命名.你可能认为它只是用来做动画的,但实际上它是从一个叫做*Lay ...
- 6个讨喜的 ES6 小技巧
[编者按]本文作者为 Axel Rauschmayer,主要介绍6个 ES6 小技巧.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 在本文中,笔者将介绍6个由 ES6 新功能带来的小技巧. ...
- 留言板0.4_model中的数据库(2)
今天就讲讲:如何将后台数据呈现在HTML页面中,以及url配置时的两点技巧吧. 1.首先在"views.py"中提取出后台数据 def getform(request): mess ...