제출 #911260

#제출 시각아이디문제언어결과실행 시간메모리
911260RockyBPermutation (APIO22_perm)C++17
0 / 100
1 ms348 KiB
#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));
	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);
	// reverse(all(tmp));
	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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...