제출 #707533

#제출 시각아이디문제언어결과실행 시간메모리
707533600MihneaPermutation (APIO22_perm)C++17
71.22 / 100
11 ms1332 KiB
#include "perm.h"
#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <cassert>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
#include <numeric>

using namespace std;

typedef long long ll;

int get_nr_bits(ll x)
{
	if (x == 0)
	{
		return 0;
	}
	assert(x >= 1);
	return get_nr_bits(x / 2) + x % 2;
}

vector<int> construct_permutation(ll total)
{
	if (total == 1)
	{
		return {};
	}
	for (int k = 1; 1; k++)
	{
		ll rem = total + (k - 1);
		int nr_bits = get_nr_bits(rem);
		if (k < nr_bits)
		{
			continue;
		}
		if (rem & (1LL << 0))
		{
			continue;
		}
		// split rem into nr_bits bits
		assert(k >= nr_bits);
		vector<int> dims;
		for (int i = 1; (1LL << i) <= rem; i++)
		{
			if (rem & (1LL << i))
			{
				dims.push_back(i);
			}
		}
		int need_extra = k - nr_bits;
		for (int i = 0; i < (int)dims.size(); i++)
		{
			while (dims[i] >= 2 && need_extra > 0)
			{
				dims.push_back(dims[i] - 1);
				dims[i]--;
				need_extra--;
			}
		}
		assert(need_extra == 0);
		int sumtotal = 0;
		for (auto& d : dims)
		{
			sumtotal += d;
		}
		vector<int> sol;
		for (auto& x : dims)
		{
			sumtotal -= x;
			for (int i = 0; i < x; i++)
			{
				sol.push_back(sumtotal + i);
			}
		}
		//cout << " : ";
		//for (auto& x : sol)
		//{
		//	cout << x << " ";
		//}
		//cout << "\n";
		return sol;
	}
	return {};
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...