Submission #1064981

#TimeUsernameProblemLanguageResultExecution timeMemory
1064981sammyuriPermutation (APIO22_perm)C++17
91.33 / 100
4 ms348 KiB
#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)

perm.cpp: In function 'std::vector<int> construct_permutation(long long int)':
perm.cpp:41:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |  for (int i = 0; i < ans.size(); i ++)
      |                  ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...