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>
#define int long long
#define rep(i,n) for(int i = 0; i < ((int) n); i++)
#define all(a) a.begin(), a.end()
using namespace std;
int N =1e5+1;
signed valid(signed n, signed inputSeq[])
{
int minn = 251000, mini=-1;
rep(i,n) if((int)inputSeq[i]<minn) {minn=inputSeq[i]; mini=i;}
vector<int>x(n);
rep(i,n) x[(i+minn-1+n)%n]=inputSeq[(i+mini)%n];
rep(i,n) if(x[i]!=i+1&&x[i]<=n) return 0;
vector<int>doub;
rep(i,n) if(x[i]>n) doub.push_back(x[i]);
sort(all(doub));
rep(i,doub.size()-1) if(doub[i]==doub[i+1]) return 0;
return 1;
}
//----------------------
signed replacement(signed n, signed gondseq[], signed replacementSeq[])
{
int minn = 251000, mini=-1;
rep(i,n) if((int)gondseq[i]<minn) {minn=gondseq[i]; mini=i;}
vector<int>x(n);
rep(i,n) x[(i+minn-1+n)%n]=gondseq[(i+mini)%n];
int cur = 0;
int maxx = n;
rep(i,n) maxx=max(maxx,x[i]);
map<int,int>mp;
rep(i,n) if(x[i]>n) mp[x[i]]=i;
auto it = mp.end();
if(mp.size()) it--;
vector<int>out;
vector<int>help(n);
rep(i,n) help[i]=i+1;
for(int i = n+1; i <= maxx; i++)
{
if(mp.count(i)) {out.push_back(help[mp[i]]); help[mp[i]]=i;}
else {out.push_back(help[(*it).second]); help[(*it).second]=i;}
}
rep(i,maxx-n) replacementSeq[i]=out[i];
return maxx-n;
}
//----------------------
const int MOD = 1e9+9;
int fastpow(int a, int p)
{
if(p==0) return 1;
int res = fastpow(a,p/2);
res=(res*res)%MOD;
if(p&1) res=(res*a)%MOD;
return res;
}
signed countReplacement(signed n, signed inputSeq[])
{
int minn = 251000, mini=-1;
rep(i,n) if((int)inputSeq[i]<minn) {minn=inputSeq[i]; mini=i;}
vector<int>x(n);
rep(i,n) x[(i+minn-1+n)%n]=inputSeq[(i+mini)%n];
if(!valid(n, inputSeq)) return 0;
bool notbroke=true;
rep(i,n) notbroke&=x[i]<=n;
if(notbroke) return 1;
vector<int>aid;
rep(i,n) if(x[i]>n) aid.push_back(x[i]);
sort(all(aid));
int res=1;
int last = n;
rep(i, aid.size())
{
res=(res*fastpow(aid.size()-i, aid[i]-last-1));
last=aid[i];
}
return res;
}
Compilation message (stderr)
gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:34:7: warning: unused variable 'cur' [-Wunused-variable]
34 | int cur = 0;
| ^~~
# | 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... |