This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho
#include "gondola.h"
using ll=long long;
using ld=long double;
int const INF=1000000005;
ll const LINF=1000000000000000005;
ll const mod=1000000009;
ld const PI=3.14159265359;
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define CODE_START ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
int valid(int n,int a[]){
int mn=INF,pos=-1;
map<ll,ll>viz;
for(int i=0;i<n;i++)
{
viz[a[i]]++;
if(a[i]<mn){
mn=a[i];
pos=i;
}
}
for(ll i=0;i<n;i++)
{
if(viz[a[i]]>=2){
return 0;
}
}
if(mn>n){
return 1;
}
int b[200005];
for(int i=0;i<n;i++)
{
b[i]=a[i];
}
a[0]=b[pos];
ll val=0;
for(ll i=pos+1;i<n;i++)
{
a[i-pos]=b[i];
val=i-pos;
}
val++;
for(ll i=0;i<pos;i++)
{
a[val]=b[i];
val++;
}
ll x=a[0];
x++;
b[0]=a[0];
for(ll i=1;i<n;i++)
{
x%=n;
if(x==0){
x=n;
}
b[i]=x;
x++;
}
for(ll i=1;i<n;i++)
{
if(b[i]!=a[i]){
if(a[i]<=n){
return 0;
}
}
}
return 1;
}
int replacement(int n,int a[],int g[]){
int mn=INF,pos=-1;
for(int i=0;i<n;i++)
{
if(a[i]<mn){
mn=a[i];
pos=i;
}
}
int b[200005],val;
for(int i=0;i<n;i++)
{
b[i]=a[i];
}
if(mn>n){
goto Next;
}
a[0]=b[pos];
val=0;
for(ll i=pos+1;i<n;i++)
{
a[i-pos]=b[i];
val=i-pos;
}
val++;
for(ll i=0;i<pos;i++)
{
a[val]=b[i];
val++;
}
for(int i=0;i<n;i++)
{
b[i]=a[i];
}
val=mn;
for(ll i=0;i<n;i++)
{
ll x=i-mn+1;
if(x<0){
x+=n;
}
a[i]=b[x];
}
Next:
int mx=0;
ll check=0;
for(ll i=0;i<n;i++)
{
mx=max(mx,a[i]);
}
pos=0;
for(ll i=0;i<n;i++)
{
if(mx==a[i]){
pos=i+1;
}
}
map<ll,ll>viz;
for(ll i=0;i<n;i++)
{
viz[a[i]]=i+1;
}
for(ll i=n+1;i<=mx;i++)
{
if(viz[i]==0||i==mx){
g[i-n-1]=pos;
pos=i;
}else {
g[i-n-1]=viz[i];
}
}
return mx-n;
}
ll pw(ll a,ll b){
if(b==0) return 1;
ll n=pw(a,b/2);
if(b%2==1){
return (((n*n)%mod)*a)%mod;
}else return (n*n)%mod;
}
int countReplacement(int n,int a[]){
if(valid(n,a)==0){
return 0;
}
int mn=INF,pos=-1;
for(int i=0;i<n;i++)
{
if(a[i]<mn){
mn=a[i];
pos=i;
}
}
int b[200005],val;
for(int i=0;i<n;i++)
{
b[i]=a[i];
}
if(mn>n){
goto Next;
}
a[0]=b[pos];
val=0;
for(ll i=pos+1;i<n;i++)
{
a[i-pos]=b[i];
val=i-pos;
}
val++;
for(ll i=0;i<pos;i++)
{
a[val]=b[i];
val++;
}
for(int i=0;i<n;i++)
{
b[i]=a[i];
}
val=mn;
for(ll i=0;i<n;i++)
{
ll x=i-mn+1;
if(x<0){
x+=n;
}
a[i]=b[x];
}
Next:
vector<ll>v;
for(ll i=0;i<n;i++)
{
if(a[i]>n){
v.pb(a[i]);
}
}
sort(v.begin(),v.end());
ll last=n;
ll ans=1;
for(ll i=0;i<v.size();i++)
{
ans=(ans*pw((v.size()-i),(v[i]-1-last))%mod)%mod;
}
if(mn>n){
ans=(ans*n)%mod;
}
return ans;
}
/*
int32_t main(){
ifstream cin("input.in");
int n,a[100005],idk[100005];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<replacement(n,a,idk)<<endl;
}
*/
Compilation message (stderr)
gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:124:4: warning: unused variable 'check' [-Wunused-variable]
124 | ll check=0;
| ^~~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:218:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
218 | for(ll i=0;i<v.size();i++)
| ~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |