#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
int valid(int n, int inputSeq[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
set<int> st;
for (int i = 0; i < n; i++) {
if (st.count(inputSeq[i])) return 0;
st.insert(inputSeq[i]);
}
int lowest = 1e9, pos = 1e9;
for (int i = 0; i < n; i++) {
if (lowest > inputSeq[i]) {
lowest = inputSeq[i], pos = i;
}
}
if (lowest > n) return 1;
for (int i = pos, cnt = lowest, len = 1; len <= n; i = (i + 1) % n, len++, cnt = cnt % n + 1) {
if (inputSeq[i] > n) continue;
if (cnt != inputSeq[i]) return 0;
}
return 1;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
if (*max_element(gondolaSeq, gondolaSeq + n) == n) return 0;
int lowest = 1e9, pos = 1e9;
for (int i = 0; i < n; i++) {
if (gondolaSeq[i] < lowest) {
lowest = gondolaSeq[i];
pos = i;
}
}
vector<pair<int, int>> arr;
if (lowest > n) {
for (int i = 0; i < n; i++) arr.push_back({gondolaSeq[i], i + 1});
sort(arr.begin(), arr.end());
} else {
for (int i = pos, len = 1, cnt = lowest; len <= n; i = (i + 1) % n, len++, cnt = cnt % n + 1) {
if (gondolaSeq[i] <= n) continue;
arr.push_back({gondolaSeq[i], cnt});
}
sort(arr.begin(), arr.end());
}
int index = 0, cur = n + 1;
for (auto&x : arr) {
int v = x.first, pos = x.second;
replacementSeq[index++] = pos;
for (int i = cur; i < v; i++) replacementSeq[index++] = i;
cur = v + 1;
}
return index;
}
//----------------------
long long binpow(int n, int k) {
const long long modulo = 1e9 + 9;
if (!k) return 1;
else {
long long ans = binpow(n, k/2);
ans *= ans, ans %= modulo;
if (k & 1) ans *= (long long)n, ans %= modulo;
return ans;
}
}
int countReplacement(int n, int inputSeq[]) {
if (!valid(n, inputSeq)) return 0;
if (*max_element(inputSeq, inputSeq + n) == n) return 1;
vector<int> arr = {n};
for (int i = 0; i < n; i++) {
if (inputSeq[i] <= n) continue;
arr.push_back(inputSeq[i]);
}
sort(arr.begin(), arr.end());
const long long modulo = 1e9 + 9;
long long ans = 1;
for (int i = 1; i < arr.size(); i++) ans *= binpow(arr.size() - i, arr[i] - arr[i - 1] - 1), ans %= modulo;
if (*min_element(inputSeq, inputSeq + n) > n) ans *= (long long)n, ans %= modulo;
return (int)ans;
}
/*int gondolaSequence[100001];
int replacementSequence[250001];
signed main()
{
int i, n, tag;
int nr;
//assert(scanf("%d", &tag)==1);
//assert(scanf("%d", &n)==1);
cin >> tag >> n;
for(i=0;i< n;i++) cin >> gondolaSequence[i];
//assert( scanf("%d", &gondolaSequence[i]) ==1);
switch (tag){
case 1: case 2: case 3:
printf("%d\n", valid(n, gondolaSequence));
break;
case 4: case 5: case 6:
nr = replacement(n, gondolaSequence, replacementSequence);
printf("%d ", nr);
if (nr > 0)
{
for (i=0; i<nr-1; i++)
printf("%d ", replacementSequence[i]);
printf("%d\n", replacementSequence[nr-1]);
}
else printf("\n");
break;
case 7: case 8: case 9: case 10:
printf("%d\n", countReplacement(n, gondolaSequence));
break;
}
return 0;
}*/
Compilation message
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:87:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int i = 1; i < arr.size(); i++) ans *= binpow(arr.size() - i, arr[i] - arr[i - 1] - 1), ans %= modulo;
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
11 ms |
2132 KB |
Output is correct |
7 |
Correct |
6 ms |
608 KB |
Output is correct |
8 |
Correct |
23 ms |
3944 KB |
Output is correct |
9 |
Correct |
7 ms |
1484 KB |
Output is correct |
10 |
Correct |
24 ms |
4452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
10 ms |
2148 KB |
Output is correct |
7 |
Correct |
6 ms |
596 KB |
Output is correct |
8 |
Correct |
20 ms |
3972 KB |
Output is correct |
9 |
Correct |
6 ms |
1492 KB |
Output is correct |
10 |
Correct |
24 ms |
4500 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
12 ms |
2004 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
30 ms |
4600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
264 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
6 ms |
596 KB |
Output is correct |
12 |
Correct |
6 ms |
596 KB |
Output is correct |
13 |
Correct |
11 ms |
1228 KB |
Output is correct |
14 |
Correct |
6 ms |
596 KB |
Output is correct |
15 |
Correct |
16 ms |
2252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
32 ms |
4032 KB |
Output is correct |
10 |
Correct |
25 ms |
3412 KB |
Output is correct |
11 |
Correct |
9 ms |
1364 KB |
Output is correct |
12 |
Correct |
11 ms |
1620 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
31 ms |
4052 KB |
Output is correct |
10 |
Correct |
35 ms |
3336 KB |
Output is correct |
11 |
Correct |
9 ms |
1460 KB |
Output is correct |
12 |
Correct |
11 ms |
1716 KB |
Output is correct |
13 |
Correct |
4 ms |
596 KB |
Output is correct |
14 |
Correct |
38 ms |
5324 KB |
Output is correct |
15 |
Correct |
44 ms |
5960 KB |
Output is correct |
16 |
Correct |
8 ms |
1364 KB |
Output is correct |
17 |
Correct |
29 ms |
4112 KB |
Output is correct |
18 |
Correct |
15 ms |
2500 KB |
Output is correct |