# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
995420 | thinknoexit | 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 "gondola.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 9;
bool ch[250500];
int p[100100];
int valid(int n, int a[]) {
int mn = n + 1;
int idx = 0;
for (int i = 0;i < n;i++) {
if (ch[a[i]]) return 0;
ch[a[i]] = 1;
if (a[i] <= n) {
if (a[i] < mn) {
mn = a[i];
idx = i;
}
}
}
if (mn == n + 1) return 1;
idx = (idx - mn + 1 + n) % n;
for (int i = 0;i < n;i++) {
if (a[(i + idx) % n] <= n && a[(i + idx) % n] != i + 1) return 0;
}
return 1;
}
int replacement(int n, int a[], int replacementSeq[]) {
int mn = n + 1;
int idx = 0;
vector<pair<int, int>> v;
for (int i = 0;i < n;i++) {
if (a[i] <= n) {
if (a[i] < mn) {
mn = a[i];
idx = i;
}
}
else v.push_back({ a[i], i });
}
idx = (idx - mn + 1 + n) % n;
for (int i = 0;i < n;i++) {
p[(i + idx) % n] = i + 1;
}
int sz = 0, now = n;
sort(v.begin(), v.end());
for (auto& x : v) {
while (p[x.second] != x.first) {
replacementSeq[sz++] = p[x.second];
p[x.second] = ++now;
}
}
return sz;
}
int countReplacement(int n, int a[]) {
if (!valid(n, a)) return 0;
ll cnt = 0;
int mx = 0;
for (int i = 0;i < n;i++) {
if (a[i] > n) cnt++;
mx = max(mx, a[i]);
}
bool ch = (cnt == n);
ll ans = 1;
for (int i = n + 1;i <= mx;i++) {
if (!ch[i]) ans = (ans * cnt) % MOD;
else cnt--;
}
if (!ch) return ans;
return ans * n % MOD;
}