From a9e7e67b771e2012892441e11690714bafa105ad Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 8 Jan 2023 15:09:08 +0800 Subject: [PATCH] =?UTF-8?q?P4331=20[BalticOI=202004]Sequence=20=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E5=BA=8F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/99023313 --- Luogu/P4331/P4331.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Luogu/P4331/P4331.cpp diff --git a/Luogu/P4331/P4331.cpp b/Luogu/P4331/P4331.cpp new file mode 100644 index 00000000..2477e9a7 --- /dev/null +++ b/Luogu/P4331/P4331.cpp @@ -0,0 +1,51 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e6 + 5; + +int n; +long long a[N], b[N], ans; +std::priority_queue q; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + + a[i] -= i; + } + + for (int i = 1; i <= n; i++) { + q.emplace(a[i]); + + if (q.top() > a[i]) { + ans += q.top() - a[i]; + q.pop(); + q.emplace(a[i]); + } + + b[i] = q.top(); + } + + for (int i = n - 1; i; i--) { + b[i] = std::min(b[i], b[i + 1]); + } + + cout << ans << endl; + + for (int i = 1; i <= n; i++) { + cout << b[i] + i << ' '; + } + + cout << endl; + + return 0; +}