This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, k, arr[N], onecnt[N], needcnt;
struct Item{
int x;
vector<int> nums;
};
vector<Item> a, b;
vector<int> newnums[N];
vector<int> v, ans;
bool caninc[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];
a.push_back({arr[i], {arr[i]}});
}
for(int num = 1; num <= 29; num++){
swap(a, b);
a.clear();
while(!b.empty()){
if(b[b.size() - 1].x == num){
if(b.size() >= 2 && b[b.size() - 2].x == num){
vector<int> tmp;
for(auto i : b[b.size() - 2].nums) tmp.push_back(i);
for(auto i : b[b.size() - 1].nums) tmp.push_back(i);
a.push_back({num + 1, tmp});
b.pop_back(), b.pop_back();
}
else{
vector<int> tmp;
tmp.push_back(num);
for(auto i : b[b.size() - 1].nums) tmp.push_back(i);
a.push_back({num + 1, tmp});
b.pop_back();
}
}
else{
a.push_back({b[b.size() - 1].x, b[b.size() - 1].nums});
b.pop_back();
}
}
reverse(a.begin(), a.end());
}
v = a[0].nums;
for(int i = 0; i < v.size(); i++) newnums[i].push_back(v[i]);
needcnt = (n + k) - v.size();
int ptr = 0;
for(int i = 0; i < v.size(); i++){
if(ptr < n && v[i] == arr[i]) ptr++;
else caninc[i] = true;
}
for(int i = 0; i < v.size(); i++){
if(caninc[i]){
while(needcnt > 0 && !newnums[i].empty()){
if(newnums[i].back() == 1){
onecnt[i]++;
newnums[i].pop_back();
}
else{
int x = newnums[i].back();
newnums[i].pop_back();
needcnt--;
newnums[i].push_back(x - 1);
newnums[i].push_back(x - 1);
}
}
}
}
for(int i = 0; i < v.size(); i++){
for(int j = 0; j < onecnt[i]; j++) ans.push_back(1);
for(auto j : newnums[i]) ans.push_back(j);
}
for(auto i : ans) cout << i << " ";
}
Compilation message (stderr)
zalmoxis.cpp: In function 'int main()':
zalmoxis.cpp:71:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int i = 0; i < v.size(); i++) newnums[i].push_back(v[i]);
| ~~^~~~~~~~~~
zalmoxis.cpp:77:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
zalmoxis.cpp:82:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
zalmoxis.cpp:103:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for(int i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |