📜  在给定数组中的所有对 (i, j) 上最大化表达式 [ij – K.(Ai | Aj)] 的值

📅  最后修改于: 2022-05-13 01:56:09.969000             🧑  作者: Mango

在给定数组中的所有对 (i, j) 上最大化表达式 [ij – K.(Ai | Aj)] 的值

给定一个长度为N的数组A [] 和一个整数K,任务是在给定数组中的所有对(i, j)上最大化表达式[ij – K.(A i | A j )]的值,其中 ( 1i < jN)|表示按位或运算符。

例子:

方法:必须进行以下观察:

从上面的属性和给定的约束,可以推断:

  • (A i | A j )的最大值可以是2*N 。因为(0A iN)
  • K的最大值可以是 100,因为(1Kmin(N, 100))
  • 因此, f(i, j)的最小值将是:
  • 可以很容易地观察到,得到的答案总是介于:
  • 另外,请注意对于i = N – 201j = N
    • f(i, j)的最大值将为N*(N – 201)
    • 这又是i = N – 1j = Nf(i, j)的最小值。
    • 因此,必须从i = N – 201i = Nj = i+1j = N检查表达式的最大值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum
// value of the given expression
long long int maxValue(int N, int K,
                       long long int A[])
{
    // Stores the maximum value of
    // the given expression
    long long int ans = LLONG_MIN;
 
    // Nested loops to find the maximum
    // value of the given expression
    for (long long int i = max(0, N - 201);
         i < N; ++i) {
        for (long long int j = i + 1;
             j < N; ++j) {
            ans = max(ans, (i + 1) * (j + 1)
                               - K * (A[i] | A[j]));
        }
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
    // Given input
    int N = 6, K = 10;
    long long int A[N]
        = { 5, 20, 1, 0, 8, 11 };
 
    // Function Call
    cout << maxValue(N, K, A);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to find the maximum
  // value of the given expression
  static int maxValue(int N, int K,
                      int A[])
  {
    // Stores the maximum value of
    // the given expression
    int ans = Integer.MIN_VALUE;
 
    // Nested loops to find the maximum
    // value of the given expression
    for (int i = Math.max(0, N - 201);
         i < N; ++i) {
      for (int j = i + 1;
           j < N; ++j) {
        ans = Math.max(ans, (i + 1) * (j + 1)
                       - K * (A[i] | A[j]));
      }
    }
 
    // Return the answer
    return ans;
  }
 
  // Driver Code
  public static void main (String[] args)
  {
 
    // Given input
    int N = 6, K = 10;
    int  A[]  = { 5, 20, 1, 0, 8, 11 };
 
    // Function Call
    System.out.println( maxValue(N, K, A));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python3 program for the above approach
LLONG_MIN = -9223372036854775808
 
# Function to find the maximum
# value of the given expression
def maxValue(N, K, A):
 
    # Stores the maximum value of
    # the given expression
    ans = LLONG_MIN
 
    # Nested loops to find the maximum
    # value of the given expression
    for i in range(max(0, N - 201), N):
        for j in range(i + 1, N):
            ans = max(ans, (i + 1) * (j + 1)
                      - K * (A[i] | A[j]))
 
    # Return the answer
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    # Given input
    N, K = 6, 10
    A = [5, 20, 1, 0, 8, 11]
 
    # Function Call
    print(maxValue(N, K, A))
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
 
class GFG {
 
  // Function to find the maximum
  // value of the given expression
  static int maxValue(int N, int K,
                      int []A)
  {
     
    // Stores the maximum value of
    // the given expression
    int ans = Int32.MinValue;
 
    // Nested loops to find the maximum
    // value of the given expression
    for (int i = Math.Max(0, N - 201);
         i < N; ++i) {
      for (int j = i + 1;
           j < N; ++j) {
        ans = Math.Max(ans, (i + 1) * (j + 1)
                       - K * (A[i] | A[j]));
      }
    }
 
    // Return the answer
    return ans;
  }
 
  // Driver Code
  public static void Main ()
  {
 
    // Given input
    int N = 6, K = 10;
    int  []A  = { 5, 20, 1, 0, 8, 11 };
 
    // Function Call
    Console.WriteLine(maxValue(N, K, A));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
2

时间复杂度: O(N 2 )
辅助空间: O(1)