a53

Rucsac

a53
Nov 27th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cassert>
  4. using namespace std;
  5. #define NN 1005
  6.  
  7. struct obiect{
  8. int greutate, valoare;
  9. };
  10.  
  11. int n, Gmax;
  12. obiect O[NN];
  13.  
  14. bool fcmp(obiect A, obiect B)
  15. {
  16. return A.valoare * B.greutate > A.greutate * B.valoare;
  17. }
  18.  
  19. int main(){
  20. assert(cin >> n >> Gmax );
  21. for(int i=1 ; i<=n ; ++i)
  22. assert(cin >> O[i].greutate >> O[i].valoare);
  23. sort (O + 1 , O + n + 1, fcmp);
  24. int G = 0;
  25. double V = 0;
  26. int i = 1;
  27. while( i <= n)
  28. {
  29. if(G + O[i].greutate <= Gmax)
  30. {
  31. G += O[i].greutate;
  32. V += O[i].valoare;
  33. i ++;
  34. }
  35. else
  36. if(Gmax - G > 0)
  37. {
  38. int x = Gmax - G;
  39. double factor = 1.0 * x / O[i].greutate;
  40. G = Gmax;
  41. V += factor * O[i].valoare;
  42. i++;
  43. }
  44. else
  45. i = n + 1;
  46. }
  47. cout << V;
  48. return 0;
  49. }
Add Comment
Please, Sign In to add comment