📌  相关文章
📜  切割所有具有一定长度的杆,使切割长度的总和最大化

📅  最后修改于: 2021-09-16 11:11:55             🧑  作者: Mango

给定 N 个不同长度的杆。任务是切割具有某个最大整数高度“h”的所有杆,以使杆的切割长度总和最大化并且必须大于 M。如果无法切割,则打印 -1。
注意:杆也不能切割。
例子:

方法:

  • 按升序对数组进行排序
  • 使用值 low=0 和 high=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 variable
        // sum is for the total length cutoff
        int f = 0, sum = 0;
 
        int mid = low + (high - low) / 2;
 
        // Loop from higher 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 variable
    // sum is for the total
    // length cutoff
    int f = 0, sum = 0;
 
    int mid = low + (high - low) / 2;
 
    // Loop from higher 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 variable
        # sum is for the total
        # length cutoff
 
        # multiple assignments
        f, sum = 0, 0
 
        # take integer value
        mid = low + (high - low) // 2;
 
        # Loop from higher 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 variable
    // sum is for the total
    // length cutoff
    int f = 0, sum = 0;
 
    int mid = low + (high - low) / 2;
 
    // Loop from higher 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
?>


Javascript


输出:
3

时间复杂度: O(N * log N)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程