#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
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 |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
9 ms |
2096 KB |
Output is correct |
7 |
Correct |
16 ms |
640 KB |
Output is correct |
8 |
Correct |
17 ms |
3728 KB |
Output is correct |
9 |
Correct |
6 ms |
1280 KB |
Output is correct |
10 |
Correct |
20 ms |
4112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
10 ms |
2096 KB |
Output is correct |
7 |
Correct |
13 ms |
640 KB |
Output is correct |
8 |
Correct |
18 ms |
3728 KB |
Output is correct |
9 |
Correct |
6 ms |
1408 KB |
Output is correct |
10 |
Correct |
20 ms |
4112 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
11 ms |
1968 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
25 ms |
4252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
12 ms |
896 KB |
Output is correct |
12 |
Correct |
17 ms |
1024 KB |
Output is correct |
13 |
Correct |
16 ms |
1288 KB |
Output is correct |
14 |
Correct |
11 ms |
896 KB |
Output is correct |
15 |
Correct |
22 ms |
2296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
30 ms |
3728 KB |
Output is correct |
10 |
Correct |
25 ms |
3504 KB |
Output is correct |
11 |
Correct |
9 ms |
1280 KB |
Output is correct |
12 |
Correct |
12 ms |
1920 KB |
Output is correct |
13 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
31 ms |
3720 KB |
Output is correct |
10 |
Correct |
25 ms |
3504 KB |
Output is correct |
11 |
Correct |
10 ms |
1280 KB |
Output is correct |
12 |
Correct |
12 ms |
1920 KB |
Output is correct |
13 |
Correct |
3 ms |
640 KB |
Output is correct |
14 |
Correct |
40 ms |
4104 KB |
Output is correct |
15 |
Correct |
44 ms |
5384 KB |
Output is correct |
16 |
Correct |
8 ms |
1280 KB |
Output is correct |
17 |
Correct |
29 ms |
4136 KB |
Output is correct |
18 |
Correct |
17 ms |
2352 KB |
Output is correct |