#include <bits/stdc++.h>
#include "gondola.h"
using namespace std;
const int MOD = 1e9 + 9;
int valid(int n, int inputSeq[]) {
int a[n];
for (int i = 0; i < n; i++) {
a[i] = inputSeq[i];
}
set<int> s;
for (int i = 0; i < n; i++) {
s.insert(a[i]);
}
if ((int) s.size() != n) {
return 0;
}
for (int i = 0; i < n; i++) {
--a[i];
}
int pos = -1;
for (int i = 0; i < n; i++) {
if (a[i] < n) {
pos = i;
}
}
if (pos == -1) {
return 1;
}
int value = a[pos];
for (int i = pos; i < pos + n; i++) {
int idx = i % n;
if (a[idx] < n && a[idx] != value) {
return 0;
}
value = (value + 1) % n;
}
return 1;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
int a[n];
for (int i = 0; i < n; i++) {
a[i] = gondolaSeq[i];
--a[i];
}
int pos = -1;
for (int i = 0; i < n; i++) {
if (a[i] < n) {
pos = i;
}
}
vector<pair<int, int>> v;
if (pos == -1) {
for (int i = 0; i < n; i++) {
v.emplace_back(a[i], i);
}
} else {
int cur = a[pos];
for (int i = pos; i < pos + n; i++) {
int idx = i % n;
if (a[idx] >= n) {
v.emplace_back(a[idx], cur);
}
cur = (cur + 1) % n;
}
}
sort(begin(v), end(v));
int len = 0;
int cur = n;
int position = 0;
for (auto& [x, y] : v) {
int prev = y;
while (cur <= x) {
replacementSeq[position] = prev + 1;
++position;
++len;
prev = cur;
++cur;
}
}
return len;
}
//----------------------
int countReplacement(int n, int inputSeq[]) {
if (valid(n, inputSeq) == 0) {
return 0;
}
int count = n;
vector<int> v;
for (int i = 0; i < n; i++) {
if (inputSeq[i] <= n) {
--count;
} else {
v.push_back(inputSeq[i]);
}
}
auto modpow = [&](int a, int b) {
int result = 1;
while (b) {
if (b & 1) {
result = (1ll * result * a) % MOD;
}
a = (1ll * a * a) % MOD;
b >>= 1;
}
return result;
};
sort(v.begin(), v.end());
int cur = n + 1;
int res;
if (count == n) {
res = n; // all the shifts
} else {
res = 1;
}
for (const int& p : v) {
int d = p - cur;
res = (1ll * res * modpow(count, d)) % MOD;
res %= MOD;
res += MOD;
res %= MOD;
cur = p + 1;
--count;
}
return res;
}
// int main() {
// // 1 2
// // 4 3
// int a[] = {2, 3, 4, 9, 6, 8, 1};
// // 0 1 6 5
// // 0 1 2 1
// int b[20];
// int sz = replacement(7, a, b);
// for (int i = 0; i < sz; i++) {
// cout << b[i] << ' ';
// }
// cout << '\n';
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
13 ms |
2508 KB |
Output is correct |
7 |
Correct |
42 ms |
4456 KB |
Output is correct |
8 |
Correct |
33 ms |
4524 KB |
Output is correct |
9 |
Correct |
8 ms |
1612 KB |
Output is correct |
10 |
Correct |
29 ms |
5240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
13 ms |
2568 KB |
Output is correct |
7 |
Correct |
40 ms |
4384 KB |
Output is correct |
8 |
Correct |
26 ms |
4556 KB |
Output is correct |
9 |
Correct |
8 ms |
1596 KB |
Output is correct |
10 |
Correct |
29 ms |
5324 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
17 ms |
2348 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
40 ms |
5504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
13 ms |
1356 KB |
Output is correct |
12 |
Correct |
13 ms |
1484 KB |
Output is correct |
13 |
Correct |
17 ms |
1564 KB |
Output is correct |
14 |
Correct |
12 ms |
1356 KB |
Output is correct |
15 |
Correct |
31 ms |
2368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
43 ms |
4784 KB |
Output is correct |
10 |
Correct |
34 ms |
3908 KB |
Output is correct |
11 |
Correct |
12 ms |
1672 KB |
Output is correct |
12 |
Correct |
16 ms |
1856 KB |
Output is correct |
13 |
Correct |
4 ms |
584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
46 ms |
4780 KB |
Output is correct |
10 |
Correct |
34 ms |
3960 KB |
Output is correct |
11 |
Correct |
12 ms |
1616 KB |
Output is correct |
12 |
Correct |
16 ms |
1920 KB |
Output is correct |
13 |
Correct |
4 ms |
588 KB |
Output is correct |
14 |
Correct |
50 ms |
5688 KB |
Output is correct |
15 |
Correct |
59 ms |
6384 KB |
Output is correct |
16 |
Correct |
10 ms |
1356 KB |
Output is correct |
17 |
Correct |
38 ms |
4392 KB |
Output is correct |
18 |
Correct |
25 ms |
2524 KB |
Output is correct |