Advertisement
Dimaush

Странные яблоки

Oct 8th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. struct apple {
  8.     unsigned num;
  9.     unsigned down, up;
  10.  
  11.     apple(unsigned n = 0, unsigned d = 0, unsigned u = 0) : num(n), down(d), up(u) {}
  12. };
  13.  
  14. int main() {
  15.     ios_base::sync_with_stdio(false);
  16.     cin.tie(nullptr);
  17.  
  18.     unsigned n;
  19.     int s;
  20.     cin >> n >> s;
  21.     vector<apple> plus, minus;
  22.     {
  23.         unsigned a, b;
  24.         apple c;
  25.         for (unsigned i = 0; i < n; ++i) {
  26.             cin >> a >> b;
  27.             c = apple(i + 1, a, b);
  28.             b < a ? minus.push_back(c) : plus.push_back(c);
  29.         }
  30.     }
  31.  
  32.     sort(plus.begin(), plus.end(),
  33.             [](apple x, apple y) { return x.down < y.down; });
  34.     sort(minus.begin(), minus.end(),
  35.             [](apple x, apple y) { return x.up > y.up; });
  36.  
  37.     bool flag = true;
  38.     for (unsigned i = 0; i < plus.size(); ++i) {
  39.         s -= plus[i].down;
  40.         if (s <= 0) { flag = false; }
  41.         s += plus[i].up;
  42.     }
  43.     for (unsigned i = 0; i < minus.size(); ++i) {
  44.         s -= minus[i].down;
  45.         if (s <= 0) { flag = false; }
  46.         s += minus[i].up;
  47.     }
  48.     if (flag) {
  49.         for (unsigned i = 0; i < plus.size(); ++i) {
  50.             cout << plus[i].num << " ";
  51.         }
  52.         for (unsigned i = 0; i < minus.size(); ++i) {
  53.             cout << minus[i].num << " ";
  54.         }
  55.     } else {
  56.         cout << -1;
  57.     }
  58.     cout << endl;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement