# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
620475 | Genius3435 | Gondola (IOI14_gondola) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}