Submission #582249

# Submission time Handle Problem Language Result Execution time Memory
582249 2022-06-23T14:43:07 Z Drew_ Gondola (IOI14_gondola) C++17
100 / 100
67 ms 5964 KB
#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define f1 first
#define s2 second

using ii = pair<int, int>;

const int MAX = 250069;
const int mod = 1e9 + 9;

template<int MOD = mod>
struct Mint
{
    int val;

    Mint() : val(0) {}
    Mint(int64_t _val) : val((int)(_val % MOD)) { if (val < 0) val += MOD; }

    Mint& operator+= (const Mint &rhs) { val += rhs.val; if (val >= MOD) val -= MOD; return *this; }
    Mint& operator-= (const Mint &rhs) { val -= rhs.val; if (val < 0) val += MOD; return *this; }
    Mint& operator*= (const Mint &rhs) { val = (int)(1ll * val * rhs.val % MOD); return *this; }

    friend Mint fpow(Mint x, int64_t y)
    {
        Mint res = 1;
        for (; y > 0; y >>= 1, x *= x)
        {
            if (y & 1)
                res *= x;
        }
        return res;
    }
    friend Mint inverse(Mint x) { return fpow(x, MOD-2); }
    Mint& operator/= (const Mint &rhs) { return *this *= inverse(rhs); }

    friend Mint operator+ (Mint a, const Mint &b) { return a += b; }
    friend Mint operator- (Mint a, const Mint &b) { return a -= b; }
    friend Mint operator- (Mint a) { return 0 - a; }
    friend Mint operator* (Mint a, const Mint &b) { return a *= b; }
    friend Mint operator/ (Mint a, const Mint &b) { return a /= b; }
    friend ostream& operator<< (ostream &os, const Mint &a) { return os << a.val; }
    friend bool operator== (const Mint &a, const Mint &b) { return a.val == b.val; }
    friend bool operator!= (const Mint &a, const Mint &b) { return a.val != b.val; }
};

int valid(int n, int inputSeq[])
{
    set<int> vst;
    for (int i = 0; i < n; ++i)
    {
        if (vst.count(inputSeq[i]))
            return 0;
        vst.insert(inputSeq[i]);
    }

    int pos = -1;
    for (int i = 0; i < n; ++i) if (inputSeq[i] <= n)
    {
        int tmp = (inputSeq[i] - i + n) % n;
        if (pos == -1) pos = tmp;
        if (pos != tmp) return 0;
    }
    return 1;
}

//----------------------

int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
    int ar[MAX];
    {
        int pos = 0;
        for (int i = 1; i < n; ++i)
            if (gondolaSeq[i] < gondolaSeq[pos])
                pos = i;

        if (gondolaSeq[pos] <= n)
            pos = (pos - gondolaSeq[pos] + 1 + n) % n;
        else pos = 0;

        iota(ar+pos, ar+n, 1);
        iota(ar, ar+pos, n-pos+1);
    }

    vector<ii> v;
    for (int i = 0; i < n; ++i)
        v.pb({gondolaSeq[i], i});
    sort(v.begin(), v.end());

    int sz = 0, cur = n+1;
    for (auto &[x, pos] : v)
    {
        while (ar[pos] != x)
        {
            replacementSeq[sz++] = ar[pos];
            ar[pos] = cur++;
        }
    }

    return sz;
}

//----------------------

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

    vector<int> v;
    for (int i = 0; i < n; ++i) if (inputSeq[i] > n)
        v.pb(inputSeq[i]);
    sort(v.begin(), v.end());

    int cur = n+1, sz = (int)v.size();
    Mint<> res = 1;
    for (int i = 0; i < sz; ++i)
    {
        res *= fpow(Mint<>(sz - i), v[i] - cur);
        cur = v[i]+1;
    }

    return (res * (sz == n ? n : 1)).val;
}
# 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 0 ms 212 KB Output is correct
5 Correct 0 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 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 16 ms 2132 KB Output is correct
7 Correct 8 ms 596 KB Output is correct
8 Correct 26 ms 3844 KB Output is correct
9 Correct 11 ms 1364 KB Output is correct
10 Correct 35 ms 4476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 15 ms 2196 KB Output is correct
7 Correct 10 ms 636 KB Output is correct
8 Correct 23 ms 3936 KB Output is correct
9 Correct 8 ms 1380 KB Output is correct
10 Correct 34 ms 4408 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 16 ms 2064 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 40 ms 4596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1236 KB Output is correct
2 Correct 1 ms 1236 KB Output is correct
3 Correct 1 ms 1236 KB Output is correct
4 Correct 1 ms 1236 KB Output is correct
5 Correct 1 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1236 KB Output is correct
2 Correct 1 ms 1236 KB Output is correct
3 Correct 1 ms 1236 KB Output is correct
4 Correct 1 ms 1236 KB Output is correct
5 Correct 1 ms 1236 KB Output is correct
6 Correct 0 ms 1236 KB Output is correct
7 Correct 1 ms 1236 KB Output is correct
8 Correct 1 ms 1236 KB Output is correct
9 Correct 1 ms 1236 KB Output is correct
10 Correct 2 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1236 KB Output is correct
2 Correct 1 ms 1236 KB Output is correct
3 Correct 1 ms 1236 KB Output is correct
4 Correct 1 ms 1236 KB Output is correct
5 Correct 1 ms 1236 KB Output is correct
6 Correct 1 ms 1236 KB Output is correct
7 Correct 1 ms 1236 KB Output is correct
8 Correct 1 ms 1236 KB Output is correct
9 Correct 2 ms 1236 KB Output is correct
10 Correct 2 ms 1236 KB Output is correct
11 Correct 12 ms 2764 KB Output is correct
12 Correct 13 ms 2752 KB Output is correct
13 Correct 15 ms 2236 KB Output is correct
14 Correct 11 ms 2636 KB Output is correct
15 Correct 22 ms 3204 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 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 43 ms 3992 KB Output is correct
10 Correct 34 ms 3396 KB Output is correct
11 Correct 13 ms 1364 KB Output is correct
12 Correct 15 ms 1624 KB Output is correct
13 Correct 4 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 232 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 38 ms 4116 KB Output is correct
10 Correct 30 ms 3408 KB Output is correct
11 Correct 10 ms 1364 KB Output is correct
12 Correct 14 ms 1620 KB Output is correct
13 Correct 3 ms 596 KB Output is correct
14 Correct 48 ms 4576 KB Output is correct
15 Correct 67 ms 5964 KB Output is correct
16 Correct 9 ms 1348 KB Output is correct
17 Correct 38 ms 4044 KB Output is correct
18 Correct 19 ms 2388 KB Output is correct