📜  在所有子阵列上有效地计算函数的最大值

📅  最后修改于: 2021-04-22 09:51:48             🧑  作者: Mango

给定一个数组arr []和一个函数F(i,j) 。任务是在所有子数组[i..j]上计算max {F(i,j)}
功能F()定义为:

F(l, r) = \sum_{i = l}^{r - 1} |arr[i] - arr[i+1]|.(-1)^{i-l}

例子:

天真的方法:天真的方法是遍历所有子数组并计算所有子数组上函数F的最大值。

高效方法:更好的方法是分别考虑F(l,r)中奇数和偶数l的段。为此,可以构造两个不同的数组B []和C [] ,以便:

B[i] = |arr[i] - arr[i + 1]| * (-1)i
C[i] = |arr[i] - arr[i + 1]| * (-1)i + 1

现在,如果我们仔细观察,我们只需要找到数组B []和C []的最大和子数组,函数的最终答案将是两个数组中的最大值。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
 
#define MAX 100005
 
using namespace std;
 
// Function to return maximum sum of a sub-array
int kadaneAlgorithm(const int* ar, int n)
{
    int sum = 0, maxSum = 0;
 
    for (int i = 0; i < n; i++) {
 
        sum += ar[i];
 
        if (sum < 0)
            sum = 0;
 
        maxSum = max(maxSum, sum);
    }
 
    return maxSum;
}
 
// Function to return maximum value of function F
int maxFunction(const int* arr, int n)
{
 
    int b[MAX], c[MAX];
 
    // Compute arrays B[] and C[]
    for (int i = 0; i < n - 1; i++) {
        if (i & 1) {
            b[i] = abs(arr[i + 1] - arr[i]);
            c[i] = -b[i];
        }
        else {
            c[i] = abs(arr[i + 1] - arr[i]);
            b[i] = -c[i];
        }
    }
 
    // Find maximum sum sub-array of both of the
    // arrays and take maximum among them
    int ans = kadaneAlgorithm(b, n - 1);
    ans = max(ans, kadaneAlgorithm(c, n - 1));
 
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 5, 4, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << maxFunction(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
static int MAX = 100005;
 
 
// Function to return maximum sum of a sub-array
static int kadaneAlgorithm(int[] ar, int n)
{
    int sum = 0, maxSum = 0;
 
    for (int i = 0; i < n; i++)
    {
        sum += ar[i];
 
        if (sum < 0)
            sum = 0;
 
        maxSum = Math.max(maxSum, sum);
    }
 
    return maxSum;
}
 
// Function to return maximum value
// of function F
static int maxFunction(int[] arr, int n)
{
 
    int []b = new int[MAX];
    int []c = new int[MAX];
 
    // Compute arrays B[] and C[]
    for (int i = 0; i < n - 1; i++)
    {
        if (i % 2 == 1)
        {
            b[i] = Math.abs(arr[i + 1] - arr[i]);
            c[i] = -b[i];
        }
        else
        {
            c[i] = Math.abs(arr[i + 1] - arr[i]);
            b[i] = -c[i];
        }
    }
 
    // Find maximum sum sub-array of both of the
    // arrays and take maximum among them
    int ans = kadaneAlgorithm(b, n - 1);
    ans = Math.max(ans, kadaneAlgorithm(c, n - 1));
 
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 5, 4, 7 };
    int n = arr.length;
    System.out.println(maxFunction(arr, n));
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the above approach
MAX = 100005;
 
# Function to return maximum
# sum of a sub-array
def kadaneAlgorithm(ar, n) :
 
    sum = 0; maxSum = 0;
 
    for i in range(n) :
 
        sum += ar[i];
 
        if (sum < 0) :
            sum = 0;
 
        maxSum = max(maxSum, sum);
 
    return maxSum;
 
# Function to return maximum
# value of function F
def maxFunction(arr, n) :
 
    b = [0] * MAX;
    c = [0] * MAX;
 
    # Compute arrays B[] and C[]
    for i in range(n - 1) :
        if (i & 1) :
            b[i] = abs(arr[i + 1] - arr[i]);
            c[i] = -b[i];
         
        else :
            c[i] = abs(arr[i + 1] - arr[i]);
            b[i] = -c[i];
 
    # Find maximum sum sub-array of both of the
    # arrays and take maximum among them
    ans = kadaneAlgorithm(b, n - 1);
    ans = max(ans, kadaneAlgorithm(c, n - 1));
 
    return ans;
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 1, 5, 4, 7 ];
    n = len(arr)
 
    print(maxFunction(arr, n));
 
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
     
class GFG
{
static int MAX = 100005;
 
// Function to return maximum sum of a sub-array
static int kadaneAlgorithm(int[] ar, int n)
{
    int sum = 0, maxSum = 0;
 
    for (int i = 0; i < n; i++)
    {
        sum += ar[i];
 
        if (sum < 0)
            sum = 0;
 
        maxSum = Math.Max(maxSum, sum);
    }
 
    return maxSum;
}
 
// Function to return maximum value
// of function F
static int maxFunction(int[] arr, int n)
{
    int []b = new int[MAX];
    int []c = new int[MAX];
 
    // Compute arrays B[] and C[]
    for (int i = 0; i < n - 1; i++)
    {
        if (i % 2 == 1)
        {
            b[i] = Math.Abs(arr[i + 1] - arr[i]);
            c[i] = -b[i];
        }
        else
        {
            c[i] = Math.Abs(arr[i + 1] - arr[i]);
            b[i] = -c[i];
        }
    }
 
    // Find maximum sum sub-array of both of the
    // arrays and take maximum among them
    int ans = kadaneAlgorithm(b, n - 1);
    ans = Math.Max(ans, kadaneAlgorithm(c, n - 1));
 
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 1, 5, 4, 7 };
    int n = arr.Length;
    Console.WriteLine(maxFunction(arr, n));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
6