#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include "gondola.h"
#define MAXN 300010
#define MOD 1000000009
using namespace std;
bool used[MAXN];
int valid(long long n, long long inputSeq[])
{
for(long long i=0;i<n;i++)
{
if(used[inputSeq[i]])return 0;
used[inputSeq[i]]=1;
if(inputSeq[i]<=n)
{
long long prev=i-1,valp=inputSeq[i]-1;
if(prev<0)prev=n-1;
if(valp<1)valp=n;
long long next=i+1,valn=inputSeq[i]+1;
if(next>=n)next=0;
if(valn>n)valn=1;
if(inputSeq[prev]<=n && inputSeq[prev]!=valp)return 0;
if(inputSeq[next]<=n && inputSeq[next]!=valn)return 0;
}
}
return 1;
}
//----------------------
vector<pair<long long,long long>> order;
int replacement(long long n, long long gondolaSeq[], long long replacementSeq[])
{
order.clear();
long long ans=0,pos=-1;
for(long long i=0;i<n;i++)if(gondolaSeq[i]<=n){pos=i;break;}
if(pos==-1)for(long long i=0;i<n;i++)order.push_back({gondolaSeq[i],i+1});
else
{
long long curr=gondolaSeq[pos];
for(long long i=pos-1;i>=0;i--)
{
curr--;
if(curr==0)curr=n;
if(gondolaSeq[i]>n)order.push_back({gondolaSeq[i],curr});
gondolaSeq[i]=curr;
}
for(long long i=n-1;i>pos;i--)
{
curr--;
if(curr==0)curr=n;
if(gondolaSeq[i]>n)order.push_back({gondolaSeq[i],curr});
gondolaSeq[i]=curr;
}
}
sort(order.begin(),order.end());
long long last=n;
for(long long i=0;i<order.size();i++)
{
long long ind=order[i].first;
long long frst=order[i].second;
replacementSeq[ans++]=frst;
for(long long j=last+2;j<=ind;j++)
{
replacementSeq[ans++]=j-1;
}
last=ind;
}
return ans;
}
//----------------------
unsigned long long power(unsigned long long x, long long n)
{
if(n==0)return 1;
if(n==1)return x;
unsigned long long half=power(x,n/2);
unsigned long long ne=(half*half)%MOD;
if(n&1){ne*=x;ne%=MOD;}
return ne%MOD;
}
int countReplacement(long long n, long long inputSeq[])
{
order.clear();
if(!valid(n,inputSeq))return 0;
long long pos=-1;
for(long long i=0;i<n;i++)if(inputSeq[i]<=n){pos=i;break;}
if(pos==-1)for(long long i=0;i<n;i++)order.push_back({inputSeq[i],i+1});
else
{
long long curr=inputSeq[pos];
for(long long i=pos-1;i>=0;i--)
{
curr--;
if(curr==0)curr=n;
if(inputSeq[i]>n)order.push_back({inputSeq[i],curr});
inputSeq[i]=curr;
}
for(long long i=n-1;i>pos;i--)
{
curr--;
if(curr==0)curr=n;
if(inputSeq[i]>n)order.push_back({inputSeq[i],curr});
inputSeq[i]=curr;
}
}
if(order.size()==0)return 1;
sort(order.begin(),order.end());
unsigned long long ans=1;
if(pos==-1)ans=n;
long long last=n;
for(long long i=0;i<order.size();i++)
{
long long ind=order[i].first;
long long frst=order[i].second;
unsigned long long use=power(order.size()-i,ind-last-1)%MOD;
ans*=use;
ans%=MOD;
last=ind;
}
return ans;
}
Compilation message
gondola.cpp: In function 'int replacement(long long int, long long int*, long long int*)':
gondola.cpp:62:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(long long i=0;i<order.size();i++)
| ~^~~~~~~~~~~~~
gondola.cpp: In function 'int countReplacement(long long int, long long int*)':
gondola.cpp:119:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | for(long long i=0;i<order.size();i++)
| ~^~~~~~~~~~~~~
gondola.cpp:122:19: warning: unused variable 'frst' [-Wunused-variable]
122 | long long frst=order[i].second;
| ^~~~
/usr/bin/ld: /tmp/ccXqJ8ZJ.o: in function `main':
grader.cpp:(.text.startup+0xb6): undefined reference to `valid'
/usr/bin/ld: grader.cpp:(.text.startup+0x108): undefined reference to `countReplacement'
/usr/bin/ld: grader.cpp:(.text.startup+0x132): undefined reference to `replacement'
collect2: error: ld returned 1 exit status