📜  使用给定点最小化方程的值 (yi + yj + |xi – xj|)

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

使用给定点最小化方程的值 (yi + yj + |xi – xj|)

给定一个大小为N的数组arr[] ,其中arr[i]的形式为[x i , y i ],表示二维平面上的点(x i , y i ) 。数组按 x 坐标排序。此外,给出了一个整数K。任务是最小化方程y i + y j + |x i – x j |的值。其中|x i – x j | ≤ K1 ≤ i < j ≤ N 。保证至少存在一对满足约束|x i – x j |的点。 ≤K。

例子:

方法:问题简化以找到(y j + x j ) + (y i – x i )的最小值。由于所考虑的点的x坐标的绝对差必须小于或等于K ,因此可以使用最小堆来解决问题。对于每个点,考虑堆的顶部值,并检查顶部和当前点的x坐标是否小于K ,在其中取最小值并更新堆,其余坐标也遵循相同的方法。请按照以下步骤解决此问题:

  • 初始化最小优先级队列minh[]。
  • 将变量min_val初始化为INT_MAX
  • 使用变量index遍历范围[0, n)并执行以下任务:
    • 从最小堆minh[]中弹出元素,直到x 坐标的差异大于K
    • 如果不为空,则更新min_val 的值。
    • 推入{arr[index][1] – arr[index][0], arr[index][0]} 的值。
  • 执行上述步骤后,打印min_val的值作为答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
typedef pair pi;
 
// Utility function to find minimum
// value of equation
int MinimumValue(int arr[][2], int n, int k)
{
 
    // Minimum heap for storing
    // (x-y) difference and x
    // coordinate as the pair
    priority_queue,
                   greater >
        minh;
 
    int min_val = INT_MAX;
 
    for (int index = 0; index < n; index++) {
 
        // Pop until the x-coordinates
        // difference is greater than K
        while ((
            !minh.empty()
            && ((arr[index][0]
                 - minh.top().second)
                > k))) {
            minh.pop();
        }
 
        // If not heap empty update min_val
        if (!minh.empty()) {
            min_val
                = min(min_val,
                      arr[index][0]
                          + arr[index][1]
                          + minh.top().first);
        }
 
        // Push the current pair in the heap
        minh.push({ arr[index][1]
                        - arr[index][0],
                    arr[index][0] });
    }
 
    return min_val;
}
 
// Driver Code
int main()
{
    // Input taken
    int arr[4][2]
        = { { 1, 3 }, { 2, 0 },
            { 5, 10 }, { 6, -10 } };
    int K = 1;
    int n = 4;
 
    // Function called
    cout << MinimumValue(arr, n, K) << "\n";
 
    return 0;
}


Python3
# python3 program for the above approach
from queue import PriorityQueue
INT_MAX = 2147483647
 
# Utility function to find minimum
# value of equation
def MinimumValue(arr, n, k):
 
   # Minimum heap for storing
   # (x-y) difference and x
   # coordinate as the pair
    minh = PriorityQueue()
    min_val = INT_MAX
    for index in range(0, n):
 
        # Pop until the x-coordinates
        # difference is greater than K
        while (not minh.empty()):
            pi = minh.get()
            if((arr[index][0] - (-1*pi[1])) <= k):
                minh.put(pi)
                break
 
        # If not heap empty update min_val
        if (not minh.empty()):
            pi = minh.get()
            min_val = min(min_val, arr[index][0] + arr[index][1] + (-1*pi[1]))
            minh.put(pi)
 
        # Push the current pair in the heap
        minh.put([-1*arr[index][1]
                  - (-1*arr[index][0]),
                  -1*arr[index][0]])
 
    return min_val
 
# Driver Code
if __name__ == "__main__":
 
        # Input taken
    arr = [[1, 3], [2, 0],
           [5, 10], [6, -10]]
    K = 1
    n = 4
 
    # Function called
    print(MinimumValue(arr, n, K))
 
    # This code is contributed by rakeshsahni



输出
1

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