`
Simone_chou
  • 浏览: 184638 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

To the Max(DP)

    博客分类:
  • POJ
 
阅读更多
To the Max
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 40625   Accepted: 21525

Description

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

 

      题意:

      给出 N(1 ~ 100),代表有一个 N X N 的矩阵,后给出这个 N X N 的矩阵,输出这个矩阵的最大子矩阵和。

 

      思路:

      DP。最大子段和是 dp [ i ] = max { dp [ i - 1 ] + num [ i ], num[ i ] },dp [ i ] 代表到 i 为止最大的子段和。

      对于矩阵的话,枚举行的首尾位置,后把列浓缩一个总和,走一遍最大字段和,同时比较出最大值即可。

 

       AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int INF = -100000000;
int num[105][105], sum[105][105];
int dp[105], ans[105];
int n;

int solve() {
    memset(dp, 0, sizeof(dp));

    int Max = INF;
    for (int i = 1; i <= n; ++i) {
        dp[i] = max(ans[i], dp[i - 1] + ans[i]);
        Max = max(Max, dp[i]);
    }

    return Max;
}

int main() {

    scanf("%d", &n);
    memset(sum, 0, sizeof(sum));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            scanf("%d", &num[i][j]);
            sum[i][j] = sum[i - 1][j] + num[i][j];
        }
    }

    int Max = INF;
    for (int i = 1; i <= n; ++i) {
        for (int j = i; j <= n; ++j) {

            for (int k = 1; k <= n; ++k) {
                ans[k] = sum[j][k] - sum[i - 1][k];
            }

            Max = max(Max, solve());
        }
    }

    printf("%d\n", Max);

    return 0;
}

 

 

分享到:
评论

相关推荐

    MaxTo(屏幕分割软件)注册版v17.07安装版(附注册码)

    MaxTo破解版是一款专业的屏幕分割软件,方便用户对于电脑屏幕进行更有效的规划以及分割,特别是对于程序员而言,能在同一时间观看多个窗口无疑能有效提升编写效率,小编为大家带来了MaxTo注册版,欢迎下载体验。...

    MAX4080.PDF

    The MAX4080/MAX4081 are high-side, current-sense amplifiers with an input voltage range that extends from 4.5V to 76V making them ideal for telecom, automotive, backplane, and other systems where high...

    pku acm 1050 To the Max代码,有详细注释

    pku acm 1050 To the Max代码,有详细注释,动态规划

    Using a PC to Experiment with the MAX7300/MAX7301 Port Expanders

    This application note describes a PC program, downloadable for free, to assist in the evaluation of the MAX7300 and MAX7301 GPIO (port expanders).

    Taking Your Android Tablets to the Max

    •How to take your tablet on the road •How to maintain your tablet •How to work and live with the cloud as part of your tablet experience •How to never worry about losing your pictures, music and ...

    MAX38902EVKIT仿真工具包

    General Description The MAX38902 evaluation kit (EV kit) evaluates the MAX38902A/B/C/D IC family of low noise linear regulators. The MAX38902 EV kit features two independent circuits to evaluate ...

    GMSL串行器MAX96705数据手册完整版

    The MAX96705 is a compact serializer with features especially suited for automotive camera applications. It is function and pin compatible with the MAX9271. In high bandwidth mode, the parallel-...

    MAX9288COAXEVKIT-MAX9290COAXEVKIT.pdf

    a proven design to evaluate the MAX9288 and MAX9290 high-bandwidth gigabit multimedia serial link (GMSL) deserializers with spread spectrum and full-duplex control channel, with the use of a ...

    DP7456完美代替MAX7456

    DP7456完美代替MAX7456

    Max3222-Max3241

    The MAX3222/MAX3232/MAX3237/MAX3241 transceivers have a proprietary low-dropout transmitter output stage enabling true RS-232 performance from a 3.0V to 5.5V supply with a dual charge pump. The ...

    MAX9288-MAX9290.pdf

    The MAX9288/MAX9290 gigabit multimedia serial link (GMSL) deserializers receive data from a GMSL serializer over 50Ω coax or 100Ω shielded twisted-pair (STP) cable and output deserialized data on ...

    3dMax离线帮助

    _Welcome to the MAXScript Reference! MAXScript是Autodesk 3ds Max®和Autodesk 3ds Max 3ds Max®Design的内置的脚本语言。 MAXScript is the built-in scripting language for Autodesk® 3ds Max® and ...

    MaxTo 分屏工具 2015.03.1

    MaxTo分屏工具 2015.03.1 MaxTo是一个窗口管理器,允许您定义屏幕区域,窗口会自动最大化,像素完美的窗口安排。充分利用屏幕空间。使用热键快速移动窗口。因为MaxTo你可以从你的电脑屏幕得到更多。理想的程序员,...

    MAX197.pdf

    The MAX197 multi-range, 12-bit data-acquisition system (DAS) requires only a single +5V supply...4V, 0V to 2V), see the MAX199 data sheet. For 12-bit bus interface, see the MAX196 and MAX198 data sheets.

    To the Max

    设计出一个程序可以找到矩阵的最大子矩阵,并且能求各项的和并输出矩阵。从一个一维数组中找到一段最大和的序列,例如0、-2、-7、0,然后再从一维的情况

    Boomer Labs MAX2AE

    鄄 highly accurate way to envision the AE layer relevant to your MAX 槽 鄄 scene. These 'helpers' can be animated just like any other MAX 槽 鄄 object, then used to output keyframe data to AE layers ...

    MaxtoMax跨场景模型复制粘贴插件-3dmax插件神器终结版-模渲CAD施工图大师

    Max to Max跨场景模型复制粘贴插件,是在3DsMax中进行不同效果图场景之间跨越来回切换,随时跨场景Max to Max跨场景模型复制粘贴不会丢失材质贴图的插件。以往设计师整合模型一向是个很麻烦的事,因为3dMax不像CAD、...

    max2990pdf

    The MAX2990 utilizes OFDM modulation techniques to enable robust data communication using the same electrical network that supplies power to all other devices on the network. The MAX2990 includes the ...

    dp7456 pin对pin代替max7456

    dp7456 pin对pin代替max7456,性能 价格更符合目前市场需求!

    elasticsearch启动后自动关闭:max virtual memory areas vm.max_map_count [65530] is too low, increase to at…

    elasticsearch启动后自动关闭:max virtual memory areas vm.max_map_count [65530] is too low, increase to at… elasticsearch 我遇到的问题是用docker 启动elasticsearch后会自动关闭,具体关闭时间点没注意,...

Global site tag (gtag.js) - Google Analytics