Submission #367430

# Submission time Handle Problem Language Result Execution time Memory
367430 2021-02-17T09:47:28 Z KoD Gondola (IOI14_gondola) C++17
100 / 100
61 ms 2544 KB
#include <iostream>
#include <numeric>
#include <vector>
#include <algorithm>
#include <utility>

#ifndef LOCAL
#include "gondola.h"
#endif

template <class T>
using Vec = std::vector<T>;

void fix(int n, int seq[]) {
    for (int i = 0; i < n; ++i) {
        seq[i] -= 1;
    }
}

int valid(int n, int inputSeq[]) {
    fix(n, inputSeq);
    Vec<int> rep;
    for (int i = 0; i < n; ++i) {
        if (inputSeq[i] >= n) {
            rep.push_back(inputSeq[i]);
        }
    }
    if (!rep.empty()) {
        std::sort(rep.begin(), rep.end());
        if (std::unique(rep.begin(), rep.end()) != rep.end()) {
            return 0;
        }
    }
    for (int i = 0; i < n; ++i) {
        if (inputSeq[i] < n) {
            for (int j = 0; j < n; ++j) {
                const auto x = inputSeq[(i + j) % n];
                if (x < n && x != (inputSeq[i] + j) % n) {
                    return 0;
                }
            }
            return 1;
        }
    }
    return 1;
}

int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
    fix(n, gondolaSeq);
    Vec<int> index(n);
    std::iota(index.begin(), index.end(), 0);
    for (int i = 0; i < n; ++i) {
        if (gondolaSeq[i] < n) {
            for (int j = 0; j < n; ++j) {
                index[(i + j) % n] = (gondolaSeq[i] + j) % n;
            }
            break;
        }
    }
    Vec<std::pair<int, int>> change;
    for (int i = 0; i < n; ++i) {
        if (gondolaSeq[i] >= n) {
            change.emplace_back(gondolaSeq[i], index[i]);
        }
    }
    std::sort(change.begin(), change.end());
    int rep = n;
    int size = 0;
    for (const auto [last, first]: change) {
        replacementSeq[size++] = first + 1;
        while (rep < last) {
            replacementSeq[size++] = rep++ + 1;
        }
        rep = last + 1;
    }
    return size;
}

using ll = long long;
constexpr ll MOD = 1000000009;

ll pow(ll x, ll e) {
    ll ret = 1;
    while (e > 0) {
        if (e & 1) {
            ret = (ret * x) % MOD;
        }
        x = (x * x) % MOD;
        e >>= 1;
    }
    return ret;
}

int countReplacement(int n, int inputSeq[]) {
    if (!valid(n, inputSeq)) {
        return 0;
    }
    Vec<int> rep;
    for (int i = 0; i < n; ++i) {
        if (inputSeq[i] >= n) {
            rep.push_back(inputSeq[i]);
        }
    }
    std::sort(rep.begin(), rep.end(), std::greater<>());
    rep.push_back(n - 1);
    ll ret = 1;
    for (int i = 1; i < (int) rep.size(); ++i) {
        ret = (ret * pow(i, rep[i - 1] - rep[i] - 1)) % MOD;
    }
    rep.pop_back();
    if ((int) rep.size() == n) {
        ret = (ret * n) % MOD;
    }
    return ret;
}

#ifdef LOCAL
int main() {
    int n;
    std::cin >> n;
    int seq[100] = {};
    for (int i = 0; i < n; ++i) {
        std::cin >> seq[i];
    }
    std::cout << countReplacement(n, seq) << '\n';
    return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 6 ms 512 KB Output is correct
7 Correct 11 ms 620 KB Output is correct
8 Correct 10 ms 620 KB Output is correct
9 Correct 3 ms 364 KB Output is correct
10 Correct 11 ms 620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 6 ms 492 KB Output is correct
7 Correct 11 ms 620 KB Output is correct
8 Correct 10 ms 640 KB Output is correct
9 Correct 4 ms 364 KB Output is correct
10 Correct 12 ms 620 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 9 ms 876 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 13 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 380 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 11 ms 876 KB Output is correct
12 Correct 12 ms 1004 KB Output is correct
13 Correct 17 ms 1268 KB Output is correct
14 Correct 12 ms 1004 KB Output is correct
15 Correct 25 ms 2276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 21 ms 1540 KB Output is correct
10 Correct 17 ms 1400 KB Output is correct
11 Correct 7 ms 788 KB Output is correct
12 Correct 10 ms 876 KB Output is correct
13 Correct 3 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 21 ms 1540 KB Output is correct
10 Correct 61 ms 1516 KB Output is correct
11 Correct 7 ms 788 KB Output is correct
12 Correct 10 ms 876 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 29 ms 2300 KB Output is correct
15 Correct 30 ms 2544 KB Output is correct
16 Correct 6 ms 804 KB Output is correct
17 Correct 21 ms 1900 KB Output is correct
18 Correct 13 ms 1260 KB Output is correct