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. CentOs + Nginx + php-fpm + MySql 依赖库安装

    依赖库和开发工具 yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype free ...

  2. Linux c 内存高速访问

    概述 要想高速利用内存就必须高效利用cpu cache,关于cpu cache这里就不多加讨论了,自己感兴趣可以google 而cpu访问内存的单位是cache line,因此高效利用cache li ...

  3. [Mark] KVM 虚拟化基本原理

    X86 操作系统是设计在直接运行在裸硬件设备上的,因此它们自动认为它们完全占有计算机硬件.x86 架构提供四个特权级别给操作系统和应用程序来访问硬件. Ring 是指 CPU 的运行级别,Ring 0 ...

  4. sql convert() 函数

    convert: 时间格式转换为其他时间格式的函数 CONVERT ( data_type [ ( length ) ] , expression [ , style ] )   data_type: ...

  5. angular localStorage使用方法

    var YourCtrl = function($scope, localStorageService, ...) { // To add to local storage localStorageS ...

  6. 如何取消input记忆功能

    默认情况下,input会有这个记忆功能,如果不想让它记忆,可以在input上加上autocomplete="off"即可.

  7. 《JavaScript高级程序设计》读书笔记 ---Array 类型

    除了Object 之外,Array 类型恐怕是ECMAScript 中最常用的类型了.而且,ECMAScript 中的数组与其他多数语言中的数组有着相当大的区别.虽然ECMAScript 数组与其他语 ...

  8. hadoop 2.2.0 关于map和reduce的个数的设置

    关于hadoop中的map过程,我的理解是每一个map系统会开启一个JVM进程来处理,map之间相互并行,map函数内串行.这样的想法是否正确? 由于想在hadoop集群上算一个初始输入数据不多,但是 ...

  9. droidcon 北京2016安卓技术大会——安卓领域国际盛会

    目前droidcon国际技术大会已成为安卓领域全球最有影响力.规模最大的技术大会,每年在世界各地举办,横跨四大洲,超过上万人次参加. droidcon国际技术大会于2009年由一个Android爱好者 ...

  10. Velocity China 2016 Web 性能与运维大会:构建快速、可扩展的弹性网站

    Velocity China 2016 Web 性能与运维大会是一场关于构建快速.可扩展的弹性网站所需要的Web性能.运维及开发运维的训练.大会将于2016年12月1日在北京拉开帷幕,此次大会被众多业 ...