# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1064981 | sammyuri | Permutation (APIO22_perm) | C++17 | 4 ms | 348 KiB |
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 "perm.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
std::vector<int> construct_permutation(long long k) {
// build initial permutation
ll curpow = 2;
vector<int> ans = {0};
while (curpow * 2 <= k) {
curpow *= 2;
ans.push_back(ans.back() + 1);
}
if (curpow == k)
return ans;
// start adding extras
k -= curpow;
while (k) {
curpow = 1;
int i = 0;
while (curpow * 2 <= k) {
curpow *= 2;
i ++;
}
k -= curpow;
vector<int> newans;
int seen = 0;
if (i == 0) newans.push_back(-1);
for (auto a : ans) {
newans.push_back(a);
if (a != -1) {
seen ++;
if (seen == i)
newans.push_back(-1);
}
}
ans = newans;
}
// replace -1s with values
int curmax = 0;
for (int i = 0; i < ans.size(); i ++)
curmax = max(curmax, ans[i]);
for (int i = ans.size() - 1; i >= 0; i --) {
if (ans[i] == -1) {
ans[i] = curmax + 1;
curmax ++;
}
}
// for (auto a : ans)
// cout << a << " "; cout << endl;
return ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |