#include <bits/stdc++.h>
using namespace std;
#include "gondola.h"
int valid(int n, int inputSeq[]) {
int k = 0;
for(int i = 0; i < n; i ++) {
if(inputSeq[k] > inputSeq[i]) {
k = i;
}
}
if(inputSeq[k] <= n) {
for(int i = 1; i <= n - inputSeq[k]; i ++) {
int j = (k + i) % n;
if(inputSeq[j] <= n && inputSeq[j] != inputSeq[k] + i) {
return 0;
}
}
for(int i = 1; i <= inputSeq[k] - 1; i ++) {
int j = (k - i + n) % n;
if(inputSeq[j] <= n && inputSeq[j] != inputSeq[k] - i) {
return 0;
}
}
}
sort(inputSeq, inputSeq + n);
for(int i = 0; i + 1 < n; i ++) {
if(inputSeq[i] == inputSeq[i + 1]) {
return 0;
}
}
return 1;
}
//----------------------
vector<int> bld(int n, int k, int gondolaSeq[]) {
vector<int> seq;
seq.push_back(gondolaSeq[k]);
for(int i = 1; i <= n - gondolaSeq[k]; i ++) {
int j = (k + i) % n;
seq.push_back(gondolaSeq[j]);
}
reverse(seq.begin(), seq.end());
for(int i = 1; i <= gondolaSeq[k] - 1; i ++) {
int j = (k - i + n) % n;
seq.push_back(gondolaSeq[j]);
}
reverse(seq.begin(), seq.end());
return seq;
}
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
int k = 0;
for(int i = 0; i < n; i ++) {
if(gondolaSeq[k] > gondolaSeq[i]) {
k = i;
}
}
int l = 0;
if(gondolaSeq[k] > n) {
vector<pair<int, int>> p;
for(int i = 0; i < n; i ++) {
p.push_back({gondolaSeq[i], i + 1});
}
sort(p.begin(), p.end());
int j = n + 1;
for(int i = 0; i < n; i ++) {
if(p[i].first > n) {
replacementSeq[l ++] = p[i].second;
}
for(; j < p[i].first; j ++) {
replacementSeq[l ++] = j;
}
if(j == p[i].first) j ++ ;
}
} else {
vector<int> seq = bld(n, k, gondolaSeq);
vector<pair<int, int>> p;
for(int i = 0; i < n; i ++) {
p.push_back({seq[i], i + 1});
}
sort(p.begin(), p.end());
int j = n + 1;
for(int i = 0; i < n; i ++) {
if(p[i].first > n) {
replacementSeq[l ++] = p[i].second;
}
for(; j < p[i].first; j ++) {
replacementSeq[l ++] = j;
}
if(j == p[i].first) j ++ ;
}
}
return l;
}
//----------------------
const int mod = 1000000009;
int binpow(int a, int b) {
if(!b) return 1;
if(b % 2 == 0) {
int x = binpow(a, b / 2);
return 1LL * x * x % mod;
}
return 1LL * binpow(a, b - 1) * a % mod;
}
int countReplacement(int n, int inputSeq[]) {
if(!valid(n, inputSeq)) return 0;
int k = 0;
for(int i = 0; i < n; i ++) {
if(inputSeq[k] > inputSeq[i]) {
k = i;
}
}
vector<int> seq;
for(int i = 0; i < n; i ++) {
if(inputSeq[i] > n) {
seq.push_back(inputSeq[i]);
}
}
sort(seq.begin(), seq.end());
int curr = n;
int total = 1;
if(inputSeq[k] > n) {
total *= n;
}
for(int i = 0; i < seq.size(); i ++) {
total = 1LL * total * binpow((seq.size() - i), seq[i] - 1 - curr) % mod;
curr = seq[i];
}
return total;
}
Compilation message
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:162:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
162 | for(int i = 0; i < seq.size(); i ++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
348 KB |
Output is correct |
7 |
Correct |
8 ms |
600 KB |
Output is correct |
8 |
Correct |
7 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
344 KB |
Output is correct |
10 |
Correct |
12 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
348 KB |
Output is correct |
7 |
Correct |
10 ms |
604 KB |
Output is correct |
8 |
Correct |
7 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
6 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
8 ms |
2136 KB |
Output is correct |
12 |
Correct |
9 ms |
2212 KB |
Output is correct |
13 |
Correct |
11 ms |
1368 KB |
Output is correct |
14 |
Correct |
8 ms |
2092 KB |
Output is correct |
15 |
Correct |
15 ms |
2352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
13 ms |
984 KB |
Output is correct |
10 |
Correct |
9 ms |
984 KB |
Output is correct |
11 |
Correct |
4 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 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 |
0 ms |
344 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 |
12 ms |
1008 KB |
Output is correct |
10 |
Correct |
9 ms |
980 KB |
Output is correct |
11 |
Correct |
4 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
15 ms |
2008 KB |
Output is correct |
15 |
Correct |
16 ms |
2360 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
11 ms |
1628 KB |
Output is correct |
18 |
Correct |
6 ms |
1112 KB |
Output is correct |