#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
int valid(int n, int inputSeq[]){
bool ss = 0;
int ind = 0 , val = 0;
map<int,int> mp;
for(int i = 0;i<n;i++){
if(inputSeq[i]<=n){
ss = 1;
val = inputSeq[i]-1;
ind = i;
}else{
if(mp[inputSeq[i]]==1){
return 0;
}mp[inputSeq[i]] = 1;
}
}
if(!ss)return 1;
for(int i = ind+1;i<n;i++){
val++;
val%=n;
if(inputSeq[i]<=n){
if((inputSeq[i]-1)!=val)return 0;
}
}
for(int i = 0;i<ind;i++){
val++;val%=n;
if(inputSeq[i]<=n){
if((inputSeq[i]-1)!=val)return 0;
}
}
return 1;
}int replacement(int n, int gondolaSeq[], int replacementSeq[]){
vector<pair<int,int>> v;
int ind = 0, val = 0;bool ss = 0;
for(int i = 0;i<n;i++){
if(gondolaSeq[i]<=n){
ind = i;val = gondolaSeq[i]-1;
ss = 1;
}
}
int lol[n];
if(ss){
lol[ind] = val;
for(int i = ind+1;i<n;i++){
val++;
val%=n;
lol[i] = val;
}
for(int i = 0;i<ind;i++){
val++;val%=n;
lol[i] = val;
}
}
for(int i = 0;i<n;i++){
if(gondolaSeq[i]>n){
v.push_back({gondolaSeq[i],(ss==0?i+1:lol[i]+1)});
}
}
sort(v.begin(),v.end());
int l = n+1;
int st =0;
for(int i = 0;i<v.size();i++){
replacementSeq[st++] = v[i].second;
++l;
while(l<=v[i].first){
replacementSeq[st++] = l-1;
l++;
}
}
return st;
}
long long mod = 1000000009;
//Fast
long long fast(long long a,long long b){
if(b==0)return 1;
if(b==1)return a%mod;
long long ha = fast(a,b/2)%mod;
if(b%2==0)return (ha*ha)%mod;
else return (((ha*ha)%mod)*a)%mod;
}
int countReplacement(int n, int inputSeq[]){
bool ss = 0;
int ind = 0 , val = 0;
map<int,int> mp;
for(int i = 0;i<n;i++){
if(inputSeq[i]<=n){
ss = 1;
val = inputSeq[i]-1;
ind = i;
}else{
if(mp[inputSeq[i]]==1){
return 0;
}mp[inputSeq[i]] = 1;
}
}
long long mul = 1;
if(!ss){
mul = n;
}
for(int i = ind+1;i<n;i++){
val++;
val%=n;
if(inputSeq[i]<=n){
if((inputSeq[i]-1)!=val)return 0;
}
}
for(int i = 0;i<ind;i++){
val++;val%=n;
if(inputSeq[i]<=n){
if((inputSeq[i]-1)!=val)return 0;
}
}
vector<long long> v;
for(int i = 0;i<n;i++){
if(inputSeq[i]>n){
v.push_back(inputSeq[i]);
}
}
sort(v.begin(),v.end());
long long all = mul;
long long la = n+1;
for(int i = 0;i<v.size();i++){
long long diff = v[i]-la , rem = v.size()-i;
all*=fast(rem,diff);all%=mod;
la = v[i]+1;
}
return all;
}
/*
int main(){
int S[] = {3,4};
cout<<countReplacement(2,S);
}*/
Compilation message
gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:67:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i = 0;i<v.size();i++){
| ~^~~~~~~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:127:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
127 | for(int i = 0;i<v.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
6 ms |
596 KB |
Output is correct |
8 |
Correct |
6 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
7 ms |
608 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 |
3 ms |
416 KB |
Output is correct |
7 |
Correct |
6 ms |
596 KB |
Output is correct |
8 |
Correct |
5 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
7 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
13 ms |
2044 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
12 ms |
1620 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
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 |
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 |
0 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 |
11 |
Correct |
6 ms |
868 KB |
Output is correct |
12 |
Correct |
8 ms |
980 KB |
Output is correct |
13 |
Correct |
10 ms |
1380 KB |
Output is correct |
14 |
Correct |
6 ms |
852 KB |
Output is correct |
15 |
Correct |
16 ms |
2260 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 |
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 |
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 |
224 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 |
308 KB |
Output is correct |
9 |
Correct |
29 ms |
4380 KB |
Output is correct |
10 |
Correct |
26 ms |
3584 KB |
Output is correct |
11 |
Correct |
10 ms |
1880 KB |
Output is correct |
12 |
Correct |
12 ms |
2220 KB |
Output is correct |
13 |
Correct |
3 ms |
724 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
29 ms |
4396 KB |
Output is correct |
10 |
Correct |
21 ms |
3500 KB |
Output is correct |
11 |
Correct |
10 ms |
1928 KB |
Output is correct |
12 |
Correct |
12 ms |
2140 KB |
Output is correct |
13 |
Correct |
3 ms |
752 KB |
Output is correct |
14 |
Correct |
42 ms |
6496 KB |
Output is correct |
15 |
Correct |
51 ms |
7088 KB |
Output is correct |
16 |
Correct |
8 ms |
1728 KB |
Output is correct |
17 |
Correct |
31 ms |
4780 KB |
Output is correct |
18 |
Correct |
17 ms |
3120 KB |
Output is correct |