zoukankan      html  css  js  c++  java
  • 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;
    }



  • 相关阅读:
    [纯奇技淫巧] 特征根
    杂题20200528
    杂题20200509
    杂题20200419
    杂题20200415
    杂题20200407
    杂题20200314
    Educational Codeforces Round 83 简要题解
    一种简单的dp trick
    杂题20200304
  • 原文地址:https://www.cnblogs.com/vongang/p/2237235.html
Copyright © 2011-2022 走看看