0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 16:36:28 +00:00

P4331 [BalticOI 2004]Sequence 数字序列

https://www.luogu.com.cn/record/99023313
This commit is contained in:
Baoshuo Ren 2023-01-08 15:09:08 +08:00
parent 2095354fea
commit a9e7e67b77
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

51
Luogu/P4331/P4331.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <iostream>
#include <queue>
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<long long> 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;
}