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. HDU 5904 LCIS

    $dp$. 这题的突破口在于要求数字是连续的. 可以分别记录两个串以某个数字为结尾的最长上升长度,然后枚举一下以哪个数字为结尾就可以得到答案了. 因为$case$有点多,不能每次$memset$,额外 ...

  2. Memcached帮助类

    一.如果用官方提供的方法,在web.config里面配置好了各个参数和服务器IP <?xml version="1.0"?> <configuration> ...

  3. ubuntu开放防火墙端口

    root@jbxue:$ sudo ufw enable  Firewall started and enabled on system startup  root@jbxue:$ sudo ufw ...

  4. Style绑定

    目的 style绑定可以添加或者移除DOM元素的样式值.这非常有用,例如,当值为负数时将颜色变为红色. (注:如果要修改CSS整个类,请使用css绑定) <div data-bind=" ...

  5. ajax jsonp 跨域请求

    $.ajax({ type:"get", url: "http://localhost/test/a.php", dataType: "jsonp&q ...

  6. C# 去除文件和文件夹的只读属性

    当我们使用 DirectoryInfo dir = Directory.CreateDirectory(pathName) 创建目录或者创建一个文件后,有时作为临时文件用完以后需要删除掉,使用File ...

  7. MyEclipse中用Maven创建Web项目

    方法/步骤     new --> other   1.Wizards: mvaen 2.Maven Project 3.Next   Use Default Workspace Locatio ...

  8. JAVA中数组总结(课堂总结)

    数组的特点: Arrays(数组)一种简单的数据结构元素具有相同的数据类型一旦创建之后,尺寸保持不变元素在内存中连续分布例子一:按引用与按值传递的示例源代码: // PassArray.java // ...

  9. hbase自带mapreduce计数表行数功能

    $HBASE_HOME/bin/hbase org.apache.hadoop.hbase.mapreduce.RowCounter ‘tablename’ mapreduce来计数,很快的!!!

  10. A. Grasshopper And the String(CF ROUND 378 DIV2)

    A. Grasshopper And the String time limit per test 1 second memory limit per test 256 megabytes input ...