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>
#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> construct_permutation(long long k) {
vector<int> p;
int l = 0, r = 1e9;
while (k != 1) {
if (k % 2 == 0) p.push_back(--r);
else {
p.push_back(++l);
p.push_back(--r);
}
k /= 2;
}
reverse(p.begin(), p.end());
vector<int> rrh = p;
sort(rrh.begin(), rrh.end());
rrh.resize(unique(rrh.begin(), rrh.end()) - rrh.begin());
// for(int &j : rrh) cout << j << " "; cout << endl;
auto get = [&] (int x) {
return lower_bound(rrh.begin(), rrh.end(), x) - rrh.begin();
};
// cout << get(1);
// cout << get(2) << endl;
// cout << endl;
for(int &j : p) j = get(j);
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;
assert(calc(ans) == k);
}
#endif // ngu
Compilation message (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |