📌  相关文章
📜  X 的最小值,使得 arr[i] – X 的 brr[i] 次方之和小于或等于 K

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

X 的最小值,使得 arr[i] – X 的 brr[i] 次方之和小于或等于 K

给定一个数组arr[]brr[]都由N个整数和一个正整数K组成,任务是找到X的最小值,使得(arr[i] – X, 0)的最大值之和增加所有数组元素(arr[i], brr[i])brr [i] 次幂最多为 K

例子:

朴素方法:解决给定问题的最简单方法是检查从0到数组最大元素的每个X值,如果存在满足给定条件的X值,则打印该X值并中断的循环。

时间复杂度: O(N*M),其中,M 是数组的最大元素
辅助空间: O(1)

高效方法:上述方法也可以通过二分查找来优化X的值,如果X某个特定值满足上述条件,那么所有较大的值也会满足,因此,尝试搜索较小的值价值观。请按照以下步骤解决问题:

  • 定义一个函数check(a[], b[], k, n, x):
    • 将变量sum初始化为0以从数组arr[]brr[] 计算所需的总和。
    • 使用变量i遍历范围[0, N]并将pow(max(arr[i] – x, 0), brr[i])的值添加到变量sum中。
    • 如果sum的值小于等于K ,则返回true 。否则,返回false
  • 初始化变量,比如0为数组的最大值。
  • 在 while 循环中迭代直到low小于high并执行以下步骤:
    • 将变量mid初始化为lowhigh的平均值。
    • 通过调用函数check(arr[], brr[], k, n, mid)检查mid的值是否满足给定条件。
    • 如果函数check(arr[], brr[], n, k, mid)返回true ,则将high更新为mid 。否则,将low的值更新为(mid + 1)
    • 完成上述步骤后,从函数返回低值作为结果。
  • 执行上述步骤后,打印低值作为X的期望值作为答案。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if there exists an
// X that satisfies the given conditions
bool check(int a[], int b[], int k, int n, int x)
{
    int sum = 0;
 
    // Find the required value of the
    // given expression
    for (int i = 0; i < n; i++) {
        sum = sum + pow(max(a[i] - x, 0), b[i]);
    }
 
    if (sum <= k)
        return true;
    else
        return false;
}
 
// Function to find the minimum value
// of X using binary search.
int findMin(int a[], int b[], int n, int k)
{
    // Boundaries of the Binary Search
    int l = 0, u = *max_element(a, a + n);
 
    while (l < u) {
 
        // Find the middle value
        int m = (l + u) / 2;
 
        // Check for the middle value
        if (check(a, b, k, n, m)) {
 
            // Update the upper
            u = m;
        }
        else {
 
            // Update the lower
            l = m + 1;
        }
    }
    return l;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 1, 4, 3, 5 };
    int brr[] = { 4, 3, 2, 3, 1 };
    int K = 12;
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << findMin(arr, brr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
   
// Function to check if it is possible to
// get desired result
static boolean check(int a[], int b[], int k, int x)
{
    int sum = 0;
    for(int i = 0; i < a.length; i++)
    {
        sum = sum + (int)Math.pow(
                         Math.max(a[i] - x, 0), b[i]);
    }
    if (sum <= k)
        return true;
    else
        return false;
}
 
// Function to find the minimum value
// of X using binary search.
static int findMin(int a[], int b[], int n, int k)
{
     
    // Boundaries of the Binary Search
    int l = 0, u = (int)1e9;
 
    while (l < u)
    {
         
        // Find the middle value
        int m = (l + u) / 2;
         
        // Check for the middle value
        if (check(a, b, k, m))
         
            // Update the upper
            u = m;
        else
         
            // Update the lower
            l = m + 1;
    }
    return l;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    int k = 12;
    int a[] = { 2, 1, 4, 3, 5 };
    int b[] = { 4, 3, 2, 3, 1 };
     
    System.out.println(findMin(a, b, n, k));
}
}
 
// This code is contributed by ayush_dragneel


Python3
# Python 3 program for the above approach
 
# Function to check if there exists an
# X that satisfies the given conditions
def check(a, b, k, n, x):
    sum = 0
 
    # Find the required value of the
    # given expression
    for i in range(n):
        sum = sum + pow(max(a[i] - x, 0), b[i])
 
    if (sum <= k):
        return True
    else:
        return False
 
# Function to find the minimum value
# of X using binary search.
def findMin(a, b, n, k):
    # Boundaries of the Binary Search
    l = 0
    u = max(a)
    while (l < u):
        # Find the middle value
        m = (l + u) // 2
 
        # Check for the middle value
        if (check(a, b, k, n, m)):
            # Update the upper
            u = m
        else:
 
            # Update the lower
            l = m + 1
    return l
 
# Driver Code
if __name__ == '__main__':
    arr = [2, 1, 4, 3, 5]
    brr = [4, 3, 2, 3, 1]
    K = 12
    N = len(arr)
    print(findMin(arr, brr, N, K))
 
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
 
public class GFG{
   
// Function to check if it is possible to
// get desired result
static bool check(int []a, int []b, int k, int x)
{
    int sum = 0;
    for(int i = 0; i < a.Length; i++)
    {
        sum = sum + (int)Math.Pow(
                         Math.Max(a[i] - x, 0), b[i]);
    }
    if (sum <= k)
        return true;
    else
        return false;
}
 
// Function to find the minimum value
// of X using binary search.
static int findMin(int []a, int []b, int n, int k)
{
     
    // Boundaries of the Binary Search
    int l = 0, u = (int)1e9;
 
    while (l < u)
    {
         
        // Find the middle value
        int m = (l + u) / 2;
         
        // Check for the middle value
        if (check(a, b, k, m))
         
            // Update the upper
            u = m;
        else
         
            // Update the lower
            l = m + 1;
    }
    return l;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 5;
    int k = 12;
    int []a = { 2, 1, 4, 3, 5 };
    int []b = { 4, 3, 2, 3, 1 };
     
    Console.WriteLine(findMin(a, b, n, k));
}
}
 
// This code is contributed by Princi Singh


Javascript


输出
2

时间复杂度: O(N*log M),其中,M 是数组的最大元素
辅助空间: O(1)