#include <bits/stdc++.h>
#define mp make_pair
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef set<pii>::iterator ite;
const int MX = 30;
void recur(int val, int left, vector<int> &arr) {
if (left == 1)
{
arr.push_back(val);
return;
} else if (left == 2) {
arr.push_back(val - 1);
arr.push_back(val - 1);
return;
}
int can_make_from_1 = 1 << (val - 1);
if (can_make_from_1 >= left)
{
recur(val - 1, left - 1, arr);
arr.push_back(val - 1);
}
else
{
recur(val - 1, can_make_from_1, arr);
recur(val - 1, left - can_make_from_1, arr);
}
}
vector<int> make(int val, int needed) {
vector<int> out;
recur(val, needed, out);
return out;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
int needed = 1 << 30, lowest = 30;
vector<int> og(n);
vector<pii> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i].second;
og[i] = arr[i].second;
arr[i].first = i;
needed -= 1 << arr[i].second;
lowest = min(lowest, arr[i].second);
}
needed = log2(needed);
vector<vector<int> > to_insert(n);
vector<pii> neww;
for (; lowest < 30; lowest++) {
for (int i = 0; i < arr.size(); i++) {
if (arr[i].second == lowest) {
int s = 0, j;
for (j = i; j < arr.size() && arr[j].second == lowest; j++)
{
s++;
if (s % 2)
neww.push_back({arr[j].first, lowest + 1});
}
i = j - 1;
if (s % 2)
{
to_insert[arr[i].first].push_back(lowest);
k--;
}
} else {
neww.push_back(arr[i]);
}
}
arr.clear();
sort(all(neww));
swap(neww, arr);
}
for (int i = 0; i < n; i++) {
if (k == 0)
break;
//reverse(all(to_insert[i]));
vector<int> neww;
for (int a : to_insert[i]) {
if (k == 0) {
neww.push_back(a);
continue;
}
int possible = (1 << a);
int to_make = min(possible, k) + 1;
vector<int> compose = make(a, to_make);
neww.insert(neww.end(), all(compose));
k++;
k -= to_make;
}
to_insert[i] = neww;
}
vector<int> out;
for (int i = 0; i < n; i++) {
out.insert(out.end(), all(to_insert[i]));
out.push_back(og[i]);
}
for (int a : out)
cout << a << " ";
cout << "\n";
}
Compilation message
zalmoxis.cpp: In function 'int main()':
zalmoxis.cpp:63:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int i = 0; i < arr.size(); i++) {
| ~~^~~~~~~~~~~~
zalmoxis.cpp:66:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (j = i; j < arr.size() && arr[j].second == lowest; j++)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
52956 KB |
Output is correct |
2 |
Correct |
219 ms |
53256 KB |
Output is correct |
3 |
Correct |
206 ms |
52920 KB |
Output is correct |
4 |
Correct |
212 ms |
53528 KB |
Output is correct |
5 |
Correct |
207 ms |
53684 KB |
Output is correct |
6 |
Correct |
196 ms |
53140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
203 ms |
53180 KB |
not a zalsequence |
2 |
Correct |
202 ms |
52668 KB |
Output is correct |
3 |
Incorrect |
205 ms |
52924 KB |
not a zalsequence |
4 |
Incorrect |
208 ms |
54780 KB |
not a zalsequence |
5 |
Correct |
212 ms |
58048 KB |
Output is correct |
6 |
Correct |
203 ms |
58560 KB |
Output is correct |
7 |
Incorrect |
206 ms |
55484 KB |
not a zalsequence |
8 |
Incorrect |
212 ms |
53760 KB |
not a zalsequence |
9 |
Incorrect |
195 ms |
52668 KB |
not a zalsequence |
10 |
Incorrect |
120 ms |
28068 KB |
not a zalsequence |
11 |
Incorrect |
144 ms |
34368 KB |
not a zalsequence |
12 |
Incorrect |
56 ms |
14796 KB |
not a zalsequence |
13 |
Incorrect |
57 ms |
14184 KB |
not a zalsequence |
14 |
Incorrect |
56 ms |
15116 KB |
not a zalsequence |