题干:

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the 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

The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines). 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

Output the sum of the maximal sub-rectangle.

Sample Input

4
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2

Sample Output

15

解题报告:

给一个N*N的矩阵,求最大子矩阵和。

AC代码1:(o(n^4)的复杂度)

#include<cstdio>
using namespace std;
int main()
{int c,res=0,k;int d[100][100];int s[101],a[100];while(scanf("%d",&c)==1) {for(int i=0; i<c; i++) {for(int j=0; j<c; j++) {scanf("%d",&d[i][j]);}}for(int i=0; i<c; i++) {for(int j=(i+1); j<c; j++) {for(int l=0; l<100; l++) {a[l]=0;s[l]=0;}for(k=0; k<c; k++) {for(int m=i; m<=j; m++)a[k]+=d[m][k];if(s[k]>=0)s[k+1]=s[k]+a[k];elses[k+1]=a[k];}for(int i=0; i<k; i++) {if(res<s[i])res=s[i];}}}printf("%d",res);}
}

AC代码2:(o(n^3)的复杂度)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int maze[105][105];
int sum[105],tmp[105];
int main()
{int n;int maxx = 0;scanf("%d",&n);memset(sum,0,sizeof sum);for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {scanf("%d",&maze[i][j]);if(sum[i-1] >=0) sum[i] = sum[i-1] + maze[i][j];else sum[i] = maze[i][j];maxx = max(maxx,sum[i]);}}//上面内容表示单行的最大字段和 for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) tmp[j] = maze[i][j];for(int j = i+1; j<=n; j++) {memset(sum,0,sizeof sum);for(int k = 1; k<=n; k++) {tmp[k] += maze[j][k];if(sum[k-1] >=0) sum[k] = sum[k-1] + tmp[k];else sum[k] = tmp[k];maxx = max(maxx,sum[k]); }}} printf("%d\n",maxx);return 0 ;
}

【POJ - 1050】To the Max (dp)相关推荐

  1. 【POJ 1187】 陨石的秘密(dp)

    题目 Description 公元11380年,一颗巨大的陨石坠落在南极.于是,灾难降临了,地球上出现了一系列反常的现象.当人们焦急万分的时候,一支中国科学家组成的南极考察队赶到了出事地点.经过一番侦 ...

  2. 【ZOJ - 2972】Hurdles of 110m (dp)

    题干: In the year 2008, the 29th Olympic Games will be held in Beijing. This will signify the prosperi ...

  3. 【 HDU - 5459】Jesus Is Here(dp)

    题干: I've sent Fang Fang around 201314 text messages in almost 5 years. Why can't she make sense of w ...

  4. 【POJ 1286】Necklace of Beads(polya定理)

    [POJ 1286]Necklace of Beads(polya定理) Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  5. 【知识总结】多项式全家桶(一)(NTT、加减乘除和求逆)

    我这种数学一窍不通的菜鸡终于开始学多项式全家桶了-- 必须要会的前置技能:FFT(不会?戳我:[知识总结]快速傅里叶变换(FFT)) 以下无特殊说明的情况下,多项式的长度指多项式最高次项的次数加\(1 ...

  6. 【数据库系统设计】关系数据库标准语言SQL(3)

    关系数据库标准语言SQL 数据更新 插入数据 插入元组 插入子查询结果 修改数据 修改某一个元组值 删除数据 删除某一个元组的值 删除多个元组的值 带子查询的删除语句 空值的处理 空值的产生 空值的判 ...

  7. 【数据库系统设计】关系数据库标准语言SQL(2)

    关系数据库标准语言SQL 数据查询(连接查询) 等值连接 `=` 自然连接 自身连接 外连接 `LEFR/RIGHT JOIN ... ON` 多表连接 数据查询(嵌套查询 ) 带有`IN`谓词的子查 ...

  8. 【数据库系统设计】关系数据库标准语言SQL(1)

    关系数据库标准语言SQL SQL介绍 SQL的特点 SQL中基本概念 示例:学生-课程 数据库 数据定义 SCHEMA定义 基本表定义(重点) 定义基本表(关系模式) 数据类型 定义基本表示例 修改基 ...

  9. 【深度学习】生成对抗网络(GAN)的tensorflow实现

    [深度学习]生成对抗网络(GAN)的tensorflow实现 一.GAN原理 二.GAN的应用 三.GAN的tensorflow实现 参考资料 GAN( Generative Adversarial ...

最新文章

  1. 小试“ASUS WL-500W无线路由”
  2. 图像特征检测描述(一):SIFT、SURF、ORB、HOG、LBP特征的原理概述及OpenCV代码实现
  3. 数据挖掘流程(三):特征工程
  4. [云炬创业学笔记]第一章创业是什么测试3
  5. Python 面向监狱编程,就靠它了
  6. 下雨天我叫了顿外卖,就成了人渣?
  7. 带你读论文丨基于视觉匹配的自适应文本识别
  8. 【kafka】Flink 消费 kafka Received unknown topic topic/partition may not exist Describe access to it
  9. 核心对象+持久对象全析(3)
  10. 三层交换机启用OSPF后,如何实现数据转发路径
  11. mustache.js html模板,js模板引擎Mustache将h5模板页面转化为小程序页面
  12. 【QT】QT从零入门教程(十六):QSS样式表
  13. 启用计算机浏览器摄像头,如何启用浏览器进行摄像头访问?
  14. Callable接口与runable和Thread类
  15. 数据结构 队列Queue
  16. 编程基础(三)——体系结构之三
  17. 单片机实现TM1620驱动 含完整程序源码
  18. Python 库 Geopy 的用法,经纬度坐标转换、经纬度距离计算
  19. H3C路由器-内/外网用户通过公网IP访问内部服务器
  20. Android Bluetooth HCI log 详解

热门文章

  1. Not Equal on a Segment CodeForces - 622C
  2. python基础公式_一、Python基础(数据类型、基本函数、基本运算)
  3. 用批处理实现文本文件中指定字符串的替换 zz
  4. 专属海报小程序_剑3泡泡 | 小程序给你一份专属的账号海报!
  5. VS集成Qt开发入门(简易时间显示)
  6. jdk1.8配置(自我速成)
  7. Python中曲率与弯曲的转换_黎曼几何学习笔记(3)——共形数量曲率与高斯曲率...
  8. selenium禁止弹窗_python:使用带有selenium的firefox时禁用下载弹出窗口
  9. if 组件是否存在_UE4 UMG简介+Slate组件问题排查
  10. linux和python的关系_Python、Linux与我的缘分