Submission #623524

# Submission time Handle Problem Language Result Execution time Memory
623524 2022-08-05T18:51:34 Z Lobo Permutation (APIO22_perm) C++17
100 / 100
2 ms 352 KB
#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) {
        // cout << " " << x << endl;
        seq.pb(x);
    }
    return seq;
}


// #include "perm.h"
// #include<iostream>
// #include<vector>
// #include<deque>
// #include<math.h>
// using namespace std;
// #define int long long

// vector<int32_t> construct_permutation(int k)
// {
// 	int k1 = k;
// 	int k2 = 1;
// 	vector<int> ks;
// 	for(int i = 2; i <= min((int) 8e5,k); i++) {
// 		while(k%i == 0) {
// 			ks.push_back(i);
// 			k/=i;
// 		}
// 	}
// 	ks.push_back(k);
//     vector<int32_t> vc;
//     for(auto k1 : ks) {
// 	    deque<int> ans;
// 	    // int lg = 0;
// 	    // for(int i = 0; i <= 60; i++) {
// 	    // 	if(k1 >= (1LL<<i)) lg = i;
// 	    // }
// 	    int lg = log2(k1);
// 	    for(int i = lg-1; i >= 0; i--) {
// 	        ans.push_back((int) ans.size()+vc.size());
// 	        if(k1&(1LL<<i)) ans.push_front((int) ans.size()+vc.size());
// 	    }
// 	    cout << "   " << k1 << endl;
// 	    for(auto x : ans) {
// 	    	// cout << " 1 " << x << endl;
// 	    	vc.push_back(x);
// 	    }
// 	}
//     return vc;
// }

Compilation message

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 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 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 352 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 300 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct