描述:

  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.

代码:

  这个题写了好几遍"( ̄▽ ̄)""",老想着有时间上的优化,然后不停的推翻。

  动态规划的依赖于问题的两个重要性质:最优子结构和重叠子问题。如果想要在不同的子矩阵之间应用动态规划进行优化是有很大困难的。因为当你对一个子矩阵施以动规之后,在下一个包含这个子矩阵的更大的子矩阵想要利用这个结果,你得知道上一个子矩阵最优解的行列,以便组成一个新的最优解的矩阵。进一步思考产生了问题:无法证明大矩阵的最优解依赖于小矩阵的最优解。

  所以还是要挨个枚举所有的子阵。这里可以只枚举行,然后以列求和,转化为最大连续字串和的问题。

  比如对于子段:9 2 -16 2,temp[i]表示以ai结尾的子段中的最大子段和。在已知temp[i]的情况下,求temp [i+1]的方法是:

  如果temp[i]>0,temp [i+1]= temp[i]+ai(继续在前一个子段上加上ai),否则temp[i+1]=ai(不加上前面的子段,越加越小不如抛弃),然后记录temp产生的最大值即可。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
#define N 105
#define MIN -999999int main(){int matrix[N][N],n,b[N],temp,max_temp,max_sum;cin>>n;for( int i=1;i<=n;i++ ){for( int j=1;j<=n;j++ ){cin>>matrix[i][j];}}max_sum=MIN;for( int i=1;i<=n;i++ ){//开始行memset(b,0,sizeof(b));for( int j=i;j<=n;j++ ){//结束行for( int k=1;k<=n;k++ ){//列b[k]+=matrix[j][k];//将行为i-j的子阵的每列单独求和,转化为1维的最大字串和问题
            }//最大字串和temp=max_temp=0;for( int k=1;k<=n;k++ ){
                temp=(temp>0)?temp+b[k]:b[k];
                max_temp=(max_temp>temp)?max_temp:temp;}max_sum=max(max_sum,max_temp);}}printf("%d\n",max_sum);system("pause");return 0;
}

转载于:https://www.cnblogs.com/lucio_yz/p/4770430.html

POJ1050-To the Max相关推荐

  1. poj1050 To the Max

    真·水题. 我一开始还担心超时,自己打了个10^8的程序测时间,发现是1.06s左右,心说打个试一下,结果63msA了...... 二维前缀和直接暴力枚举,O(n^4) 1 #include < ...

  2. POJ-1050 To the Max 二维最大子段和

    题意 给我们一个二维矩阵 让我们在找出其中的最大子矩阵和 分析 对输入的一个矩阵 我们考虑一维线性矩阵 上的最大子段和 对于一个数串 我们的选择策略是 res = max(res,max( sum+a ...

  3. 《算法竞赛进阶指南(by 李煜东)》习题题解 集合

    又是笔者给自己挖的大坑. 这里是李煜东所著<算法竞赛进阶指南(by 李煜东)>的习题题解集合. 有任何错误请在对应文章下反馈或联系 nicest1919@163.com ,谢谢 qwq 从 ...

  4. 【算法笔记】【几何初步、数学初步、矩阵计算】ICPC训练联盟2021寒假冬令营(3)笔记

    题目链接 A题题解:枚举法+简单直线方程知识. 本题采取枚举方法,在[-500, 500]的范围内枚举A和B,将樱桃坐标代入直线方程Ax+By,如果Ax+By大于0,则樱桃在直线方:小于0,则樱桃在直 ...

  5. OI每周刷题记录——lrllrl

    看这标题就知道我是模仿的hzwer大佬,远程%%% 大佬的OI经历让蒟蒻我深受感触,为了晚一些AFO本蒟蒻也得加油了 从高二上期第一周开始计数,每个星期天更一次,一直更到我AFO 如果这是我此生最后一 ...

  6. NUC1157 To the Max【最大子段和+DP】

    To the Max 时间限制: 1000ms 内存限制: 65536KB 通过次数: 1总提交次数: 1 问题描述 Given a two-dimensional array of positive ...

  7. pytorch之expand,gather,squeeze,sum,contiguous,softmax,max,argmax

    目录 gather squeeze expand sum contiguous softmax max argmax gather torch.gather(input,dim,index,out=N ...

  8. PyTorch 笔记(08)— Tensor 比较运算(torch.gt、lt、ge、le、eq、ne、torch.topk、torch.sort、torch.max、torch.min)

    1. 常用函数 比较函数中有一些是逐元素比较,操作类似逐元素操作,还有一些类似归并操作,常用的比较函数如下表所示. 表中第一行的比较操作已经实现了运算符重载,因此可以使用 a>=b,a>b ...

  9. 关于maya与max互导FBX出现错误的解决方案。

    因为自己实在是不愿意一次又一次把时间浪费在导入导出的问题上.每一次都是多试几次才成功,也没有真正去测试这个东西.但是今天实在是碰到了错误中的极品了.最后还是决定写下来..算是给自己做笔记吧..大家如果 ...

  10. c++中求字符串数组的min/max

    1.函数:(作用:返回容器中最小值和最大值.) min_element() max_element max_element(first,end,cmp);其中cmp为可选择参数! 第三个参数cmp可写 ...

最新文章

  1. HA03-fence设置
  2. MySQL高级 - like模糊匹配
  3. restTemplate使用和踩坑总结
  4. CXF 调用C#.net的WebService
  5. CSDN中图片缩放与居中
  6. 开源数据库兴起,你需要了解的三件事
  7. Cuda-convnet配置指南 on Windows8.1+CUDA6.5+VS2013
  8. Atitit. 数据库-----catalog与schema的设计区别以及在实际中使用 获取数据库所有库表 java jdbc php  c#.Net
  9. python爬取妹子图片1_利用爬虫爬取清纯妹子图片
  10. 什么叫小米粒吸尘机器人_mac上的Automator小机器人的有什么妙用?
  11. 【linux运维】linux运维常用工具有哪些?
  12. 计算机word简历制作教程,用word制作个人简历的方法
  13. 使用phpquery采集小说
  14. matlab文本文件操作
  15. 云计算机是什么技术,什么是云计算技术?
  16. JPEG图像存储格式
  17. c语言微信昵称大全女生,微信昵称女生大全(精选210个)
  18. linux 多个csv合并成一个csv
  19. matlab中omega是什么意思_小鸡宝宝考考你南柯一梦中的南柯是什么意思?
  20. S3DIS数据集解析为点云

热门文章

  1. Microsoft Dynamics CRM Server 2011安装配置
  2. PAT1040。有几个PAT
  3. ROS学习笔记3(创建一个ROS Package)
  4. js 导出pdf上传至oss_前端上传图片到oss,压缩图片后上传至oss(补充图片文件旋转90度问题)...
  5. 微信昵称上标电话号码,实用的新玩法
  6. Messari:自2019年,DeFi领域因黑客攻击损失超2.84亿美元资产
  7. TokenInsight:反映区块链行业整体表现的TI指数较昨日同期下降3.29%
  8. 杭州滨江工作方案:将区块链等产业与“数字滨江”、“数字经济”紧密相连
  9. 今日恐慌与贪婪指数为83 贪婪程度大幅上升
  10. SAP License:家庭主妇与ERP