#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
const int MOD = (int) 1e9 + 9;
const int INF = (int) 1e9;
int valid(int n, int inputSeq[]) {
int id = 0, pos;
map <int, bool> mp;
for(int i = 0; i < n; i++) {
if(mp[inputSeq[i]] == 1) {
return 0;
}
mp[inputSeq[i]] = 1;
if(inputSeq[i] <= n) {
id = inputSeq[i];
pos = i;
}
}
if(id != 0) {
int cur = id;
int i = pos;
do {
if(inputSeq[i] <= n && inputSeq[i] != cur) {
return 0;
}
cur++;
if(cur == n + 1) {
cur = 1;
}
i = (i + 1) % n;
}while(i != pos);
}
return 1;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
vector <int> positions;
for(int i = 0; i < n; i++) {
if(gondolaSeq[i] > n) {
positions.push_back(i);
}
}
sort(positions.begin(), positions.end(), [&](const int &a, const int &b) {
return gondolaSeq[a] < gondolaSeq[b];
});
map <int, int> mp;
int id = 0, pos, mx = 0;
for(int i = 0; i < n; i++) {
mp[gondolaSeq[i]] = i + 1;
mx = max(mx, gondolaSeq[i]);
if(gondolaSeq[i] <= n) {
id = gondolaSeq[i];
pos = i;
}
}
vector <int> ord(n);
iota(ord.begin(), ord.end(), 1);
if(id != 0) {
int i = pos;
do {
ord[i] = id;
id++;
if(id == n + 1) {
id = 1;
}
i = (i + 1) % n;
}while(i != pos);
}
pos = 0;
int sz = 0;
for(int i = n + 1; i <= mx; i++) {
replacementSeq[sz++] = ord[positions[pos]];
ord[positions[pos]] = i;
if(mp[i] > 0) {
pos++;
}
}
return sz;
}
//----------------------
inline int lgput(int a, int b) {
int ans = 1;
while(b > 0) {
if(b & 1)
ans = (1LL * ans * a) % MOD;
b >>= 1;
a = (1LL * a * a) % MOD;
}
return ans;
}
int countReplacement(int n, int inputSeq[]) {
if(!valid(n, inputSeq)) {
return 0;
}
vector <int> vals;
int cnt = 0;
for(int i = 0; i < n; i++) {
if(inputSeq[i] > n) {
cnt++;
vals.push_back(inputSeq[i]);
}
}
sort(vals.begin(), vals.end());
int ans = 1, last = n;
for(auto it : vals) {
ans = (1LL * ans * lgput(cnt, it - last - 1)) % MOD;
last = it;
cnt--;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
304 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
23 ms |
2248 KB |
Output is correct |
7 |
Correct |
14 ms |
640 KB |
Output is correct |
8 |
Correct |
33 ms |
3968 KB |
Output is correct |
9 |
Correct |
11 ms |
1536 KB |
Output is correct |
10 |
Correct |
43 ms |
4568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
304 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
16 ms |
2304 KB |
Output is correct |
7 |
Correct |
13 ms |
768 KB |
Output is correct |
8 |
Correct |
34 ms |
3940 KB |
Output is correct |
9 |
Correct |
11 ms |
1536 KB |
Output is correct |
10 |
Correct |
48 ms |
4600 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
23 ms |
2040 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
65 ms |
4856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
300 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
640 KB |
Output is correct |
11 |
Correct |
32 ms |
4736 KB |
Output is correct |
12 |
Correct |
36 ms |
5240 KB |
Output is correct |
13 |
Correct |
41 ms |
4720 KB |
Output is correct |
14 |
Correct |
36 ms |
4696 KB |
Output is correct |
15 |
Correct |
61 ms |
10200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
356 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
316 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
356 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
69 ms |
4088 KB |
Output is correct |
10 |
Correct |
50 ms |
3448 KB |
Output is correct |
11 |
Correct |
17 ms |
1408 KB |
Output is correct |
12 |
Correct |
21 ms |
1664 KB |
Output is correct |
13 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
84 ms |
4088 KB |
Output is correct |
10 |
Correct |
48 ms |
3448 KB |
Output is correct |
11 |
Correct |
16 ms |
1408 KB |
Output is correct |
12 |
Correct |
23 ms |
1792 KB |
Output is correct |
13 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |