给定一个大小为N的排序二维数组arr[][2]使得(arr[i][0], arr[i][1])表示笛卡尔平面中第i个点的坐标和一个整数K ,则任务是找到表达式(|arr[i][0] – arr[j][0]| + arr[i][1] + arr[j][1])使得|arr[i][0] – arr[j][0]|对于任何可能的坐标对(i, j), ≤ K。
例子:
Input: arr[][] = {{1, 3}, {2, 0}, {5, 10}, {6, -10}}, K = 1
Output: 4
Explanation:
Choose pairs (0, 1). Now the value of the expression is given by:
value = (abs(1 – 2) + 3 + 0) = 4, which is maximum and abs(1 – 2) = 1(≤ K).
Therefore, print 4.
Input: arr[][] = {{0, 0}, {3, 0}, {9, 2}}, K = 3
Output: 3
方法:可以使用基于以下观察的优先队列使用贪心算法解决给定的问题:
- 将所有i > j的表达式重新排列为(arr[i][0] – arr[i][1] + arr[j][0] + arr[j][1]) 。
- 现在,保持{arr[i] x – arr[i] y , arr[i] x }对的排序顺序,可以计算索引j处每个数组元素的给定表达式的值。
请按照以下步骤解决问题:
- 初始化成对的priority_queue 说PQ ,它存储了一个点的坐标轴和该点的X坐标的差值对。
- 初始化一个变量res为 INT_MIN 以存储最大值。
- 遍历数组arr[][]并考虑{X, Y}是当前点执行以下操作:
- 在PQ不为空且(X – PQ.top()[1])大于K 时进行迭代,并从 priority_queue PQ 中删除顶部元素。
- 如果PQ不为空,则将res的值更新为res和PQ.top()[0] + X + Y)的最大值。
- 将{Y – X, X}对推入 priority_queue PQ 。
- 完成上述步骤后,打印res的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum value
// of the given expression possible
// for any pair of co-ordinates
void findMaxValueOfEquation(
vector >& arr, int K)
{
// Stores the differences between pairs
priority_queue > pq;
// Stores the maximum value
int res = INT_MIN;
// Traverse the array arr[][]
for (auto point : arr) {
// While pq is not empty and
// difference between point[0]
// and pq.top()[1] > K
while (!pq.empty()
&& point[0] - pq.top()[1]
> K) {
// Removes the top element
pq.pop();
}
// If pq is not empty
if (!pq.empty()) {
// Update the value res
res = max(res,
pq.top()[0] + point[0] + point[1]);
}
// Push pair {point[1] - point[0],
// point[0]} in pq
pq.push({ point[1] - point[0],
point[0] });
}
// Print the result
cout << res;
}
// Driver Code
int main()
{
vector > arr
= { { 1, 3 }, { 2, 0 },
{ 5, 10 }, { 6, -10 } };
int K = 1;
findMaxValueOfEquation(arr, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG
{
// Function to find the maximum value
// of the given expression possible
// for any pair of co-ordinates
static void findMaxValueOfEquation(int arr[][], int K)
{
// Stores the differences between pairs
PriorityQueue pq
= new PriorityQueue<>((a, b) -> {
if (a[0] != b[0])
return b[0] - a[0];
return b[1] - a[1];
});
// Stores the maximum value
int res = Integer.MIN_VALUE;
// Traverse the array arr[][]
for (int point[] : arr) {
// While pq is not empty and
// difference between point[0]
// and pq.top()[1] > K
while (!pq.isEmpty()
&& point[0] - pq.peek()[1] > K) {
// Removes the top element
pq.poll();
}
// If pq is not empty
if (!pq.isEmpty()) {
// Update the value res
res = Math.max(res, pq.peek()[0] + point[0]
+ point[1]);
}
// Push pair {point[1] - point[0],
// point[0]} in pq
pq.add(new int[] { point[1] - point[0],
point[0] });
}
// Print the result
System.out.println(res);
}
// Driver Code
public static void main(String[] args)
{
int[][] arr
= { { 1, 3 }, { 2, 0 }, { 5, 10 }, { 6, -10 } };
int K = 1;
findMaxValueOfEquation(arr, K);
}
}
// This code is contributed by Kingash.
Python3
# Python3 program for the above approach
# Function to find the maximum value
# of the given expression possible
# for any pair of co-ordinates
def findMaxValueOfEquation(arr, K):
# Stores the differences between pairs
pq = []
# Stores the maximum value
res = -10**8
# Traverse the array arr[][]
for point in arr:
# While pq is not empty and
# difference between point[0]
# and pq.top()[1] > K
while (len(pq)>0 and point[0] - pq[-1][1] > K):
# Removes the top element
del pq[-1]
# If pq is not empty
if (len(pq) > 0):
# Update the value res
res = max(res, pq[-1][0] + point[0] + point[1])
# Push pair {point[1] - point[0],
# point[0]} in pq
pq.append([point[1] - point[0], point[0]])
pq = sorted(pq)
# Prthe result
print (res)
# Driver Code
if __name__ == '__main__':
arr = [ [ 1, 3 ], [ 2, 0 ], [ 5, 10 ], [ 6, -10 ] ]
K = 1
findMaxValueOfEquation(arr, K)
# This code is contributed by mohit kumar 29.
输出:
4
时间复杂度: O(N * log N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live