📌  相关文章
📜  售票后利润最大化

📅  最后修改于: 2021-10-28 01:50:05             🧑  作者: Mango

给定阵列的座位[]其中座椅[i]为的空闲座位的第i行中的用于板球比赛运动场数量。有N个人在排队等着买票。每个座位的成本等于它所属排的空座位数。任务是通过将门票出售给N个人来最大化利润。

例子:

做法:为了利润最大化,票必须是空座位数最多的一排座位,当一个座位刚售完时,该排空座位数减1 .所有的人都可以卖一张座位票,直到有空位。这可以在 priority_queue 的帮助下有效计算。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the maximized profit
int maxProfit(int seats[], int k, int n)
{
 
    // Push all the vacant seats
    // in a priority queue
    priority_queue pq;
    for (int i = 0; i < k; i++)
        pq.push(seats[i]);
 
    // To store the maximized profit
    int profit = 0;
 
    // To count the people that
    // have been sold a ticket
    int c = 0;
    while (c < n) {
 
        // Get the maximimum number of
        // vacant seats for any row
        int top = pq.top();
 
        // Remove it from the queue
        pq.pop();
 
        // If there are no vacant seats
        if (top == 0)
            break;
 
        // Update the profit
        profit = profit + top;
 
        // Push the updated status of the
        // vacant seats in the current row
        pq.push(top - 1);
 
        // Update the count of persons
        c++;
    }
    return profit;
}
 
