Submission #984095

# Submission time Handle Problem Language Result Execution time Memory
984095 2024-05-16T10:02:08 Z vjudge1 Permutation (APIO22_perm) C++17
91.3333 / 100
3 ms 444 KB
#include <bits/stdc++.h>
#include "perm.h"

using namespace std;

const int N = 200;

int node = 0;
vector<int> g[N];

int a[N];
int sz[N];

void precalc(int v) {
	sz[v] = 1;
	for (int to : g[v]) {
		precalc(to);
		sz[v] += sz[to];
	}
}

void add(int v, int x) {
	a[v] += x;
	for (int to : g[v]) 
		add(to, x);
}

void dfs(int v) {
	int x = (v > 0);
	for (int to : g[v]) {
		dfs(to);
		add(to, x);
		x += sz[to];
	}
}

vector<int> b;
void construct(int v) {
	sort(g[v].begin(), g[v].end(), [&](int x, int y) {
		return a[x] > a[y];
	});
	if (v) b.push_back(a[v]);
	for (int to : g[v]) 
		construct(to);
}

vector<int> construct_permutation(long long k) {
	if (k == 1) 
		return {};
	
	{
		g[0].clear();
		while (node) {
			g[node].clear();
			a[node] = 0;
			sz[node] = 0;
			node--;
		}
		b.clear();
	}

	g[0].push_back(1);
	
	int mx = 63 - __builtin_clzll(k);
	node = 1;
	while (node < mx) {
		g[node].push_back(node + 1);
		++node;
	}
	k ^= (1ll << mx);
	vector<pair<int, int>> st;
	for (int i = 0; i < mx; i++) {
		if (~k >> i & 1) continue;
		if (st.empty() || st.back().first + st.back().second < i) 
			st.push_back({i, 1});
		else 
			st.back().second++;
	}

	for (auto [pos, k] : st) {
		++node; k--;
		g[pos].push_back(node);
		while (k--) {
			g[node].push_back(++node);
		}
	}

	precalc(0);
	dfs(0);
	construct(0);

	return b;
}	

Compilation message

perm.cpp: In function 'std::vector<int> construct_permutation(long long int)':
perm.cpp:84:22: warning: operation on 'node' may be undefined [-Wsequence-point]
   84 |    g[node].push_back(++node);
      |                      ^~~~~~
# 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 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Partially correct 2 ms 344 KB Partially correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 3 ms 344 KB Output is correct
8 Partially correct 3 ms 348 KB Partially correct
9 Correct 2 ms 444 KB Output is correct
10 Partially correct 3 ms 344 KB Partially correct
11 Partially correct 3 ms 348 KB Partially correct
12 Partially correct 3 ms 344 KB Partially correct
13 Partially correct 3 ms 348 KB Partially correct