#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
int valid(int N, int A[]) {
int j = -1;
for (int i = 0; i < N; i++)
if (A[i] >= 1 && A[i] <= N) {
j = i;
break;
}
set<int> vals;
for (int i = 0; i < N; i++)
vals.insert(A[i]);
if (vals.size() != N)
return 0;
if (j == -1)
return 1;
int val = A[j];
for (int i = 0; i < N; i++) {
int id = (i + j) % N;
if (A[id] >= 1 && A[id] <= N && A[id] != val)
return 0;
val++;
if (val == N + 1)
val = 1;
}
return 1;
}
int replacement(int N, int A[], int ans[]) {
vector<int> B;
for (int i = 0; i < N; i++)
if (A[i] > N)
B.push_back(i);
int start_id = -1;
for (int i = 0; i < N; i++)
if (A[i] >= 1 && A[i] <= N) {
start_id = i;
break;
}
if (start_id == -1) {
start_id = 0;
for (int i = 0; i < N; i++)
if (A[start_id] > A[i])
start_id = i;
}
vector<int> start_val(N);
for (int i = 0; i < N; i++) {
int index = (start_id + i) % N;
int val = (A[start_id] > N ? i + 1 : A[start_id] + i);
if (val > N)
val -= N;
start_val[index] = val;
assert(A[index] >= start_val[index]);
}
sort(B.begin(), B.end(), [&](int i, int j){
return A[i] < A[j];
});
if (B.empty())
return 0;
int index = 0;
for (int i = 0; i < (int) B.size(); i++) {
ans[index++] = start_val[B[i]];
if (i == 0)
for (int j = N + 1; j < A[B[i]]; j++)
ans[index++] = j;
else
for (int j = A[B[i - 1]] + 1; j < A[B[i]]; j++)
ans[index++] = j;
}
return index;
}
const int MOD = 1e9 + 9;
int64_t power(int64_t x, int64_t y) {
int64_t res = 1;
while (y > 0) {
if (y & 1)
res *= x, res %= MOD;
x *= x;
x %= MOD;
y >>= 1;
}
return res;
}
int countReplacement(int N, int A[]) {
if (!valid(N, A))
return 0;
vector<int> B(1, N);
for (int i = 0; i < N; i++)
if (A[i] > N)
B.push_back(A[i]);
sort(B.begin(), B.end());
int64_t answer = (B.size() == N + 1 ? N : 1);
for (int i = 1, cnt = B.size() - 1; i < B.size(); i++)
answer *= power(cnt - i + 1, B[i] - B[i - 1] - 1), answer %= MOD;
return answer;
}
Compilation message
gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:17:21: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
17 | if (vals.size() != N)
| ~~~~~~~~~~~~^~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:117:32: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
117 | int64_t answer = (B.size() == N + 1 ? N : 1);
| ~~~~~~~~~^~~~~~~~
gondola.cpp:118:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | for (int i = 1, cnt = B.size() - 1; i < B.size(); i++)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
12 ms |
2124 KB |
Output is correct |
7 |
Correct |
28 ms |
3664 KB |
Output is correct |
8 |
Correct |
24 ms |
3836 KB |
Output is correct |
9 |
Correct |
9 ms |
1380 KB |
Output is correct |
10 |
Correct |
29 ms |
4456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
16 ms |
2124 KB |
Output is correct |
7 |
Correct |
34 ms |
3524 KB |
Output is correct |
8 |
Correct |
25 ms |
3932 KB |
Output is correct |
9 |
Correct |
8 ms |
1356 KB |
Output is correct |
10 |
Correct |
29 ms |
4420 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
16 ms |
1992 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
37 ms |
4612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 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 |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
10 ms |
844 KB |
Output is correct |
12 |
Correct |
11 ms |
972 KB |
Output is correct |
13 |
Correct |
17 ms |
1220 KB |
Output is correct |
14 |
Correct |
14 ms |
832 KB |
Output is correct |
15 |
Correct |
31 ms |
2196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
43 ms |
4000 KB |
Output is correct |
10 |
Correct |
30 ms |
3680 KB |
Output is correct |
11 |
Correct |
11 ms |
1592 KB |
Output is correct |
12 |
Correct |
14 ms |
1868 KB |
Output is correct |
13 |
Correct |
4 ms |
564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
47 ms |
3912 KB |
Output is correct |
10 |
Correct |
30 ms |
3724 KB |
Output is correct |
11 |
Correct |
11 ms |
1484 KB |
Output is correct |
12 |
Correct |
14 ms |
1740 KB |
Output is correct |
13 |
Correct |
3 ms |
588 KB |
Output is correct |
14 |
Correct |
51 ms |
5292 KB |
Output is correct |
15 |
Correct |
62 ms |
5956 KB |
Output is correct |
16 |
Correct |
9 ms |
1284 KB |
Output is correct |
17 |
Correct |
35 ms |
4112 KB |
Output is correct |
18 |
Correct |
18 ms |
2380 KB |
Output is correct |