#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
int valid(int n, int gondolaSeq[]) {
int mi = INT_MAX, ind = -1;
set<int> c;
for (int i = 0; i < n; i++) {
c.insert(gondolaSeq[i]);
if (mi > gondolaSeq[i]) {
mi = gondolaSeq[i];
ind = i;
}
}
if (c.size() < n) {
return 0;
}
if (mi > n) {
return 1;
}
ind -= (mi-1);
ind += n;
ind %= n;
vector<int> a;
for (int i = ind; i < n; i++) {
a.push_back(gondolaSeq[i]);
}
for (int i = 0; i < ind; i++) {
a.push_back(gondolaSeq[i]);
}
for (int i = 0; i < n; i++) {
if (a[i] <= n && a[i] != i+1) {
return 0;
}
}
return 1;
}
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
int mi = INT_MAX, ind = -1, ma = 0;
set<int> c;
for (int i = 0; i < n; i++) {
ma = max(ma, gondolaSeq[i]);
c.insert(gondolaSeq[i]);
if (mi > gondolaSeq[i]) {
mi = gondolaSeq[i];
ind = i;
}
}
vector<pair<int, int>> num;
if (ma <= n) {
return 0;
}
if (mi > n) {
for (int i = 0; i < n; i++) {
num.push_back({gondolaSeq[i], i+1});
}
int l = 0;
sort(num.begin(), num.end());
l = num.back().first-n;
for (int i = 0; i < l; i++) {
replacementSeq[i] = num.back().second;
}
for (int i = 0; i < num.size(); i++) {
replacementSeq[num[i].first-n-1] = num[i].second;
}
vector<int> ans(n+1, 0);
for (int i = 1; i <= n; i++) {
ans[i] = i;
}
for (int i = 0; i < l; i++) {
int v = replacementSeq[i];
replacementSeq[i] = ans[replacementSeq[i]];
ans[v] = n+i+1;
}
return l;
} else {
ind -= (mi-1);
ind += n;
ind %= n;
vector<int> a;
for (int i = ind; i < n; i++) {
a.push_back(gondolaSeq[i]);
}
for (int i = 0; i < ind; i++) {
a.push_back(gondolaSeq[i]);
}
for (int i = 0; i < n; i++) {
if (a[i] > n) {
num.push_back({a[i], i+1});
}
}
int l = 0;
sort(num.begin(), num.end());
l = num.back().first-n;
for (int i = 0; i < l; i++) {
replacementSeq[i] = num.back().second;
}
for (int i = 0; i < num.size(); i++) {
replacementSeq[num[i].first-n-1] = num[i].second;
}
vector<int> ans(n+1, 0);
for (int i = 1; i <= n; i++) {
ans[i] = i;
}
for (int i = 0; i < l; i++) {
int v = replacementSeq[i];
replacementSeq[i] = ans[replacementSeq[i]];
ans[v] = n+i+1;
}
return l;
}
}
long long int binexp(long long int a, long long int b, long long int m) {
long long int r = 1;
while(b > 0) {
if (b & 1) {
b--;
r *= a;
r %= m;
}
b /= 2;
a *= a;
a %= m;
}
return r;
}
int countReplacement(int n, int gondolaSeq[]) {
if (!valid(n, gondolaSeq)) {
return 0;
}
vector<int> num;
long long int ans = 1, mod = 1000000009;
bool f = 0;
for (int i = 0; i < n; i++) {
if (gondolaSeq[i] > n) {
num.push_back(gondolaSeq[i]);
}
}
if (num.size() == n) {
ans = num.size();
}
if (num.size() == 0) {
return ans;
}
int v = num.size();
num.push_back(n);
sort(num.begin(), num.end());
for (int i = 1; i < num.size(); i++) {
ans *= binexp(v, num[i]-num[i-1]-1, mod);
ans %= mod;
v--;
}
return ans;
}
Compilation message
gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:14:18: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
14 | if (c.size() < n) {
| ~~~~~~~~~^~~
gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:62:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int i = 0; i < num.size(); i++) {
| ~~^~~~~~~~~~~~
gondola.cpp:97:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for (int i = 0; i < num.size(); i++) {
| ~~^~~~~~~~~~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:138:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
138 | if (num.size() == n) {
| ~~~~~~~~~~~^~~~
gondola.cpp:147:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
147 | for (int i = 1; i < num.size(); i++) {
| ~~^~~~~~~~~~~~
gondola.cpp:132:10: warning: unused variable 'f' [-Wunused-variable]
132 | bool f = 0;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
2648 KB |
Output is correct |
7 |
Correct |
30 ms |
3564 KB |
Output is correct |
8 |
Correct |
13 ms |
4568 KB |
Output is correct |
9 |
Correct |
5 ms |
1624 KB |
Output is correct |
10 |
Correct |
19 ms |
5096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
2664 KB |
Output is correct |
7 |
Correct |
29 ms |
3780 KB |
Output is correct |
8 |
Correct |
23 ms |
4556 KB |
Output is correct |
9 |
Correct |
5 ms |
1628 KB |
Output is correct |
10 |
Correct |
18 ms |
5084 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
10 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
28 ms |
5300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
432 KB |
Output is correct |
8 |
Correct |
1 ms |
436 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
25 ms |
4216 KB |
Output is correct |
12 |
Correct |
16 ms |
4956 KB |
Output is correct |
13 |
Correct |
27 ms |
2904 KB |
Output is correct |
14 |
Correct |
16 ms |
4184 KB |
Output is correct |
15 |
Correct |
15 ms |
3088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
36 ms |
4808 KB |
Output is correct |
10 |
Correct |
29 ms |
3920 KB |
Output is correct |
11 |
Correct |
10 ms |
1628 KB |
Output is correct |
12 |
Correct |
9 ms |
1884 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
25 ms |
4668 KB |
Output is correct |
10 |
Correct |
20 ms |
3932 KB |
Output is correct |
11 |
Correct |
7 ms |
1628 KB |
Output is correct |
12 |
Correct |
10 ms |
1964 KB |
Output is correct |
13 |
Correct |
3 ms |
628 KB |
Output is correct |
14 |
Correct |
33 ms |
4588 KB |
Output is correct |
15 |
Correct |
39 ms |
5980 KB |
Output is correct |
16 |
Correct |
8 ms |
1372 KB |
Output is correct |
17 |
Correct |
34 ms |
4048 KB |
Output is correct |
18 |
Correct |
15 ms |
2400 KB |
Output is correct |