Advertisement
satishfrontenddev5

Untitled

Jan 6th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. /*
  2. Given an array A, where A[i] is the number of vacant seats in the ith row in a stadium.
  3.  
  4. There are B people in a queue waiting to buy the tickets.
  5.  
  6. Each seat costs equal to the number of vacant seats in the row it belongs to.
  7.  
  8. The task is to maximize the profit by selling the tickets to B people.
  9.  
  10. NOTE: If all the tickets are sold then rest have to go empty handed.
  11.  
  12. Input format
  13. First line contains two space separated integers N and B.
  14.  
  15. Second line contains N space separated integers representing the array A.
  16.  
  17. Output format
  18. Print answer in a single line
  19.  
  20. Sample Input 1
  21. 2 3
  22.  
  23. 2 3
  24.  
  25. Sample Output 1
  26. 7
  27.  
  28. Explanation
  29. First ticket will be for any seat of second row will be sold for Rs 3.
  30.  
  31. Second ticket will be for any seat of second row which will be sold for 2 because only 2 seats were left in the second row at that time.
  32.  
  33. Third ticket will be for any seat in first row will be sold for 2.
  34.  
  35. Constraints
  36. 1 <= N <= 100000
  37.  
  38. 1 <= |A| <= 100000
  39.  
  40. 1 <= B <= 200000
  41. */
  42.  
  43. long long maxKProfit(int n, int b, vector<int >& a){
  44.     priority_queue<int>pq;
  45.     long ans=0;
  46.     for(int i=0;i<n;i++){
  47.         pq.push(a[i]);
  48.     }
  49.     while(b>0&&!pq.empty()){
  50.         long topEle=pq.top();
  51.         pq.pop();
  52.         ans+=topEle;
  53.         topEle--;
  54.         if(topEle>0)
  55.         pq.push(topEle);
  56.         b--;
  57.     }
  58.     return ans;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement