#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const long long inf = 2e18;
long long calc (vector<int> a) {
vector<long long> f(a.size());
long long ret = 1;
for(int i = 0; i < a.size(); i++) {
f[i] = 1;
for(int j = 0; j < i; j++) if (a[j] < a[i]) f[i] = min(inf, f[i] + f[j]);
ret += f[i];
}
return ret;
}
vector<int> get (vector<int> p) {
vector<int> rrh = p;
sort(rrh.begin(), rrh.end());
rrh.resize(unique(rrh.begin(), rrh.end()) - rrh.begin());
auto get = [&] (int x) {
return lower_bound(rrh.begin(), rrh.end(), x) - rrh.begin();
};
for(auto &j : p) j = get(j);
return p;
}
vector<int> construct_permutation(long long k) {
vector<int> a;
while (k >= 4) {
a.push_back(k % 4);
k /= 4;
}
reverse(a.begin(), a.end());
vector<int> p;
if (k == 2) p = {0};
if (k == 3) p = {1, 0};
for(int &j : a) {
int mx = p.size();
if (j == 0) {
p.push_back(mx);
p.push_back(mx + 1);
}
if (j == 1) {
p.push_back(mx);
p.push_back(mx + 1);
p.push_back(-1);
}
if (j == 2) {
p.push_back(mx);
p.push_back(-1);
p.push_back(mx + 1);
}
if (j == 3) {
int x = -1;
for(auto &j : p) if (j == 0 || j == 1) {
x = j;
break;
}
if (p.size() >= 2 && x == 1) {
p.push_back(mx);
p.push_back(mx + 1);
for(int &j : p) if (j >= 2) j++;
p.push_back(2);
}
else {
p.push_back(mx);
p.push_back(-1);
p.push_back(mx + 1);
p.push_back(-2);
}
}
p = get(p);
}
return p;
}
#ifdef ngu
int main() {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
long long k; cin >> k;
vector<int> ans = construct_permutation(k);
for(int &j : ans) cout << j << " "; cout << endl;
cerr << ans.size() << endl;
assert(calc(ans) == k);
}
#endif // ngu
Compilation message
perm.cpp: In function 'long long int calc(std::vector<int>)':
perm.cpp:13:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for(int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
348 KB |
Output is correct |
8 |
Correct |
6 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
348 KB |
Output is correct |
11 |
Correct |
6 ms |
344 KB |
Output is correct |
12 |
Correct |
6 ms |
348 KB |
Output is correct |
13 |
Correct |
6 ms |
348 KB |
Output is correct |