#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int N = 1e6 + 5;
struct Element {
int x, l, r;
bool operator<(Element other) const {
if(x == other.x) {
return l > other.l;
}
return x > other.x;
}
};
int n, k, a[N];
priority_queue<Element> q;
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> k;
for(int i = 0; i < n; i++) {
cin >> a[i];
q.push((Element){a[i], i, i});
}
vector<pair<int,int>> Ans;
while(1) {
if(q.top().x == 30) break;
int mn = q.top().x;
pair<int,int> le = make_pair(q.top().l, q.top().r);
q.pop();
if(mn == q.top().x && le.second == q.top().l - 1) {
auto val = (Element){mn + 1, le.first, q.top().r};
q.pop();
q.push(val);
}
else {
Ans.push_back(make_pair(mn, le.second + 1));
q.push((Element){mn + 1, le.first, le.second});
}
}
stack<pair<int,int>> s;
for(int i = (int)Ans.size() - 1; i >= 0; i--) {
s.push(Ans[i]);
}
int ptr = 0;
int need = k - (int)s.size();
assert(need >= 0);
for(int i = 0; i <= n; i++) {
while(s.size()) {
if(s.top().second == i) {
if(need && s.top().first > 0) {
int x = s.top().first;
s.pop();
s.push(make_pair(x - 1, i));
s.push(make_pair(x - 1, i));
need--;
}
else {
cout << s.top().first << " ";
s.pop();
}
}
else {
break;
}
}
if(i != n) cout << a[i] << " ";
}
return 0;
}
Compilation message
zalmoxis.cpp: In function 'int main()':
zalmoxis.cpp:43:9: warning: unused variable 'ptr' [-Wunused-variable]
43 | int ptr = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
637 ms |
18332 KB |
Output is correct |
2 |
Correct |
657 ms |
18248 KB |
Output is correct |
3 |
Correct |
662 ms |
18304 KB |
Output is correct |
4 |
Correct |
662 ms |
18228 KB |
Output is correct |
5 |
Correct |
662 ms |
18252 KB |
Output is correct |
6 |
Correct |
654 ms |
18224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
662 ms |
18248 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
646 ms |
18252 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
665 ms |
18248 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
663 ms |
18248 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
661 ms |
18240 KB |
Unexpected end of file - int32 expected |
6 |
Correct |
661 ms |
18248 KB |
Output is correct |
7 |
Incorrect |
677 ms |
18252 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
658 ms |
18244 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
565 ms |
17784 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
205 ms |
8792 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
346 ms |
12632 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
0 ms |
384 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
1 ms |
512 KB |
Unexpected end of file - int32 expected |
14 |
Correct |
118 ms |
2300 KB |
Output is correct |