#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
int valid(int n, int inputSeq[]){
int index = 0;
int num = n;
map< int, bool> visited;
for(int i = 0; i < n; i++){
if(visited[inputSeq[i]]) return 0;
visited[inputSeq[i]] = true;
if(inputSeq[i] <= num){
num = inputSeq[i];
index = i;
}
}
int start = (index-num+1+n)%n;
int curr = 1;
for(int i = start; i < n; i++){
if(inputSeq[i] <= n && inputSeq[i] != curr) return 0;
curr++;
}
for(int j = 0; j < start; j++){
if(inputSeq[j] <= n && inputSeq[j] != curr) return 0;
curr++;
}
return 1;
}
int replacement(int n, int gondolaSeq[], int replacementSeq[]){
int index = 0;
int num = n;
int maxi = 0;
for(int i = 0; i < n; i++){
if(gondolaSeq[i] <= num){
num = gondolaSeq[i];
index = i;
}
maxi = max(maxi, gondolaSeq[i]);
}
int start = (index-num+1+n)%n;
if(gondolaSeq[index] > n) start = 0;
int curr = 1;
map<int, int> mape;
vector< int > current(n+1);
for(int i = 1; i <= n; i++){
current[i] = i;
}
int in;
for(int i = start; i < n; i++){
mape[gondolaSeq[i]] = curr;
if(gondolaSeq[i] == maxi) in = curr;
curr++;
}
for(int j = 0; j < start; j++){
mape[gondolaSeq[j]] = curr;
if(gondolaSeq[j] == maxi) in = curr;
curr++;
}
int c = 0;
for(int i = n+1; i <= maxi; i++){
if(mape[i] != 0){
replacementSeq[c] = current[mape[i]];
current[mape[i]] = i;
}else{
replacementSeq[c] = current[in];
current[in] = i;
}
c++;
}
return c;
}
const int mod = 1e9+9;
long long power(long long b, int n){
long long ans = 1;
while(n){
if(n%2){
ans*=b;
ans %= mod;
}
n/=2;
b*=b;
b%=mod;
}
return ans;
}
int countReplacement(int n, int inputSeq[]){
if(!valid(n,inputSeq)) return 0;
vector< int > cnt;
for(int i = 0; i < n; i++){
if(inputSeq[i] > n){
cnt.push_back(inputSeq[i]);
}
}
sort(cnt.begin(), cnt.end());
long long ans = 1;
int last = n+1;
for(int i = 0; i < (int)cnt.size(); i++){
ans *= power(cnt.size()-i, cnt[i]-last);
ans %= mod;
last = cnt[i]+1;
}
if((int)cnt.size() == n) return ((long long)n*ans)%mod;
return ans;
}
/*
int main(){
int n;
cin >> n;
int v[n];
int maxi = 0;
for(int i = 0; i < n; i++){
cin >> v[i];
maxi = max(maxi, v[i]);
}
int ans[maxi-n];
cout << replacement(n,v,ans) << endl;
for(int a : ans){
cout << a << ' ';
}
cout << endl;
cout << "NUMBER " << countReplacement(n,v) << endl;
}*/
Compilation message
gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:85:43: warning: 'in' may be used uninitialized in this function [-Wmaybe-uninitialized]
replacementSeq[c] = current[in];
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
23 ms |
2296 KB |
Output is correct |
7 |
Correct |
13 ms |
640 KB |
Output is correct |
8 |
Correct |
32 ms |
3960 KB |
Output is correct |
9 |
Correct |
10 ms |
1536 KB |
Output is correct |
10 |
Correct |
39 ms |
4480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
18 ms |
2176 KB |
Output is correct |
7 |
Correct |
17 ms |
768 KB |
Output is correct |
8 |
Correct |
40 ms |
4032 KB |
Output is correct |
9 |
Correct |
11 ms |
1536 KB |
Output is correct |
10 |
Correct |
43 ms |
4600 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
3 ms |
256 KB |
Output is correct |
13 |
Correct |
26 ms |
2176 KB |
Output is correct |
14 |
Correct |
3 ms |
256 KB |
Output is correct |
15 |
Correct |
86 ms |
4856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
412 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
4 ms |
640 KB |
Output is correct |
11 |
Correct |
37 ms |
4600 KB |
Output is correct |
12 |
Correct |
38 ms |
5240 KB |
Output is correct |
13 |
Correct |
46 ms |
4720 KB |
Output is correct |
14 |
Correct |
31 ms |
4608 KB |
Output is correct |
15 |
Correct |
69 ms |
10104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
72 ms |
4216 KB |
Output is correct |
10 |
Correct |
62 ms |
3448 KB |
Output is correct |
11 |
Correct |
19 ms |
1408 KB |
Output is correct |
12 |
Correct |
21 ms |
1664 KB |
Output is correct |
13 |
Correct |
7 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
67 ms |
4080 KB |
Output is correct |
10 |
Correct |
66 ms |
3448 KB |
Output is correct |
11 |
Correct |
16 ms |
1408 KB |
Output is correct |
12 |
Correct |
22 ms |
1792 KB |
Output is correct |
13 |
Correct |
6 ms |
640 KB |
Output is correct |
14 |
Correct |
83 ms |
5388 KB |
Output is correct |
15 |
Correct |
128 ms |
6152 KB |
Output is correct |
16 |
Correct |
17 ms |
1408 KB |
Output is correct |
17 |
Correct |
79 ms |
4088 KB |
Output is correct |
18 |
Correct |
29 ms |
2432 KB |
Output is correct |