#include "gondola.h"
#include<bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vi = vector<int>;
int valid(int n, int a[]) {
set<int> x;
int p = 0;
for(int i = 0; i < n; i++) {
if(!x.insert(a[i]).second) return 0;
if(a[p]>a[i]) p = i;
}
if(p>n) return true;
int u = a[p];
for(int i = 0; i < n; i++) {
if(a[p]<=n&&a[p]!=u) return 0;
if(a[p]>n)
a[p]=u;
p = (p+1)%n;
u = (u%n)+1;
}
return 1;
}
//----------------------
int pos[4*250250];
int replacement(int n, int a[], int b[]) {
memset(pos, -1, sizeof pos);
int mp = 0, p = 0;
for(int i = 0; i < n; i++) {
if(a[i] > n) {
pos[a[i]] = i;
}
if(a[i]>a[mp]) mp=i;
if(a[i]<a[p]) p=i;
}
if(a[mp]<=n) return 0;
int M = a[mp], l = 0;
if(a[p]>n) a[p] = 1;
int u = a[p];
for(int i = 0; i < n; i++) {
a[p] = u;
p = (p+1)%n;
u = (u%n)+1;
}
for(int i = n+1; i <= M; i++) {
if(pos[i]!=-1) {
b[l++] = a[pos[i]];
a[pos[i]] = i;
} else {
b[l++] = a[mp];
a[mp] = i;
}
}
return l;
}
//----------------------
ll mod = 1e9+9;
ll bp(ll a, ll p) {
ll r = 1;
while(p) {
if(p&1) r = (r*a)%mod;
a = (a*a)%mod;
p>>=1;
}
return r;
}
int countReplacement(int n, int a[]) {
ll ans = 1, use = 0;
memset(pos, -1, sizeof pos);
int mp = 0, p = 0;
vi useful;
for(int i = 0; i < n; i++) {
if(a[i] > n) {
useful.pb(a[i]);
use++;
}
if(a[i]>a[mp]) mp=i;
if(a[i]<a[p]) p=i;
}
int M = a[mp], l = 0, GG = 0;
if(a[p]>n) a[p] = 1, GG = 1;
if(a[mp]<=n) return 1;
if(!valid(n, a)) return 0;
p = n+1;
sort(all(useful));
for(auto i : useful) {
ans = (ans*bp(use, i-p))%mod;
p = i+1;
use--;
}
if(GG) (ans *= n)%=mod;
return ans;
}
Compilation message
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:83:6: warning: unused variable 'M' [-Wunused-variable]
int M = a[mp], l = 0, GG = 0;
^
gondola.cpp:83:17: warning: unused variable 'l' [-Wunused-variable]
int M = a[mp], l = 0, GG = 0;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
252 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
128 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
16 ms |
2296 KB |
Output is correct |
7 |
Correct |
14 ms |
760 KB |
Output is correct |
8 |
Correct |
29 ms |
3960 KB |
Output is correct |
9 |
Correct |
10 ms |
1528 KB |
Output is correct |
10 |
Correct |
37 ms |
4600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
16 ms |
2168 KB |
Output is correct |
7 |
Correct |
14 ms |
760 KB |
Output is correct |
8 |
Correct |
29 ms |
3960 KB |
Output is correct |
9 |
Correct |
10 ms |
1532 KB |
Output is correct |
10 |
Correct |
38 ms |
4600 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
20 ms |
2168 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
47 ms |
4728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4220 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Correct |
6 ms |
4216 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4216 KB |
Output is correct |
2 |
Correct |
6 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4220 KB |
Output is correct |
4 |
Correct |
6 ms |
4216 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
6 |
Correct |
6 ms |
4216 KB |
Output is correct |
7 |
Correct |
6 ms |
4216 KB |
Output is correct |
8 |
Correct |
6 ms |
4216 KB |
Output is correct |
9 |
Correct |
6 ms |
4324 KB |
Output is correct |
10 |
Correct |
6 ms |
4344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4216 KB |
Output is correct |
2 |
Correct |
6 ms |
4216 KB |
Output is correct |
3 |
Correct |
6 ms |
4216 KB |
Output is correct |
4 |
Correct |
5 ms |
4216 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
6 |
Correct |
5 ms |
4216 KB |
Output is correct |
7 |
Correct |
6 ms |
4216 KB |
Output is correct |
8 |
Correct |
6 ms |
4216 KB |
Output is correct |
9 |
Correct |
6 ms |
4216 KB |
Output is correct |
10 |
Correct |
6 ms |
4216 KB |
Output is correct |
11 |
Correct |
15 ms |
4600 KB |
Output is correct |
12 |
Correct |
17 ms |
4604 KB |
Output is correct |
13 |
Correct |
18 ms |
5028 KB |
Output is correct |
14 |
Correct |
15 ms |
4472 KB |
Output is correct |
15 |
Correct |
26 ms |
6012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4216 KB |
Output is correct |
2 |
Correct |
6 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
6 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Correct |
6 ms |
4220 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
6 |
Correct |
6 ms |
4216 KB |
Output is correct |
7 |
Correct |
6 ms |
4220 KB |
Output is correct |
8 |
Correct |
6 ms |
4216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
6 ms |
4220 KB |
Output is correct |
3 |
Correct |
6 ms |
4216 KB |
Output is correct |
4 |
Correct |
6 ms |
4216 KB |
Output is correct |
5 |
Correct |
6 ms |
4216 KB |
Output is correct |
6 |
Correct |
5 ms |
4216 KB |
Output is correct |
7 |
Correct |
5 ms |
4216 KB |
Output is correct |
8 |
Correct |
5 ms |
4220 KB |
Output is correct |
9 |
Correct |
58 ms |
8168 KB |
Output is correct |
10 |
Correct |
45 ms |
7540 KB |
Output is correct |
11 |
Correct |
20 ms |
5496 KB |
Output is correct |
12 |
Correct |
23 ms |
5752 KB |
Output is correct |
13 |
Correct |
9 ms |
4600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Correct |
5 ms |
4216 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
6 |
Correct |
5 ms |
4216 KB |
Output is correct |
7 |
Correct |
5 ms |
4216 KB |
Output is correct |
8 |
Correct |
5 ms |
4216 KB |
Output is correct |
9 |
Correct |
56 ms |
8264 KB |
Output is correct |
10 |
Correct |
44 ms |
7476 KB |
Output is correct |
11 |
Correct |
19 ms |
5544 KB |
Output is correct |
12 |
Correct |
23 ms |
5752 KB |
Output is correct |
13 |
Correct |
9 ms |
4600 KB |
Output is correct |
14 |
Correct |
72 ms |
8812 KB |
Output is correct |
15 |
Correct |
83 ms |
10352 KB |
Output is correct |
16 |
Correct |
17 ms |
5368 KB |
Output is correct |
17 |
Correct |
88 ms |
8436 KB |
Output is correct |
18 |
Correct |
31 ms |
6516 KB |
Output is correct |