集训第五周动态规划 F题 最大子矩阵和

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.

Input

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

Output the sum of the maximal sub-rectangle.

Sample Input

4
0 -2 -7 0 9 2 -6 2
-4 1 -4  1 -1

8  0 -2

Sample Output

15

这道题其实就是求最大子段和,需要把原题数据变化一下,例如0 -2 -7 0   9 2 -6 2 -4 1 -4 1 -1 8 0 -2 这个矩阵我选择的是9 2-4 1-1 8那么我还可以把这个选择过程看待为求数组 4 11 -10 1 的最大子段和,很显然是选择4 11,答案为15那么4 11 -10 1是怎么来的呢,是我把2 3 4行数组组合成一个数组得来的那么这道题的解法就出来了,不断枚举行区间,得到一个新数组,然后求最大子段和
#include"iostream"
#include"cstring"
using namespace std;
const int maxn=110;
int b[maxn],a[maxn][maxn];
int n;

void Init()
{
    int t;
    memset(a,0,sizeof(a));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>t;
            a[i][j]=a[i-1][j]+t;
        }
    }
}

int main()
{
    while(cin>>n)
    {
     Init();
     int sum,ans,temp;
     sum=0;
     ans=-1000000000;
     int c=1;
     for(int i=1;i<=n;i++)
     for(int j=i;j<=n;j++)
     {
     sum=0;
     for(int k=1;k<=n;k++)
     {
         temp=a[j][k]-a[i-1][k];
         sum+=temp;
         if(sum>ans)
         {
             ans=sum;
         }
         if(sum<0)
         {
             sum=0;
         }
     }
     }
      cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-10-09 17:35:42

集训第五周动态规划 F题 最大子矩阵和的相关文章

集训第五周动态规划 H题 回文串统计

Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A sequence of characters is a palindrome if it is the same written forwards and backwards. For example, 'abeba' is a palindrome, but 'abcd' is not.A pa

集训第五周动态规划 I题 记忆化搜索

Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小.在上面的例子

集训第五周动态规划 G题 回文串

Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into t

集训第五周动态规划 J题 括号匹配

Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and if a and b are regul

集训第五周动态规划 K题 背包

Description Now you are asked to measure a dose of medicine with a balance and a number of weights. Certainly it is not always achievable. So you should find out the qualities which cannot be measured from the range [1,S]. S is the total quality of a

2018暑假集训第五周感想

第五周有点漫长..题打得有点磨,急躁,自卑等等负面情绪不断出来(ㄒoㄒ) 线段树真难,dp也真难..如果线段树是有思路实现不了,dp就是完全没思路,核心思想就是找一个转移方程,然而ヽ(´¬`)ノ 寻找dp的转移方程真是一个艰难的过程,同时还伴随着恐怖的状态压缩,也没有什么固定的套路和方法,只能靠多练习和领悟了(?•ω•?) dp也就是动态规划是针对一类最优解的算法,核心思想是类似分治,把一个问题分解成若干个子问题,通过每一个子问题的最优决策得到最优解(- ̄▽ ̄)- dp的实现有递推,也有记忆化搜

集训第五周D题 LCS

Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germa

第十五周oj刷题——Problem F: C++习题 商品销售

Description 商店销售某一商品,每天公布统一的折扣(discount).同时允许销售人员在销售时灵活掌握售价(price),在此基础上,一次购10件以上者,还可以享受9.8折优惠.现已知当天m个销货员销售情况为 销货员号(num)            销货件数(quantity)       销货单价(price) 101                                              5                            23.5 10

程序设计实习MOOC / 继承和派生——编程作业 第五周程序填空题1

描述 写一个MyString 类,使得下面程序的输出结果是: 1. abcd-efgh-abcd- 2. abcd- 3. 4. abcd-efgh- 5. efgh- 6. c 7. abcd- 8. ijAl- 9. ijAl-mnop 10. qrst-abcd- 11. abcd-qrst-abcd- uvw xyz about big me take abcd qrst-abcd- 要 求:MyString类必须是从C++的标准类string类派生而来.提示1:如果将程序中所有 "My