Submission #1104872

#TimeUsernameProblemLanguageResultExecution timeMemory
1104872RiverFlowPermutation (APIO22_perm)C++17
91.33 / 100
2 ms504 KiB
#include <bits/stdc++.h>

#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

template<typename U, typename V> bool maxi(U &a, V b) {
    if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
    if (a > b) { a = b; return 1; } return 0;
}

const int N = (int)2e5 + 9;
const int mod = (int)1e9 + 7;

#define ll long long

const int LG = 60;

vector<int> construct_permutation(long long k)
{
    int h = 0;
    FOD(i, LG, 0) if (1 & k >> i) {
        h = i; break ;
    }
    int p = __builtin_popcountll(k) - 1;

    int n = h + p;

    int ux = 0;

    vec<int> d;

    FOR(i, 0, h - 1) if (1 & k >> i) d.push_back(i);

    if (sz(d) == 0) {
        vec<int> res;
        FOR(i, 0, h - 1) res.push_back(i);
        return res;
    }
    vec<int> r1;
    vec<int> r2;

    int tx = 0;
    int pp = 0;
    FOD(i, n, 1) {
        int cc = n - i - tx;
        if (pp < sz(d) and d[pp] == cc) {
            r1.push_back(i-1);
            ++tx;
            ++pp;
        } else {
            r2.push_back(i-1);
        }
    }
    reverse(all(r2));
    vec<int> res;
    res = r1;
    for(auto x : r2) res.push_back(x);

    return res;
}


/*     Let the river flows naturally     */

Compilation message (stderr)

perm.cpp: In function 'std::vector<int> construct_permutation(long long int)':
perm.cpp:45:9: warning: unused variable 'ux' [-Wunused-variable]
   45 |     int ux = 0;
      |         ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...