Raslboyy

113190

Dec 18th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <math.h>
  6. #include <climits>
  7.  
  8. //#pragma GCC optimize("O3, unroll-loops");
  9.  
  10. #include <list>
  11. #include <map>
  12. #include <set>
  13. #include <queue>
  14. #include <unordered_map>
  15. #include <unordered_set>
  16.  
  17. //#include <chrono>
  18. //#include <fstream>
  19. //#include <bits/stdc++.h>
  20.  
  21. #define mp make_pair
  22. #define pb push_back
  23. #define eb emplace_back
  24. #define x first
  25. #define y second
  26. #define sz(x) (int)x.size()
  27. #define all(x) begin(x), end(x)
  28. #define rall(x) rbegin(x), rend(x)
  29. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  30. #define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)
  31.  
  32. using namespace std;
  33.  
  34. typedef unsigned long long ull;
  35. typedef long long ll;
  36. typedef long double ld;
  37. typedef pair<int, int> pi;
  38. typedef pair<ll, ll> pl;
  39. typedef vector<int> veci;
  40. typedef vector<bool> vecb;
  41. typedef vector<vector<int>> vvi;
  42. typedef vector<vector<bool>> vvb;
  43.  
  44. const int INF_I = 1e9;
  45. const ll INF_LL = 1e18;
  46.  
  47. struct boy
  48. {
  49.     int h;
  50.     int l;
  51.     int index;
  52. };
  53.  
  54. struct CompareHeight {
  55.     bool operator()(boy const& p1, boy const& p2)
  56.     {
  57.         if (p1.h != p2.h)
  58.             return p1.h < p2.h;
  59.         return p1.l > p2.l;
  60.     }
  61. };
  62.  
  63. int main() {
  64.     /*ifstream cin("in.txt");
  65.     ofstream cout("out.txt");
  66.     auto start_time = chrono::high_resolution_clock::now();*/
  67.  
  68.     ios::sync_with_stdio(false);
  69.     cin.tie(0);
  70.     cout.tie(0);
  71.  
  72.    
  73.     int n, h;
  74.     cin >> n;
  75.     int max = 0;
  76.     vector<boy> b(n);
  77.     priority_queue<boy, vector<boy>, CompareHeight> q;
  78.     for (int i = 0; i < n; i++) {
  79.         cin >> b[i].h >> b[i].l;
  80.         b[i].index = i;
  81.         max += b[i].h;
  82.         q.emplace(b[i]);
  83.     }
  84.        
  85.     cin >> h;
  86.  
  87.     sort(all(b), [](boy b1, boy b2) {
  88.         if (b1.h + b1.l != b2.h + b2.l)
  89.             return b1.h + b1.l > b2.h + b2.l;
  90.         return b1.h > b2.h;
  91.     });
  92.  
  93.     vecb used(n);
  94.     list<boy> res;
  95.     int H = 0;
  96.     for (int i = 0; i < n; i++) {
  97.         if (used[b[i].index])
  98.             continue;
  99.         while (used[q.top().index])
  100.             q.pop();
  101.         if (H + b[i].h + b[i].l >= h) {
  102.             used[b[i].index] = 1;
  103.             res.eb(b[i]);
  104.             H += b[i].h;
  105.         }
  106.         else {
  107.             if (q.top().h >= b[i].h/* || (q.top().h == b[i].h && q.top().l < b[i].l)*/) {
  108.                 used[q.top().index] = 1;
  109.                 H += q.top().h;
  110.                 res.eb(q.top());
  111.                 i--;
  112.             }
  113.             else {
  114.                 used[b[i].index] = 1;
  115.                 res.eb(b[i]);
  116.                 H += b[i].h;
  117.             }
  118.         }
  119.     }
  120.     int cnt = 0;
  121.     for (auto it = res.rbegin(); it != res.rend(); it++) {
  122.         if (max + (*it).l >= h) {
  123.             cnt++;
  124.             max -= (*it).h;
  125.         }
  126.         else {
  127.             break;
  128.         }
  129.     }
  130.     cout << cnt << "\n";
  131.     for (auto it = res.rbegin(); it != res.rend() && cnt > 0; it++, cnt--)
  132.         cout << (*it).index + 1 << " ";
  133.  
  134.     /*auto end_time = chrono::high_resolution_clock::now();
  135.     chrono::duration<double> duration = end_time - start_time;
  136.     cout << duration.count() << "\n";
  137.     cin.close();
  138.     cout.close();*/
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment