#include "gondola.h"
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
#define UNDEF -1
typedef pair<int, int> pii;
long long mod = 1000000009;
int valid(int n, int inputSeq[])
{
vector<int> L;
L.resize(n);
int temp;
int shift = -1;
for (int i=0; i<n; i++){
L[i] = inputSeq[i];
if (L[i] <= n){
temp = (i-L[i]+n+1)%n;
if (shift == UNDEF) shift = temp;
else if (shift != temp) return 0;
}
}
sort(L.begin(), L.end());
for (int i=1; i<n; i++) if (L[i-1] == L[i]) return 0;
L.clear();
return 1;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
int min_val = 1000000, min_index = -1;
vector<pii> L;
for (int i=0; i<n; i++){
if (min_val > gondolaSeq[i]){
min_index = i;
min_val = gondolaSeq[i];
}
}
bool b = (min_val > n);
for (int j=0; j<n; j++){
int i = min_index + j;
int c;
if (b) c = j;
else{
c = (j + min_val)%n;
}
L.push_back(pii(gondolaSeq[i%n], (c == 0 ? n : c)));
}
sort(L.begin(), L.end());
int l = 0;
for (int i=0; i<n; i++){
while (L[i].first != L[i].second){
replacementSeq[l] = L[i].second;
l++;
L[i].second = n+l;
}
}
return l;
}
//----------------------
long long llpow(long long a, int b){
long long ans = 1;
while(b){
if (b & 1) ans = (ans * a) % mod;
a = (a*a)%mod;
b >>=1;
}
return ans;
}
int countReplacement(int n, int inputSeq[])
{
if (!valid(n, inputSeq)) return 0;
int min_val = 1000000, min_index = -1;
vector<int> L;
for (int i=0; i<n; i++){
if (min_val > inputSeq[i]){
min_index = i;
min_val = inputSeq[i];
}
}
bool b = (min_val > n);
for (int i=0; i<n; i++){
if (inputSeq[i] > n) L.push_back(inputSeq[i]);
}
sort(L.begin(), L.end());
long long ans = 1;
int m = L.size();
int k = n - m;
for (int i=0; i<m; i++){
if (i == 0) {
ans = (ans * llpow((long long) m, L[i]-n-1))%mod;
continue;
}
ans = (ans * llpow((long long) m-i, L[i]-1-L[i-1]))%mod;
}
if (m == n) ans = (ans * n) % mod;
return (int) ans;
}
Compilation message
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:82:25: warning: variable 'min_index' set but not used [-Wunused-but-set-variable]
int min_val = 1000000, min_index = -1;
^~~~~~~~~
gondola.cpp:90:7: warning: unused variable 'b' [-Wunused-variable]
bool b = (min_val > n);
^
gondola.cpp:97:6: warning: unused variable 'k' [-Wunused-variable]
int k = n - m;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
480 KB |
Output is correct |
3 |
Correct |
3 ms |
480 KB |
Output is correct |
4 |
Correct |
2 ms |
480 KB |
Output is correct |
5 |
Correct |
2 ms |
480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
480 KB |
Output is correct |
2 |
Correct |
2 ms |
480 KB |
Output is correct |
3 |
Correct |
2 ms |
480 KB |
Output is correct |
4 |
Correct |
2 ms |
480 KB |
Output is correct |
5 |
Correct |
2 ms |
480 KB |
Output is correct |
6 |
Correct |
16 ms |
796 KB |
Output is correct |
7 |
Correct |
16 ms |
1164 KB |
Output is correct |
8 |
Correct |
13 ms |
1164 KB |
Output is correct |
9 |
Correct |
8 ms |
1164 KB |
Output is correct |
10 |
Correct |
25 ms |
1196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1196 KB |
Output is correct |
2 |
Correct |
2 ms |
1196 KB |
Output is correct |
3 |
Correct |
2 ms |
1196 KB |
Output is correct |
4 |
Correct |
2 ms |
1196 KB |
Output is correct |
5 |
Correct |
2 ms |
1196 KB |
Output is correct |
6 |
Correct |
12 ms |
1196 KB |
Output is correct |
7 |
Correct |
15 ms |
1236 KB |
Output is correct |
8 |
Correct |
14 ms |
1236 KB |
Output is correct |
9 |
Correct |
12 ms |
1236 KB |
Output is correct |
10 |
Correct |
21 ms |
1236 KB |
Output is correct |
11 |
Correct |
2 ms |
1236 KB |
Output is correct |
12 |
Correct |
3 ms |
1236 KB |
Output is correct |
13 |
Correct |
8 ms |
1236 KB |
Output is correct |
14 |
Correct |
3 ms |
1236 KB |
Output is correct |
15 |
Correct |
17 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1236 KB |
Output is correct |
2 |
Correct |
2 ms |
1236 KB |
Output is correct |
3 |
Correct |
3 ms |
1236 KB |
Output is correct |
4 |
Correct |
2 ms |
1236 KB |
Output is correct |
5 |
Correct |
2 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
1236 KB |
Output is correct |
2 |
Correct |
2 ms |
1236 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
2 ms |
1236 KB |
Output is correct |
5 |
Correct |
3 ms |
1236 KB |
Output is correct |
6 |
Correct |
2 ms |
1236 KB |
Output is correct |
7 |
Correct |
2 ms |
1236 KB |
Output is correct |
8 |
Correct |
3 ms |
1236 KB |
Output is correct |
9 |
Correct |
3 ms |
1236 KB |
Output is correct |
10 |
Correct |
4 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1236 KB |
Output is correct |
2 |
Correct |
2 ms |
1236 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
2 ms |
1236 KB |
Output is correct |
5 |
Correct |
2 ms |
1236 KB |
Output is correct |
6 |
Correct |
2 ms |
1236 KB |
Output is correct |
7 |
Correct |
3 ms |
1236 KB |
Output is correct |
8 |
Correct |
2 ms |
1236 KB |
Output is correct |
9 |
Correct |
3 ms |
1236 KB |
Output is correct |
10 |
Correct |
4 ms |
1236 KB |
Output is correct |
11 |
Correct |
17 ms |
2136 KB |
Output is correct |
12 |
Correct |
20 ms |
2136 KB |
Output is correct |
13 |
Correct |
23 ms |
2136 KB |
Output is correct |
14 |
Correct |
17 ms |
2152 KB |
Output is correct |
15 |
Correct |
26 ms |
2476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2476 KB |
Output is correct |
2 |
Correct |
2 ms |
2476 KB |
Output is correct |
3 |
Correct |
2 ms |
2476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2476 KB |
Output is correct |
2 |
Correct |
2 ms |
2476 KB |
Output is correct |
3 |
Correct |
2 ms |
2476 KB |
Output is correct |
4 |
Correct |
2 ms |
2476 KB |
Output is correct |
5 |
Correct |
3 ms |
2476 KB |
Output is correct |
6 |
Correct |
3 ms |
2476 KB |
Output is correct |
7 |
Correct |
3 ms |
2476 KB |
Output is correct |
8 |
Correct |
3 ms |
2476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2476 KB |
Output is correct |
2 |
Correct |
2 ms |
2476 KB |
Output is correct |
3 |
Correct |
2 ms |
2476 KB |
Output is correct |
4 |
Correct |
2 ms |
2476 KB |
Output is correct |
5 |
Correct |
2 ms |
2476 KB |
Output is correct |
6 |
Correct |
2 ms |
2476 KB |
Output is correct |
7 |
Correct |
2 ms |
2476 KB |
Output is correct |
8 |
Correct |
2 ms |
2476 KB |
Output is correct |
9 |
Correct |
25 ms |
2476 KB |
Output is correct |
10 |
Correct |
22 ms |
2476 KB |
Output is correct |
11 |
Correct |
15 ms |
2476 KB |
Output is correct |
12 |
Correct |
11 ms |
2476 KB |
Output is correct |
13 |
Correct |
4 ms |
2476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2476 KB |
Output is correct |
2 |
Correct |
2 ms |
2476 KB |
Output is correct |
3 |
Correct |
2 ms |
2476 KB |
Output is correct |
4 |
Correct |
2 ms |
2476 KB |
Output is correct |
5 |
Correct |
2 ms |
2476 KB |
Output is correct |
6 |
Correct |
2 ms |
2476 KB |
Output is correct |
7 |
Correct |
2 ms |
2476 KB |
Output is correct |
8 |
Correct |
2 ms |
2476 KB |
Output is correct |
9 |
Correct |
25 ms |
2476 KB |
Output is correct |
10 |
Correct |
25 ms |
2476 KB |
Output is correct |
11 |
Correct |
12 ms |
2476 KB |
Output is correct |
12 |
Correct |
12 ms |
2476 KB |
Output is correct |
13 |
Correct |
5 ms |
2476 KB |
Output is correct |
14 |
Correct |
39 ms |
2476 KB |
Output is correct |
15 |
Correct |
55 ms |
2876 KB |
Output is correct |
16 |
Correct |
10 ms |
2876 KB |
Output is correct |
17 |
Correct |
29 ms |
3088 KB |
Output is correct |
18 |
Correct |
26 ms |
3164 KB |
Output is correct |