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;
typedef long long ll;
ll m = 1000000009;
int valid(int n, int inputSeq[]){
unordered_map<int,bool> seen;
int mnv = 250002;
int mnind = -1;
for (int i = 0; i<n; i++){
if (seen[inputSeq[i]]){
return 0;
}
seen[inputSeq[i]]=true;
if (mnv>inputSeq[i]){
mnv=inputSeq[i];
mnind=i;
}
}
if (mnv>n){
return 1;
}
mnv++;
for (int i = (mnind+1)%n; i!=mnind; i=(i+1)%n){
if (1<=inputSeq[i]&&inputSeq[i]<=n){
if (mnv!=inputSeq[i]){
return 0;
}
}
mnv++;
}
return 1;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[]){
vector<pair<int,int>> gondind;
int oneind = 0;
for (int i = 0; i<n; i++){
if (gondolaSeq[i]<n){
oneind=(i+n-(gondolaSeq[i]-1))%n;
break;
}
}
for (int i = 0; i<n; i++){
if (gondolaSeq[i]>=n){
gondind.push_back({gondolaSeq[i],i});
}
}
vector<int> gondolas(n);
int gptr = oneind;
int incr = 0;
do {
gondolas[gptr]=incr;
gptr++;incr++;
gptr%=n;
} while (gptr!=oneind);
sort(gondind.begin(),gondind.end());
int ptr = 0;
int gprev = n;
int nextgondola = n;
for (auto g : gondind){
for (int h = gprev; h<g.first; h++){
//replace gondola at index g.second
replacementSeq[ptr]=gondolas[g.second]+1;
ptr++;
gondolas[g.second]=nextgondola;
nextgondola++;
}
gprev=g.first;
}
return ptr;
}
//----------------------
ll mexp(ll a, ll b){
if (b==0){
return 1;
}
if (b%2==1){
ll ans = mexp(a,(b-1)/2);
return (((ans*ans)%m)*a)%m;
} else {
ll ans = mexp(a,b/2);
return (ans*ans)%m;
}
}
int countReplacement(int n, int inputSeq[]){
if (!valid(n,inputSeq)){
return 0;
}
vector<ll> highervals;
for (int i = 0; i<n; i++){
if (inputSeq[i]>n){
highervals.push_back(inputSeq[i]);
}
}
if (highervals.size()==0){
return 1;
}
ll ans = 1;
if (highervals.size()==n){
ans=n;
}
sort(highervals.begin(),highervals.end());
ll prevval = n;
for (ll i = 0; i<highervals.size(); i++){
ll diff = highervals[i]-prevval-1;
ans*=mexp(highervals.size()-i,diff);
ans%=m;
prevval=highervals[i];
}
return ans;
}
Compilation message (stderr)
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:108:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (highervals.size()==n){
~~~~~~~~~~~~~~~~~^~~
gondola.cpp:113:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (ll i = 0; i<highervals.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... |