Submission #587119

#TimeUsernameProblemLanguageResultExecution timeMemory
587119JomnoiGondola (IOI14_gondola)C++17
100 / 100
44 ms5508 KiB
#include <bits/stdc++.h>
#include "gondola.h"
using namespace std;

const int MOD = 1e9 + 9;

int valid(int n, int inputSeq[]) {
    set <int> s;
    int pos = -1;
    for(int i = 0; i < n; i++) {
        s.insert(inputSeq[i]);
        if(inputSeq[i] <= n) {
            pos = i;
        }
    }
    
    if(s.size() < n) {
        return 0;
    }
    if(pos == -1) {
        return 1;
    }

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

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

int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
    map <int, int> location;
    int start = 1, max_val = 0;
    for(int i = 0; i < n; i++) {
        if(gondolaSeq[i] <= n) {
            start = (gondolaSeq[i] - i - 1 + 2 * n) % n + 1;
        }

        max_val = max(max_val, gondolaSeq[i]);
        location[gondolaSeq[i]] = i;
    }

    int j = n + 1, len = 0;
    for(int i = n + 1; i <= max_val; i++) {
        if(!location.count(i)) {
            continue;
        }

        replacementSeq[len++] = (location[i] + start - 1) % n + 1;
        while(j < i) {
            if(!location.count(j)) {
                replacementSeq[len++] = j;
            }
            j++;
        }
    }
    return len;
}

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

long long expo(int x, int y) {
    if(y == 0) {
        return 1;
    }

    long long res = expo(x, y / 2);
    res = res * res % MOD;
    if(y % 2 == 1) {
        res = res * x % MOD;
    }
    return res;
}

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

    vector <int> newGondola;
    for(int i = 0; i < n; i++) {
        if(inputSeq[i] > n) {
            newGondola.push_back(inputSeq[i]);
        }
    }


    sort(newGondola.begin(), newGondola.end());

    long long ans = 1;
    int prev = n;
    for(int i = 0; i < newGondola.size(); i++) {
        ans = ans * expo(newGondola.size() - i, newGondola[i] - prev - 1) % MOD;
        prev = newGondola[i];
    }

    if(newGondola.size() == n) {
        ans = ans * n % MOD;
    }
    return ans;
}

Compilation message (stderr)

gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:17:17: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   17 |     if(s.size() < n) {
      |        ~~~~~~~~~^~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:96:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |     for(int i = 0; i < newGondola.size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~~~~
gondola.cpp:101:26: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  101 |     if(newGondola.size() == n) {
      |        ~~~~~~~~~~~~~~~~~~^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...