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. CentOS7 安装 OpenSSL 1.0.1m 和 OpenSSH 6.8p1

    # 下载软件 wget http://zlib.net/zlib-1.2.8.tar.gz wget ftp://ftp.openssl.org/source/openssl-1.0.1m.tar.g ...

  2. 回文质数 Prime Palindromes

    题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000 ...

  3. linux统计单词数

    sort +awk+uniq 统计文件中出现次数最多的前10个单词 实例 cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sor ...

  4. html-----vedio标签(HTML5新标签VIDEO在IOS上默认全屏播放)

    今天做一个app时发现一个问题,应用html5中的video标签加载视频,在Android手机上默认播放大小,但是换成iPhone手机上出问题了,默认弹出全屏播放,查找了好多论坛,都没有谈论这个的.然 ...

  5. jar中没有主清单属性

    在导出jar时指定主类位置 或手动配置jar包中的MANIFEST.MF文件: 添加Main-Class: gui 参考: JAVA环境变量 CLASSPATH .;%JAVA_HOME%\lib\d ...

  6. 浙大 pat 1003 题解

    1003. Emergency (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  7. 安卓---Tabhost实现页面局部刷新--父页子页之间的传值

    TabHost的实现分为两种,一个是不继承TabActivity,一个是继承自TabActivity:当然了选用继承自TabActivity的话就相对容易一些,下面来看看分别是怎样来实现的吧. 我只写 ...

  8. WinForm 进程和线程

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

  9. 自动化辅助工具Firebug和Firepath的安装

    1.安装firefox浏览器,点击主菜单,选择“附加组件” 2.搜索Firebug和firepath点击安装 3.安装后点击浏览器的主菜单-web开发者-firebug即可打开 4.或者在页面右键选择 ...

  10. 安装TensorFlow的步骤

    安装步骤: 1.安装虚拟机: 2.安装liunx系统: 3.安装TensorFlow. 1.安装虚拟机:虚拟机的版本是不能太低的.我使用的是:VMware-workstation-full-12.0. ...