이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#ifndef wws
#include "perm.h"
#endif
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define pp pop_back
#define mp make_pair
#define sz(x) (int)x.size()
#define sqr(x) ((x) * 1ll * (x))
#define all(x) x.begin(), x.end()
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, l, r) for (int i = (l); i >= (r); i--)
#define Kazakhstan ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define nl '\n'
#define ioi exit(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = (int)5e5 + 7;
const int inf = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const ll linf = (ll)1e18 + 7;
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
using namespace std;
ll n;
vector<int>ans, tmp, was, cnt;
vector<vector<int>> g;
void go(ll x) {
if (!x) return;
if (x & 1) {
go(x / 2);
ans.pb(sz(ans) ? *max_element(all(ans)) + 1 : 0);
}
else {
go(x - 1);
ans.pb(sz(ans) ? *min_element(all(ans)) - 1 : 0);
}
}
void dfs(int v) {
was[v] = 1;
for (auto to : g[v]) if (!was[to]) dfs(to);
tmp.pb(v);
}
void addEdge(int x, int y) {
cnt[y]++;
g[x].pb(y);
}
void solve(ll n) {
// cin >> n;
ans.clear();
g.clear();
cnt.clear();
tmp.clear();
was.clear();
go(n-1);
g.resize(sz(ans));
was.resize(sz(ans));
cnt.resize(sz(ans), 0);
rep(i, 0, sz(ans) - 1) {
// cerr << ans[i] << ' ';
rep(j, i + 1, sz(ans) - 1) {
if (ans[i] < ans[i]) {
addEdge(i, j);
}
if (ans[i] > ans[j]) {
addEdge(j, i);
}
}
}
queue<int> q;
rep(i, 0, sz(ans) - 1) if (!cnt[i]) q.push(i);
while (sz(q)) {
int v = q.front();
q.pop();
tmp.pb(v);
// cerr << v << nl;
for (auto to : g[v]) {
if (!--cnt[to]) q.push(to);
}
}
}
vector<int> construct_permutation(ll n) {
solve(n);
vector<int> perm(sz(tmp));
rep(i, 0, sz(tmp) - 1) {
perm[tmp[i]] = i;
}
return perm;
// for (auto it : tmp) cout << it << ' ';
}
#ifdef wws
int main() {
freopen ("in.txt", "r", stdin);
for (auto it : construct_permutation(5)) cout << it << ' ';
ioi
}
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |