#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
constexpr int X = 1e6 + 1, N = 250001, MOD = 1e9 + 9;
int inv[X], tmp[N];
int valid(int n, int inputSeq[]) {
memset(inv, -1, sizeof(inv));
for (int i = 0; i < n; ++i) {
if (inputSeq[i] <= n) {
inv[inputSeq[i]] = i;
}
}
memcpy(tmp, inputSeq, sizeof(tmp[0]) * n);
sort(tmp, tmp + n);
if (unique(tmp, tmp + n) != tmp + n) {
return 0;
}
int offset = -1;
for (int x = 1; x <= n; ++x) {
if (inv[x] != -1) {
int now = x - inv[x];
if (now < 0) now += n;
if (offset != -1 && offset != now) {
return 0;
}
offset = now;
}
}
return 1;
}
int replacement(int n, int inputSeq[], int replacementSeq[]) {
memset(inv, -1, sizeof(inv));
const int A = *max_element(inputSeq, inputSeq + n);
if (A == n) {
return 0;
}
for (int i = 0; i < n; ++i) {
if (inv[inputSeq[i]] != -1) {
return 0;
}
inv[inputSeq[i]] = i;
}
int offset = -1;
for (int x = 1; x <= n; ++x) {
if (inv[x] != -1) {
int now = x - inv[x];
if (now < 0) now += n;
if (offset != -1 && offset != now) {
return 0;
}
offset = now;
}
}
vector<int> initial(n);
if (offset == -1) {
iota(initial.begin(), initial.end(), 1);
} else {
for (int x = 1; x <= n; ++x) {
int i = x - offset;
if (i < 0) i += n;
initial[i] = x;
}
}
set<int> st;
for (int i = 0; i < n; ++i) {
if (inputSeq[i] != initial[i]) {
st.insert(i);
}
}
int l = 0;
for (int x = n + 1; x <= A; ++x) {
int i;
if (inv[x] != -1) {
i = inv[x];
} else {
i = *st.begin();
}
replacementSeq[l++] = initial[i];
initial[i] = x;
if (initial[i] == inputSeq[i]) {
st.erase(i);
}
}
return l;
}
int power(int a, int b) {
int res = 1;
for (; b > 0; b >>= 1, a = 1LL * a * a % MOD) {
if (b & 1) {
res = 1LL * a * res % MOD;
}
}
return res;
}
int countReplacement(int n, int inputSeq[]) {
if (!valid(n, inputSeq)) {
return 0;
}
memset(inv, -1, sizeof(inv));
const int A = *max_element(inputSeq, inputSeq + n);
for (int i = 0; i < n; ++i) {
if (inputSeq[i] <= n) {
inv[inputSeq[i]] = i;
}
}
int offset = -1;
for (int x = 1; x <= n; ++x) {
if (inv[x] != -1) {
int now = x - inv[x];
if (now < 0) now += n;
if (offset != -1 && offset != now) {
return 0;
}
offset = now;
}
}
vector<int> initial(n);
constexpr int MOD = 1e9 + 9;
int ans = 1;
if (offset == -1) {
iota(initial.begin(), initial.end(), 1);
ans = n;
} else {
for (int x = 1; x <= n; ++x) {
int i = x - offset;
if (i < 0) i += n;
initial[i] = x;
}
}
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (inputSeq[i] != initial[i]) {
cnt += 1;
}
}
sort(inputSeq, inputSeq + n);
int last = n;
for (int ii = 0; ii < n; ++ii) {
int x = inputSeq[ii];
if (x <= n) {
continue;
}
ans = 1LL * ans * power(cnt, x - last - 1) % MOD;
last = x;
cnt -= 1;
}
return ans;
}
Compilation message
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:109:15: warning: unused variable 'A' [-Wunused-variable]
109 | const int A = *max_element(inputSeq, inputSeq + n);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4152 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4148 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
8 ms |
4672 KB |
Output is correct |
7 |
Correct |
16 ms |
5372 KB |
Output is correct |
8 |
Correct |
12 ms |
5076 KB |
Output is correct |
9 |
Correct |
6 ms |
4412 KB |
Output is correct |
10 |
Correct |
17 ms |
5256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
9 ms |
4732 KB |
Output is correct |
7 |
Correct |
19 ms |
5364 KB |
Output is correct |
8 |
Correct |
12 ms |
5076 KB |
Output is correct |
9 |
Correct |
7 ms |
4528 KB |
Output is correct |
10 |
Correct |
16 ms |
5264 KB |
Output is correct |
11 |
Correct |
2 ms |
4148 KB |
Output is correct |
12 |
Correct |
2 ms |
4148 KB |
Output is correct |
13 |
Correct |
8 ms |
4692 KB |
Output is correct |
14 |
Correct |
2 ms |
4180 KB |
Output is correct |
15 |
Correct |
17 ms |
5332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4148 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4152 KB |
Output is correct |
6 |
Correct |
2 ms |
4152 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
3 ms |
4156 KB |
Output is correct |
10 |
Correct |
3 ms |
4280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4156 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
3 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
2 ms |
4180 KB |
Output is correct |
10 |
Correct |
2 ms |
4180 KB |
Output is correct |
11 |
Correct |
10 ms |
4948 KB |
Output is correct |
12 |
Correct |
14 ms |
4988 KB |
Output is correct |
13 |
Correct |
24 ms |
6592 KB |
Output is correct |
14 |
Correct |
10 ms |
4948 KB |
Output is correct |
15 |
Correct |
23 ms |
6888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4148 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
3 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Correct |
3 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4156 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
21 ms |
5452 KB |
Output is correct |
10 |
Correct |
18 ms |
5216 KB |
Output is correct |
11 |
Correct |
8 ms |
4648 KB |
Output is correct |
12 |
Correct |
10 ms |
4648 KB |
Output is correct |
13 |
Correct |
4 ms |
4288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4148 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
3 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4192 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
23 ms |
5548 KB |
Output is correct |
10 |
Correct |
19 ms |
5404 KB |
Output is correct |
11 |
Correct |
7 ms |
4564 KB |
Output is correct |
12 |
Correct |
9 ms |
4692 KB |
Output is correct |
13 |
Correct |
4 ms |
4308 KB |
Output is correct |
14 |
Correct |
30 ms |
6092 KB |
Output is correct |
15 |
Correct |
31 ms |
6144 KB |
Output is correct |
16 |
Correct |
7 ms |
4524 KB |
Output is correct |
17 |
Correct |
23 ms |
5588 KB |
Output is correct |
18 |
Correct |
12 ms |
4928 KB |
Output is correct |