Submission #701927

# Submission time Handle Problem Language Result Execution time Memory
701927 2023-02-22T11:00:36 Z Cyanmond Gondola (IOI14_gondola) C++17
100 / 100
43 ms 6028 KB
#include "gondola.h"
#include <bits/stdc++.h>

int valid(int n, int inputSeq[]) {
    for (int i = 0; i < n; ++i) {
        --inputSeq[i];
    }
    {
        // double
        std::set<int> s;
        for (int i = 0; i < n; ++i) {
            s.insert(inputSeq[i]);
        }
        if (s.size() != n) {
            return false;
        }
    }
    for (int f = 0; f < n; ++f) {
        if (inputSeq[f] >= n) {
            continue;
        }
        int x = inputSeq[f];
        bool answer = true;
        for (int i = f + 1; i < n; ++i) {
            const int y = (x + 1) % n;
            if (inputSeq[i] < n and y != inputSeq[i]) {
                answer = false;
            }
            x = y;
        }
        return answer;
    }
    return true;
}

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

int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
    assert(valid(n, gondolaSeq) == 1);
    int maxGondola = 0;
    for (int i = 0; i < n; ++i) {
        maxGondola = std::max(maxGondola, gondolaSeq[i]);
    }
    std::vector<int> gondolaPos(maxGondola + 1, -1);
    for (int i = 0; i < n; ++i) {
        gondolaPos[gondolaSeq[i]] = i;
    }
    std::vector<std::pair<int, int>> eventList;
    for (int i = 0; i < n; ++i) {
        eventList.push_back({gondolaSeq[i], i});
    }
    std::sort(eventList.begin(), eventList.end());
    std::vector<int> answer;
    int spareGondola = n;
    std::vector<int> nowGondolaSeq(n, -1);
    for (int i = 0; i < n; ++i) {
        if (gondolaSeq[i] < n) {
            nowGondolaSeq[i] = gondolaSeq[i];
            int j = (i + 1) % n, lastVal  = nowGondolaSeq[i];
            while (j != i) {
                lastVal = (lastVal + 1) % n;
                nowGondolaSeq[j] = lastVal;
                j = (j + 1) % n;
            }
            break;
        }
    }
    if (nowGondolaSeq[0] == -1) {
        std::iota(nowGondolaSeq.begin(), nowGondolaSeq.end(), 0);
    }
    for (int i = 0; i < n; ++i) {
        const auto [v, pos] = eventList[i];
        if (v < n) {
            continue;
        }
        int &g = nowGondolaSeq[pos];
        while (spareGondola <= v) {
            answer.push_back(g);
            g = spareGondola++;
        }
    }

    // cvr
    for (int i = 0; i < (int)answer.size(); ++i) {
        replacementSeq[i] = answer[i] + 1;
    }
    return (int)answer.size();
}

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

using i64 = long long;
constexpr i64 mod = 1000000009;
i64 add(i64 a, i64 b) {
    return (a + b) % mod;
}
i64 sub(i64 a, i64 b) {
    return (a + mod - b) % mod;
}
i64 mul(i64 a, i64 b) {
    return (a * b) % mod;
}
void eadd(i64 &a, i64 b) {
    a = add(a, b);
}
void esub(i64 &a, i64 b) {
    a = sub(a, b);
}
void emul(i64 &a, i64 b) {
    a = mul(a, b);
}

i64 moduloPow(i64 a, i64 n) {
    i64 ret = 1;
    while (n != 0) {
        if (n % 2 == 1) {
            emul(ret, a);
        }
        emul(a, a);
        n /= 2;
    }
    return ret;
}

int countReplacement(int n, int inputSeq[]) {
    if (valid(n, inputSeq) == 0) {
        return 0;
    }
    std::vector<int> initSeq(n, -1);
    for (int i = 0; i < n; ++i) {
        if (inputSeq[i] < n) {
            initSeq[i] = inputSeq[i];
            int j = (i + 1) % n, lastVal  = initSeq[i];
            while (j != i) {
                lastVal = (lastVal + 1) % n;
                initSeq[j] = lastVal;
                j = (j + 1) % n;
            }
            break;
        }
    }

    bool isFree = false;
    if (initSeq[0] == -1) {
        std::iota(initSeq.begin(), initSeq.end(), 0);
        isFree = true;
    }
    std::vector<std::pair<int, int>> eventList;
    for (int i = 0; i < n; ++i) {
        eventList.push_back({inputSeq[i], i});
    }
    std::sort(eventList.begin(), eventList.end());
    int lastVal = n - 1;
    i64 answer = 1;
    for (int i = 0; i < n; ++i) {
        const auto [v, pos] = eventList[i];
        if (v < n) {
            continue;
        }
        const int a = n - i, b = v - lastVal- 1;
        const auto k = moduloPow(a, b);
        emul(answer, k);
        lastVal = v;
    }
    if (isFree) {
        emul(answer, n);
    }

    assert(0 <= answer and answer < mod);
    return (int)answer;
}

Compilation message

gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:14:22: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   14 |         if (s.size() != n) {
      |             ~~~~~~~~~^~~~
# 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 1 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 0 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
6 Correct 10 ms 2216 KB Output is correct
7 Correct 22 ms 3600 KB Output is correct
8 Correct 17 ms 3856 KB Output is correct
9 Correct 6 ms 1364 KB Output is correct
10 Correct 22 ms 4520 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 10 ms 2192 KB Output is correct
7 Correct 22 ms 3640 KB Output is correct
8 Correct 20 ms 3940 KB Output is correct
9 Correct 6 ms 1364 KB Output is correct
10 Correct 21 ms 4456 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 11 ms 2076 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 28 ms 4544 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
# 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 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 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 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 22 ms 4260 KB Output is correct
12 Correct 26 ms 4876 KB Output is correct
13 Correct 20 ms 3284 KB Output is correct
14 Correct 24 ms 4180 KB Output is correct
15 Correct 21 ms 3276 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
# 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
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 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 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
9 Correct 32 ms 3944 KB Output is correct
10 Correct 25 ms 3324 KB Output is correct
11 Correct 8 ms 1364 KB Output is correct
12 Correct 12 ms 1620 KB Output is correct
13 Correct 2 ms 596 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 224 KB Output is correct
6 Correct 0 ms 252 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 32 ms 3996 KB Output is correct
10 Correct 25 ms 3364 KB Output is correct
11 Correct 9 ms 1364 KB Output is correct
12 Correct 14 ms 1748 KB Output is correct
13 Correct 2 ms 596 KB Output is correct
14 Correct 38 ms 5292 KB Output is correct
15 Correct 43 ms 6028 KB Output is correct
16 Correct 7 ms 1288 KB Output is correct
17 Correct 28 ms 4044 KB Output is correct
18 Correct 15 ms 2736 KB Output is correct