# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
916983 | Jakub_Wozniak | Permutation (APIO22_perm) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
vector <pair<int,int>> V;
vector<int> construct_permutation(long long n)
{
if(n == 2)return {0};
if(n == 3)return {1,0};
if(n == 4)return {0,1};
long long MAX = 300;
vector <int> v;
vector <int> B;
long long akt = 0;
int r[200];
while(n)
{
B.push_back(n%2);
n/=2;
}
reverse(B.begin() , B.end());
if(B[1] == '1')
{
v.push_back(2);
v.push_back(1);
akt = 2;
}
else
{
if(B[2] == '0')
{
v.push_back(2);
v.push_back(1);
v.push_back(1);
}
else
{
v.push_back(2);
v.push_back(1);
v.push_back(2);
}
akt = 3;
}
while(akt+1 < B.size())
{
v.push_back(MAX);
MAX++;
v.push_back(MAX);
MAX++;
if(B[akt]*2+B[akt+1])v.push_back(B[akt]*2+B[akt+1]);
akt += 2;
}
if(akt < B.size())
{
v.push_back(MAX);
MAX++;
if(B[akt])v.push_back(B[akt]);
}
for(int i = 0 ; i < v.size() ; i++)
{
V.push_back({v[i],-i});
}
sort(V.begin() , V.end());
akt = 0;
for(int i = 0 ; i < V.size() ;i++)
{
r[-V[i].second] = akt;
akt++;
}
for(int i = 0 ; i < V.size() ; i++)
{
v[i] = r[i];
}
V.clear();
return v;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
long long t, n, m, f, xa, ya, xb, yb;
cin >> t;
while (t--)
{
cin >> n;
vector <int> k = construct_permutation(n);
cout << k.size() << '\n';
for(int i= 0 ; i < k.size() ; i++)
{
cout << k[i] << ' ';
}
cout << '\n';
}
return 0;
}