#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
#define scd(t) scanf("%d", &t)
#define sclld(t) scanf("%lld", &t)
#define forr(i, j, k) for (int i = j; i < k; i++)
#define frange(i, j) forr(i, 0, j)
#define all(cont) cont.begin(), cont.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef long long int lli;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<lli> vll;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int, int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long double ld;
int valid(int n, int inputSeq[])
{
pii mi = mp(2e9, 1e9);
seti st;
frange(i, n) {
mi = min(mi, mp(inputSeq[i], i));
st.insert(inputSeq[i]);
}
if(st.size() != n) return 0;
if(mi.f > n) return 1;
int v = mi.f;
forr(i, mi.s, n) {
if(inputSeq[i] <= n) {
if(inputSeq[i] != v) return 0;
}
v++;
}
forr(i, 0, mi.s) {
if(inputSeq[i] <= n) {
if(inputSeq[i] != v) return 0;
}
v++;
}
return 1;
}
//----------------------
int replacement(int n, int inputSeq[], int replacementSeq[])
{
pii mi = mp(2e9, 1e9);
int ma = 0;
vi pos(3e5+1, -1);
frange(i, n) {
mi = min(mi, mp(inputSeq[i], i));
ma = max(ma, inputSeq[i]);
pos[inputSeq[i]] = i;
}
if(mi.f > n) {
int v = pos[ma]+1;
pos[ma] = -1;
forr(i, n+1, ma+1) {
if(pos[i] == -1) {
replacementSeq[i-(n+1)] = v;
v = i;
}
else {
replacementSeq[i-(n+1)] = pos[i]+1;
}
}
}
else {
vi val(n);
int v1 = mi.f;
frange(i, n) {
val[i] = ((v1-1) + (i-mi.s) + n) % n + 1;
// printf("%d ", val[i]);
}
int v = val[pos[ma]];
pos[ma] = -1;
forr(i, n+1, ma+1) {
// printf("%d\n", i);
if(pos[i] == -1) {
// printf("%d\n", v);
replacementSeq[i-(n+1)] = v;
v = i;
}
else {
replacementSeq[i-(n+1)] = val[pos[i]];
}
}
}
return ma-n;
}
//----------------------
const lli mod = 1e9 + 9;
lli modexp(lli x, int c) {
if(c == 0) return 1;
lli v = modexp(x, c/2);
v = v*v%mod;
if(c%2) return v*x%mod;
else return v;
}
int countReplacement(int n, int inputSeq[])
{
if(!valid(n, inputSeq)) return 0;
lli out = 1;
int ma = 0;
int mi = 2e9;
int c = n;
seti st;
frange(i, n) {mi = min(mi, inputSeq[i]); if(inputSeq[i] > n) st.insert(inputSeq[i]);}
if(mi > n) {
out = n;
}
frange(i, n) {
if(inputSeq[i] <= n) c--;
}
int pre = n;
for(auto e : st) {
out = out * modexp(c, e-pre-1) % mod;
c--;
pre = e;
}
return out;
}
Compilation message
gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:36:16: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
36 | if(st.size() != n) return 0;
| ~~~~~~~~~~^~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:124:7: warning: unused variable 'ma' [-Wunused-variable]
124 | int ma = 0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
2136 KB |
Output is correct |
7 |
Correct |
21 ms |
3672 KB |
Output is correct |
8 |
Correct |
15 ms |
3928 KB |
Output is correct |
9 |
Correct |
5 ms |
1368 KB |
Output is correct |
10 |
Correct |
20 ms |
4544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
512 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
2140 KB |
Output is correct |
7 |
Correct |
20 ms |
3676 KB |
Output is correct |
8 |
Correct |
16 ms |
3848 KB |
Output is correct |
9 |
Correct |
6 ms |
1368 KB |
Output is correct |
10 |
Correct |
21 ms |
4692 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
9 ms |
2000 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
24 ms |
4688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1624 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1368 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1624 KB |
Output is correct |
10 |
Correct |
2 ms |
1624 KB |
Output is correct |
11 |
Correct |
7 ms |
2140 KB |
Output is correct |
12 |
Correct |
7 ms |
2136 KB |
Output is correct |
13 |
Correct |
9 ms |
2140 KB |
Output is correct |
14 |
Correct |
6 ms |
2140 KB |
Output is correct |
15 |
Correct |
15 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
33 ms |
3932 KB |
Output is correct |
10 |
Correct |
25 ms |
3420 KB |
Output is correct |
11 |
Correct |
11 ms |
1372 KB |
Output is correct |
12 |
Correct |
12 ms |
1744 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
36 ms |
4096 KB |
Output is correct |
10 |
Correct |
26 ms |
3420 KB |
Output is correct |
11 |
Correct |
10 ms |
1372 KB |
Output is correct |
12 |
Correct |
12 ms |
1628 KB |
Output is correct |
13 |
Correct |
3 ms |
968 KB |
Output is correct |
14 |
Correct |
45 ms |
4436 KB |
Output is correct |
15 |
Correct |
50 ms |
6152 KB |
Output is correct |
16 |
Correct |
8 ms |
1624 KB |
Output is correct |
17 |
Correct |
33 ms |
4176 KB |
Output is correct |
18 |
Correct |
21 ms |
2592 KB |
Output is correct |