Submission #985569

# Submission time Handle Problem Language Result Execution time Memory
985569 2024-05-18T08:32:01 Z ParsaGolestani Permutation (APIO22_perm) C++17
93.6667 / 100
2 ms 348 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 2'000'0000;

vector<int> makeOk(deque<int> dq) {
    vector<int> vec1, vec2;
    for (int i = 0; i < dq.size(); i++)
        vec1.push_back(dq[i]);
    sort(vec1.begin(), vec1.end());
    for (int i = 0; i < dq.size(); i++)
        vec2.push_back(lower_bound(vec1.begin(), vec1.end(), dq[i]) - vec1.begin());
    return vec2;
}

string to3(ll k) {
    string res = "";
    while (k) {
        res += ('0' + k % 3);
        k /= 3;
    }
    reverse(res.begin(), res.end());
    return res;
}

vector<int> construct_permutation(ll k) {
    deque<int> dq;
    int bit = 63 - __builtin_clzll(k);
    /*if ((k & (1ll << (bit - 1)))) {
        dq.push_back(1);
        dq.push_back(0);
    }
    else
        dq.push_back(0);
    int pnt = 0;
    for (int j = bit - 2; j >= 0; j--) {
        if (j >= 1 && (k & (1ll << j)) && (k & (1ll << (j - 1)))) {
            dq.push_front(pnt - 1);
            dq.push_back(pnt - 2);
            dq.push_front(pnt - 3);
            pnt -= 3;
            j--;
        }
        else {
            dq.push_front(--pnt);
            if ((k & (1ll << j)))
                dq.push_back(--pnt);
        }

    }*/
    int pnt = 0, idx;
    string s = to3(k);
    if (s[0] == '1') {
        if (s[1] == '0') {
            dq.push_front(1);
            dq.push_front(2);
        }
        else if (s[1] == '1') {
            dq.push_front(1);
            dq.push_back(2);
        }
        else {
            dq.push_front(1);
            dq.push_front(3);
            dq.push_front(2);
        }
        idx = 2;
    }
    else {
        dq.push_front(1);
        idx = 1;
    }
    for (int i = idx; i < s.size(); i++) {
        if (s[i] == '0') {
            dq.push_front(pnt - 2);
            dq.push_front(pnt - 1);
            pnt -= 2;
        }
        else if (s[i] == '1') {
            dq.push_front(pnt - 2);
            dq.push_front(pnt - 1);
            dq.push_back(pnt - 3);
            pnt -= 3;
        }
        else {
            dq.push_front(pnt - 3);
            dq.push_front(pnt - 1);
            dq.push_back(pnt - 2);
            pnt -= 3;
        }
    }
    return makeOk(dq);
}

Compilation message

perm.cpp: In function 'std::vector<int> makeOk(std::deque<int>)':
perm.cpp:10:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 |     for (int i = 0; i < dq.size(); i++)
      |                     ~~^~~~~~~~~~~
perm.cpp:13:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |     for (int i = 0; i < dq.size(); i++)
      |                     ~~^~~~~~~~~~~
perm.cpp: In function 'std::vector<int> construct_permutation(ll)':
perm.cpp:75:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |     for (int i = idx; i < s.size(); i++) {
      |                       ~~^~~~~~~~~~
perm.cpp:30:9: warning: unused variable 'bit' [-Wunused-variable]
   30 |     int bit = 63 - __builtin_clzll(k);
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Partially correct 1 ms 348 KB Partially correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Partially correct 2 ms 348 KB Partially correct
9 Partially correct 2 ms 348 KB Partially correct
10 Partially correct 2 ms 344 KB Partially correct
11 Partially correct 2 ms 348 KB Partially correct
12 Partially correct 2 ms 348 KB Partially correct
13 Partially correct 2 ms 348 KB Partially correct