// Driver code
int main()
{
    int seats[] = { 2, 3, 4, 5, 1 };
    int k = sizeof(seats) / sizeof(int);
    int n = 6;
 
    cout << maxProfit(seats, k, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG {
 
// Function to return the maximized profit
static int maxProfit(int seats[], int k, int n)
{
     
    // Push all the vacant seats
    // in a priority queue
    PriorityQueue pq;
    pq = new PriorityQueue<>(Collections.reverseOrder());
     
    for(int i = 0; i < k; i++)
       pq.add(seats[i]);
 
    // To store the maximized profit
    int profit = 0;
 
    // To count the people that
    // have been sold a ticket
    int c = 0;
    while (c < n)
    {
 
        // Get the maximimum number of
        // vacant seats for any row
        int top = pq.remove();
 
        // If there are no vacant seats
        if (top == 0)
            break;
 
        // Update the profit
        profit = profit + top;
 
        // Push the updated status of the
        // vacant seats in the current row
        pq.add(top - 1);
 
        // Update the count of persons
        c++;
    }
     
    return profit;
}
     
// Driver Code
public static void main(String args[])
{
    int seats[] = { 2, 3, 4, 5, 1 };
    int k = seats.length;
    int n = 6;
 
    System.out.println(maxProfit(seats, k ,n));
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 implementation of the approach
 
# Function to return the maximized profit
def maxProfit(seats, k, n) :
 
    # Push all the vacant seats
    # in a priority queue
    pq = [];
    for i in range(k) :
        pq.append(seats[i]);
     
    # for maintaining the property of max heap
    pq.sort(reverse = True);
     
    # To store the maximized profit
    profit = 0;
 
    # To count the people that
    # have been sold a ticket
    c = 0;
    while (c < n) :
         
        # for maintaining the property of max heap
        pq.sort(reverse = True);
         
        # Get the maximimum number of
        # vacant seats for any row
        top = pq[0];
         
        # Remove it from the queue
        pq.pop(0);
         
        # If there are no vacant seats
        if (top == 0) :
            break;
             
        # Update the profit
        profit = profit + top;
         
        # Push the updated status of the
        # vacant seats in the current row
        pq.append(top - 1);
         
        # Update the count of persons
        c += 1;
     
    return profit;
 
# Driver code
if __name__ == "__main__" :
 
    seats = [ 2, 3, 4, 5, 1 ];
    k = len(seats);
    n = 6;
 
    print(maxProfit(seats, k, n));
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to return the maximized profit
static int maxProfit(int[] seats, int k, int n)
{
     
    // Push all the vacant seats
    // in a priority queue
    List pq = new List();
    for(int i = 0; i < k; i++)
        pq.Add(seats[i]);
   
    // To store the maximized profit
    int profit = 0;
   
    // To count the people that
    // have been sold a ticket
    int c = 0;
     
    while (c < n)
    {
         
        // Get the maximimum number of
        // vacant seats for any row
        pq.Sort();
        pq.Reverse();
        int top = pq[0];
   
        // Remove it from the queue
        pq.RemoveAt(0);
   
        // If there are no vacant seats
        if (top == 0)
            break;
   
        // Update the profit
        profit = profit + top;
   
        // Push the updated status of the
        // vacant seats in the current row
        pq.Add(top - 1);
   
        // Update the count of persons
        c++;
    }
    return profit;
}
 
// Driver Code
static void Main()
{
    int[] seats = { 2, 3, 4, 5, 1 };
    int k = seats.Length;
    int n = 6;
     
    Console.Write(maxProfit(seats, k, n));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


C++
#include 
using namespace std;
int maxProfit(int seats[],int k, int n)
{
    sort(seats,seats+k);
    int ans = 0;
    int i = k - 1;
    int j = k - 2;
    while (n > 0 && j >= 0) {
        if (seats[i] > seats[j]) {
            ans = ans + min(n, (i - j)) * seats[i];
            n = n - (i - j);
            seats[i]--;
        }
        else {
            while (j >= 0 && seats[j] == seats[i])
                j--;
            if (j < 0)
                break;
            ans = ans + min(n, (i - j)) * seats[i];
            n = n - (i - j);
            seats[i]--;
        }
    }
    while (n > 0 && seats[i] != 0) {
        ans = ans + min(n, k) * seats[i];
        n -= k;
        seats[i]--;
    }
    return ans;
}
int main()
{
    int seats[] = { 2, 3, 4, 5, 1 };
    int k = sizeof(seats) / sizeof(int);
    int n = 6;
 
    cout << maxProfit(seats, k, n);
 
    return 0;
}


输出
22

滑动窗口方法:

这个问题也可以使用滑动窗口技术来解决。

  • 对于每个人,我们需要出售具有最高价格的票并将其价值减 1。
  • 对阵列座位进行排序。
  • 保持两个指针指向当前最大座位数和下一个最大座位数。
  • 我们迭代直到我们的 n>0 并且数组中有第二大元素。
  • 在每次迭代中,如果座位 [i] > 座位 [j] ,我们将座位 [i] 处的值,min(n, ij) 次添加到我们的答案中,并减少第 i 个索引处的值,否则我们找到 j 使得座位 [j] ]<座位[i]。如果没有这样的 j,我们就中断。
  • 如果在迭代结束时我们的 n>0 和座位 [i]!=0,我们添加座位 [i] 直到 n>0 和座位 [i]!=0。

C++

#include 
using namespace std;
int maxProfit(int seats[],int k, int n)
{
    sort(seats,seats+k);
    int ans = 0;
    int i = k - 1;
    int j = k - 2;
    while (n > 0 && j >= 0) {
        if (seats[i] > seats[j]) {
            ans = ans + min(n, (i - j)) * seats[i];
            n = n - (i - j);
            seats[i]--;
        }
        else {
            while (j >= 0 && seats[j] == seats[i])
                j--;
            if (j < 0)
                break;
            ans = ans + min(n, (i - j)) * seats[i];
            n = n - (i - j);
            seats[i]--;
        }
    }
    while (n > 0 && seats[i] != 0) {
        ans = ans + min(n, k) * seats[i];
        n -= k;
        seats[i]--;
    }
    return ans;
}
int main()
{
    int seats[] = { 2, 3, 4, 5, 1 };
    int k = sizeof(seats) / sizeof(int);
    int n = 6;
 
    cout << maxProfit(seats, k, n);
 
    return 0;
}
输出
22

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