#include "gondola.h"
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e6 + 123;
int u[N];
int valid(int n, int inputSeq[]) {
int id = -1;
for (int i = 0 ; i < n ; ++ i) {
if (inputSeq[i] <= n) id = i;
}
for (int i = 0 ; i < n ; ++ i) u[i] = inputSeq[i];
sort(u, u + n);
for (int i = 1 ; i < n ; ++ i) if (u[i] == u[i - 1]) return 0;
if (id == -1) return 1;
int x = inputSeq[id];
for (int i = 0 ; i + x <= n ; ++ i) {
int j = (i + id) % n;
if (inputSeq[j] != i + x && inputSeq[j] <= n) return 0;
}
for (int i = 0 ; i < x ; ++ i) {
int j = (id - i + n) % n;
if (inputSeq[j] != x - i && inputSeq[j] <= n) return 0;
}
return 1;
}
//----------------------
pair<int,int>pp[N];
int tt[N];
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
for (int i = 0 ; i < n ; ++ i) {
pp[i] = {gondolaSeq[i], i};
}
sort(pp, pp + n);
int id = 0, x = 1;
if (pp[0].first <= n) id = pp[0].second, x = pp[0].first;
for (int i = 0 ; i + x <= n ; ++ i) {
int j = (i + id) % n;
tt[j] = i + x;
}
for (int i = 1 ; i < x ; ++ i) {
int j = (id - i + n) % n;
tt[j] = x - i;
}
int m = 0;
int t = n;
for (int i = 0 ; i < n ; ++ i) {
int x = pp[i].first;
int y = pp[i].second;
if (x <= n) continue;
while (tt[y] < x) {
replacementSeq[m++] = tt[y];
tt[y] = ++t;
}
}
return m;
}
//----------------------
const int mod = 1e9 + 9;
using ll = long long;
ll binpow(ll a, ll b) {
ll r = 1;
while (b) {
if (b & 1) r = r * a % mod;
b >>= 1;
a = a * a % mod;
}
return r;
}
ll fact[N];
int countReplacement(int n, int inputSeq[]) {
if (!valid(n, inputSeq)) {
return 0;
}
for (int i = 0 ; i < n ; ++ i) {
u[i] = inputSeq[i];
}
sort(u, u + n);
int t = n;
ll ans = 1;
if (u[0] > n) {
fact[0] = 1;
for (int i = 1 ; i < N ; ++ i) fact[i] = fact[i - 1] * i % mod;
ans = n;
}
for (int i = 0 ; i < n ; ++ i) {
int x = u[i];
if (x <= n) continue;
long long z = x - t - 1;
ans *= binpow(n - i, z) % mod;
ans %= mod;
t = x;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
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 |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
212 KB |
Output is correct |
6 |
Correct |
6 ms |
612 KB |
Output is correct |
7 |
Correct |
13 ms |
1008 KB |
Output is correct |
8 |
Correct |
11 ms |
804 KB |
Output is correct |
9 |
Correct |
4 ms |
468 KB |
Output is correct |
10 |
Correct |
12 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
596 KB |
Output is correct |
7 |
Correct |
13 ms |
1008 KB |
Output is correct |
8 |
Correct |
9 ms |
852 KB |
Output is correct |
9 |
Correct |
6 ms |
476 KB |
Output is correct |
10 |
Correct |
14 ms |
852 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
5 ms |
576 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
20 ms |
968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
10 ms |
1492 KB |
Output is correct |
12 |
Correct |
10 ms |
1620 KB |
Output is correct |
13 |
Correct |
17 ms |
1488 KB |
Output is correct |
14 |
Correct |
8 ms |
1500 KB |
Output is correct |
15 |
Correct |
27 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
17 ms |
852 KB |
Output is correct |
10 |
Correct |
15 ms |
724 KB |
Output is correct |
11 |
Correct |
6 ms |
468 KB |
Output is correct |
12 |
Correct |
7 ms |
468 KB |
Output is correct |
13 |
Correct |
12 ms |
8148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
17 ms |
804 KB |
Output is correct |
10 |
Correct |
13 ms |
724 KB |
Output is correct |
11 |
Correct |
6 ms |
468 KB |
Output is correct |
12 |
Correct |
7 ms |
468 KB |
Output is correct |
13 |
Correct |
9 ms |
8148 KB |
Output is correct |
14 |
Correct |
30 ms |
9280 KB |
Output is correct |
15 |
Correct |
32 ms |
9364 KB |
Output is correct |
16 |
Correct |
17 ms |
8404 KB |
Output is correct |
17 |
Correct |
27 ms |
9040 KB |
Output is correct |
18 |
Correct |
22 ms |
8752 KB |
Output is correct |