#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 9;
template<class T> inline void fix(T &x) {if(labs(x) >= mod) {x %= mod;} if(x < 0) {x += mod;}}
#define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl
bool different(int n, int inputSeq[]) {
vector<int> ret;
for(int i = 0; i < n; i ++) {
ret.push_back(inputSeq[i]);
}
sort(ret.begin(), ret.end());
for(int i = 0; i < n - 1; i ++) {
if(ret[i] == ret[i + 1]) {
return false;
}
}
return true;
}
int valid(int n, int inputSeq[]) {
int ind = -1;
for(int i = 0; i < n; i ++) {
if(inputSeq[i] <= n) {
ind = i;
break;
}
}
if(ind != -1) {
int rot = (-inputSeq[ind] + ind + 1 + n) % n;
rotate(inputSeq, inputSeq + rot, inputSeq + n);
}
for(int i = 0; i < n; i ++) {
if(inputSeq[i] <= n && inputSeq[i] != i + 1) {
return 0;
}
}
return different(n, inputSeq);
}
//----------------------
const int MAX_N = 3e5 + 10;
int arr[MAX_N];
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
int ind = -1;
vector<pair<int, int> > pos;
for(int i = 0; i < n; i ++) {
if(gondolaSeq[i] <= n) {
ind = i;
break;
}
}
sort(pos.begin(), pos.end());
if(ind != -1) {
int rot = (-gondolaSeq[ind] + ind + 1 + n) % n;
rotate(gondolaSeq, gondolaSeq + rot, gondolaSeq + n);
}
for(int i = 0; i < n; i ++) {
arr[i] = i + 1;
if(gondolaSeq[i] > n) {
pos.push_back({gondolaSeq[i], i});
}
}
sort(pos.begin(), pos.end());
int l = 0, curr = n + 1;
for(auto it : pos) {
while(curr <= it.first) {
replacementSeq[l ++] = arr[it.second];
arr[it.second] = curr;
curr ++;
}
}
return l;
}
//----------------------
ll fpow(ll x, ll p) {
if(p == 0) {
return 1;
}
ll ans = fpow(x, p / 2ll); ans = (ans * ans) % mod;
if(p & 1) {
return (ans * x) % mod;
} else {
return ans;
}
}
int countReplacement(int n, int inputSeq[]) {
vector<pair<ll, ll> > pos;
for(ll i = 0; i < n; i ++) {
if(inputSeq[i] > n) {
pos.push_back({inputSeq[i], i});
}
}
sort(pos.begin(), pos.end());
ll lft = pos.size();
ll ret = 1, curr = n + 1;
if(pos.size() == n) {
ret = n;
}
for(ll i = 0; i < pos.size(); i ++) {
ret = (ret * fpow(pos.size() - i, pos[i].first - curr)) % mod;
curr = pos[i].first + 1;
}
return ret * valid(n, inputSeq);
}
Compilation message
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:112:19: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
112 | if(pos.size() == n) {
| ~~~~~~~~~~~^~~~
gondola.cpp:115:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for(ll i = 0; i < pos.size(); i ++) {
| ~~^~~~~~~~~~~~
gondola.cpp:110:8: warning: unused variable 'lft' [-Wunused-variable]
110 | ll lft = pos.size();
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
7 ms |
896 KB |
Output is correct |
7 |
Correct |
14 ms |
640 KB |
Output is correct |
8 |
Correct |
14 ms |
1404 KB |
Output is correct |
9 |
Correct |
4 ms |
640 KB |
Output is correct |
10 |
Correct |
14 ms |
1276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
8 ms |
896 KB |
Output is correct |
7 |
Correct |
14 ms |
708 KB |
Output is correct |
8 |
Correct |
12 ms |
1276 KB |
Output is correct |
9 |
Correct |
4 ms |
640 KB |
Output is correct |
10 |
Correct |
15 ms |
1276 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
14 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
288 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 |
384 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 |
11 |
Correct |
12 ms |
896 KB |
Output is correct |
12 |
Correct |
14 ms |
1024 KB |
Output is correct |
13 |
Correct |
17 ms |
1424 KB |
Output is correct |
14 |
Correct |
12 ms |
896 KB |
Output is correct |
15 |
Correct |
24 ms |
2288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
512 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
25 ms |
2284 KB |
Output is correct |
10 |
Correct |
20 ms |
1784 KB |
Output is correct |
11 |
Correct |
8 ms |
1020 KB |
Output is correct |
12 |
Correct |
10 ms |
1148 KB |
Output is correct |
13 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
25 ms |
2292 KB |
Output is correct |
10 |
Correct |
20 ms |
1784 KB |
Output is correct |
11 |
Correct |
8 ms |
1020 KB |
Output is correct |
12 |
Correct |
10 ms |
1144 KB |
Output is correct |
13 |
Correct |
3 ms |
640 KB |
Output is correct |
14 |
Correct |
33 ms |
2804 KB |
Output is correct |
15 |
Correct |
37 ms |
3056 KB |
Output is correct |
16 |
Correct |
7 ms |
1020 KB |
Output is correct |
17 |
Correct |
24 ms |
2036 KB |
Output is correct |
18 |
Correct |
14 ms |
1656 KB |
Output is correct |