#include<bits/stdc++.h>
#include<gondola.h>
using namespace std;
const int inf=1e9+9;
const int mod=1e9+9;
vector<int> reorder(int n, int s[]){
int small=inf; int ind=-1;
for(int i=0; i<n; i++){
if(s[i]<small){
small=s[i];
ind=i;
}
}
if(n<small){
vector<int> ans(n);
for(int i=0; i<n; i++){
ans[i]=s[i];
}
return ans;
}
int start=(n+ind-(small-1))%n;
vector<int> ans(n);
for(int i=0; i<n; i++){
ans[i]=s[(start+i)%n];
}
return ans;
}
int valid(int n, int inputSeq[]){
vector<int> s=reorder(n, inputSeq);
set<int> used;
for(int i=0; i<n; i++){
if(s[i]!=i+1){
if(s[i]<=n || used.find(s[i])!=used.end()){
return false;
}
used.insert(s[i]);
}
}
return true;
}
int maximum(vector<int>& s){
int ans=-1;
for(auto e: s){
ans=max(ans, e);
}
return ans;
}
int replacement(int n, int gondolaSeq[], int ans[]){
vector<int> s=reorder(n, gondolaSeq);
int size=maximum(s)-n;
for(int i=0; i<size; i++){
ans[i]=-1;
}
set<int> usable;
for(int i=0; i<n; i++){
if(s[i]>n){
ans[s[i]-n-1]=i;
usable.insert(i);
}
}
iota(s.begin(), s.end(), 1);
for(int i=0; i<size; i++){
if(ans[i]==-1){
int ind=*usable.begin();
ans[i]=s[ind];
s[ind]=n+i+1;
}
else{
int ind=ans[i];
usable.erase(ind);
ans[i]=s[ind];
s[ind]=n+i+1;
}
}
return size;
}
int minimum(vector<int>& s){
int ans=inf;
for(auto e: s){
ans=min(ans, e);
}
return ans;
}
long long fastpow(int a, int e){
if(e==0) return 1;
long long ans=fastpow(a, e/2);
ans=(ans*ans)%mod;
if(e%2==1){
ans*=a;
ans%=mod;
}
return ans;
}
int countReplacement(int n, int inputSeq[]){
if(!valid(n, inputSeq)) return 0;
vector<int> s=reorder(n, inputSeq);
long long ans=1ll;
int usable=0;
vector<int> b;
for(int i=0; i<n; i++){
if(s[i]>n){
usable++;
b.push_back(s[i]);
}
}
sort(b.begin(), b.end());
int l=n+1;
for(auto e: b){
ans*=fastpow(usable, e-l);
ans%=mod;
l=e+1;
usable--;
}
if(minimum(s)>n){
ans*=n;
ans%=mod;
}
return ans;
}
/*signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int s[n];
for(int i=0; i<n; i++){
cin >> s[i];
}
cout << valid(n, s) << "\n";
int rep[n*3];
int size=replacement(n, s, rep);
cout << size << "\n";
for(auto e: rep){
cout << e << " ";
}
cout << "\n";
cout << countReplacement(n, s) << "\n";
}*/
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
4 ms |
592 KB |
Output is correct |
7 |
Correct |
8 ms |
964 KB |
Output is correct |
8 |
Correct |
7 ms |
872 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
8 ms |
852 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 |
4 ms |
596 KB |
Output is correct |
7 |
Correct |
8 ms |
980 KB |
Output is correct |
8 |
Correct |
8 ms |
816 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
8 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 |
3 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 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
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 |
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 |
8 ms |
936 KB |
Output is correct |
12 |
Correct |
8 ms |
980 KB |
Output is correct |
13 |
Correct |
21 ms |
2536 KB |
Output is correct |
14 |
Correct |
7 ms |
836 KB |
Output is correct |
15 |
Correct |
24 ms |
2916 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 |
224 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 |
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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
35 ms |
3580 KB |
Output is correct |
10 |
Correct |
24 ms |
2772 KB |
Output is correct |
11 |
Correct |
10 ms |
1432 KB |
Output is correct |
12 |
Correct |
14 ms |
1748 KB |
Output is correct |
13 |
Correct |
3 ms |
596 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
35 ms |
3520 KB |
Output is correct |
10 |
Correct |
24 ms |
2716 KB |
Output is correct |
11 |
Correct |
10 ms |
1492 KB |
Output is correct |
12 |
Correct |
13 ms |
1748 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
45 ms |
4812 KB |
Output is correct |
15 |
Correct |
51 ms |
5432 KB |
Output is correct |
16 |
Correct |
9 ms |
1152 KB |
Output is correct |
17 |
Correct |
33 ms |
3728 KB |
Output is correct |
18 |
Correct |
18 ms |
2260 KB |
Output is correct |