Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <string>
- #include <math.h>
- #include <climits>
- //#pragma GCC optimize("O3, unroll-loops");
- #include <list>
- #include <map>
- #include <set>
- #include <queue>
- #include <unordered_map>
- #include <unordered_set>
- //#include <chrono>
- //#include <fstream>
- //#include <bits/stdc++.h>
- #define mp make_pair
- #define pb push_back
- #define eb emplace_back
- #define x first
- #define y second
- #define sz(x) (int)x.size()
- #define all(x) begin(x), end(x)
- #define rall(x) rbegin(x), rend(x)
- #define FOR(i,a,b) for (int i = (a); i < (b); i++)
- #define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)
- using namespace std;
- typedef unsigned long long ull;
- typedef long long ll;
- typedef long double ld;
- typedef pair<int, int> pi;
- typedef pair<ll, ll> pl;
- typedef vector<int> veci;
- typedef vector<bool> vecb;
- typedef vector<vector<int>> vvi;
- typedef vector<vector<bool>> vvb;
- const int INF_I = 1e9;
- const ll INF_LL = 1e18;
- struct boy
- {
- int h;
- int l;
- int index;
- };
- struct CompareHeight {
- bool operator()(boy const& p1, boy const& p2)
- {
- if (p1.h != p2.h)
- return p1.h < p2.h;
- return p1.l > p2.l;
- }
- };
- int main() {
- /*ifstream cin("in.txt");
- ofstream cout("out.txt");
- auto start_time = chrono::high_resolution_clock::now();*/
- ios::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- int n, h;
- cin >> n;
- int max = 0;
- vector<boy> b(n);
- priority_queue<boy, vector<boy>, CompareHeight> q;
- for (int i = 0; i < n; i++) {
- cin >> b[i].h >> b[i].l;
- b[i].index = i;
- max += b[i].h;
- q.emplace(b[i]);
- }
- cin >> h;
- sort(all(b), [](boy b1, boy b2) {
- if (b1.h + b1.l != b2.h + b2.l)
- return b1.h + b1.l > b2.h + b2.l;
- return b1.h > b2.h;
- });
- vecb used(n);
- list<boy> res;
- int H = 0;
- for (int i = 0; i < n; i++) {
- if (used[b[i].index])
- continue;
- while (used[q.top().index])
- q.pop();
- if (H + b[i].h + b[i].l >= h) {
- used[b[i].index] = 1;
- res.eb(b[i]);
- H += b[i].h;
- }
- else {
- if (q.top().h >= b[i].h/* || (q.top().h == b[i].h && q.top().l < b[i].l)*/) {
- used[q.top().index] = 1;
- H += q.top().h;
- res.eb(q.top());
- i--;
- }
- else {
- used[b[i].index] = 1;
- res.eb(b[i]);
- H += b[i].h;
- }
- }
- }
- int cnt = 0;
- for (auto it = res.rbegin(); it != res.rend(); it++) {
- if (max + (*it).l >= h) {
- cnt++;
- max -= (*it).h;
- }
- else {
- break;
- }
- }
- cout << cnt << "\n";
- for (auto it = res.rbegin(); it != res.rend() && cnt > 0; it++, cnt--)
- cout << (*it).index + 1 << " ";
- /*auto end_time = chrono::high_resolution_clock::now();
- chrono::duration<double> duration = end_time - start_time;
- cout << duration.count() << "\n";
- cin.close();
- cout.close();*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment