📜  规定时间内可装满的容器数

📅  最后修改于: 2021-10-26 05:29:33             🧑  作者: Mango

给定一个数字N和一个时间X单位,如果容器按金字塔方式排列,则任务是找到在 X 单位中完全装满的容器数量,如下所示。

注意:下面的例子是 N = 3 的金字塔排列,其中N表示容器的金字塔形排列中的级别数,这样级别 1 有 1 个容器,级别 2 有 2 个容器,直到级别 N 。液体总是倒在第一层最上面的容器中。当一层容器从两侧溢出时,下层容器就会被装满。每秒倒入的液体量等于容器的体积。

例子:

方法:这个问题是用贪心方法解决的。以下是步骤:

  • 容器装满后,液体从容器的两侧等量溢出并充满其下方的容器。
  • 将此容器金字塔视为一个矩阵。因此, cont[i][j]ij容器中的液体。
  • 因此,当发生溢出时,液体流向cont[i+1][j]cont[i+1][j+1]
  • 由于液体将倾倒X秒,因此倾倒的液体总量为X 个单位。
  • 因此,可以倒入容器的最大值可以为X 个单位,而可以倒入以完全填充容器的最小值为1 个单位。

由于液体总是倒入最上面的容器,让最上面的容器有一个最大值,即 X 个单位。
算法步骤如下:

  1. 对于每个级别的n 个容器,从1 到 N开始外循环。在这个循环中,为每行的每个容器启动另一个循环,从1 到 i 。还要声明一个计数器, count = 0 ,用于计算正在装满的容器。
  2. 如果cont[i][j]的值大于或等于1 (表示已填充),则 count 增加1 ,然后在cont[i+1][j] & g[i+1 ][j+1]液体被倾倒并且它们的值分别增加了(g[i][j]-1)/2 的值,因为液体被分成了一半。
  3. 以这种方式继续循环,并增加每个填充容器的计数。当循环结束时,我们的计数将是所需的答案。

下面是上述方法的实现:

C++
// C++ program for the above problem
#include 
using namespace std;
 
// matrix of containers
double cont[1000][1000];
 
// function to find the number
// of containers that will be
// filled in X seconds
void num_of_containers(int n,
                       double x)
{
    int count = 0;
 
    // container on top level
    cont[1][1] = x;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
 
            // if container gets filled
            if (cont[i][j] >= (double)1) {
                count++;
 
                // dividing the liquid
                // equally in two halves
                cont[i + 1][j]
                    += (cont[i][j]
                        - (double)1)
                       / (double)2;
 
                cont[i + 1][j + 1]
                    += (cont[i][j]
                        - (double)1)
                       / (double)2;
            }
        }
    }
    cout << count;
}
 
// driver code
int main()
{
    int n = 3;
    double x = 5;
    num_of_containers(n, x);
    return 0;
}


Java
// Java program for the above problem
import java.util.*;
 
class GFG{
     
// Matrix of containers
static double cont[][] = new double[1000][1000];
 
// Function to find the number
// of containers that will be
// filled in X seconds
static void num_of_containers(int n, double x)
{
    int count = 0;
 
    // Container on top level
    cont[1][1] = x;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= i; j++)
        {
 
            // If container gets filled
            if (cont[i][j] >= (double)1)
            {
                count++;
 
                // Dividing the liquid
                // equally in two halves
                cont[i + 1][j] += (cont[i][j] -
                                   (double)1) /
                                   (double)2;
 
                cont[i + 1][j + 1] += (cont[i][j] -
                                       (double)1) /
                                       (double)2;
            }
        }
    }
    System.out.print(count);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
    double x = 5;
     
    num_of_containers(n, x);
}
}
 
// This code is contributed by jrishabh99


Python3
# Python3 program for the above problem
 
# Matrix of containers
cont = [[ 0 for i in range(1000)]
            for j in range(1000)]
             
# Function to find the number          
# of containers that will be          
# filled in X seconds          
def num_of_containers(n, x):         
     
    count = 0    
     
    # Container on top level          
    cont[1][1] = x    
     
    for i in range(1, n + 1):
        for j in range(1, i + 1):
             
             # If container gets filled          
             if (cont[i][j] >= 1):         
                count += 1         
                 
                # Dividing the liquid          
                # equally in two halves          
                cont[i + 1][j] += (cont[i][j] - 1) / 2         
                cont[i + 1][j + 1] += (cont[i][j] - 1) / 2
                 
    print(count)
     
# Driver code          
n = 3         
x = 5   
 
num_of_containers(n, x)
 
# This code is contributed by yatinagg


C#
// C# program for the above problem
using System;
 
class GFG{
     
// Matrix of containers
static double [,]cont = new double[1000, 1000];
 
// Function to find the number
// of containers that will be
// filled in X seconds
static void num_of_containers(int n, double x)
{
    int count = 0;
 
    // Container on top level
    cont[1, 1] = x;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= i; j++)
        {
             
            // If container gets filled
            if (cont[i, j] >= (double)1)
            {
                count++;
 
                // Dividing the liquid
                // equally in two halves
                cont[i + 1, j] += (cont[i, j] -
                                  (double)1) /
                                  (double)2;
 
                cont[i + 1, j + 1] += (cont[i, j] -
                                      (double)1) /
                                      (double)2;
            }
        }
    }
    Console.Write(count);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
    double x = 5;
     
    num_of_containers(n, x);
}
}
 
// This code is contributed by Princi Singh


Javascript


输出
4

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

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