From 2b49a690f6edd4f8b152f5f5616a4f600c17c5d0 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Fri, 27 Aug 2021 14:17:02 +0800 Subject: [PATCH] =?UTF-8?q?#790.=20[51nod=20=E6=8F=90=E9=AB=98=E7=BB=84400?= =?UTF-8?q?+=E8=AF=95=E9=A2=98=20=E7=AC=AC=E4=BA=8C=E7=BB=84]=E8=B5=B0?= =?UTF-8?q?=E5=A4=9C=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/23309 --- S2OJ/790/790.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 S2OJ/790/790.cpp diff --git a/S2OJ/790/790.cpp b/S2OJ/790/790.cpp new file mode 100644 index 00000000..bfd89666 --- /dev/null +++ b/S2OJ/790/790.cpp @@ -0,0 +1,76 @@ +#pragma GCC optimize("Ofast") + +#include + +using namespace std; + +struct node { + int t; + long long p; + + node() {} + node(int _t, long long _p) + : t(_t), p(_p) {} +}; + +int n, t; +long long d, p, ans, bat, res[500005], tmp; +set> s; + +int main() { + std::ios::sync_with_stdio(false); + cin >> n >> t; + for (int i = 1; i <= n; i++) { + bat -= d; + while (d > 0) { + while (!s.empty() && !res[s.begin()->second]) { + s.erase(s.begin()); + } + tmp = min(d, res[s.begin()->second]); + d -= tmp; + res[s.begin()->second] -= tmp; + while (!s.empty() && res[s.begin()->second] == 0) { + s.erase(s.begin()); + } + } + cin >> d >> p; + if (d > t) { + cout << -1 << endl; + exit(0); + } + while (!s.empty() && p < s.rbegin()->first) { + ans -= res[s.rbegin()->second] * s.rbegin()->first; + ans += p * res[s.rbegin()->second]; + res[i] += res[s.rbegin()->second]; + res[s.rbegin()->second] = 0; + s.erase(--s.end()); + s.insert(make_pair(p, i)); + } + if (bat < t) { + tmp = t - bat; + ans += tmp * p; + res[i] += tmp; + s.insert(make_pair(p, i)); + bat = t; + } + } + bat -= d; + while (d > 0) { + while (!s.empty() && (res[s.begin()->second] == 0)) { + s.erase(s.begin()); + } + tmp = min(d, res[s.begin()->second]); + d -= tmp; + res[s.begin()->second] -= tmp; + while (!s.empty() && res[s.begin()->second] == 0) { + s.erase(s.begin()); + } + } + while (!s.empty()) { + ans -= res[s.begin()->second] * s.begin()->first; + res[s.begin()->second] = 0; + s.erase(s.begin()); + } + cout << ans << endl; + return 0; +}