Submission #574171

# Submission time Handle Problem Language Result Execution time Memory
574171 2022-06-08T05:46:37 Z Lawliet Permutation (APIO22_perm) C++17
92.6667 / 100
2 ms 360 KB
#include "perm.h"
#include <bits/stdc++.h>

using namespace std;

vector<int> buildBaseTwo(long long k)
{
	int mn = 0;
	vector<int> ans;

	while( k > 1 )
	{
		if( k & 1 )
			ans.push_back( mn++ ), k ^= 1;
		else
			ans.push_back( -1 ), k >>= 1;
	}

	int mx = (int)ans.size() - 1;

	for(int i = 0 ; i < ans.size() ; i++)
		if( ans[i] == -1 ) ans[i] = mx--;

	reverse( ans.begin() , ans.end() );
	return ans;
}

vector<int> buildBaseThree(long long k)
{
	int mn = 0;
	vector<int> ans;

	while( k > 2 )
	{
		if( k%3 == 1 )
			ans.push_back( mn++ ), k--;
		else if( k%3 == 2 )
			ans.insert( ans.end() , { mn , mn + 1 } ), k -= 2, mn += 2;
		else
			ans.insert( ans.end() , { -1 , -1 } ), k /= 3;
	}

	if( k == 2 )
		ans.push_back( mn++ );

	int mx = (int)ans.size() - 1;

	for(int i = 0 ; i < ans.size() ; i++)
		if( ans[i] == -1 ) ans[i + 1] = mx--, ans[i] = mx--;

	reverse( ans.begin() , ans.end() );

	return ans;
}

vector<int> buildBaseFour(long long k)
{
	int mn = 0;
	vector<int> ans;

	while( k > 3 )
	{
		if( k%4 == 1 )
			ans.push_back( mn++ ), k ^= 1;
		else if( k%4 == 2 )
			ans.push_back( -1 ), k >>= 1;
		else if( k%4 == 3 )
			ans.insert( ans.end() , { mn + 1 , mn } ), k -= 3, mn += 2;
		else
			ans.insert( ans.end() , { -1 , -1 } ), k >>= 2;
	}

	if( k == 2 )
		ans.push_back( mn++ );

	if( k == 3 )
		ans.insert( ans.end() , { mn , mn + 1 } );

	int mx = (int)ans.size() - 1;

	for(int i = 0 ; i < ans.size() ; i++)
		if( ans[i] == -1 ) ans[i] = mx--;

	reverse( ans.begin() , ans.end() );

	return ans;
}

vector<int> construct_permutation(long long k)
{
	vector<int> bases[] = {
		buildBaseTwo(k),
		buildBaseThree(k),
		buildBaseFour(k)
	};
	
	sort( bases , bases + 3 , [&](vector<int>& a, vector<int>& b){
		return a.size() < b.size();
	});

	return bases[0];
}

Compilation message

perm.cpp: In function 'std::vector<int> buildBaseTwo(long long int)':
perm.cpp:21:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |  for(int i = 0 ; i < ans.size() ; i++)
      |                  ~~^~~~~~~~~~~~
perm.cpp: In function 'std::vector<int> buildBaseThree(long long int)':
perm.cpp:48:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |  for(int i = 0 ; i < ans.size() ; i++)
      |                  ~~^~~~~~~~~~~~
perm.cpp: In function 'std::vector<int> buildBaseFour(long long int)':
perm.cpp:81:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |  for(int i = 0 ; i < ans.size() ; i++)
      |                  ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Partially correct 2 ms 360 KB Partially correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Partially correct 2 ms 340 KB Partially correct
9 Correct 1 ms 340 KB Output is correct
10 Partially correct 2 ms 340 KB Partially correct
11 Partially correct 2 ms 340 KB Partially correct
12 Partially correct 2 ms 340 KB Partially correct
13 Partially correct 2 ms 340 KB Partially correct