#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 = 0; 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);
}
int ptr = 0;
for(int i = 0; i < vec.size(); i++){
if(ptr < n && vec[ptr] == arr[i]) isneeded[i] = true, ptr++;
}
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 == 0) ans.push_back(0);
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:59:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i = 0; i < vec.size(); i++){
| ~~^~~~~~~~~~~~
zalmoxis.cpp:63:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i = 0; i < vec.size(); i++){
| ~~^~~~~~~~~~~~
zalmoxis.cpp:70:73: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
70 | while(!stk.empty() && ans.size() + (vec.size() - i - 1) + stk.size() < n + k){
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
88920 KB |
Output is correct |
2 |
Correct |
219 ms |
89276 KB |
Output is correct |
3 |
Correct |
218 ms |
89084 KB |
Output is correct |
4 |
Correct |
245 ms |
89040 KB |
Output is correct |
5 |
Correct |
218 ms |
89032 KB |
Output is correct |
6 |
Correct |
217 ms |
88852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
239 ms |
90040 KB |
Output is correct |
2 |
Correct |
234 ms |
88272 KB |
Output is correct |
3 |
Incorrect |
295 ms |
88092 KB |
doesn't contain S as a subsequence |
4 |
Correct |
239 ms |
90820 KB |
Output is correct |
5 |
Correct |
237 ms |
90644 KB |
Output is correct |
6 |
Correct |
268 ms |
90284 KB |
Output is correct |
7 |
Correct |
234 ms |
88156 KB |
Output is correct |
8 |
Correct |
267 ms |
88852 KB |
Output is correct |
9 |
Incorrect |
252 ms |
80568 KB |
doesn't contain S as a subsequence |
10 |
Incorrect |
182 ms |
51880 KB |
doesn't contain S as a subsequence |
11 |
Incorrect |
200 ms |
57836 KB |
doesn't contain S as a subsequence |
12 |
Incorrect |
87 ms |
29780 KB |
doesn't contain S as a subsequence |
13 |
Incorrect |
102 ms |
29844 KB |
doesn't contain S as a subsequence |
14 |
Correct |
81 ms |
29756 KB |
Output is correct |