📌  相关文章
📜  查找通过出售电影票可以收取的最大金额

📅  最后修改于: 2021-04-17 11:53:45             🧑  作者: Mango

给定一个整数N和一个数组seat [] ,其中N是排队购买电影票的人数, seat [i]是电影院i行中空座位的数量。任务是找到剧院所有者可以通过向N个人出售电影票来赚取的最大金额。一张机票的价格等于所有行中最大空座位数。
例子:

方法:可通过使用优先级队列来解决此问题,该队列将存储每一行的空座位数,并且其中的最大座位数将位于顶部。

  • 创建一个空的priority_queue q并遍历seat []数组,然后将所有元素插入priority_queue。
  • 初始化两个整数变量ticketSold = 0ans = 0 ,这将存储已售出票数和到目前为止的总金额。
  • 现在检查ticketSold q.top()> 0,然后从priority_queue中删除top元素,并通过添加优先级队列的top元素来更新ans。还要将此最高值存储在变量temp中,并将temp – 1插入到priority_queue中。
  • 重复这些步骤,直到所有人员都被售出并打印最终结果。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the maximum amount
// that can be collected by selling tickets
int maxAmount(int M, int N, int seats[])
{
 
    // Priority queue that stores
    // the count of empty seats
    priority_queue q;
 
    // Insert each array element
    // into the priority queue
    for (int i = 0; i < M; i++) {
        q.push(seats[i]);
    }
 
    // To store the  total
    // number of tickets sold
    int ticketSold = 0;
 
    // To store the total amount
    // of collection
    int ans = 0;
 
    // While tickets sold are less than N
    // and q.top > 0 then update the collected
    // amount with the top of the priority
    // queue
    while (ticketSold < N && q.top() > 0) {
        ans = ans + q.top();
        int temp = q.top();
        q.pop();
        q.push(temp - 1);
        ticketSold++;
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int seats[] = { 1, 2, 4 };
    int M = sizeof(seats) / sizeof(int);
    int N = 3;
 
    cout << maxAmount(N, M, seats);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
    static int[] seats = new int[]{ 1, 2, 4 };
 
    // Function to return the maximum amount
    // that can be collected by selling tickets
    public static int maxAmount(int M, int N)
    {
 
        // Priority queue that stores
        // the count of empty seats
        PriorityQueue q =
            new PriorityQueue(Collections.reverseOrder());
     
        // Insert each array element
        // into the priority queue
        for (int i = 0; i < M; i++)
        {
            q.add(seats[i]);
        }
 
        // To store the total
        // number of tickets sold
        int ticketSold = 0;
     
        // To store the total amount
        // of collection
        int ans = 0;
     
        // While tickets sold are less than N
        // and q.top > 0 then update the collected
        // amount with the top of the priority
        // queue
        while (ticketSold < N && q.peek() > 0)
        {
            ans = ans + q.peek();
            int temp = q.peek();
            q.poll();
            q.add(temp - 1);
            ticketSold++;
        }
        return ans;
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int M = seats.length;
        int N = 3;
     
        System.out.print(maxAmount(M, N));
    }
}
 
// This code is contributed by Sanjit_Prasad


Python 3
# Python 3 implementation of the approach
 
# Function to return the maximum amount
# that can be collected by selling tickets
def maxAmount(M, N, seats):
     
    # Priority queue that stores
    # the count of empty seats
    q = []
 
    # Insert each array element
    # into the priority queue
    for i in range(M):
        q.append(seats[i])
 
    # To store the total
    # number of tickets sold
    ticketSold = 0
 
    # To store the total amount
    # of collection
    ans = 0
 
    # While tickets sold are less than N
    # and q.top > 0 then update the collected
    # amount with the top of the priority
    # queue
    q.sort(reverse = True)
    while (ticketSold < N and q[0] > 0):
        ans = ans + q[0]
        temp = q[0]
        q = q[1:]
        q.append(temp - 1)
        q.sort(reverse = True)
        ticketSold += 1
 
    return ans
 
# Driver code
if __name__ == '__main__':
    seats = [1, 2, 4]
    M = len(seats)
    N = 3
 
    print(maxAmount(N, M, seats))
 
# This code is contributed by Surendra_Gangwar


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
     
    // Function to return the maximum amount
    // that can be collected by selling tickets
    static int maxAmount(int M, int N, int[] seats)
    {
       
        // Priority queue that stores
        // the count of empty seats
        List q = new List();
       
        // Insert each array element
        // into the priority queue
        for (int i = 0; i < M; i++) {
            q.Add(seats[i]);
        }
         
        q.Sort();
        q.Reverse();
       
        // To store the  total
        // number of tickets sold
        int ticketSold = 0;
       
        // To store the total amount
        // of collection
        int ans = 0;
       
        // While tickets sold are less than N
        // and q.top > 0 then update the collected
        // amount with the top of the priority
        // queue
        while (ticketSold < N && q[0] > 0) {
            ans = ans + q[0];
            int temp = q[0];
            q.RemoveAt(0);
            q.Add(temp - 1);
            q.Sort();
            q.Reverse();
            ticketSold++;
        }  
        return ans;
    }
 
  // Driver code
  static void Main()
  {
    int[] seats = { 1, 2, 4 };
    int M = seats.Length;
    int N = 3;
    Console.WriteLine(maxAmount(N, M, seats));
  }
}
 
// This code is contributed by divyesh072019.


Javascript


输出:
9