Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct mal
  6. {
  7. int price;
  8. int weight;
  9. };
  10.  
  11.  
  12. bool cmp(struct mal a, struct mal b)
  13. {
  14. double unitprice1 = (double)a.price / a.weight;
  15. double unitprice2 = (double)b.price / b.weight;
  16. return unitprice1 > unitprice2;
  17. }
  18.  
  19. double buijjhalow(struct mal mals[],int bostarWeight, int n)
  20. {
  21. sort(mals, mals + n, cmp);
  22.  
  23. int w = 0;
  24. double res = 0.0;
  25.  
  26.  
  27. for (int i = 0; i < n; i++)
  28. {
  29.  
  30. if (w + mals[i].weight <= bostarWeight)
  31. {
  32. w += mals[i].weight;
  33. res += mals[i].price;
  34. }
  35.  
  36.  
  37. else
  38. {
  39. int left = bostarWeight - w;
  40. res += mals[i].price * ((double) left / mals[i].weight);
  41. break;
  42. }
  43. }
  44.  
  45. return res;
  46. }
  47.  
  48. int main()
  49. {
  50. int a,n,c;
  51. cin>>n>>c;
  52. mal mals[n];
  53. for(int i=0;i<n;i++){
  54. cin>>a;
  55. mals[i].weight=a;
  56. }
  57. for(int i=0;i<n;i++){
  58. cin>>a;
  59. mals[i].price=a;
  60. }
  61.  
  62. printf("%.2lf\n",buijjhalow(mals,c,n));
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement