inhuman_Arif

uri 1000

Nov 5th, 2021
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long ll;
  5.  
  6. struct item
  7. {
  8.     double weight;
  9.     double price;
  10.     double unitprice;
  11. };
  12.  
  13. int main()
  14. {
  15.     #ifndef ONLINE_JUDGE
  16.         freopen("input.txt", "r", stdin);
  17.         freopen("output.txt", "w", stdout);
  18.     #endif
  19.  
  20.     int n;
  21.     cin >> n;
  22.     item arr[n];
  23.     int capacity;
  24.     cin >> capacity;
  25.     for(int i=0;i<n;i++)
  26.         cin >> arr[i].weight >> arr[i].price;
  27.  
  28.     for(int i=0;i<3;i++)
  29.         arr[i].unitprice = (arr[i].price/arr[i].weight);
  30.    
  31.     for(int i=0;i<n-1;i++)
  32.     {
  33.         int key=i;
  34.         for(int j=i+1;j<n;j++)
  35.             if(arr[j].unitprice>arr[key].unitprice)
  36.                 key = j;
  37.         swap(arr[key],arr[i]);
  38.     }
  39.     //sort(arr,arr+n,cmp);
  40.     for(int i=0;i<n;i++)
  41.         cout << arr[i].unitprice << endl;
  42.     double bag=0;
  43.     int ind=0;
  44.     while(ind!=n and capacity>0)
  45.     {
  46.         if(arr[ind].weight<=capacity)
  47.         {
  48.             bag+=arr[ind].price;
  49.             capacity-=arr[ind].weight;
  50.             arr[ind].weight=0;
  51.         }
  52.         else
  53.         {
  54.             bag+=(arr[ind].unitprice*capacity);
  55.             capacity=0;
  56.         }
  57.         ind++;
  58.     }
  59.     cout << bag << endl;
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment