跳到正文

剪贴板

剪贴板 x15x5gjm

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>
#include "perm.h"
using namespace std;
vector<int> construct_permutation(long long k)
{
	vector<int>a,b;
	int cnt=__lg(k),sm=cnt,c1=0;
	k^=1ll<<cnt;
	while(k)
	{
		int x=__lg(k);
		k^=1ll<<x;
		if(c1<2||!x||!(k>>(x-1)))
			a.push_back(x),++sm,++c1;
		else
		{
			k^=1ll<<(x-1);
			b.push_back(x-1),++sm;
		}
	}
	vector<int>res,ok(sm-cnt+4);
	int p0=0,p1=0,cc=0;
	while(cc<2)
		if(!ok[++p1])++cc;
	for(int i=0;i<cnt;++i)
	{
		if(!a.empty()&&a.back()==i)
		{
			res.push_back(p0),a.pop_back(),--cc,ok[p0]=1;
			while(ok[++p0]);
			while(cc<2)
				if(!ok[++p1])++cc;
		}
		if(!b.empty()&&b.back()==i)
		{
			res.push_back(p1),b.pop_back(),ok[p1]=1;
			while(ok[++p1]);
		}
		res.push_back(--sm);
	}
	reverse(res.begin(),res.end());
	return res;
}