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;
const int M = (int) 1e6 + 1;
const int md = (int) 1e9 + 9;
int n;
int mx;
int a[M];
int b[M];
bitset<M> vis;
bool Go (int id, int cur, int step) {
if (id < 0 || id >= n) {
return true;
}
if (cur == 0) cur = n;
if (cur == n + 1) cur = 1;
b[id] = cur;
if (a[id] <= n && a[id] != cur) {
return false;
}
return Go(id + step, cur + step, step);
};
int valid(int n_, int inputSeq[]) {
n = n_;
for (int i = 0; i < n; ++i) {
a[i] = inputSeq[i];
}
for (int i = 0; i < n; ++i) {
if (vis[a[i]] == true) {
return false;
}
mx = max(mx, a[i]);
vis[a[i]] = true;
}
bool ret = true;
for (int i = 0; i < n; ++i) {
if (inputSeq[i] <= n) {
ret &= Go(i, a[i], 1);
vis[inputSeq[i]] = 0;
ret &= Go(i, a[i], -1);
break;
}
}
return ret;
}
int replacement(int n_, int gondolaSeq[], int replacementSeq[]) {
n = n_;
for (int i = 0; i < n; ++i) {
a[i] = gondolaSeq[i];
}
bool f = false;
for (int i = 0; i < n; ++i) {
mx = max(mx, a[i]);
vis[a[i]] = true;
if (a[i] <= n) {
f = true;
}
}
int ans = 0;
if (!f) {
for (int i = 1; i <= mx; ++i) {
if (!vis[i]) {
replacementSeq[ans++] = i;
}
}
return ans;
}
for (int i = 0; i < n; ++i) {
if (a[i] <= n) {
Go(i, a[i], 1);
Go(i, a[i], -1);
break;
}
}
vector<pair<int, int>> ord;
for (int i = 0; i < n; ++i) {
if (a[i] > n) {
ord.push_back(make_pair(a[i], b[i]));
}
}
sort(ord.begin(), ord.end());
for (int p = 0, g = n + 1; p < (int) ord.size(); ++p) {
replacementSeq[ans++] = ord[p].second;
while (g < ord[p].first) {
if (vis[g]) assert(false);
replacementSeq[ans++] = g++;
}
g++;
}
return ans;
}
//----------------------
int countReplacement(int n_, int inputSeq[]) {
n = n_;
if (!valid(n, inputSeq)) {
return 0;
}
bool f = false;
for (int i = 0; i < n; ++i) {
if (inputSeq[i] <= n) {
f = true;
}
}
int ans = 1;
if (!f) {
ans = n;
}
int cnt = 0;
for (int i = mx; i > n; --i) {
if (vis[i]) {
cnt++;
continue;
}
ans = (long long) ans * cnt % md;
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |