[Algorithm analysis]-the largest sub-segment sum problem

Algorithm design column opens: transmission gate

Welcome all stars, of course, it will be custom: first like it and watch it, develop a habit~~~

Maximum sub-segment sum

Problem surface: Given an integer array a[n], find the maximum value of the sum of its consecutive subsections. When all integers are negative, the sum is 0.

Analysis: Let dp[i] represent a[i]the maximum value of the sum of the sub-segments ending with a number , then dp[i] = max(a[i], a[i]+dp[i-1]).

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int a[50005], n;
LL ans, dp[50005];
int main() {
    
    
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++) {
    
    
        dp[i] = max(dp[i-1] + a[i], (LL)a[i]);
        ans = max(ans, dp[i]);
    }
    cout << ans << endl;
    return 0;
}

Maximum sub-segment sum of loop array

Problem surface: Given a cyclic array a[n] composed of integers, find the maximum value of the sum of its consecutive subsections. When all integers are negative, the sum is 0.

Analysis: If the first and last elements are not crossed when the optimal value is obtained, then it can be regarded as the largest sub-segment sum of the ordinary array; otherwise, the smallest sub-segment sum can be calculated according to the ordinary array, and then the sum is subtracted from the smallest sub-segment sum. can.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n, a[50005], dp1[50005], dp2[50005];
LL ans1, ans2, sum;
int main() {
    
    
    cin >> n;
    for (int i = 1; i <= n; i++) {
    
    
        cin >> a[i];
        sum += a[i];
    }
    for (int i = 1; i <= n; i++) {
    
    
        dp1[i] = max(0LL, max(a[i], a[i]+dp1[i-1]));
        ans1 = max(ans1, dp1[i]);
        dp2[i] = min(0LL, min(a[i], a[i]+dp2[i-1]));
        ans2 = min(ans2, dp2[i]);
    }
    cout << max(ans1, sum-ans2) << endl;
    return 0;
}

Minimum positive subsection sum

Problem surface: Given an integer array a[n], select a subsequence from it, and the sum of the sequence is required to be greater than 0 and the smallest.

Analysis: Preprocess the prefix sum, binary search.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n, a[50005], p[50005], ans = LLONG_MAX;
map<LL,LL> mp;
int main() {
    
    
    cin >> n;
    for (int i = 1; i <= n; i++) {
    
    
        cin >> a[i];
        p[i] = a[i] + p[i-1];
        mp[p[i]] = i;
    }
    for (int i = 1; i <= n; i++) {
    
    
        auto it = mp.upper_bound(p[i-1]);
        if (it != mp.end() && it->second >= i) {
    
    
            ans = min(ans, it->first - p[i-1]);
        }
    }
    cout << ans << endl;
    return 0;
}

Maximum submatrix sum

Problem surface : Given a matrix of m*n, find a sub-matrix and require the sum of the elements of the sub-matrix to be the largest.

Analysis : Enumerate the upper and lower boundaries of the sub-matrix, fix the row and find the sum of the elements in each column, and convert it into the one-dimensional maximum sub-segment and the column range where the problem can be optimally solved. The prefix sum of each column can be preprocessed, which is convenient for quickly calculating the sum of each column element in the specified row range.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL m, n, a[505][505], d[505][505], x[505], dp[505], ans;
int main() {
    
    
    cin >> m >> n;
    for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
        cin >> a[i][j];
    for (int j = 1; j <= m; j++)
    for (int i = 1; i <= n; i++)
            d[i][j] = a[i][j] + d[i-1][j];
    for (int i = 1; i <= n; i++)
    for (int j = i; j <= n; j++) {
    
    
        for (int k = 1; k <= m; k++)
            x[k] = d[j][k] - d[i-1][k];
        for (int k = 1; k <= m; k++) {
    
    
            dp[k] = max(x[k], x[k] + dp[k-1]);
            ans = max(ans, dp[k]);
        }
    }
    cout << ans << endl;
    return 0;
}

Resource portal

  • Pay attention to [ Be a tender program ape ] public account
  • Reply to the [ python information ] [ 2020 Autumn Recruitment ] in the background of the [ Be a tender program ape ] public account to get the corresponding surprise!

「❤️ Thank you everyone」

  • Like to support it, so that more people can see this content (If you don’t like it, it’s all hooligans-_-)
  • Welcome to share your thoughts with me in the message area, and you are also welcome to record your thought process in the message area.

Guess you like

Origin blog.csdn.net/ywsydwsbn/article/details/109298102
Recommended