Submission #620475

# Submission time Handle Problem Language Result Execution time Memory
620475 2022-08-03T06:21:51 Z Genius3435 Gondola (IOI14_gondola) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;

constexpr int N = 100005;
constexpr int M = 250005;
constexpr int MOD = 1000000009;

int valid(int n, int inputSeq[]) {
    // Check that values \leq n are in order, and that there are no repeats

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


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

    return 1;
}



int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
    static int position[M];
    auto it = max_element(gondolaSeq, gondolaSeq+n);
    int m = *it, max_idx = it-gondolaSeq;
    for (int i = 1; i <= m; ++i) {
        position[i] = -1;
    }
    for (int i = 0; i < n; ++i) {
        position[gondolaSeq[i]] = i;
    }


    int start_index = -1;
    for (int i = 0; i < n; ++i) {
        if (gondolaSeq[i] <= n) {
            start_index = (gondolaSeq[i] - (i+1) + n) % n;
            break;
        }
    }
    if (start_index == -1) {
        start_index = 0;
    }


    int *currSeq = new int[n];
    for (int i = 0; i < n; ++i) {
        currSeq[i] = (start_index + i) % n + 1;
    }

    for (int i = n+1; i <= m; ++i) {
        if (position[i] == -1) {
            replacementSeq[i-n-1] = currSeq[max_idx];
            currSeq[max_idx] = i;
        } else {
            replacementSeq[i-n-1] = currSeq[position[i]];
            currSeq[position[i]] = i;
        }
    }

    delete[] currSeq;

    return m-n;
}



int pow(long long b, int e, int m) {
    long long p = 1;
    while (e) {
        if (e&1) p = p * b % m;
        e >>= 1, b = b * b % m;
    }
    return p;
}

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

    std::set<int> contained;
    for (int i = 0; i < n; ++i) {
        if (inputSeq[i] > n) {
            contained.insert(inputSeq[i]);
        }
    }

    int positions = contained.size();
    long long ans = positions<n ? 1 : n;
    int prv = n;
    for (const int i : contained) {
        // All values in the interval (prv, i) have positions spots they could go in
        ans = ans * pow(positions, i-prv-1, MOD) % MOD;
        prv = i;
        positions--;
    }
    return ans;
}

Compilation message

/usr/bin/ld: /tmp/ccsyc4DR.o: in function `main':
grader.cpp:(.text.startup+0xb6): undefined reference to `valid'
/usr/bin/ld: grader.cpp:(.text.startup+0x108): undefined reference to `countReplacement'
/usr/bin/ld: grader.cpp:(.text.startup+0x132): undefined reference to `replacement'
collect2: error: ld returned 1 exit status