`
Simone_chou
  • 浏览: 192671 次
  • 性别: 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;
}

 

 

分享到:
评论

相关推荐

    android-viewflow

    The default sidebuffer is 3, making up a grand total of 7 (3 * 2 + 1) Views loaded at a time (at max). To be able to use the more convenient app:sidebuffer attribute, the application namespace must ...

    leetcode中国-DP:DP

    max and min element of an array 4. Given an array which consists of only 0, 1 and 2. Sort the array without using any sorting algo 5. Move all the negative elements to one side of the array 6. Find ...

    ViewPager 放大缩小左右移动

    * user begins dragging, when the pager is automatically settling to the * current page, or when it is fully stopped/idle. * * @param state * The new scroll state. * @see ViewPager#...

    occam一维反演

    c optimization routines, we leave it to the user to find the response of c the final model, using low cunning or the program 'FindResponse.f'. c c includes: include 'imp.inc' include 'occamdim.inc' ...

    poj题目分类...

    * 1050 To the Max * 1088 滑雪 * 1125 Stockbroker Grapevine * 1141 Brackets Sequence * 1159 Palindrome * 1160 Post Office * 1163 The Triangle * 1458 Common Subsequence * 1579 Function Run Fun * 1887 ...

    最长公共子序列的动态规划算法

    dp[i][j] = max(dp[i-1][j], dp[i][j-1]); } } } // Backtrack to find the LCS int index = dp[m][n]; string result(index, ' '); char* lcsStr = &result[0]; int i = m, j = n; while (i &gt; 0 && j &gt;...

    Android代码-带波动效果的索引侧边栏,支持左右手模式和自定义索引

    Include the WaveSideBar to Your Project With gradle: dependencies { compile 'com.gjiazhe:wavesidebar:1.3' } Use WaveSideBar in Layout File Description of Attributes Attributes Format Default ...

    ZJU_ACM_All_Anwer 搞编程的都知道的浙江大学A 题库.本书 集了所有经 Z 题解集,集合并附 Mathimaticsumerical algorithms 数值算法

    1062 Trees Made to Order 简单题 1070 Bode Plot 简单题 1073 Round and Round We Go 简单题,142857,我喜欢^_^ 1078 Palindrom Numbers 简单题 1086 Octal Fractions 简单题 1199 Point of Intersection...

    浙江大学ACM题解/ZJU 题型分类

    1062 Trees Made to Order 简单题 1070 Bode Plot 简单题 1073 Round and Round We Go 简单题,142857,我喜欢^_^ 1078 Palindrom Numbers 简单题 1086 Octal Fractions 简单题 1199 Point of Intersection...

    用单调性优化动态规划

    状态转移方程为\(dp[i] = max(dp[j] + profit[i])\),其中\(profit[i]\)表示第\(i\)天生产产品的利润,\(j )。通过使用单调队列,可以有效地找到\(dp[j]\)的最大值,从而快速完成状态转移。 **【例二】Cut the ...

    poj题目类型总结(每题用到的算法)

    - **1054 (To the Max)**:本题涉及图的遍历(DFS/BFS)和最短路径算法,目的是找到图中两个点之间的最大边权值路径。 ### 5. 字符串处理 - **1159 (Palindrome)**:这道题考察的是字符串处理中的回文串判断,可以...

    北京大学acm题库 题目分类

    相关题目有:1037 A decorative fence、1050 To the Max、1088 滑雪、1125 Stockbroker Grapevine、1141 Brackets Sequence、1159 Palindrome、1160 Post Office、1163 The Triangle、1458 Common Subsequence、1579...

    北大ACM分类 北大ACM分类

    例如题目1037 "A decorative fence" 和 11050 "To the Max"。 3. **贪心算法**:贪心算法是在每一步选择中都采取在当前状态下最好或最优的选择,希望导致结果是全局最好或最优的。如题目1042、1065等。 4. **图论*...

    ural vol I 题解 by yuhch123 pdf

    dp[j] = max(dp[j], dp[j-item weight] + item value) end for print(dp[capacity]) ``` #### 1006 ké Ý/§ **题目概述:** 本题要求解决一个概率统计问题,需要分析给出的概率分布情况。 **解题思路:*...

    Java 动态规划求解TSP问题

    double result = Double.MAX_VALUE; for (int city = 0; city ; city++) { if (city == startCity) continue; result = Math.min(result, dp[(1 ) - 1][city] + distanceMatrix[city][startCity]); } return ...

    K_Mean_Java.rar_algorithms

    // check if centroids have changed significantly or max iterations reached } } ``` 在这个过程中,我们需要实现`assignToNearestCluster`和`computeNewCentroid`方法,它们分别用于数据点的分配和质心的更新...

    POJ 1062 昂贵的聘礼 解题报告

    Code to read input and construct the graph ... // ... Apply Dijkstra's algorithm for each valid range and find the minimum cost ... return 0; } ``` #### 总结 通过对题目描述的理解,我们可以将其...

    国外FDI公司LPC3250开发板原理图完整版

    - **LCD Used in 18-bit Mode Max**: 液晶显示器接口,这里指明了最大支持18位的颜色深度。 - **USB OTG PHY**: USB On-The-Go (OTG)物理层接口,支持USB OTG功能。 - **256MB NAND Flash**: 内置256MB的NAND闪存,...

    LeetCode最全代码

    (Notes: "馃摉" means you need to subscribe to [LeetCode premium membership](https://leetcode.com/subscribe/) for the access to premium questions.) ## Algorithms * [Bit Manipulation]...

Global site tag (gtag.js) - Google Analytics