From 723456f05e49ee1dc9b55719bcc39208b002065a Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 18 Jul 2024 14:46:47 +0800 Subject: [PATCH] P2949 [USACO09OPEN] Work Scheduling G https://www.luogu.com.cn/record/166876962 --- Luogu/P2949/P2949.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Luogu/P2949/P2949.cpp diff --git a/Luogu/P2949/P2949.cpp b/Luogu/P2949/P2949.cpp new file mode 100644 index 00000000..12388abf --- /dev/null +++ b/Luogu/P2949/P2949.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +int n; +long long ans; +std::pair a[N]; // +std::priority_queue, std::greater<>> q; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> a[i].first >> a[i].second; + } + + std::sort(a + 1, a + 1 + n); + + for (int i = 1; i <= n; i++) { + // 时间充足(每项工作消耗 1 个单位时间) + if (a[i].first > q.size()) { + q.emplace(a[i].second); // 记录报酬 + ans += a[i].second; + } else if (a[i].second > q.top()) { // 时间不足,并且报酬更高 + + // 推掉旧工作 + ans -= q.top(); + q.pop(); + + // 做新的工作 + q.emplace(a[i].second); + ans += a[i].second; + } + } + + cout << ans << endl; + + return 0; +}