#include "gondola.h"
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef long long ll;
#define mp make_pair
const int maxl = 25e4 + 5;
const int mod = 1e9 + 9;
int valid(int n, int inputSeq[])
{
int root = -1;
map<int,int> cnt;
for(int i = 0 ; i < n ; ++i){
if(cnt[inputSeq[i]]++ == 1)return 0;
if(inputSeq[i] <= n){
root = i;
}
}
if(root == -1)return 1;
for(int j = (root + 1) % n , i = inputSeq[root] % n + 1 ; j != root ; j = (j + 1) % n , i = i % n + 1){
if(inputSeq[j] <= n && inputSeq[j] != i){
return 0;
}
}
return 1;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
assert(valid(n,gondolaSeq));
vector<ii> a;
for(int i = 0 ; i < n ; ++i){
if(gondolaSeq[i] > n)a.push_back(mp(gondolaSeq[i],i));
}
sort(a.begin(),a.end());
int now = 0;
int l = 0;
vector<int> avai , pos(maxl);
int root = -1;
for(int i = 0 ; i < n ; ++i){
if(gondolaSeq[i] <= n){
root = i;
break;
}
}
// printf("%d",root);
for(int i = (root == -1 ? 1 : gondolaSeq[root]) , j = (root == -1 ? 0 : root) , cnt = n ; cnt-- ; j = (j + 1) % n , i = i % n + 1){
pos[i] = j;
if(gondolaSeq[j] != i){
gondolaSeq[j] = i;
avai.push_back(i);
}
}
int res = 1;
for(int j = n + 1 ; now < a.size() ; ++j){
if(a[now].first == j){
// printf("%d\n", gondolaSeq[a[now].second]);
replacementSeq[l++] = gondolaSeq[a[now].second];
avai.erase(find(avai.begin(),avai.end(),gondolaSeq[a[now].second]));
// cerr << gondolaSeq[a[now].second] << endl;
++now;
}else{
replacementSeq[l++] = avai.back();
gondolaSeq[pos[avai.back()]] = j;
pos[j] = pos[avai.back()];
avai.back() = j;
res = (ll)res * avai.size() % mod;
}
}
for(int i = 1 ; i <= n ; ++i)res = res * i % mod;
return l;
}
//----------------------
int Pow(int x , int y){
if(y == 0)return 1;
int r = Pow(x,y/2);
if(y & 1)return (ll)r * r % mod * x % mod;
return (ll)r * r % mod;
}
int countReplacement(int n, int gondolaSeq[])
{
if(valid(n,gondolaSeq) == 0)return 0;
vector<ii> a(1,mp(n,1));
for(int i = 0 ; i < n ; ++i){
if(gondolaSeq[i] > n)a.push_back(mp(gondolaSeq[i],i));
}
sort(a.begin(),a.end());
int root = -1;
for(int i = 0 ; i < n ; ++i){
if(gondolaSeq[i] <= n){
root = i;
break;
}
}
int cnt = 0;
for(int i = (root == -1 ? 1 : gondolaSeq[root]) , j = (root == -1 ? 0 : root) , cnt1 = n ; cnt1-- ; j = (j + 1) % n , i = i % n + 1){
if(gondolaSeq[j] != i){
cnt++;
}
}
int res = (root == -1 ? n : 1);
for(int j = 1 ; j < a.size() ; j++){
res = (ll)res * Pow(cnt--,a[j].first - a[j - 1].first - 1) % mod;
}
return res;
}
Compilation message
gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:61:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = n + 1 ; now < a.size() ; ++j){
~~~~^~~~~~~~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:109:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 1 ; j < a.size() ; j++){
~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 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 |
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 |
13 ms |
2176 KB |
Output is correct |
7 |
Correct |
11 ms |
768 KB |
Output is correct |
8 |
Correct |
24 ms |
3968 KB |
Output is correct |
9 |
Correct |
8 ms |
1408 KB |
Output is correct |
10 |
Correct |
30 ms |
4608 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 |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
13 ms |
2176 KB |
Output is correct |
7 |
Correct |
11 ms |
640 KB |
Output is correct |
8 |
Correct |
24 ms |
3968 KB |
Output is correct |
9 |
Correct |
8 ms |
1536 KB |
Output is correct |
10 |
Correct |
30 ms |
4608 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
18 ms |
2176 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
49 ms |
4728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1408 KB |
Output is correct |
2 |
Correct |
2 ms |
1280 KB |
Output is correct |
3 |
Correct |
3 ms |
1280 KB |
Output is correct |
4 |
Correct |
2 ms |
1280 KB |
Output is correct |
5 |
Correct |
2 ms |
1280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1280 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
3 |
Correct |
3 ms |
1280 KB |
Output is correct |
4 |
Correct |
3 ms |
1380 KB |
Output is correct |
5 |
Correct |
3 ms |
1280 KB |
Output is correct |
6 |
Correct |
3 ms |
1280 KB |
Output is correct |
7 |
Correct |
3 ms |
1408 KB |
Output is correct |
8 |
Correct |
3 ms |
1408 KB |
Output is correct |
9 |
Correct |
3 ms |
1408 KB |
Output is correct |
10 |
Correct |
3 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1280 KB |
Output is correct |
2 |
Correct |
2 ms |
1280 KB |
Output is correct |
3 |
Correct |
2 ms |
1408 KB |
Output is correct |
4 |
Correct |
3 ms |
1408 KB |
Output is correct |
5 |
Correct |
2 ms |
1280 KB |
Output is correct |
6 |
Correct |
3 ms |
1280 KB |
Output is correct |
7 |
Correct |
3 ms |
1408 KB |
Output is correct |
8 |
Correct |
3 ms |
1408 KB |
Output is correct |
9 |
Correct |
3 ms |
1408 KB |
Output is correct |
10 |
Correct |
2 ms |
1408 KB |
Output is correct |
11 |
Correct |
30 ms |
4352 KB |
Output is correct |
12 |
Correct |
33 ms |
4856 KB |
Output is correct |
13 |
Correct |
113 ms |
2200 KB |
Output is correct |
14 |
Correct |
30 ms |
4216 KB |
Output is correct |
15 |
Correct |
51 ms |
2400 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
4 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 |
2 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 |
256 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 |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
50 ms |
4088 KB |
Output is correct |
10 |
Correct |
40 ms |
3456 KB |
Output is correct |
11 |
Correct |
13 ms |
1408 KB |
Output is correct |
12 |
Correct |
17 ms |
1792 KB |
Output is correct |
13 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 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 |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
49 ms |
4088 KB |
Output is correct |
10 |
Correct |
35 ms |
3456 KB |
Output is correct |
11 |
Correct |
14 ms |
1536 KB |
Output is correct |
12 |
Correct |
17 ms |
1792 KB |
Output is correct |
13 |
Correct |
5 ms |
640 KB |
Output is correct |
14 |
Correct |
62 ms |
4600 KB |
Output is correct |
15 |
Correct |
77 ms |
6052 KB |
Output is correct |
16 |
Correct |
12 ms |
1408 KB |
Output is correct |
17 |
Correct |
43 ms |
4216 KB |
Output is correct |
18 |
Correct |
37 ms |
2552 KB |
Output is correct |