#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
int n, k, arr[N];
vector<int> nums[N];
vector<pair<int, int>> v, vnext;
vector<int> vec, ans;
bool isneeded[N];
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> k;
for(int i = 0; i < n; i++){
cin >> arr[i];
nums[i].push_back(arr[i]);
v.push_back({arr[i], i});
}
for(int cur = 1; cur <= 29; cur++){
reverse(v.begin(), v.end());
while(!v.empty()){
pair<int, int> p = v.back(); v.pop_back();
if(p.first != cur) vnext.push_back(p);
else{
if(!v.empty() && v.back().first == cur){
vnext.push_back({cur + 1, v.back().second});
v.pop_back();
}
else{
nums[p.second].push_back(cur);
vnext.push_back({cur + 1, p.second});
}
}
}
swap(v, vnext);
vnext.clear();
}
for(int i = 0; i < n; i++){
for(auto x : nums[i]) vec.push_back(x);
assert(vec.size() <= N);
}
int ptr = 0;
for(int i = 0; i < vec.size(); i++){
if(ptr < n && vec[ptr] == arr[i]) isneeded[i] = true, ptr++;
}
assert(ptr == n);
for(int i = 0; i < vec.size(); i++){
if(isneeded[i]) ans.push_back(vec[i]);
else{
vector<int> stk;
stk.push_back(vec[i]);
while(!stk.empty() && ans.size() + (vec.size() - i - 1) + stk.size() < n + k){
int cur = stk.back(); stk.pop_back();
if(cur == 1) ans.push_back(1);
else stk.push_back(cur - 1), stk.push_back(cur - 1);
}
while(!stk.empty()){
ans.push_back(stk.back());
stk.pop_back();
}
}
}
assert(ans.size() == n + k || ans.size() == n + k - 1);
// cout << ans.size() << "\n";
for(auto x : ans) cout << x << " ";
cout << "\n";
}
Compilation message
zalmoxis.cpp: In function 'int main()':
zalmoxis.cpp:61:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i = 0; i < vec.size(); i++){
| ~~^~~~~~~~~~~~
zalmoxis.cpp:67:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i = 0; i < vec.size(); i++){
| ~~^~~~~~~~~~~~
zalmoxis.cpp:74:73: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
74 | while(!stk.empty() && ans.size() + (vec.size() - i - 1) + stk.size() < n + k){
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from zalmoxis.cpp:1:
zalmoxis.cpp:88:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
88 | assert(ans.size() == n + k || ans.size() == n + k - 1);
| ~~~~~~~~~~~^~~~~~~~
zalmoxis.cpp:88:43: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
88 | assert(ans.size() == n + k || ans.size() == n + k - 1);
| ~~~~~~~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
385 ms |
180288 KB |
Execution killed with signal 6 |
2 |
Runtime error |
339 ms |
177984 KB |
Execution killed with signal 6 |
3 |
Runtime error |
348 ms |
179272 KB |
Execution killed with signal 6 |
4 |
Runtime error |
355 ms |
179020 KB |
Execution killed with signal 6 |
5 |
Runtime error |
335 ms |
178680 KB |
Execution killed with signal 6 |
6 |
Runtime error |
378 ms |
181884 KB |
Execution killed with signal 6 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
349 ms |
179784 KB |
Execution killed with signal 6 |
2 |
Runtime error |
375 ms |
181464 KB |
Execution killed with signal 6 |
3 |
Runtime error |
336 ms |
178120 KB |
Execution killed with signal 6 |
4 |
Runtime error |
350 ms |
178076 KB |
Execution killed with signal 6 |
5 |
Runtime error |
339 ms |
178804 KB |
Execution killed with signal 6 |
6 |
Runtime error |
380 ms |
179848 KB |
Execution killed with signal 6 |
7 |
Runtime error |
338 ms |
178532 KB |
Execution killed with signal 6 |
8 |
Runtime error |
362 ms |
177256 KB |
Execution killed with signal 6 |
9 |
Runtime error |
311 ms |
157820 KB |
Execution killed with signal 6 |
10 |
Runtime error |
155 ms |
91596 KB |
Execution killed with signal 6 |
11 |
Runtime error |
224 ms |
117408 KB |
Execution killed with signal 6 |
12 |
Runtime error |
32 ms |
48096 KB |
Execution killed with signal 6 |
13 |
Runtime error |
32 ms |
48076 KB |
Execution killed with signal 6 |
14 |
Correct |
82 ms |
29812 KB |
Output is correct |