Submission #482996

# Submission time Handle Problem Language Result Execution time Memory
482996 2021-10-27T10:34:07 Z jalsol Gondola (IOI14_gondola) C++17
100 / 100
24 ms 2892 KB
#include "gondola.h"

#ifdef LOCAL
#include "local.h"
#else
#include <bits/stdc++.h>
#define debug(...)
#define DB(...)
#endif

using namespace std;

const bool __initialization = []() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << setprecision(12) << fixed;
    return true;
}();

using ll = long long;
using ld = long double;

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

#define For(i, l, r) for (int i = (l); i <= (r); ++i)
#define Ford(i, r, l) for (int i = (r); i >= (l); --i)
#define Rep(i, n) For (i, 0, (n) - 1)
#define Repd(i, n) Ford (i, (n) - 1, 0)
#define Fe(...) for (auto __VA_ARGS__)

template <class C> inline int isz(const C& c) { return static_cast<int>(c.size()); }
template <class T> inline bool chmin(T& a, const T& b) { return (a > b) ? a = b, true : false; }
template <class T> inline bool chmax(T& a, const T& b) { return (a < b) ? a = b, true : false; }

constexpr ld eps = 1e-9;
constexpr int inf = 1e9;
constexpr ll linf = 1e18;

// =============================================================================

template <typename T> T mod_inv_in_range(T a, T m) {
    // assert(0 <= a && a < m);
    T x = a, y = m;
    T vx = 1, vy = 0;
    while (x) {
        T k = y / x;
        y %= x;
        vy -= k * vx;
        std::swap(x, y);
        std::swap(vx, vy);
    }
    assert(y == 1);
    return vy < 0 ? m + vy : vy;
}

template <typename T> T mod_inv(T a, T m) {
    a %= m;
    a = a < 0 ? a + m : a;
    return mod_inv_in_range(a, m);
}

template <int MOD_> struct modnum {
    static constexpr int MOD = MOD_;
    static_assert(MOD_ > 0, "MOD must be positive");

private:
    using ll = long long;

    int v;

public:

    modnum() : v(0) {}
    modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
    explicit operator int() const { return v; }
    friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); }
    friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; }

    friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
    friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }

    modnum inv() const {
        modnum res;
        res.v = mod_inv_in_range(v, MOD);
        return res;
    }
    friend modnum inv(const modnum& m) { return m.inv(); }
    modnum neg() const {
        modnum res;
        res.v = v ? MOD-v : 0;
        return res;
    }
    friend modnum neg(const modnum& m) { return m.neg(); }

    modnum operator- () const {
        return neg();
    }
    modnum operator+ () const {
        return modnum(*this);
    }

    modnum& operator ++ () {
        v ++;
        if (v == MOD) v = 0;
        return *this;
    }
    modnum& operator -- () {
        if (v == 0) v = MOD;
        v --;
        return *this;
    }
    modnum& operator += (const modnum& o) {
        v -= MOD-o.v;
        v = (v < 0) ? v + MOD : v;
        return *this;
    }
    modnum& operator -= (const modnum& o) {
        v -= o.v;
        v = (v < 0) ? v + MOD : v;
        return *this;
    }
    modnum& operator *= (const modnum& o) {
        v = int(ll(v) * ll(o.v) % MOD);
        return *this;
    }
    modnum& operator /= (const modnum& o) {
        return *this *= o.inv();
    }

    friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; }
    friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; }
    friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
    friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
    friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
    friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
};

template <typename T> T pow(T a, long long b) {
    assert(b >= 0);
    T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}

constexpr int maxn = 3e5 + 5;
constexpr int mod = 1e9 + 9;

using mint = modnum<mod>;

int valid(int n, int a[]) {
    deque<int> og(n, inf);
    vector<int> add; add.reserve(n);

    Rep (i, n) {
        if (a[i] <= n) {
            og[i] = a[i];
        }
        else {
            add.push_back(a[i]);
        }
    }

    int pre = *min_element(all(og));

    if (pre <= n) {
        while (og[pre - 1] != pre) {
            og.push_back(og.front());
            og.pop_front();
        }

        Rep (i, isz(og)) {
            if (og[i] == inf) continue;

            if (og[i] != i + 1) {
                return false;
            }
        }
    }

    sort(all(add));

    Rep (i, isz(add) - 1) {
        if (add[i] == add[i + 1]) {
            return false;
        }
    }

    return true;
}
// I'm just so fucking dumb...


int replacement(int n, int a[], int ret[]) {
    set<pair<int, int>> st;

    Rep (i, n) {
        if (a[i] > n) {
            st.emplace(a[i], i);
        }
    }

    int p = min_element(a, a + n) - a;

    if (a[p] <= n) {
        For (off, 1, n - p - 1) {
            a[p + off] = a[p] + off;
            if (a[p + off] > n) a[p + off] -= n;
        }

        For (off, 1, p) {
            a[p - off] = a[p] - off;
            if (a[p - off] <= 0) a[p - off] += n;
        }
    }
    else {
        Rep (i, n) {
            a[i] = i + 1;
        }
    }

    if (!isz(st)) return 0;

    int maxi = st.rbegin()->first;
    int ans = 0;

    For (val, n + 1, maxi) {
        auto it = st.lower_bound(pair(val, -1));

        ret[ans++] = a[it->second];
        a[it->second] = val;

        if (val == it->first) {
            st.erase(it);
        }
    }

    return ans;
}


int countReplacement(int n, int a[]) {
    if (!valid(n, a)) return 0;

    mint ans = 1;
    sort(a, a + n);

    Rep (i, n) {
        if (a[i] <= n) continue;

        if (!i) {
            ans *= pow(mint(n), a[i] - n - 1);
        } else {
            ans *= pow(mint(n - i), a[i] - max(a[i - 1], n) - 1);
        }
    }

    if (a[0] > n) ans *= n;

    return int(ans);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 4 ms 580 KB Output is correct
7 Correct 8 ms 964 KB Output is correct
8 Correct 8 ms 884 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 8 ms 972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 4 ms 588 KB Output is correct
7 Correct 9 ms 1048 KB Output is correct
8 Correct 7 ms 844 KB Output is correct
9 Correct 4 ms 460 KB Output is correct
10 Correct 7 ms 972 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 5 ms 716 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 9 ms 1100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 7 ms 588 KB Output is correct
12 Correct 7 ms 588 KB Output is correct
13 Correct 18 ms 2508 KB Output is correct
14 Correct 7 ms 588 KB Output is correct
15 Correct 22 ms 2892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 320 KB Output is correct
9 Correct 19 ms 1612 KB Output is correct
10 Correct 14 ms 1356 KB Output is correct
11 Correct 5 ms 716 KB Output is correct
12 Correct 7 ms 744 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 18 ms 1612 KB Output is correct
10 Correct 13 ms 1356 KB Output is correct
11 Correct 5 ms 716 KB Output is correct
12 Correct 7 ms 716 KB Output is correct
13 Correct 3 ms 332 KB Output is correct
14 Correct 21 ms 2060 KB Output is correct
15 Correct 24 ms 2380 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 16 ms 1612 KB Output is correct
18 Correct 9 ms 1100 KB Output is correct