To the Max

时间限制: 1000ms 内存限制: 65536KB

通过次数: 1总提交次数: 1

问题描述

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.

输入描述
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 the sum of the maximal sub-rectangle.
样例输入
4
0 -2 -7 0 9 2 -6 2
-4 1 -4  1 -18  0 -2

样例输出
15

来源
Greater New York 2001

问题分析:(略)

这个问题和《HDU1081 POJ1050 LA2288 ZOJ1074 To The Max【最大子段和+DP+滑动窗口法】》是同一个问题,代码直接用就AC了。

程序说明:参见参考链接。

参考链接:HDU1081 POJ1050 LA2288 ZOJ1074 To The Max【最大子段和+DP+滑动窗口法】

题记:程序做多了,不定哪天遇见似曾相识的。

AC的C++程序如下:

/* UVALive2288 POJ1050 HDU1081 ZOJ1074 To The Max */#include <iostream>
#include <limits.h>
#include <string.h>using namespace std;const int N = 100;
int a[N][N], b[N];int main()
{int n, maxval;while(cin >> n) {for(int i=0; i<n; i++)for(int j=0; j<n; j++)cin >> a[i][j];maxval = INT_MIN;for(int i=0; i<n; i++) {memset(b, 0, sizeof(b));for(int j=i; j<n; j++) {int sum = 0;for(int k=0; k<n; k++) {b[k] += a[j][k];if(sum + b[k] > 0)sum += b[k];elsesum = b[k];maxval = max(maxval, sum);}}}cout << maxval << endl;}return 0;
}

NUC1157 To the Max【最大子段和+DP】相关推荐

  1. UVA10074 Take the Land【最大子段和+DP】

    The poor man went to the King and said, "Lord, I cannot maintain my family. Please give me some ...

  2. Bailian2766 最大子矩阵【最大子段和+DP】

    最大子矩阵 总时间限制: 1000ms 内存限制: 65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 ...

  3. 51Nod-1051 最大子矩阵和【最大子段和+DP】

    1051 最大子矩阵和 基准时间限制:2秒 空间限制:131072KB 分值:40难度:4级算法题 一个M*N的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值. 例如 ...

  4. hdu 1024 Max Sum Plus Plus(dp 最大m子段和)

    题意是输入m,n. m为你要求的子段个数,n为数据个数. 由于是很早的题型了,但是理解起来还是很是无力. 并于是用了三天来搞懂此类问题.发现网上大多代码无思路整个过程. 就大致讲解一下DP的整个思路. ...

  5. HDU1024 Max Sum Plus Plus【最大子段和+DP】

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...

  6. Luogu P1115 最大子段和(dp 贪心)

    P1115 最大子段和 题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大. 输入输出格式 输入格式: 输入文件maxsum1.in的第一行是一个正整数N,表示了序列的长度. 第2行包含N ...

  7. 51Nod-1050 循环数组最大段和【最大子段和+最小子段和+DP】

    1050 循环数组最大子段和 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 N个整数组成的循环序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+ ...

  8. HDU 1024 ~ Max Sum Plus Plus (DP + XJB优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意:给你 n 个数字,你可以从中选m个子段(子段不能相交).让选出的子段和和最大. 题解:明显 ...

  9. Max Sum(经典DP)

    求最长总和序列,状态转移方程:dp[i] = max(dp[i-1]+a[i].a[i]) 因为可能有负数,所以要判断dp是否大于0,如果小于0则序列中断,从中断点开始 起始点可以用数组s保存,有中断 ...

最新文章

  1. 关于Unity3D中鼠标移动指定物体的解决方案
  2. Session Sticky
  3. Linux下sed命令替换配置文件中某个变量的值(改变包含字符的一行的值)
  4. java锁_Java锁
  5. “约见”面试官系列之常见面试题第二十三篇之get和post区别(建议收藏)
  6. 基于java ssm springboot选课推荐交流平台系统设计和实现
  7. 《深入理解 Spring Cloud 与微服务构建》第四章 Dubbo
  8. 互联网的大厂就那么几家,所以很多员工跳槽后都是互相流动的
  9. 2018贝壳找房研发校招笔试题
  10. implements Serializable有什么作用
  11. 【Python】 html解析BeautifulSoup
  12. SHELL下根据进程号得到内存,并截取为整数
  13. Python 画数学函数图像 matplotlib.pyplot
  14. Excel对比重复项
  15. TMS570-3-RTI中断
  16. uint16 累加_在一个驱动程序中看到uint16,uint32,unit8,int8是什么意思?有何作用?...
  17. linux系统下html工具,Linux下五个好用的HTML编缉器
  18. (C语言)对称数判断
  19. 尚硅谷 --佟刚 SpringData JPA
  20. 计算机键盘录入指法视频,第一章计算机文录入键盘指法.ppt

热门文章

  1. MUI+Htmlplus开发APP实现页面之间传值
  2. GhostCat - AS3工具类库(AS3 library of generic tools)
  3. arrays中copyof复制两个数组_Core Java - Arrays
  4. 【java学习之路】(java SE篇)005.面向对象基础入门part2
  5. Spring boot的helloword
  6. Android复习准备
  7. 中考英语听说计算机考试满分,2020北京中考英语听说机考题型分值及满分技巧...
  8. android firefox 版本,Android版本Firefox初期预览版发布
  9. premium导入后缀为db_Python 导入txt文件到mongo
  10. 在Navicat中修改表结构