Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. struct Boxes {
  8. long long a;
  9. long long b;
  10. };
  11.  
  12. bool cmp(Boxes lhs, Boxes rhs) {
  13. if (lhs.a < lhs.b) {
  14. if (rhs.a < rhs.b)
  15. return lhs.a < rhs.a;
  16. else
  17. return lhs.a < rhs.b;
  18. }
  19. else {
  20. if (rhs.a < rhs.b)
  21. return lhs.b < rhs.a;
  22. else
  23. return lhs.b < rhs.b;
  24. }
  25. }
  26.  
  27. int main() {
  28. int n;
  29. long long L;
  30. cin >> n;
  31. vector<Boxes> Boxes(n);
  32. for (int i = 0; i < n; ++i) {
  33. cin >> Boxes[i].a >> Boxes[i].b;
  34. }
  35. cin >> L;
  36.  
  37. sort(Boxes.begin(), Boxes.end(), cmp);
  38.  
  39. int count = 0;
  40. int k = 0;
  41. while (L > 0) {
  42. if (L - min(Boxes[k].a, Boxes[k].b) < 0) {
  43. if (min(Boxes[k].a, Boxes[k].b) / 2 >= L) {
  44. L += min(Boxes[k - 1].a, Boxes[k - 1].b) / 2;
  45. if (min(Boxes[k].a, Boxes[k].b) / 2 >= L) {
  46. break;
  47. } else {
  48. ++count;
  49. break;
  50. }
  51. }
  52. else {
  53. L -= min(Boxes[k].a, Boxes[k].b) / 2;
  54. ++count;
  55. ++k;
  56. if (k == n)
  57. break;
  58. }
  59. }
  60. else {
  61. L -= min(Boxes[k].a, Boxes[k].b);
  62. ++count;
  63. ++k;
  64. if (k == n)
  65. break;
  66. }
  67. }
  68.  
  69. if (count == 0)
  70. count++;
  71.  
  72. cout << count;
  73.  
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement