在给定数组中的所有对 (i, j) 上最大化表达式 [ij – K.(Ai | Aj)] 的值
给定一个长度为N的数组A [] 和一个整数K,任务是在给定数组中的所有对(i, j)上最大化表达式[ij – K.(A i | A j )]的值,其中 ( 1 ≤ i < j ≤ N)和|表示按位或运算符。
例子:
Input: A[] = {5, 20, 1, 0, 8, 11}, K = 10
Output: 2
Explanation: The maximum value of the expression f(i, j) = i.j – K.(A[i] | A[j]) can be found for below pair:
f(3, 4) = 3.4 – 10.(1 | 0) = 2
Input: A[] = {1, 5, 6, 7, 8, 19}, K = 3
Output: -9
方法:必须进行以下观察:
Let f(i, j) = i.j – K.(Ai | Aj).
=> Notice that for f(i, j) to be maximum, i.j should be maximum and K.(Ai | Aj) should be minimum.
=> Thus, in the best case, f(i, j) will be maximum if
=> K.(Ai | Aj) is equal to 0
=> i = (N-1) and j = N.
=> So, the maximum value of the expression can be f(i, j) = (N-1)*N.
=> Also, the minimum value will be obtained by subtracting the maximum value of K.(Ai | Aj) from (N-1)*N.
=> It is known that
a | b < 2*max(a, b) where | is the bitwise OR operator
从上面的属性和给定的约束,可以推断:
- (A i | A j )的最大值可以是2*N 。因为(0 ≤ A i ≤ N)
- K的最大值可以是 100,因为(1 ≤ K ≤ min(N, 100)) 。
- 因此, f(i, j)的最小值将是:
f(i, j) = (N-1)*N – K*(Ai | Aj)
= (N-1)*N – 100*2*N
= N*(N – 201)
- 可以很容易地观察到,得到的答案总是介于:
N*(N-201) <= ans <= (N-1)*N
- 另外,请注意对于i = N – 201和j = N ,
- f(i, j)的最大值将为N*(N – 201) ,
- 这又是i = N – 1和j = N的f(i, j)的最小值。
- 因此,必须从i = N – 201到i = N和j = i+1到j = 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)