Advertisement
a53

cafea

a53
Apr 20th, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. ifstream fin("cafea.in");
  5. ofstream fout("cafea.out");
  6. struct cafea
  7. {
  8. int kg, pret;
  9. };
  10. cafea v[100005];
  11.  
  12. int K, S, N;
  13. bool cmp(struct cafea a, struct cafea b)
  14. {
  15. double r1 = (double)a.pret/(double)a.kg;
  16. double r2 = (double)b.pret/(double)b.kg;
  17. return r1 < r2;
  18. }
  19. int fractionalKnapsack(int W, struct cafea arr[], int n)
  20. {
  21. sort(arr, arr + n, cmp);
  22. int curWeight = 0;
  23. int finalvalue = 0;
  24. for(int i = 0; i < n; i++)
  25. {
  26. if (curWeight + arr[i].kg <= W)
  27. {
  28. curWeight += arr[i].kg;
  29. finalvalue += arr[i].pret;
  30. }
  31. else
  32. {
  33. int remain = W - curWeight;
  34. int pr=(arr[i].pret*remain)/arr[i].kg;
  35. if((arr[i].pret*remain)%arr[i].kg)
  36. pr++;
  37. finalvalue += pr;
  38. break;
  39. }
  40. }
  41. return finalvalue;
  42. }
  43. int main()
  44. {
  45. fin>>K>>S>>N;
  46. for(int i=0; i<N; ++i)
  47. fin>>v[i].kg>>v[i].pret;
  48. int fsol=fractionalKnapsack(K,v,N);
  49. if(S>=fsol)
  50. fout<<S-fsol;
  51. else
  52. fout<<0;
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement