From 737db357066f8261af9e8232f88fd3351bb7fc7f Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Thu, 19 Aug 2021 09:40:28 +0800 Subject: [PATCH] P3045 [USACO12FEB]Cow Coupons G R56334026 --- Luogu/problem/P3045/P3045.cpp | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Luogu/problem/P3045/P3045.cpp diff --git a/Luogu/problem/P3045/P3045.cpp b/Luogu/problem/P3045/P3045.cpp new file mode 100644 index 00000000..65607639 --- /dev/null +++ b/Luogu/problem/P3045/P3045.cpp @@ -0,0 +1,47 @@ +#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() { + 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; +}