#include<algorithm>
#include<vector>
#include<utility>
#include<iostream>
#include"gondola.h"
#define ll long long
using namespace std;
const ll mod=1e9+9;
int valid(int n, int inp[])
{
vector<int> vec;
for(int i=0;i<n;i++) vec.push_back(inp[i]);
sort(vec.begin(),vec.end());
if(unique(vec.begin(),vec.end())-vec.begin()!=n) return 0;
int first=-1;
for(int i=0;i<n;i++){
if(inp[i]<=n){
first=i;
break;
}
}
if(first==-1) return 1;
bool flag=1;
for(int i=0;i<n;i++){
if(inp[i]!=(inp[first]+i-first-1+n)%n+1&&inp[i]<=n){
flag=0;
break;
}
}
return flag;
}
//----------------------
int replacement(int n, int inp[], int ans[])
{
int first=-1,fnum;
vector<pair<int,int> > repl;
for(int i=0;i<n;i++){
if(inp[i]<=n){
first=i;
fnum=inp[i];
}
else repl.push_back(make_pair(inp[i],i));
}
if(first==-1) first=0,fnum=1;
sort(repl.begin(),repl.end());
if(repl.size()==0) return 0;
int cnt=0,last=n+1;
for(auto x:repl){
ans[cnt++]=(fnum+x.second-first-1+n)%n+1;
for(int i=last;i<x.first;i++) ans[cnt++]=i;
last=x.first+1;
}
return cnt;
}
//----------------------
ll power(ll a,ll b){
if(b==0) return 1;
if(b&1) return power(a,b-1)*a%mod;
ll tmp=power(a,b/2);
return tmp*tmp%mod;
}
int countReplacement(int n, int inp[])
{
if(valid(n,inp)==0) return 0;
ll ans=1;
int first=-1,fnum;
vector<pair<int,int> > repl;
for(int i=0;i<n;i++){
if(inp[i]<=n){
first=i;
fnum=inp[i];
}
else repl.push_back(make_pair(inp[i],i));
}
sort(repl.begin(),repl.end());
if(repl.size()==0) return 1;
ll last=n,cnt=repl.size();
for(auto x:repl){
ans=ans*power(cnt--,x.first-last-1)%mod;
//cout<<ans<<" "<<cnt+1<<" "<<x.first-last-1<<"\n";
last=x.first;
}
if(first==-1) ans=ans*(ll)n%mod;
return ans;
}
Compilation message
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:71:18: warning: variable 'fnum' set but not used [-Wunused-but-set-variable]
71 | int first=-1,fnum;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
6 ms |
1112 KB |
Output is correct |
7 |
Correct |
13 ms |
1752 KB |
Output is correct |
8 |
Correct |
10 ms |
1748 KB |
Output is correct |
9 |
Correct |
4 ms |
856 KB |
Output is correct |
10 |
Correct |
14 ms |
1748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
6 ms |
1112 KB |
Output is correct |
7 |
Correct |
13 ms |
2004 KB |
Output is correct |
8 |
Correct |
10 ms |
1752 KB |
Output is correct |
9 |
Correct |
4 ms |
856 KB |
Output is correct |
10 |
Correct |
13 ms |
1748 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
6 ms |
1112 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
13 ms |
1748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
356 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
9 ms |
1112 KB |
Output is correct |
12 |
Correct |
8 ms |
1116 KB |
Output is correct |
13 |
Correct |
11 ms |
1512 KB |
Output is correct |
14 |
Correct |
7 ms |
1112 KB |
Output is correct |
15 |
Correct |
18 ms |
2376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
17 ms |
1972 KB |
Output is correct |
10 |
Correct |
13 ms |
1768 KB |
Output is correct |
11 |
Correct |
6 ms |
1028 KB |
Output is correct |
12 |
Correct |
7 ms |
1008 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
600 KB |
Output is correct |
9 |
Correct |
17 ms |
1972 KB |
Output is correct |
10 |
Correct |
13 ms |
1768 KB |
Output is correct |
11 |
Correct |
6 ms |
1028 KB |
Output is correct |
12 |
Correct |
7 ms |
1008 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
22 ms |
2948 KB |
Output is correct |
15 |
Correct |
25 ms |
3168 KB |
Output is correct |
16 |
Correct |
5 ms |
1044 KB |
Output is correct |
17 |
Correct |
17 ms |
2012 KB |
Output is correct |
18 |
Correct |
9 ms |
1612 KB |
Output is correct |