From f593a4176c98a4d0bf597deb16bbba6f81a3d3cf Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Sat, 21 Aug 2021 18:52:53 +0800 Subject: [PATCH] =?UTF-8?q?#636.=200819=E8=B4=AD=E7=89=A9=20(shopping)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/19204 --- S2OJ/636/636.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 S2OJ/636/636.cpp diff --git a/S2OJ/636/636.cpp b/S2OJ/636/636.cpp new file mode 100644 index 00000000..8a0f1a71 --- /dev/null +++ b/S2OJ/636/636.cpp @@ -0,0 +1,50 @@ +#pragma GCC optimize(2) + +#include + +using namespace std; + +struct node { + long long p, q; +} a[50005]; + +int n, k; +long long m, ans; +priority_queue, greater> q; + +int main() { + std::ios::sync_with_stdio(false); + cin >> n >> k >> m; + for (int i = 0; i < n; i++) { + cin >> a[i].p >> a[i].q; + } + sort(a, a + n, [](node a, node b) { return a.q < b.q; }); + for (int i = 0; i < k; i++) { + ans += a[i].q; + q.push(a[i].p - a[i].q); + if (ans > m) { + cout << i << endl; + exit(0); + } + if (i == n - 1) { + cout << n << endl; + exit(0); + } + } + sort(a + k, a + n, [](node a, node b) { return a.p < b.p; }); + for (int i = k; i < n; i++) { + auto t = q.top(); + ans += a[i].p; + if (a[i].q + t < a[i].p) { + q.pop(); + ans += t + a[i].q - a[i].p; + q.push(a[i].p - a[i].q); + } + if (ans > m) { + cout << i << endl; + exit(0); + } + } + cout << n << endl; + return 0; +}