📌  相关文章
📜  执行给定操作后数组所有元素的最大和

📅  最后修改于: 2021-04-24 15:24:56             🧑  作者: Mango

给定一个整数数组。任务是在一次执行给定的两个操作后,找到数组所有元素的最大和。
操作是:

例子:

Input : arr[] = {-1, 10, -5, 10, -2}
Output : 18
After 1st operation : 1 10 -5 10 -2
After 2nd operation : 1 10 -5 10 2

Input : arr[] = {-9, -8, -7}
Output : 24
After 1st operation : 9 8 -7
After 2nd operation : 9 8 7

方法:可以使用以下思路在线性时间内解决此问题:

  • 令元素A1 .. An的总和等于S。然后当反转符号时,我们得到-A1,-A2 .. -An,此后总和更改为-S,即段上元素的总和将发生变化反转整个线段的符号时的符号。
  • 考虑如下的初始问题:选择一个连续的子序列,然后将剩余的所有数字求反。
  • 使用Kadane算法找到最大子数组总和。
  • 保持该子数组完整无缺,然后将其余的乘以-1。
  • 考虑到整个数组的和为S,最大和连续子数组为S1,则总和等于-(S-S1)+ S1 = 2 * S1-S。这是必需的和。

下面是上述方法的实现:

C++
// CPP program to find the maximum
// sum after given operations
 
#include 
using namespace std;
 
// Function to calculate Maximum Subarray Sum
// or Kadane's Algorithm
int maxSubArraySum(int a[], int size)
{
    int max_so_far = INT_MIN, max_ending_here = 0;
 
    for (int i = 0; i < size; i++) {
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
 
        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}
 
// Function to find the maximum
// sum after given operations
int maxSum(int a[], int n)
{
    // To store sum of all elements
    int S = 0;
 
    // Maximum sum of a subarray
    int S1 = maxSubArraySum(a, n);
 
    // Calculate the sum of all elements
    for (int i = 0; i < n; i++)
        S += a[i];
 
    return (2 * S1 - S);
}
 
// Driver Code
int main()
{
    int a[] = { -35, 32, -24, 0, 27, -10, 0, -19 };
 
    // size of an array
    int n = sizeof(a) / sizeof(a[0]);
 
    cout << maxSum(a, n);
 
    return 0;
}


Java
// Java program to find the maximum
// sum after given operations
 
import java.io.*;
 
class GFG {
   
// Function to calculate Maximum Subarray Sum
// or Kadane's Algorithm
static int maxSubArraySum(int a[], int size)
{
    int max_so_far = Integer.MIN_VALUE, max_ending_here = 0;
 
    for (int i = 0; i < size; i++) {
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
 
        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}
 
// Function to find the maximum
// sum after given operations
static int maxSum(int a[], int n)
{
    // To store sum of all elements
    int S = 0;
 
    // Maximum sum of a subarray
    int S1 = maxSubArraySum(a, n);
 
    // Calculate the sum of all elements
    for (int i = 0; i < n; i++)
        S += a[i];
 
    return (2 * S1 - S);
}
 
// Driver Code
 
 
    public static void main (String[] args) {
    int a[] = { -35, 32, -24, 0, 27, -10, 0, -19 };
 
    // size of an array
    int n = a.length;
 
    System.out.println( maxSum(a, n));
    }
}
// This code is contributed by inder_verma


Python3
# Python3 program to find the maximum
# sum after given operations
import sys
 
# Function to calculate Maximum
# Subarray Sum or Kadane's Algorithm
def maxSubArraySum(a, size) :
         
    max_so_far = -(sys.maxsize - 1)
    max_ending_here = 0
 
    for i in range(size) :
         
        max_ending_here = max_ending_here + a[i]
         
        if (max_so_far < max_ending_here) :
                max_so_far = max_ending_here
 
        if (max_ending_here < 0) :
                max_ending_here = 0
     
    return max_so_far
 
# Function to find the maximum
# sum after given operations
def maxSum(a, n) :
     
    # To store sum of all elements
    S = 0;
 
    # Maximum sum of a subarray
    S1 = maxSubArraySum(a, n)
 
    # Calculate the sum of all elements
    for i in range(n) :
        S += a[i]
 
    return (2 * S1 - S)
 
# Driver Code
if __name__ == "__main__" :
 
    a = [ -35, 32, -24, 0,
           27, -10, 0, -19 ]
 
    # size of an array
    n = len(a)
 
    print(maxSum(a, n))
 
# This code is contributed by Ryuga


C#
// C# program to find the maximum
// sum after given operations
 
using System;
 
class GFG {
 
// Function to calculate Maximum Subarray Sum
// or Kadane's Algorithm
static int maxSubArraySum(int []a, int size)
{
    int max_so_far = int.MinValue, max_ending_here = 0;
 
    for (int i = 0; i < size; i++) {
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
 
        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}
 
// Function to find the maximum
// sum after given operations
static int maxSum(int []a, int n)
{
    // To store sum of all elements
    int S = 0;
 
    // Maximum sum of a subarray
    int S1 = maxSubArraySum(a, n);
 
    // Calculate the sum of all elements
    for (int i = 0; i < n; i++)
        S += a[i];
 
    return (2 * S1 - S);
}
 
// Driver Code
 
 
    public static void Main () {
    int []a = { -35, 32, -24, 0, 27, -10, 0, -19 };
 
    // size of an array
    int n = a.Length;
 
    Console.WriteLine( maxSum(a, n));
    }
}
// This code is contributed by inder_verma


PHP


Javascript


输出:
99