Submission #634774

# Submission time Handle Problem Language Result Execution time Memory
634774 2022-08-24T22:44:26 Z MinaRagy06 Permutation (APIO22_perm) C++17
100 / 100
2 ms 340 KB
#include <bits/stdc++.h>
#include "perm.h"
using namespace std;

vector<int> construct_permutation(long long m)
{
    long long k = m;
    if (k == 2) return {0};
    if (k == 3) return {1, 0};
    stack<int> ops; //0 -> *2, 1 -> +1, 2 -> +2, 3 -> +3
    while (((k>>4)&1) || k >= (1<<5)-1)
    {
        if ((k&3) == 3) ops.push(3), ops.push(0), ops.push(0), k>>=2;
        else if ((k&1) == 1) ops.push(1), ops.push(0), ops.push(0), k>>=2;
        else ops.push(0), k>>=1;
    }
    if (k == 0b0100) ops.push(1);
    if (k == 0b0101) ops.push(2);
    if (k == 0b0110) ops.push(3);
    if (k == 0b0111) ops.push(3), ops.push(1);
    if (k == 0b1000) ops.push(0), ops.push(1);
    if (k == 0b1001) ops.push(1), ops.push(0), ops.push(1);
    if (k == 0b1010) ops.push(0), ops.push(2);
    if (k == 0b1011) ops.push(3), ops.push(0), ops.push(1);
    if (k == 0b1100) ops.push(0), ops.push(0);
    if (k == 0b1101) ops.push(1), ops.push(0), ops.push(0);
    if (k == 0b1110) ops.push(0), ops.push(1), ops.push(0);
    if (k == 0b1111) ops.push(3), ops.push(0), ops.push(0);
    // long long sum = 3;
    // while (ops.size()) sum = sum*(ops.top() == 0? 2 : 1) + ops.top(), ops.pop();
    // return sum;
    vector<int> a = {1, 0};
    while (ops.size())
    {
        int op = ops.top(); ops.pop();
        if (op == 0) a.push_back(a.size());
        if (op == 1)
        {
            vector<int> temp = a;
            a.clear(), a.push_back(temp.size());
            for (auto i : temp) a.push_back(i);
        }
        if (op == 2)
        {
            vector<int> temp = a;
            a.clear(), a.push_back(temp[0]), a.push_back(temp.size());
            for (auto i : temp) if (i != temp[0]) a.push_back(i);
        }
        if (op == 3)
        {
            for (auto &i : a) if (i >= 2) i++;
            a.push_back(2);
        }
    }
    return a;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 260 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 260 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 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 340 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 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct