随手练——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个格子,允许上下左右走,每次走的格子 ...
随机推荐
- Java迭代器的一般用法
迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为“轻量级”对象,因为创建它的代价小. Java中的I ...
- mybatis-generator-maven逆向工程
在idea 中使用 mybatis的 mybatis-generator-maven-plugin 可以根据数据库 生成 dao层,pojo类,Mapper文件. 一: 在 pom.xml ...
- redis-java基础操作
安装 windows版的Redis,打开即可,默认端口6379 导入两个jar包 commons-pool2-2.3.jar jedis-2.7.0.jar 一 写配置文件 redis.setM ...
- Java-函数式编程(一)初识篇
开发者使用Java8编写复杂的集合处理算法,只需要简单的代码就能在多喝cpu上高效运行,这就是Lambda表达式的初衷. 提示:函数式编程和语言无关,它是一种思想,任何语言都可以实现函数式编程,区别只 ...
- 初识DOM
初识DOM 一. 节点的获取.插入.创建.删除.替换.修改文本节点 获取 1.document.getElementById 2.(element)document.getElementsByTagN ...
- MySQL两种存储引擎: MyISAM和InnoDB
MySQL两种存储引擎: MyISAM和InnoDB 简单总结 MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Access Me ...
- js-ES6学习笔记-Class
1.ES6提供了更接近传统语言的写法,引入了Class(类)这个概念,作为对象的模板.通过class关键字,可以定义类. 2. //定义类 class Point { constructor(x, y ...
- 【js实例】Array类型的9个数组方法,Date类型的41个日期方法,Function类型
前文提要:[js实例]js中的5种基本数据类型和9种操作符 Array类型的9个数组方法 Array中有9个数组方法: 1.检测数组 2.转换方法 3.栈方法 4.队列方法 5.冲排序方法6.操作方法 ...
- 《Spring实战》-- 'cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element' 错误的解决办法
在Eclipse中新建了一个maven项目学习Spring,在 service.xml 中配置 Spring,想要学习'面向切面的Spring',service.xml 内容如下: <beans ...
- Finereport和Finebi的区别
1.问题描述 大家现在可能都知道,目前帆软旗下特色产品主要是FineReport和FineBI,但是部分用户可能会有个疑问:FineReport和FineBI到底有什么区别? 2.产品介绍FineRe ...