# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1017819 | simona1230 | Gondola (IOI14_gondola) | C++17 | 53 ms | 6700 KiB |
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 "gondola.h"
#include <bits/stdc++.h>
using namespace std;
const long long mod=1e9+9;
long long mult(long long x,long long p)
{
if(p==0)return 1;
if(p==1)return x;
long long half=mult(x,p/2);
if(p%2==0)return (half*half)%mod;
return (((half*half)%mod)*x)%mod;
}
int n;
long long a[100001];
map<long long,long long> mp;
bool check()
{
mp.clear();
long long idx=-1;
for(int i=0; i<n; i++)
{
if(mp[a[i]])return 0;
mp[a[i]]++;
if(a[i]<=n)
{
idx=i;
}
}
if(idx==-1)return 1;
long long curr=a[idx];
for(int i=idx; i<n; i++)
{
if(a[i]<=n&&a[i]!=curr)return 0;
curr++;
if(curr==n+1)curr=1;
}
for(int i=0; i<idx; i++)
{
if(a[i]<=n&&a[i]!=curr)return 0;
curr++;
if(curr==n+1)curr=1;
}
return 1;
}
int valid(int N, int inputSeq[])
{
n=N;
for(int i=0; i<n; i++)
a[i]=inputSeq[i];
//cout<<check()<<endl;
return check();
}
//----------------------
int req[100001];
void needed()
{
int idx=-1;
for(int i=0; i<n; i++)
{
if(a[i]<=n)
{
idx=i;
}
}
if(idx==-1)
{
for(int i=0; i<n; i++)
req[i]=i+1;
return;
}
int curr=a[idx];
for(int i=idx; i<n; i++)
{
req[i]=curr;
curr++;
if(curr==n+1)curr=1;
}
for(int i=0; i<idx; i++)
{
req[i]=curr;
curr++;
if(curr==n+1)curr=1;
}
}
bool cmp(int i,int j)
{
return a[i]<a[j];
}
int replacement(int N, int gondolaSeq[], int replacementSeq[])
{
n=N;
for(int i=0; i<n; i++)
a[i]=gondolaSeq[i];
needed();
vector<int> v;
for(int i=0; i<n; i++)
{
if(a[i]!=req[i])v.push_back(i);
}
sort(v.begin(),v.end(),cmp);
/*for(int i=0;i<n;i++)
cout<<req[i]<<" ";
cout<<endl;*/
int all=n+1;
int cnt=0;
for(int i=0; i<v.size(); i++)
{
int idx=v[i];
//cout<<req[idx]<<endl;
while(req[idx]!=a[idx])
{
replacementSeq[cnt++]=req[idx];
req[idx]=all++;
}
}
return cnt;
}
//----------------------
int countReplacement(int N, int inputSeq[])
{
n=N;
for(int i=0; i<n; i++)
a[i]=inputSeq[i];
if(check()==0)return 0;
needed();
vector<long long> v;
mp.clear();
long long maxx=n+1;
for(int i=0; i<n; i++)
{
mp[a[i]]=i;
maxx=min(maxx,a[i]);
if(a[i]!=req[i])v.push_back(i);
if(req[i]==n)mp[n]=i;
}
sort(v.begin(),v.end(),cmp);
long long ans=1;
long long last=n;
for(int i=0; i<v.size(); i++)
{
long long pos=v.size()-i;
long long cnt=a[v[i]]-last-1;
//cout<<last<<" "<<a[v[i]]<<" "<<pos<<" "<<cnt<<endl;
ans=(ans*mult(pos,cnt))%mod;
last=a[v[i]];
}
if(maxx>n)
{
for(int i=1; i<n; i++)
{
ans=(ans*i)%mod;
}
}
return (int)ans;
}
Compilation message (stderr)
# | 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... |