给定N个不同长度的棒。任务是切割所有具有最大整数高度“ h”的杆,以使杆的截止长度之和最大,并且必须大于M。如果无法进行此类切割,则打印-1。
注意:棒也不能切割。
例子:
Input: N = 7, M = 8, a[] = {1, 2, 3, 5, 4, 7, 6}
Output: 3
Rod 1 and 2 are untouched, and rod 3, 4, 5, 6, 7 are cut with the cut-off lengths being (3-3) + (4-3) + (5-3) + (7-3) + (6-3) which is equal to 10 which is greater than M = 8.
Input: N = 4, M = 2, a[] = {1, 2, 3, 3}
Output: 2
方法:
- 以升序对数组进行排序
- 以低= 0和高= length [n-1]的值运行二进制搜索,以使mid =(low + high)/ 2。
- 从n-1到0运行一个循环,将杆截止的高度加到总和上。
- 如果总和大于或等于m,则将low分配为mid + 1,否则将使用mid更新为high。
- 二进制搜索完成后,答案将为低-1。
下面是上述方法的实现:
C++
// C++ program to find the maximum possible
// length of rod which will be cut such that
// sum of cut off lengths will be maximum
#include
using namespace std;
// Function to run Binary Search to
// find maximum cut off length
int binarySearch(int adj[], int target, int length)
{
int low = 0;
int high = adj[length - 1];
while (low < high) {
// f is the flag varibale
// sum is for the total length cutoff
int f = 0, sum = 0;
int mid = low + (high - low) / 2;
// Loop from higer to lower
// for optimization
for (int i = length - 1; i >= 0; i--) {
// Only if length is greater
// than cut-off length
if (adj[i] > mid) {
sum = sum + adj[i] - mid;
}
// When total cut off length becomes greater
// than desired cut off length
if (sum >= target) {
f = 1;
low = mid + 1;
break;
}
}
// If flag variable is not set
// Change high
if (f == 0)
high = mid;
}
// returning the maximum cut off length
return low - 1;
}
// Driver Function
int main()
{
int n1 = 7;
int n2 = 8;
int adj[] = { 1, 2, 3, 4, 5, 7, 6 };
// Sorting the array in ascending order
sort(adj, adj + n1);
// Calling the binarySearch Function
cout << binarySearch(adj, n2, n1);
}
Java
// Java program to find the
// maximum possible length
// of rod which will be cut
// such that sum of cut off
// lengths will be maximum
import java.util.*;
class GFG
{
// Function to run Binary
// Search to find maximum
// cut off length
static int binarySearch(int adj[],
int target,
int length)
{
int low = 0;
int high = adj[length - 1];
while (low < high)
{
// f is the flag varibale
// sum is for the total
// length cutoff
int f = 0, sum = 0;
int mid = low + (high - low) / 2;
// Loop from higer to lower
// for optimization
for (int i = length - 1;
i >= 0; i--)
{
// Only if length is greater
// than cut-off length
if (adj[i] > mid)
{
sum = sum + adj[i] - mid;
}
// When total cut off length
// becomes greater than
// desired cut off length
if (sum >= target)
{
f = 1;
low = mid + 1;
break;
}
}
// If flag variable is
// not set Change high
if (f == 0)
high = mid;
}
// returning the maximum
// cut off length
return low - 1;
}
// Driver Code
public static void main(String args[])
{
int n1 = 7;
int n2 = 8;
int adj[] = { 1, 2, 3, 4, 5, 7, 6 };
// Sorting the array
// in ascending order
Arrays.sort(adj);
// Calling the binarySearch Function
System.out.println(binarySearch(adj, n2, n1));
}
}
// This code is contributed
// by Arnab Kundu
Python3
# Python 3 program to find the
# maximum possible length of
# rod which will be cut such
# that sum of cut off lengths
# will be maximum
# Function to run Binary Search
# to find maximum cut off length
def binarySearch(adj, target, length) :
low = 0
high = adj[length - 1]
while (low < high) :
# f is the flag varibale
# sum is for the total
# length cutoff
# multiple assignments
f, sum = 0, 0
# take integer value
mid = low + (high - low) // 2;
# Loop from higer to lower
# for optimization
for i in range(length - 1, -1 , -1) :
# Only if length is greater
# than cut-off length
if adj[i] > mid :
sum = sum + adj[i] - mid
# When total cut off length
# becomes greater than
# desired cut off length
if sum >= target :
f = 1
low = mid + 1
break
# If flag variable is
# not set. Change high
if f == 0 :
high = mid
# returning the maximum
# cut off length
return low - 1
# Driver code
if __name__ == "__main__" :
n1 = 7
n2 = 8
# adj = [1,2,3,3]
adj = [ 1, 2, 3, 4, 5, 7, 6]
# Sorting the array
# in ascending order
adj.sort()
# Calling the binarySearch Function
print(binarySearch(adj, n2, n1))
# This code is contributed
# by ANKITRAI1
C#
// C# program to find the
// maximum possible length
// of rod which will be cut
// such that sum of cut off
// lengths will be maximum
using System;
class GFG
{
// Function to run Binary
// Search to find maximum
// cut off length
static int binarySearch(int []adj,
int target,
int length)
{
int low = 0;
int high = adj[length - 1];
while (low < high)
{
// f is the flag varibale
// sum is for the total
// length cutoff
int f = 0, sum = 0;
int mid = low + (high - low) / 2;
// Loop from higer to lower
// for optimization
for (int i = length - 1;
i >= 0; i--)
{
// Only if length is greater
// than cut-off length
if (adj[i] > mid)
{
sum = sum + adj[i] - mid;
}
// When total cut off length
// becomes greater than
// desired cut off length
if (sum >= target)
{
f = 1;
low = mid + 1;
break;
}
}
// If flag variable is
// not set Change high
if (f == 0)
high = mid;
}
// returning the maximum
// cut off length
return low - 1;
}
// Driver Code
public static void Main()
{
int n1 = 7;
int n2 = 8;
int []adj = {1, 2, 3, 4, 5, 7, 6};
// Sorting the array
// in ascending order
Array.Sort(adj);
// Calling the binarySearch Function
Console.WriteLine(binarySearch(adj, n2, n1));
}
}
// This code is contributed
// by Subhadeep Gupta
PHP
= 0; $i--)
{
// Only if length is greater
// than cut-off length
if ($adj[$i] > $mid)
{
$sum = $sum + $adj[$i] - $mid;
}
// When total cut off length becomes
// greater than desired cut off length
if ($sum >= $target)
{
$f = 1;
$low = $mid + 1;
break;
}
}
// If flag variable is not
// set Change high
if ($f == 0)
$high = $mid;
}
// returning the maximum cut off length
return $low - 1;
}
// Driver Code
$n1 = 7;
$n2 = 8;
$adj = array( 1, 2, 3, 4, 5, 7, 6 );
// Sorting the array in ascending order
sort($adj);
// Calling the binarySearch Function
echo (int)binarySearch($adj, $n2, $n1);
// This code is contributed by ChitraNayal
?>
输出:
3
时间复杂度: O(N * log N)
辅助空间: O(1)