1146. Maximum Sum

Time limit: 1.0 second Memory limit: 64 MB
Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. 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. A sub-rectangle is any contiguous sub-array of size 1 × 1 or greater located within the whole array.
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-hand corner and has the sum of 15.

Input

The input consists of an N × N array of integers. The input begins with a single positive integer Non a line by itself indicating the size of the square two dimensional array. This is followed by N 2integers separated by white-space (newlines and spaces). These N 2 integers make up the array in row-major order (i.e., all numbers on the first row, left-to-right, then all numbers on 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 output is the sum of the maximal sub-rectangle.

Sample

input output
4
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
15

题意:输入部分第一行是一个正整数N,说明矩阵的大小。下一行后面跟着 个整数。留意这些整数的输入格式可能比较混乱。矩阵是以行优先存储的。N可能和100一样大,矩阵中每个数字的范围为 [-127, 127].

思路1:对矩阵进行穷举。找出所有的子矩阵计算矩阵和并找出最大值。

 #include<iostream>
#include<cstdio>
#include<string>
#include<cstring> using namespace std;
int s[][]= {};
int dp[][]= {}; int main()
{
// freopen("1.txt","r",stdin);
int i,j,ki,kj;
int n;
while(cin>>n)
{
for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
cin>>s[i][j];
s[i][j]+=s[i-][j];
s[i-][j]+=s[i-][j-];
}//输入矩阵并且计算从(1,1)到(i-1,j)的矩阵的和值
for(i=; i<=n; i++)
s[n][i]+=s[n][i-];//计算从(1,1)到(n,i)的矩阵的和值
int max=-;//特别注意因为
for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
max=-;
for(ki=; ki<i; ki++)
for(kj=; kj<j; kj++)
if(max<s[i][j]-s[i][kj]-s[ki][j]+s[ki][kj])//计算(ki,kj)到(i,j)的子矩阵的和并且和max比较;
max=s[i][j]-s[i][kj]-s[ki][j]+s[ki][kj];
dp[i][j]=max;
}
max=-;
for(i=; i<=n; i++)
for(j=; j<=n; j++)
if(max<=dp[i][j])
max=dp[i][j];
cout<<max<<endl;
}
return ;
}

思路2求最大子矩阵的和,先按列进行枚举起始列和结束列,用数据够快速求出每行起始列到结束列之间各数据的和,然后就可以降成一维的,问题就变成了求最大连续子序列了

 #include<cstdio>
#include<cstring>
#include<iostream> using namespace std; #define N 1100
#define INF 0x3f3f3f3f
int s[N][N]= {},dp[N],n; void get_sum(int x ,int y)
{
for(int i=; i<=n; i++)
{
dp[i]=s[y][i]-s[x-][i];
}
return ;
} int DP()
{
int sum=,max=-INF;
for(int i=; i<=n; i++)
{
sum+=dp[i];
max=sum>max?sum:max;
if(sum<) sum=;
}
return max;
} int main()
{
int i,j;
// freopen("1.txt","r",stdin);
while(cin>>n)
{
for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
cin>>s[i][j];
s[i][j]+=s[i-][j];
}//输入矩阵并且计算从(1,j)到(i,j)的矩阵的和值
int max=-INF,ans;
for(i=; i<=n; i++)
for(j=i; j<=n; j++)
{
get_sum(i,j);
ans=DP();
max=ans>max?ans:max;
}
printf("%d\n",max);
}
return ;
}

ural 1146. Maximum Sum(动态规划)的更多相关文章

  1. 最大子矩阵和 URAL 1146 Maximum Sum

    题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...

  2. ural 1146. Maximum Sum

    1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...

  3. URAL 1146 Maximum Sum(最大子矩阵的和 DP)

    Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...

  4. URAL 1146 Maximum Sum(DP)

    Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the large ...

  5. URAL 1146 Maximum Sum & HDU 1081 To The Max (DP)

    点我看题目 题意 : 给你一个n*n的矩阵,让你找一个子矩阵要求和最大. 思路 : 这个题都看了好多天了,一直不会做,今天娅楠美女给讲了,要转化成一维的,也就是说每一列存的是前几列的和,也就是说 0 ...

  6. URAL 1146 Maximum Sum 最大子矩阵和

    题目:click here #include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; con ...

  7. Timus 1146. Maximum Sum

    1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...

  8. POJ 2479 Maximum sum 解题报告

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40596   Accepted: 12663 Des ...

  9. [LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

随机推荐

  1. 博客搬到CSDN了,以后就老实的呆在这儿吧~~

    几年前读书的时候就自己在做独立的个人博客网站,重做 + 改版好多次,域名也换了好几个- 163fly.com.godbz.com.zhouz.me ... 都是我曾经用过的域名,都放弃了- 发现到头来 ...

  2. POJ 3111 K Best

    二分,排序,贪心. 最优比率生成树,可以二分$+$贪心来实现,不过这样做精度不行. 如果是这样一个问题,该如何解决:问你$n$个里面选择$k$个,能否使得$\frac{{\sum\limits_{j ...

  3. PHP学习过程_Symfony_(4)_命令创建实体_以及实体关系

    //项目运行php app/console server:run//创建实体php app/console doctrine:generate:entitybundle名称:实体名称例如:Symfon ...

  4. Proxy SwitchySharp chrome网络代理【转】

    Proxy SwitchySharp chrome网络代理插件概述 SwitchySharp 是 Google Chrome 浏览器上的一个代理管理扩展程序,是一款可以自己设置谷歌浏览器使用方式的ch ...

  5. WinForm 进程和线程

    进程: //进程用到的类Process,需要进行解析 using System.Diagnostics Process.Start("calc");//Process是非静态方法, ...

  6. Python笔记4-20151029

    一.切片 L = [''Michael','Sarah','Tracy','Bob','Jack'] 取前N个元素,也就是索引为0-(N-1)的元素,可以用循环: >>> r = [ ...

  7. 万恶的tileMap

    先吐槽下.. 本来,我们准备用tileMap来做地图的,但发现一个问题,就是tileMap层中不能添加cc.Sprite,这导致了tileMap只适合做2D平面没有遮挡的游戏,并且主角是不能有效率的进 ...

  8. Linux中的shell函数编写

    function huge_cp() { while read line1; do cp $line1 ../; done; } function huge_rm() { while read lin ...

  9. kafka_2.11-0.10.0.0安装步骤

    Kafka安装配置 我们使用5台机器搭建Kafka集群: 1. cluster-1-namenode-1-001       172.16.0.147 2. cluster-1-datanode-1- ...

  10. Linux服务器rsync自动备份

    一.在 server 端配置 1. 编辑配置文件 #vi /etc/rsyncd.conf 添加下面的配置参数: uid = nobody # 该选项指定当该模块传输文件时守护进程应该具有的uid.默 ...