#include "gondola.h"
#include <bits/stdc++.h>
#define int long long
using namespace std;
int valid(int n, int inputSeq[]){
int index = 0;
int num = n;
map< int, bool> visited;
for(int i = 0; i < n; i++){
if(visited[inputSeq[i]]) return 0;
visited[inputSeq[i]] = true;
if(inputSeq[i] <= num){
num = inputSeq[i];
index = i;
}
}
int start = (index-num+1+n)%n;
int curr = 1;
for(int i = start; i < n; i++){
if(inputSeq[i] <= n && inputSeq[i] != curr) return 0;
curr++;
}
for(int j = 0; j < start; j++){
if(inputSeq[j] <= n && inputSeq[j] != curr) return 0;
curr++;
}
return 1;
}
int replacement(int n, int gondolaSeq[], int replacementSeq[]){
int index = 0;
int num = n;
int maxi = 0;
for(int i = 0; i < n; i++){
if(gondolaSeq[i] <= num){
num = gondolaSeq[i];
index = i;
}
maxi = max(maxi, gondolaSeq[i]);
}
int start = (index-num+1+n)%n;
if(gondolaSeq[index] > n) start = 0;
int curr = 1;
map<int, int> mape;
vector< int > current(n+1);
for(int i = 1; i <= n; i++){
current[i] = i;
}
int in;
for(int i = start; i < n; i++){
mape[gondolaSeq[i]] = curr;
if(gondolaSeq[i] == maxi) in = curr;
curr++;
}
for(int j = 0; j < start; j++){
mape[gondolaSeq[j]] = curr;
if(gondolaSeq[j] == maxi) in = curr;
curr++;
}
int c = 0;
for(int i = n+1; i <= maxi; i++){
if(mape[i] != 0){
replacementSeq[c] = current[mape[i]];
current[mape[i]] = i;
}else{
replacementSeq[c] = current[in];
current[in] = i;
}
c++;
}
return c;
}
int last;
vector< int > memo;
map<int, bool> visited;
vector< int > ve;
vector< int > cnt;
const int mod = 1e9+9;
int dp(int curr, int n){
if(curr >= last){
return 1;
}
if(memo[curr] != -1) return memo[curr];
memo[curr] = 0;
if(visited[curr]){
return memo[curr] = dp(curr+1,n);
}
int index = lower_bound(cnt.begin(), cnt.end(), curr)-cnt.begin();
int nb = n-index-1+cnt[index]-curr;
nb = nb%mod;
// cout << curr << ' ' << nb << endl;
/*
for(int i = 0; i < n; i++){
if(ve[i] > curr){
memo[curr] += dp(curr+1,n);
memo[curr] %= mod;
}
}
*/
return memo[curr] = (nb*dp(curr+1,n))%mod;
}
int countReplacement(int n, int inputSeq[]){
if(!valid(n,inputSeq)) return 0;
ve.resize(n);
cnt.resize(n);
for(int i = 0; i < n; i++){
last = max(last, inputSeq[i]);
ve[i] = inputSeq[i];
cnt[i] = inputSeq[i];
visited[inputSeq[i]] = true;
}
sort(cnt.begin(), cnt.end());
memo.resize(last+1,-1);
return dp(n+1, n);
}
/*
int main(){
int n;
cin >> n;
int v[n];
int maxi = 0;
for(int i = 0; i < n; i++){
cin >> v[i];
maxi = max(maxi, v[i]);
}
int ans[maxi-n];
cout << replacement(n,v,ans) << endl;
for(int a : ans){
cout << a << ' ';
}
cout << endl;
cout << "NUMBER " << countReplacement(n,v) << endl;
}
*/
Compilation message
gondola.cpp: In function 'long long int replacement(long long int, long long int*, long long int*)':
gondola.cpp:87:43: warning: 'in' may be used uninitialized in this function [-Wmaybe-uninitialized]
replacementSeq[c] = current[in];
^
/tmp/cc5793v0.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