Submission #623528

#TimeUsernameProblemLanguageResultExecution timeMemory
623528LoboPermutation (APIO22_perm)C++17
100 / 100
2 ms340 KiB
#include "perm.h"
#include<iostream>
#include<vector>
#include<deque>
#include<math.h>
using namespace std;
#define pb push_back
#define pf push_front
#define int long long

vector<int32_t> construct_permutation(int k)
{
    vector<int32_t> seq;
    int lg = 0;
    for(int i = 0; i <= 60; i++) {
        if(k >= (1LL<<i)) lg = i;
    }
    if(lg < 2) {
        for(int i = k-2; i >= 0; i--) {
            seq.pb(i);
        }
        return seq;
    }
    deque<int> ans;
    int cur = 0;
    //do the first 2/3 separate
    if((1LL<<(lg-1))&k) {
        //begins with 11
        ans.pb(cur++); //10
        ans.pf(cur++); //11
        //ans = 10
        cur = ans.size();
        lg-=2;
    }
    else if((1LL<<(lg-2))&k){
        //begins with 100
        ans.pb(cur++); //10
        ans.pf(cur++); //11
        ans.pf(cur++); //100
        ans.pf(cur++); //101
        //ans = 3210
        cur = ans.size();
        lg-=3;
        
    }
    else {
        //begins with 101
        ans.pb(cur++); //10
        ans.pf(cur++); //11
        ans.pf(cur++); //100
        //ans = 210
        cur = ans.size();
        lg-=3;
    }

    //do the rest in groups of 2
    while(lg != -1) {
        if(lg == 0 || ((1LL<<lg)&k) == 0) {
            //if it is the last or 0
            ans.pb(cur++); //_0
            if((1LL<<lg)&k) ans.pf(cur++); //_1
            lg--;
        }
        else if((1LL<<(lg-1))&k) {
            //it is 11 -> sum 3
            ans.pb(cur++); //_0
            ans.pb(cur++); //_00
            for(int i = 0; i < ans.size(); i++) {
                if(ans[i] > 1) ans[i]++;
            }
            ans.pb(2); //_11
            cur++;
            lg-=2;
        }
        else {
            //it is 10
            ans.pb(cur++); //_0
            ans.pf(cur++); //_1
            ans.pb(cur++); //_10
            lg-=2;
        }
    }

    for(auto x : ans) {
        seq.pb(x);
    }
    return seq;
}

Compilation message (stderr)

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