#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 9;
int valid(int n, int a[])
{
set <int> st;
for (int i = 0; i < n; i++) st.insert(a[i]);
if (st.size() != n) return 0;
vector <pair<int, int>> b;
for (int i = 0; i < n; i++){
if (a[i] <= n) b.push_back({a[i], i});
}
for (int i = 1; i < b.size(); i++){
int need = b[i - 1].first + b[i].second - b[i - 1].second;
if (need > n) need -= n;
if (b[i].first != need) return 0;
}
return 1;
}
//----------------------
int replacement(int n, int a[], int b[])
{
int mx = 0;
for (int i = 0; i < n; i++) mx = max(mx, a[i]);
int mn = 1e6;
int c[n];
for (int i = 0; i < n; i++) mn = min(mn, a[i]);
if (mn > n){
for (int i = 0; i < n; i++) c[i] = i + 1;
} else {
int p = -1;
for (int i = 0; i < n; i++){
if (a[i] <= n) p = i;
}
c[p] = a[p];
for (int i = p + 1; i < n; i++){
c[i] = c[i - 1] + 1;
if (c[i] > n) c[i] = 1;
}
if (p != 0){
c[0] = c[n - 1] + 1;
if (c[0] > n) c[0] = 1;
for (int i = 1; i < p; i++){
c[i] = c[i - 1] + 1;
if (c[i] > n) c[i] = 1;
}
}
}
int l = 0;
set <pair<int, int>> st;
for (int i = 0; i < n; i++){
if (c[i] != a[i]){
st.insert(make_pair(a[i], i));
}
}
int repl = n + 1;
while (st.size()){
auto u = *st.begin();
b[l++] = c[u.second];
c[u.second] = repl++;
if (c[u.second] == a[u.second]) st.erase(u);
}
return l;
}
//----------------------
long long power(long long x, int y){
if (y==0) return 1;
long long v = power(x, y/2);
v *= v; v %= mod;
if (y & 1) return v * x % mod;
else return v;
}
int countReplacement(int n, int a[])
{
if (!valid(n, a)) {
return 0;
}
long long ans = n;
vector <int> v;
for (int i = 0; i < n; i++){
if (a[i] <= n) ans = 1;
else v.push_back(a[i]);
}
sort(v.begin(), v.end());
int choices = v.size();
int curr = n;
for (auto x : v){
int change = x - curr - 1;
ans *= power(choices, change);
ans %= mod;
choices--;
curr = x;
}
return (int)ans;
}
// int main(){
// int n; cin >> n;
// int a[n];
// for (int i = 0; i < n; i++) cin >> a[i];
// cout << countReplacement(n, a) << "\n";
// return 0;
// }
Compilation message
gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:12:19: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
12 | if (st.size() != n) return 0;
| ~~~~~~~~~~^~~~
gondola.cpp:19:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for (int i = 1; i < b.size(); i++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
2768 KB |
Output is correct |
7 |
Correct |
19 ms |
3568 KB |
Output is correct |
8 |
Correct |
19 ms |
5096 KB |
Output is correct |
9 |
Correct |
5 ms |
1876 KB |
Output is correct |
10 |
Correct |
19 ms |
5576 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 |
1 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 |
9 ms |
2768 KB |
Output is correct |
7 |
Correct |
19 ms |
3668 KB |
Output is correct |
8 |
Correct |
15 ms |
5104 KB |
Output is correct |
9 |
Correct |
5 ms |
1748 KB |
Output is correct |
10 |
Correct |
20 ms |
5612 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
9 ms |
2012 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
24 ms |
5244 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
224 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 |
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 |
352 KB |
Output is correct |
11 |
Correct |
7 ms |
904 KB |
Output is correct |
12 |
Correct |
7 ms |
1108 KB |
Output is correct |
13 |
Correct |
16 ms |
2588 KB |
Output is correct |
14 |
Correct |
6 ms |
852 KB |
Output is correct |
15 |
Correct |
19 ms |
2872 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
308 KB |
Output is correct |
7 |
Correct |
1 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 |
1 ms |
308 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 |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
304 KB |
Output is correct |
9 |
Correct |
25 ms |
4788 KB |
Output is correct |
10 |
Correct |
20 ms |
4180 KB |
Output is correct |
11 |
Correct |
7 ms |
1492 KB |
Output is correct |
12 |
Correct |
9 ms |
1892 KB |
Output is correct |
13 |
Correct |
2 ms |
596 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 |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 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 |
304 KB |
Output is correct |
9 |
Correct |
25 ms |
4720 KB |
Output is correct |
10 |
Correct |
20 ms |
4192 KB |
Output is correct |
11 |
Correct |
7 ms |
1492 KB |
Output is correct |
12 |
Correct |
9 ms |
1832 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
31 ms |
5372 KB |
Output is correct |
15 |
Correct |
35 ms |
5992 KB |
Output is correct |
16 |
Correct |
6 ms |
1256 KB |
Output is correct |
17 |
Correct |
23 ms |
4140 KB |
Output is correct |
18 |
Correct |
12 ms |
2484 KB |
Output is correct |