//#include "gondola.h"
#include <bits/stdc++.h>
#define MOD 1000000009
using namespace std;
struct Gondola{
int id, val;
Gondola(int _id, int _val){
id = _id;
val = _val;
}
bool operator<(const Gondola& rhs) const{
return val < rhs.val;
}
};
int ord[100001];
map<int, int> in;
vector<Gondola> v;
int valid(int n, int inputSeq[]){
int i;
for(i = 0; i < n; i++){
if(in[inputSeq[i]]) return 0;
in[inputSeq[i]] = 1;
}
for(i = 0; i < n; i++){
if(inputSeq[i] <= n) break;
}
if(i == n) return 1;
int k = inputSeq[i] - 1;
for(int j = 0; j < n; j++){
ord[(k + j) % n] = inputSeq[(i + j) % n];
if((ord[(k + j) % n] != (k + j) % n + 1 && ord[(k + j) % n] <= n) || ord[(k + j) % n] < 1) return 0;
}
for(i = 0; i < n; i++){
if((ord[i] != i + 1 && ord[i] <= n) || ord[i] < 1) return 0;
}
return 1;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[]){
int i;
for(i = 0; i < n; i++){
if(gondolaSeq[i] <= n) break;
}
int k;
if(i == n){
i = 0;
k = 0;
} else {
k = gondolaSeq[i] - 1;
}
for(int j = 0; j < n; j++){
v.push_back(Gondola((k + j) % n + 1, gondolaSeq[(i + j) % n]));
}
sort(v.begin(), v.end());
i = 0;
int s = n + 1;
for(auto x : v){
if(x.val <= n) continue;
replacementSeq[i] = x.id;
i++;
for(; s < x.val; s++){
replacementSeq[i] = s;
i++;
}
s = x.val + 1;
}
return i;
}
//----------------------
long long pow(long long x, int p){
long long s = 1;
while(p > 0){
if(p % 2 == 1){
s *= x;
s %= MOD;
}
x *= x;
x %= MOD;
p /= 2;
}
return s;
}
int countReplacement(int n, int gondolaSeq[]){
int i;
for(i = 0; i < n; i++){
if(gondolaSeq[i] <= n) break;
}
bool in = 0;
int k;
if(i == n){
i = 0;
k = 0;
in = 1;
} else {
k = gondolaSeq[i] - 1;
}
for(int j = 0; j < n; j++){
v.push_back(Gondola((k + j) % n + 1, gondolaSeq[(i + j) % n]));
}
sort(v.begin(), v.end());
i = 0;
long long ans = 1;
int mx = n + 1, mlt = n + 1;
for(auto x : v){
mlt--;
if(x.val < mx) continue;
while(mx < x.val){
ans = ans * mlt % MOD;
mx++;
}
mx = x.val + 1;
}
if(in){
ans = ans * n % MOD;
return ans;
}
return (int) ans;
}
int main(){
int n;
cin>>n;
int a[1000], ans[1000];
for(int i = 0; i < n; i++){
cin>>a[i];
}
cout<< countReplacement(n, a);
}
Compilation message
gondola.cpp: In function 'int main()':
gondola.cpp:133:15: warning: unused variable 'ans' [-Wunused-variable]
int a[1000], ans[1000];
^~~
/tmp/ccd4gZnk.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/cce79hgt.o:gondola.cpp:(.text.startup+0x0): first defined here
/tmp/ccd4gZnk.o: In function `main':
grader.cpp:(.text.startup+0xc3): undefined reference to `countReplacement'
grader.cpp:(.text.startup+0xe2): undefined reference to `valid'
grader.cpp:(.text.startup+0x106): undefined reference to `replacement'
collect2: error: ld returned 1 exit status