随手练——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个格子,允许上下左右走,每次走的格子 ...
随机推荐
- [日常] Go语言圣经--作用域,基础数据类型,整型
go语言圣经-作用域 1.一个声明语句将程序中的实体和一个名字关联,比如一个函数或一个变量 2.一个变量的生命周期是指程序运行时变量存在的有效时间段;声明语句的作用域对应的是一个源代码的文本区域,它是 ...
- ubuntu 17.10 安装后的应用软件安装
目录 安装 sogou 拼音 安装markdown编辑器 安装codeblocks 下载工具uGet+aira2 安装QT 安装remarkable(markdown工具) 安装StarUML(UML ...
- HDU3359(SummerTrainingDay05-I 高斯消元)
Kind of a Blur Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 【学习笔记】--- 老男孩学Python,day3 while 循环 运算符 逻辑、赋值运算
1. 循环. while循环 while 条件: 代码块(循环体) 执行流程: 1. 判断条件是否为真. 如果真. 执行代码块 2. 再次判断条件是否为真...... 3. 当条件为假.执行else ...
- python学习之老男孩python全栈第九期_day004知识点总结
1. 列表list: 列表转换成字符串: s = 'kidd' s1 = '_'.join(s) # 用_连接 字符串转换成列表: split() range(头,尾,步长): [0,1,2,3,4, ...
- format格式化字符串
假如想要表达这样一条语句:李明今年十二岁 输出这样一条语句 name = 'LiMing' age = 12 print( name + 'is' + age + 'years old') #输出 L ...
- ArrayList初步
使用ArrayList,需添加引用:using System.Collections: 第一个例子: ArrayList list = new ArrayList(); list.Add(" ...
- The D Programming Language 书评
此书的作者 Andrei Alexandrescu 作为前 C++ 社区的一朵奇葩,因为实在是不满 C++ 标准委员会的官僚作风,跳槽到了 D 社区,成为了 D 发明人 Walt Brightman ...
- ActiveReports 报表应用教程 (15)---报表换肤
在葡萄城ActiveReports报表中,可以设置报表中不同控件的样式,然后把这些样式保存到一个外部的XML文件当中,供其他报表使用.如果用户希望同一份报表以不用的外观分发,只需要简单地修改样式表单, ...
- 留言板0.4_model中的数据库(1)
1.先在数据库中加入一天测试数据先 2.在model的"views"中载入数据库和model的类 import pymysql from .models import UserMe ...