• Ural_1146. Maximum Sum (DP)


      /*题意是求最大子矩阵,表示线代没学好,好在不影响这道题。这题的主要思路就是二维化成一维。把二维数组前一列的元素累加到后边一列上,例如:

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

    处理后就是:
    0 -2 -9 -9
    9 11 5 7
    -4 -3 -7 -6
    -1 7 7 5

    然后就是
    dp[i][j] = max(dp[i][j-1], 0) + sum[j];

    My Code:
    */

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>

    using namespace std;

    const int N = 110;
    const int inf = 0x7fffffff;

    int sum[N][N];

    int main() {
    //freopen("data.in", "r", stdin);

    int n, i, j, x, max, flag, k;
    while(~scanf("%d", &n)) {
    memset(sum, 0, sizeof(sum));
    for(i = 1; i <= n; i++) {
    for(j = 1; j <= n; j++) {
    scanf("%d", &x);
    sum[i][j] = sum[i][j-1] + x;
    }
    }
    max = -inf;
    for(i = 1; i <= n; i++) {
    for(j = 1; j <= i; j++) {
    for(flag = 0, k = 1; k <= n; k++) {
    flag += sum[k][i] - sum[k][j-1];
    if(flag > max) max = flag;
    if(flag < 0) flag = 0;
    }
    }
    }
    printf("%d\n", max);
    }
    return 0;
    }



  • 相关阅读:
    JavaScript丨数组元素反转
    Executor框架浅析
    索引相关
    关系型数据库
    Cookie和Session
    HTTP和HTTPS
    TCP,UDP,HTTP
    OSI模型
    剑指Offer19
    剑指Offer03
  • 原文地址:https://www.cnblogs.com/vongang/p/2237235.html
Copyright © 2020-2023  润新知