Submission #1065217

# Submission time Handle Problem Language Result Execution time Memory
1065217 2024-08-19T03:37:22 Z kkzyr Gondola (IOI14_gondola) C++17
100 / 100
49 ms 6148 KB
#include <iostream>
#include <algorithm>
#include <map>
#include <gondola.h>
using namespace std;

const int mod = 1e9 + 9;

int input[100000];

map<int, bool> has;

int original[100001];
int index[250001];

int use_for_replacement[100000];

long long binary_exponentiation(int a, int b){
    if (b == 0){
        return 1;
    }
    long long cache = binary_exponentiation(a, b/2);
    long long ans = (cache * cache) % mod;
    if (b % 2 == 1){
        ans = (ans * a) % mod;
    }
    return ans;
}

int valid(int n, int inputSeq[]){
    for (int i = 0;i < n;i++){
        if (has[inputSeq[i]]){
            return 0;
        }
        has[inputSeq[i]] = true;
    }
    int first_original = -1;
    for (int i = 0;i < n;i++){
        if (inputSeq[i] <= n){
            first_original = i;
            break;
        }
    }
    if (first_original == -1){
        return 1;
    }
    for (int i = 0;i < n;i++){
        if (inputSeq[i] <= n){
            int diff1 = i - first_original;
            int diff2 = inputSeq[i] - inputSeq[first_original];
            if (diff2 < 0){
                diff2 += n;
            }
            if (diff1 != diff2){
                return 0;
            }
        }
    }
    return 1;
}

int replacement(int n, int gondolaSeq[], int replacementSeq[]){
    int has_index = -1;
    int max_num = -1;
    for (int i = 0;i < n;i++){
        if (gondolaSeq[i] <= n){
            has_index = i;
        }
        max_num = max(max_num, gondolaSeq[i]);
        index[gondolaSeq[i]] = i;
    }
    if (has_index == -1){
        for (int i = 0;i < n;i++){
            original[i] = i + 1;
        }
    }
    else{
        int first = gondolaSeq[has_index] - has_index;
        if (first < 0){
            first += n;
        }
        for (int i = 0;i < n;i++){
            original[i] = (first + i) % n;
            if (original[i] == 0){
                original[i] = n;
            }
        }
    }
    int length = 0;
    for (int i = n + 1;i <= max_num;i++){
        if (index[i] != 0 or gondolaSeq[0] == i){
            replacementSeq[length] = original[index[i]];
            length++;
            original[index[i]] = i;
        }
        else{
            replacementSeq[length] = original[index[max_num]];
            length++;
            original[index[max_num]] = i;
        }
    }
    return length;
}

int countReplacement(int n, int inputSeq[]){
    if (valid(n, inputSeq) == 0){
        return 0;
    }
    sort(inputSeq, inputSeq + n);
    if (inputSeq[n - 1] == n){
        return 1;
    }
    int first_greater_n = -1;
    for (int i = 0;i < n;i++){
        if (inputSeq[i] > n){
            first_greater_n = i;
            break;
        }
    }
    long long ans = binary_exponentiation(n - first_greater_n, (inputSeq[first_greater_n] - 1) - n);
    ans %= mod;
    for (int i = first_greater_n;i < (n - 1);i++){
        ans *= binary_exponentiation(n - (i + 1), (inputSeq[i + 1] - 1) - inputSeq[i]);
        ans %= mod;
    }
    if (inputSeq[0] > n){
        ans *= n;
        ans %= mod;
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 9 ms 2396 KB Output is correct
7 Correct 6 ms 1116 KB Output is correct
8 Correct 17 ms 4344 KB Output is correct
9 Correct 6 ms 1880 KB Output is correct
10 Correct 23 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 10 ms 2580 KB Output is correct
7 Correct 6 ms 1112 KB Output is correct
8 Correct 30 ms 4436 KB Output is correct
9 Correct 6 ms 1628 KB Output is correct
10 Correct 25 ms 4984 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 13 ms 2320 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 31 ms 5228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 448 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 420 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 356 KB Output is correct
8 Correct 1 ms 356 KB Output is correct
9 Correct 0 ms 464 KB Output is correct
10 Correct 1 ms 352 KB Output is correct
11 Correct 8 ms 1712 KB Output is correct
12 Correct 7 ms 1896 KB Output is correct
13 Correct 9 ms 1868 KB Output is correct
14 Correct 6 ms 1640 KB Output is correct
15 Correct 12 ms 2920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 360 KB Output is correct
2 Correct 0 ms 360 KB Output is correct
3 Correct 0 ms 360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 448 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 31 ms 4440 KB Output is correct
10 Correct 27 ms 3824 KB Output is correct
11 Correct 9 ms 1676 KB Output is correct
12 Correct 11 ms 1956 KB Output is correct
13 Correct 2 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 448 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 31 ms 4640 KB Output is correct
10 Correct 25 ms 3932 KB Output is correct
11 Correct 10 ms 1628 KB Output is correct
12 Correct 13 ms 1880 KB Output is correct
13 Correct 3 ms 604 KB Output is correct
14 Correct 49 ms 5372 KB Output is correct
15 Correct 48 ms 6148 KB Output is correct
16 Correct 7 ms 1372 KB Output is correct
17 Correct 29 ms 4180 KB Output is correct
18 Correct 15 ms 2396 KB Output is correct