#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
int valid(int n, int inputSeq[])
{
int s = -1;
set<int> ud;
for(int i=0; i<n; i++) {
if(inputSeq[i] <= n) {
s = i;
}
if(ud.count(inputSeq[i])) {
// cout << " :: " << inputSeq[i] << endl;
return 0;
}
// cout << "ud[" << inputSeq[i] << "] = 1\n";
ud.insert(inputSeq[i]);
}
if(s != -1) {
int val = inputSeq[s]-1;
s--;
for(int i=0; i<n; i++) {
s = (s+1)%n;
val = ((val) % n) + 1;
//cerr << inputSeq[s] << " " << val << endl;
if(inputSeq[s] <= n && inputSeq[s] != val) return 0;
}
}
return 1;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
int l = 0;
vector<pair<int,int>> gseq;
vector<int> rseq;
for(int i=0; i<n; i++) {
gseq.push_back({gondolaSeq[i], i});
}
gseq.pb({n, n});
sort(gseq.begin(), gseq.end(), greater<pii>());
int t = 0;
for(int i = 0; i<n && gseq[i].fi > n; i++) {
//cout << i << " " << gseq[i].fi << endl;
t = gseq[i].fi - gseq[i+1].fi;
while(t--) rseq.pb(gseq[i].se);
}
int s = 0;
for(int i = 0; i<n; i++) {
if(gondolaSeq[i] <= n) {
s = gondolaSeq[i] - i - 1;
break;
}
}
l = rseq.size();
reverse(rseq.begin(), rseq.end());
// for(int i=0; i<l; i++) {
// rseq[i] = ((rseq[i] + s + n) % n) + 1;
// //cerr << replacementSeq[i] << " ";
// }
for(int i=0; i<n; i++) {
gondolaSeq[i] = ((i+s) % n) + 1;
//cout << i << " " << gondolaSeq[i] << endl;
}
int cur = n+1;
for(int i=0; i<l; i++) {
replacementSeq[i] = gondolaSeq[rseq[i]];
gondolaSeq[rseq[i]] = cur++;
}
//cerr << "\n";
return l;
}
//----------------------
const int m = 1e9+9;
ll mul (ll a, ll b) {
a %= m;
b %= m;
return (a*b) % m;
}
ll binpow(ll a, ll b) {
ll ret = 1;
while(b) {
if(b&1) ret = mul(a, ret);
a = mul(a, a);
b >>= 1;
}
return ret;
}
int countReplacement(int n, int inputSeq[])
{
if(!valid(n, inputSeq)) return 0;
int maxi = 0;
int mini = INT_MAX;
vector<pii> v;
for(int i=0; i<n; i++) {
v.push_back({inputSeq[i], i});
maxi = max(maxi, inputSeq[i]);
mini = min(mini, inputSeq[i]);
}
if(maxi == n) return 1;
ll ans = 1;
int ls = n;
ll rem = n;
sort(v.begin(), v.end());
for(int i=0; i<v.size(); i++) {
if(v[i].fi > n) {
ans = mul(ans, binpow(rem, v[i].fi - ls - 1));
ls = v[i].fi;
}
rem--;
// cout << i << " " << ans << endl;
}
if(mini > n) ans = mul(ans, n);
return ans;
}
/*
replacement seq is a permutation of maxi - n numbers
there are 2 cases :
all of them is greater than n
at least one of them <= n
*/
Compilation message
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:133:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | for(int i=0; i<v.size(); i++) {
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
13 ms |
2180 KB |
Output is correct |
7 |
Correct |
7 ms |
588 KB |
Output is correct |
8 |
Correct |
21 ms |
3916 KB |
Output is correct |
9 |
Correct |
7 ms |
1356 KB |
Output is correct |
10 |
Correct |
30 ms |
4420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
14 ms |
2216 KB |
Output is correct |
7 |
Correct |
11 ms |
588 KB |
Output is correct |
8 |
Correct |
21 ms |
3908 KB |
Output is correct |
9 |
Correct |
7 ms |
1356 KB |
Output is correct |
10 |
Correct |
29 ms |
4404 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
13 ms |
1996 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
33 ms |
4588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
10 ms |
1732 KB |
Output is correct |
12 |
Correct |
12 ms |
1732 KB |
Output is correct |
13 |
Correct |
12 ms |
1476 KB |
Output is correct |
14 |
Correct |
11 ms |
1760 KB |
Output is correct |
15 |
Correct |
21 ms |
2208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
208 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
37 ms |
3916 KB |
Output is correct |
10 |
Correct |
30 ms |
3276 KB |
Output is correct |
11 |
Correct |
10 ms |
1356 KB |
Output is correct |
12 |
Correct |
13 ms |
1612 KB |
Output is correct |
13 |
Correct |
3 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
38 ms |
3940 KB |
Output is correct |
10 |
Correct |
30 ms |
3276 KB |
Output is correct |
11 |
Correct |
10 ms |
1356 KB |
Output is correct |
12 |
Correct |
13 ms |
1684 KB |
Output is correct |
13 |
Correct |
3 ms |
588 KB |
Output is correct |
14 |
Correct |
45 ms |
5284 KB |
Output is correct |
15 |
Correct |
54 ms |
5896 KB |
Output is correct |
16 |
Correct |
8 ms |
1240 KB |
Output is correct |
17 |
Correct |
33 ms |
4036 KB |
Output is correct |
18 |
Correct |
17 ms |
2740 KB |
Output is correct |