From 8df881adf092b0cfd69bd5ebaeb095bbe56a181e Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 6 Oct 2022 08:35:39 +0800 Subject: [PATCH] =?UTF-8?q?P1792=20[=E5=9B=BD=E5=AE=B6=E9=9B=86=E8=AE=AD?= =?UTF-8?q?=E9=98=9F]=E7=A7=8D=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/88801338 --- Luogu/P1792/P1792.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Luogu/P1792/P1792.cpp diff --git a/Luogu/P1792/P1792.cpp b/Luogu/P1792/P1792.cpp new file mode 100644 index 00000000..9f39cfe2 --- /dev/null +++ b/Luogu/P1792/P1792.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 2e5 + 5; + +int n, m, a[N], pre[N], suf[N], ans; +bool vis[N]; +std::priority_queue> q; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + if (n / 2 < m) { + cout << "Error!" << endl; + + exit(0); + } + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + + pre[i] = i - 1 ? i - 1 : n; + suf[i] = i == n ? 1 : i + 1; + + q.emplace(a[i], i); + } + + for (int i = 1; i <= m; i++) { + while (!q.empty() && vis[q.top().second]) q.pop(); + + auto o = q.top(); + q.pop(); + + vis[pre[o.second]] = vis[suf[o.second]] = true; + a[o.second] = a[pre[o.second]] + a[suf[o.second]] - a[o.second]; + q.emplace(a[o.second], o.second); + ans += o.first; + + pre[o.second] = pre[pre[o.second]]; + suf[o.second] = suf[suf[o.second]]; + pre[suf[o.second]] = o.second; + suf[pre[o.second]] = o.second; + } + + cout << ans << endl; + + return 0; +}