#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1000000009;
ll mpow(ll b, ll e) {
if (e == 0) return 1;
if (e & 1) return mpow(b, e - 1) * b % mod;
return mpow(b * b % mod, e >> 1);
}
vector<int> arr(int *s, int n, int d = 0) {
vector<int> v(s, s + n);
for (int i = 0; i < n; i++) v[i] += d;
return v;
}
int rotatoize(vector<int>& v) {
int p = 0, n = v.size();
while (p < n && v[p] >= n) p++;
if (p < n) rotate(v.begin(), v.begin() + (p - v[p] + n) % n, v.end());
return p;
}
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
vector<int> gon = arr(gondolaSeq, n, -1);
vector<int> rep;
int p = rotatoize(gon);
vector<int> inds(n), curr(n);
iota(curr.begin(), curr.end(), 0);
iota(inds.begin(), inds.end(), 0);
sort(inds.begin(), inds.end(), [&](int i, int j) { return gon[i] < gon[j]; });
int spare = n;
for (int i : inds) {
while (spare <= gon[i]) {
rep.push_back(curr[i]);
curr[i] = spare++;
}
}
for (int i = 0; i < rep.size(); i++) replacementSeq[i] = rep[i] + 1;
return rep.size();
}
int countReplacement(int n, int inputSeq[]) {
vector<int> gon = arr(inputSeq, n, -1);
int p = rotatoize(gon);
int blanks = 0;
for (int i = 0; i < n; i++) {
if (gon[i] < n) {
if (gon[i] != i) return 0;
} else blanks++;
}
vector<int> vals = gon;
sort(vals.begin(), vals.end());
ll ans = blanks == n ? n : 1;
vector<int> inds(n);
iota(inds.begin(), inds.end(), 0);
sort(inds.begin(), inds.end(), [&](int i, int j) { return gon[i] < gon[j]; });
int spare = n, prev = -1;
for (int i : inds) {
if (prev == gon[i]) return 0;
prev = gon[i];
if (spare <= gon[i]) {
ans = ans * mpow(blanks--, max(0, gon[i] - spare)) % mod;
spare = gon[i] + 1;
}
}
assert(blanks == 0);
return int(ans);
}
int valid(int n, int inputSeq[]) {
return countReplacement(n, inputSeq) != 0;
}
Compilation message
gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:44:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i = 0; i < rep.size(); i++) replacementSeq[i] = rep[i] + 1;
| ~~^~~~~~~~~~~~
gondola.cpp:30:9: warning: unused variable 'p' [-Wunused-variable]
30 | int p = rotatoize(gon);
| ^
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:50:9: warning: unused variable 'p' [-Wunused-variable]
50 | int p = rotatoize(gon);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
8 ms |
1152 KB |
Output is correct |
7 |
Correct |
15 ms |
1536 KB |
Output is correct |
8 |
Correct |
16 ms |
1792 KB |
Output is correct |
9 |
Correct |
5 ms |
768 KB |
Output is correct |
10 |
Correct |
18 ms |
2176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
8 ms |
1152 KB |
Output is correct |
7 |
Correct |
15 ms |
1536 KB |
Output is correct |
8 |
Correct |
15 ms |
1792 KB |
Output is correct |
9 |
Correct |
5 ms |
896 KB |
Output is correct |
10 |
Correct |
18 ms |
2176 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
7 ms |
896 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
15 ms |
1536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
15 ms |
2048 KB |
Output is correct |
12 |
Correct |
17 ms |
2304 KB |
Output is correct |
13 |
Correct |
19 ms |
1784 KB |
Output is correct |
14 |
Correct |
17 ms |
2048 KB |
Output is correct |
15 |
Correct |
27 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
28 ms |
1912 KB |
Output is correct |
10 |
Correct |
22 ms |
1664 KB |
Output is correct |
11 |
Correct |
9 ms |
768 KB |
Output is correct |
12 |
Correct |
10 ms |
896 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
27 ms |
1920 KB |
Output is correct |
10 |
Correct |
22 ms |
1664 KB |
Output is correct |
11 |
Correct |
9 ms |
768 KB |
Output is correct |
12 |
Correct |
11 ms |
896 KB |
Output is correct |
13 |
Correct |
3 ms |
640 KB |
Output is correct |
14 |
Correct |
34 ms |
2428 KB |
Output is correct |
15 |
Correct |
39 ms |
2688 KB |
Output is correct |
16 |
Correct |
7 ms |
768 KB |
Output is correct |
17 |
Correct |
25 ms |
1920 KB |
Output is correct |
18 |
Correct |
14 ms |
1280 KB |
Output is correct |