📜  查找给定F(x)的max(f(x))– min(f(x))的值

📅  最后修改于: 2021-04-24 04:47:22             🧑  作者: Mango

给定两个由N个整数组成的序列,这些整数由正整数和负整数(包括0)组成(序列A和序列B)。然后会有Q个查询,在每个查询中,您将得到两个整数l和r(r> = l)。让我们定义一个函数:

   \h \LARGE f(x, y)=\sum_{i=l}^r(A_i*x-B_i*y)
任务是为每个查询打印Max(f(x(y,y))Min(f(x(y,y)))的值。

例子:

Input: 
N = 5, Q = 2
A[] = 0 7 3 4 5
B[] = 0 3 1 2 3
l = 1, r = 1
l = 1, r = 3

Output: 
0
917448

Input: 
N = 5, Q = 2
A[] = 0 -8 3 4 -9
B[] = 0 -3 -5 2 3
l = 1, r = 1
l = 1, r = 3

Output:
0
851916

方法:如果我们清楚地看到函数仅取决于x和y的值,则可以通过查看各个数组的总和的性质轻松地计算出x和y的值。首先,我们预先计算两个数组的前缀和,然后我们就可以看到求和的性质。并且x和y的值始终为32766或-32766,因为我们采用它们的模。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
#define ll long long
#define MAX 200006
#define CONS 32766
  
// Function to calculate the value
void calc(ll a[], ll b[], ll lr[], ll q, ll n)
{
    ll M, m, i, j, k, l, r, suma, sumb, cc;
    cc = 0;
  
    // forming the prefix sum arrays
    for (i = 0; i < n - 1; ++i) {
        a[i + 1] += a[i];
        b[i + 1] += b[i];
    }
  
    while (q--) {
        // Taking the query
        l = lr[cc++];
        r = lr[cc++];
        l -= 2;
        r -= 1;
  
        // finding the sum in the range l to r in array a
        suma = a[r];
  
        // finding the sum in the range l to r in array b
        sumb = b[r];
        if (l >= 0) {
            suma -= a[l];
            sumb -= b[l];
        }
  
        // Finding the max value of the function
        M = max(CONS * suma + CONS * sumb,
                -CONS * suma - CONS * sumb);
        M = max(M, max(CONS * suma - CONS * sumb,
                       -CONS * suma + CONS * sumb));
  
        // Finding the min value of the function
        m = min(CONS * suma + CONS * sumb,
                -CONS * suma - CONS * sumb);
        m = min(m, min(CONS * suma - CONS * sumb,
                       -CONS * suma + CONS * sumb));
  
        cout << (M - m) << "\n";
    }
}
  
// Driver code
int main()
{
    ll n = 5, q = 2;
    ll a[5] = { 0, 7, 3, 4, 5 };
    ll b[5] = { 0, 3, 1, 2, 3 };
  
    ll lr[q * 2];
    lr[0] = 1;
    lr[1] = 1;
    lr[2] = 1;
    lr[3] = 3;
  
    calc(a, b, lr, q, n);
  
    return 0;
}


Java
// Java implementation of above approach
  
import java.util.*;
  
class GFG
{
      
        static final int MAX=200006;
        static final int CONS=32766;
          
        // Function to calculate the value
        static void calc(int a[], int b[], int lr[], int q, int n)
        {
            int M, m, i, j, k, l, r, suma, sumb, cc;
            cc = 0;
          
            // forming the prefix sum arrays
            for (i = 0; i < n - 1; ++i) 
            {
                a[i + 1] += a[i];
                b[i + 1] += b[i];
            }
          
            while (q!=0)
            {
                // Taking the query
                l = lr[cc++];
                r = lr[cc++];
                l -= 2;
                r -= 1;
          
                // finding the sum in the range l to r in array a
                suma = a[r];
          
                // finding the sum in the range l to r in array b
                sumb = b[r];
                if (l >= 0) {
                    suma -= a[l];
                    sumb -= b[l];
                }
          
                // Finding the max value of the function
                M = Math.max(CONS * suma + CONS * sumb,
                        -CONS * suma - CONS * sumb);
                M = Math.max(M, Math.max(CONS * suma - CONS * sumb,
                            -CONS * suma + CONS * sumb));
          
                // Finding the min value of the function
                m = Math.min(CONS * suma + CONS * sumb,
                        -CONS * suma - CONS * sumb);
                m = Math.min(m, Math.min(CONS * suma - CONS * sumb,
                            -CONS * suma + CONS * sumb));
          
                System.out.println((M - m) );
                q--;
            }
        }
          
        // Driver code
        public static void  main(String [] args)
        {
            int n = 5, q = 2;
            int []a = { 0, 7, 3, 4, 5 };
            int []b = { 0, 3, 1, 2, 3 };
          
            int []lr=new int[q * 2];
            lr[0] = 1;
            lr[1] = 1;
            lr[2] = 1;
            lr[3] = 3;
          
            calc(a, b, lr, q, n);
          
              
        }
// This code is contributed by ihritik
  
}


Python3
# Python 3 implementation of 
# above approach
  
MAX = 200006
CONS = 32766
  
# Function to calculate the value
def calc(a, b, lr, q, n):
  
    cc = 0
  
    # forming the prefix sum arrays
    for i in range(n - 1) :
        a[i + 1] += a[i]
        b[i + 1] += b[i]
  
    while (q > 0) :
        # Taking the query
        l = lr[cc]
        cc +=1
        r = lr[cc]
        cc += 1
        l -= 2
        r -= 1
  
        # finding the sum in the range l 
        # to r in array a
        suma = a[r]
  
        # finding the sum in the range 
        # l to r in array b
        sumb = b[r]
        if (l >= 0) :
            suma -= a[l]
            sumb -= b[l]
  
        # Finding the max value of the function
        M = max(CONS * suma + CONS * sumb,
                -CONS * suma - CONS * sumb)
        M = max(M, max(CONS * suma - CONS * sumb,
                    -CONS * suma + CONS * sumb))
  
        # Finding the min value of the function
        m = min(CONS * suma + CONS * sumb,
                -CONS * suma - CONS * sumb)
        m = min(m, min(CONS * suma - CONS * sumb,
                    -CONS * suma + CONS * sumb))
  
        print(M - m)
          
        q -= 1
  
# Driver code
if __name__ == "__main__":
      
    n = 5
    q = 2
    a = [ 0, 7, 3, 4, 5 ]
    b = [ 0, 3, 1, 2, 3 ]
  
    lr = [0]*(q * 2)
    lr[0] = 1
    lr[1] = 1
    lr[2] = 1
    lr[3] = 3
  
    calc(a, b, lr, q, n)
  
# This code is contributed by 
# ChitraNayal


C#
// C# implementation of above approach
using System;
  
class GFG
{
  
// static int MAX=200006;
static int CONS = 32766;
  
// Function to calculate the value
static void calc(int []a, int []b, 
                 int []lr, int q, int n)
{
    int M, m, i, l, r, suma, sumb, cc;
    cc = 0;
  
    // forming the prefix sum arrays
    for (i = 0; i < n - 1; ++i) 
    {
        a[i + 1] += a[i];
        b[i + 1] += b[i];
    }
  
    while (q != 0)
    {
        // Taking the query
        l = lr[cc++];
        r = lr[cc++];
        l -= 2;
        r -= 1;
  
        // finding the sum in the
        // range l to r in array a
        suma = a[r];
  
        // finding the sum in the 
        // range l to r in array b
        sumb = b[r];
        if (l >= 0) 
        {
            suma -= a[l];
            sumb -= b[l];
        }
  
        // Finding the max value of the function
        M = Math.Max(CONS * suma + CONS * sumb, 
                    -CONS * suma - CONS * sumb);
        M = Math.Max(M, Math.Max(CONS * suma - CONS * sumb,
                                -CONS * suma + CONS * sumb));
  
        // Finding the min value of the function
        m = Math.Min(CONS * suma + CONS * sumb,
                    -CONS * suma - CONS * sumb);
        m = Math.Min(m, Math.Min(CONS * suma - CONS * sumb,
                                -CONS * suma + CONS * sumb));
  
        Console.WriteLine((M - m));
        q--;
    }
}
  
// Driver code
public static void Main()
{
    int n = 5, q = 2;
    int []a = { 0, 7, 3, 4, 5 };
    int []b = { 0, 3, 1, 2, 3 };
  
    int []lr = new int[q * 2];
    lr[0] = 1;
    lr[1] = 1;
    lr[2] = 1;
    lr[3] = 3;
  
    calc(a, b, lr, q, n);
}
}
  
// This code is contributed by anuj_67


PHP
= 0)
        {
            $suma -= $a[$l];
            $sumb -= $b[$l];
        }
  
        // Finding the max value of the function
        $M = max($CONS * $suma + $CONS * $sumb,
                -$CONS * $suma - $CONS * $sumb);
        $M = max($M, max($CONS * $suma - $CONS * $sumb, 
                        -$CONS * $suma + $CONS * $sumb));
  
        // Finding the min value of the function
        $m = min($CONS * $suma + $CONS * $sumb,
                -$CONS * $suma - $CONS * $sumb);
        $m = min($m, min($CONS * $suma - $CONS * $sumb,
                        -$CONS * $suma + $CONS * $sumb));
  
        echo ($M - $m) , "\n";
    }
}
  
// Driver code
$n = 5; $q = 2;
$a = array(0, 7, 3, 4, 5 );
$b = array( 0, 3, 1, 2, 3 );
$lr[0] = 1;
$lr[1] = 1;
$lr[2] = 1;
$lr[3] = 3;
  
calc($a, $b, $lr, $q, $n);
  
// This code is contributed by anuj_67
?>


输出:
0
917448