#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
#define pii pair <int,int>
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ll long long
const ll INF = (ll)1e9 + 9ll;
int valid(int n, int a[]) {
int c[n + 10];
map <int,int> was;
for(int i = 0;i < n;i++) {
if(a[i] <= n) {
int tek = a[i];
c[i] = tek;
for(int j = i + 1;j < n;j++) {
tek++;
if(tek == n + 1)tek = 1;
c[j] = tek;
}
for(int j = 0;j < i;j++) {
tek++;
if(tek == n + 1)tek = 1;
c[j] = tek;
}
break;
}
}
for(int i = 0;i < n;i++) {
if(a[i] <= n && c[i] != a[i])return 0;
if(was[a[i]])return 0;
was[a[i]] = 1;
}
return 1;
}
/*
1
30
16 26 18 19 20 13 22 21 24 25 17 27 28 29 30 1 2 3 11 5 6 8 7 9 10 12 4 23 14 15
*/
//----------------------
int replacement(int n, int a[], int b[]) {
int c[n + 1];
bool ch = 0;
for(int i = 0;i < n;i++) {
if(a[i] <= n) {
int tek = a[i];
c[i] = tek;
for(int j = i + 1;j < n;j++) {
tek++;
if(tek == n + 1)tek = 1;
c[j] = tek;
}
for(int j = 0;j < i;j++) {
tek++;
if(tek == n + 1)tek = 1;
c[j] = tek;
}
ch = 1;
break;
}
}
if(!ch) {
for(int i = 0;i < n;i++) {
c[i] = i + 1;
}
}
set <pii> g;
for(int i = 0;i < n;i++) {
if(a[i] > n)g.insert(mp(a[i],i));
}
int k = 0;
int cnt = n + 1;
while(!g.empty()) {
pii vv = *(g.begin());
int val = vv.f,bilo = c[vv.s];
while(1) {
b[k++] = bilo;
if(cnt == val) {
cnt++;
break;
}
bilo = cnt++;
}
g.erase(vv);
}
return k;
}
/*
4
2
3 2
8
14
6 94 8 9 10 93 12 13 95 1 2 3 4 5
*/
//----------------------
ll binpow(ll a,ll b) {
ll ans = 1,k = a;
while(b) {
if(b % 2)ans *= k;
ans %= INF;
k *= k;
k %= INF;
b /= 2ll;
}
return ans;
}
int countReplacement(int n, int a[]) {
ll d[n + 10];
bool ch = 0;
for(int i = 0;i < n;i++) {
if(a[i] <= n) {
d[i] = 0;
ch = 1;
}else d[i] = a[i] - n;
}
if(!valid(n,a))return 0;
sort(d,d + n);
ll ans = 1;
ll kol = n,last = 0;
for(int i = 0;i < n;i++) {
if(d[i] == 0) {
kol--;
continue;
}
ll cnt = d[i] - last - 1;
ans = (ans * binpow(kol,cnt)) % INF;
kol--;
last = d[i];
}
if(!ch) {
ll tek = n;
ans = (ans * tek) % INF;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
15 ms |
2260 KB |
Output is correct |
7 |
Correct |
7 ms |
980 KB |
Output is correct |
8 |
Correct |
23 ms |
4340 KB |
Output is correct |
9 |
Correct |
8 ms |
1492 KB |
Output is correct |
10 |
Correct |
30 ms |
4812 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
12 ms |
2248 KB |
Output is correct |
7 |
Correct |
7 ms |
980 KB |
Output is correct |
8 |
Correct |
21 ms |
4096 KB |
Output is correct |
9 |
Correct |
7 ms |
1492 KB |
Output is correct |
10 |
Correct |
30 ms |
4764 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 |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
9 ms |
980 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 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 |
7 ms |
840 KB |
Output is correct |
12 |
Correct |
7 ms |
980 KB |
Output is correct |
13 |
Correct |
18 ms |
2612 KB |
Output is correct |
14 |
Correct |
7 ms |
852 KB |
Output is correct |
15 |
Correct |
20 ms |
2872 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
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 |
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 |
37 ms |
4880 KB |
Output is correct |
10 |
Correct |
32 ms |
4044 KB |
Output is correct |
11 |
Correct |
11 ms |
1620 KB |
Output is correct |
12 |
Correct |
15 ms |
1896 KB |
Output is correct |
13 |
Correct |
5 ms |
596 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 |
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 |
42 ms |
4796 KB |
Output is correct |
10 |
Correct |
30 ms |
4052 KB |
Output is correct |
11 |
Correct |
11 ms |
1600 KB |
Output is correct |
12 |
Correct |
17 ms |
2004 KB |
Output is correct |
13 |
Correct |
5 ms |
596 KB |
Output is correct |
14 |
Correct |
51 ms |
6340 KB |
Output is correct |
15 |
Correct |
53 ms |
7056 KB |
Output is correct |
16 |
Correct |
9 ms |
1576 KB |
Output is correct |
17 |
Correct |
37 ms |
4900 KB |
Output is correct |
18 |
Correct |
19 ms |
2772 KB |
Output is correct |