#include <bits/stdc++.h>
#include "gondola.h"
using namespace std;
const int mod = 1e9+9;
int valid(int n, int inputSeq[])
{
deque<int> a(n);
deque<int> perm(n);
for (int i = 0; i<n; i++) {
perm[i] = i;
a[i] = inputSeq[i]-1;
}
for (int i=0; i<=n; i++) {
if (a==perm) return 1;
perm.push_back(perm[0]);
perm.pop_front();
}
return 0;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
int start = -1;
for (int i = 0; i<n; i++) {
gondolaSeq[i]--;
if (gondolaSeq[i] < n) {
if (start == -1)
start = (n+i-gondolaSeq[i])%n;
assert(start == (n+i-gondolaSeq[i])%n);
}
}
if (start == -1) start = 0;
set<pair<int, int>> replacements;
set<int> okToReplace;
for (int i = start; i<n+start; i++) {
if (gondolaSeq[i%n] != i-start) {
replacements.insert({gondolaSeq[i%n], i-start});
okToReplace.insert(i-start);
}
}
int curr = n;
map<int, int> dict;
for (int i: okToReplace) {
dict[i]=i;
}
while (replacements.size()) {
assert((*replacements.begin()).first >= curr);
assert(okToReplace.size());
if ((*replacements.begin()).first == curr) {
replacementSeq[curr-n] = dict[(*replacements.begin()).second]+1;
okToReplace.erase((*replacements.begin()).second);
replacements.erase(replacements.begin());
}
else {
replacementSeq[curr-n] = dict[*okToReplace.begin()]+1;
dict[*okToReplace.begin()] = curr;
}
curr++;
}
return curr-n;
}
//----------------------
long int fpow(long int b, long int e) {
if (e == 0) return 1;
long int res = fpow(b, e/2);
res *= res;
res %= mod;
if (e%2 == 1) res *= b;
res %= mod;
return res;
}
int countReplacement(int n, int inputSeq[])
{
vector<int> seq(n);
int start = -1;
long int res = 1;
for (int i = 0; i<n; i++) {
inputSeq[i]--;
seq[i] = inputSeq[i];
if (inputSeq[i] < n) {
if (start == -1)
start = (n+i-inputSeq[i])%n;
if(start != (n+i-inputSeq[i])%n) return 0;
}
}
if (start == -1) {start = 0; res = n;}
// cerr << "ok\n";
sort(seq.begin(), seq.end());
int last = -1;
for (int i: seq) {
// cerr << i << " ";
if (last == i) return 0;
last = i;
}
// cerr << "\n";
// cerr << "ok\n";
last = n-1;
for (int i = 0; i<n; i++) {
if (seq[i] <= last) continue;
res *= fpow(n-i, seq[i]-last-1);
res %= mod;
last = seq[i];
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
4 ms |
724 KB |
Output is correct |
7 |
Correct |
10 ms |
1364 KB |
Output is correct |
8 |
Correct |
10 ms |
1108 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
13 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
4 ms |
724 KB |
Output is correct |
7 |
Correct |
11 ms |
1364 KB |
Output is correct |
8 |
Correct |
9 ms |
1172 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
7 ms |
1196 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
5 ms |
724 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
10 ms |
1248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
448 KB |
Output is correct |
11 |
Correct |
8 ms |
540 KB |
Output is correct |
12 |
Correct |
15 ms |
596 KB |
Output is correct |
13 |
Correct |
46 ms |
5324 KB |
Output is correct |
14 |
Correct |
8 ms |
504 KB |
Output is correct |
15 |
Correct |
42 ms |
4432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
14 ms |
832 KB |
Output is correct |
10 |
Correct |
10 ms |
724 KB |
Output is correct |
11 |
Correct |
4 ms |
468 KB |
Output is correct |
12 |
Correct |
5 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
220 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
13 ms |
852 KB |
Output is correct |
10 |
Correct |
12 ms |
724 KB |
Output is correct |
11 |
Correct |
7 ms |
468 KB |
Output is correct |
12 |
Correct |
5 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
17 ms |
852 KB |
Output is correct |
15 |
Correct |
19 ms |
980 KB |
Output is correct |
16 |
Correct |
4 ms |
340 KB |
Output is correct |
17 |
Correct |
13 ms |
724 KB |
Output is correct |
18 |
Correct |
7 ms |
552 KB |
Output is correct |