在最多 C 次拆分给定 Array 后最大化第 K 个最大元素
给定一个数组arr[]和两个正整数K和C ,任务是最大化将数组元素arr[]分成两部分(不一定是整数) C次后获得的第 K个最大元素。如果不存在第 K个最大元素,则打印-1 。
注意:必须进行拆分操作,直到数组arr[]的大小≥ K 。
例子:
Input: arr[] = {5, 8}, K = 1, C = 1
Output: 8.0
Explanation: There is no need to perform any operations. The finally array will be {8, 5} Hence 8.0 is the maximum achievable value.
Input: arr[] = {5, 9}, K = 3, C = 1
Output: 4.5
Explanation: The value 9 can be splitted as 4.5 and 4.5. The final array will be {5, 4.5, 4.5} where the 3rd value is 4.5 which is maximum achievable.
方法:给定的问题可以通过对答案使用二分搜索来解决。请按照以下步骤解决给定的问题。
- 初始化两个变量,比如low和high分别为0和10 9 ,表示可以执行二分搜索的范围。
- 使用以下步骤执行二分搜索:
- 找到mid的值为(low + high)*0.5 。
- 遍历给定的数组arr[]并将元素的计数存储在变量中,至少是mid的值,比如A并找到在变量中执行的操作数,比如B 。
- 如果(A ≥ K)和(B + C ≥ K)的值,则将low的值更新为mid 。否则,将high的值更新为mid 。
- 完成上述步骤后,变量low中存储的值就是结果最大化的第K个最大元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the K-th maximum
// element after upto C operations
double maxKth(int arr[], int N,
int C, int K)
{
// Check for the base case
if (N + C < K) {
return -1;
}
// Stores the count iterations of BS
int iter = 300;
// Create the left and right bounds
// of binary search
double l = 0, r = 1000000000.0;
// Perform binary search
while (iter--) {
// Find the value of mid
double mid = (l + r) * 0.5;
double a = 0;
double b = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
a += int((double)arr[i] / mid);
if ((double)arr[i] >= mid) {
b++;
}
}
// Update the ranges
if (a >= K && b + C >= K) {
l = mid;
}
else {
r = mid;
}
}
// Return the maximum value obtained
return l;
}
// Driver Code
int main()
{
int arr[] = { 5, 8 };
int K = 1, C = 1;
int N = sizeof(arr) / sizeof(arr[0]);
cout << maxKth(arr, N, C, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find the K-th maximum
// element after upto C operations
static double maxKth(int arr[], int N, int C, int K)
{
// Check for the base case
if (N + C < K) {
return -1;
}
// Stores the count iterations of BS
int iter = 300;
// Create the left and right bounds
// of binary search
double l = 0, r = 1000000000.0;
// Perform binary search
while (iter-- > 0) {
// Find the value of mid
double mid = (l + r) * 0.5;
double a = 0;
double b = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
a += (int)((double)arr[i] / mid);
if ((double)arr[i] >= mid) {
b++;
}
}
// Update the ranges
if (a >= K && b + C >= K) {
l = mid;
}
else {
r = mid;
}
}
// Return the maximum value obtained
return l;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 5, 8 };
int K = 1, C = 1;
int N = arr.length;
System.out.println(maxKth(arr, N, C, K));
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python Program to implement
# the above approach
# Function to find the K-th maximum
# element after upto C operations
def maxKth(arr, N, C, K):
# Check for the base case
if (N + C < K):
return -1
# Stores the count iterations of BS
iter = 300
# Create the left and right bounds
# of binary search
l = 0
r = 1000000000.0
# Perform binary search
while (iter):
iter = iter - 1
# Find the value of mid
mid = (l + r) * 0.5
a = 0
b = 0
# Traverse the array
for i in range(N) :
a += arr[i] // mid
if (arr[i] >= mid) :
b += 1
# Update the ranges
if (a >= K and b + C >= K) :
l = mid
else :
r = mid
# Return the maximum value obtained
return int(l)
# Driver Code
arr = [5, 8]
K = 1
C = 1
N = len(arr)
print(maxKth(arr, N, C, K))
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the K-th maximum
// element after upto C operations
static double maxKth(int []arr, int N, int C, int K)
{
// Check for the base case
if (N + C < K) {
return -1;
}
// Stores the count iterations of BS
int iter = 300;
// Create the left and right bounds
// of binary search
double l = 0, r = 1000000000.0;
// Perform binary search
while (iter-- > 0) {
// Find the value of mid
double mid = (l + r) * 0.5;
double a = 0;
double b = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
a += (int)((double)arr[i] / mid);
if ((double)arr[i] >= mid) {
b++;
}
}
// Update the ranges
if (a >= K && b + C >= K) {
l = mid;
}
else {
r = mid;
}
}
// Return the maximum value obtained
return l;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 5, 8 };
int K = 1, C = 1;
int N = arr.Length;
Console.Write(maxKth(arr, N, C, K));
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
8
时间复杂度: O(N*log M)
辅助空间: O(1)