Advertisement
mickypinata

PROG-T1030: Rice

Mar 26th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. typedef struct rice{
  6.     double price;
  7.     int qt;
  8.     bool operator < (const rice &rhs)const{
  9.         return price > rhs.price;
  10.     }
  11. }rice;
  12.  
  13. priority_queue<rice> pile;
  14. int np, nm;
  15.  
  16. int main(){
  17.  
  18.     double p, sum;
  19.     int q, want;
  20.  
  21.     scanf("%d", &np);
  22.     for(int i = 0; i < np; ++i){
  23.         scanf("%lf %d", &p, &q);
  24.         pile.push({p / (double)q, q});
  25.     }
  26.     scanf("%d", &nm);
  27.     for(int i = 0; i < nm; ++i){
  28.         scanf("%d", &want);
  29.         sum = 0;
  30.         while(!pile.empty() && want > 0){
  31.             p = pile.top().price;
  32.             q = pile.top().qt;
  33.             if(want < q){
  34.                 sum += p * want;
  35.                 pile.push({p, q - want});
  36.                 pile.pop();
  37.                 want -= q;
  38.             } else {
  39.                 sum += p * q;
  40.                 want -= q;
  41.                 pile.pop();
  42.             }
  43.         }
  44.         printf("%.3f\n", sum);
  45.     }
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement