#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mod 1000000009
#define h1 7897897897897897
#define h2 7897466719774591
#define b1 98762051
#define b2 98765431
#define inf 1000000000
#define pi 3.1415926535897932384626
#define LMAX 9223372036854775807
#define ll long long
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vp vector<pii>
#define SET(a, b) memset(a, b, sizeof(a))
#define all(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
int f[250005], ans[250005], required[250005];
int valid(int n, int a[])
{
map <int, int> m;
FOR(i, 0, n-1) {
if (m.find(a[i]) != m.end()) return 0;
m[a[i]]++;
}
int st = -1;
FOR(i, 0, n-1) {
if (a[i] <= n) {
st = i;
break;
}
}
if (st == -1) return 1;
int i = st, cur = a[i];
do {
if (a[i] == cur || a[i] > n) {}
else return 0;
cur++;
if (cur > n) cur = 1;
i++;
if (i == n) i = 0;
} while (i != st);
return 1;
}
//----------------------
int replacement(int n, int a[], int replace[])
{
int st = -1;
FOR(i, 0, n-1) {
if (a[i] <= n) {
st = i;
break;
}
}
set <int> s;
int maxx = 0;
SET(required, -1);
FOR(i, 0, n-1) {
required[a[i]] = i;
maxx = max(maxx, a[i]);
if (a[i] > n) s.insert(i);
}
if (st == -1) {
FOR(i, 0, n-1) f[i] = i+1;
} else {
int i = st, cur = a[i];
do {
f[i] = cur;
cur++;
if (cur > n) cur = 1;
i++;
if (i == n) i = 0;
} while (i != st);
}
FOR(i, n+1, maxx) {
if (required[i] != -1) {
replace[i-n-1] = f[required[i]];
cerr<<replace[i-n-1]<<endl;
s.erase(required[i]);
f[required[i]] = i;
} else {
int pos = *s.begin();
replace[i-n-1] = f[pos];
if (i == a[pos]) s.erase(s.begin());
f[pos] = i;
}
}
return maxx - n;
}
//----------------------
ll binexp(ll x, ll y) {
ll ret = 1, base = x;
while (y) {
if (y % 2) ret = ret * base % mod;
base = base * base % mod;
y /= 2;
}
return ret;
}
int countReplacement(int n, int a[])
{
ll ret = 1;
if (!valid(n, a)) return 0;
int maxx = 0;
ll sz = 0;
vi v;
FOR(i, 0, n-1) {
if (a[i] > n) {
sz++;
v.pb(a[i]);
}
maxx = max(maxx, a[i]);
}
if (sz == n) {
ret = n;
}
sort(all(v));
int last = n;
for (auto it: v) {
ret = ret * binexp(sz, it - last - 1) % mod;
sz--;
last = it;
}
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
15 ms |
2560 KB |
Output is correct |
7 |
Correct |
13 ms |
1152 KB |
Output is correct |
8 |
Correct |
31 ms |
4344 KB |
Output is correct |
9 |
Correct |
9 ms |
1632 KB |
Output is correct |
10 |
Correct |
35 ms |
4984 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 |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
15 ms |
2432 KB |
Output is correct |
7 |
Correct |
12 ms |
1152 KB |
Output is correct |
8 |
Correct |
29 ms |
4352 KB |
Output is correct |
9 |
Correct |
11 ms |
1792 KB |
Output is correct |
10 |
Correct |
35 ms |
4984 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
21 ms |
2432 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
52 ms |
5500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1280 KB |
Output is correct |
2 |
Correct |
1 ms |
1280 KB |
Output is correct |
3 |
Correct |
1 ms |
1280 KB |
Output is correct |
4 |
Correct |
2 ms |
1280 KB |
Output is correct |
5 |
Correct |
1 ms |
1280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1312 KB |
Output is correct |
2 |
Correct |
1 ms |
1280 KB |
Output is correct |
3 |
Correct |
1 ms |
1280 KB |
Output is correct |
4 |
Correct |
1 ms |
1280 KB |
Output is correct |
5 |
Correct |
1 ms |
1280 KB |
Output is correct |
6 |
Correct |
2 ms |
1280 KB |
Output is correct |
7 |
Correct |
3 ms |
1280 KB |
Output is correct |
8 |
Correct |
2 ms |
1408 KB |
Output is correct |
9 |
Correct |
2 ms |
1408 KB |
Output is correct |
10 |
Correct |
3 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1280 KB |
Output is correct |
2 |
Correct |
2 ms |
1280 KB |
Output is correct |
3 |
Correct |
1 ms |
1280 KB |
Output is correct |
4 |
Correct |
1 ms |
1280 KB |
Output is correct |
5 |
Correct |
1 ms |
1280 KB |
Output is correct |
6 |
Correct |
1 ms |
1280 KB |
Output is correct |
7 |
Correct |
3 ms |
1280 KB |
Output is correct |
8 |
Correct |
2 ms |
1408 KB |
Output is correct |
9 |
Correct |
2 ms |
1280 KB |
Output is correct |
10 |
Correct |
2 ms |
1408 KB |
Output is correct |
11 |
Correct |
11 ms |
2432 KB |
Output is correct |
12 |
Correct |
20 ms |
2560 KB |
Output is correct |
13 |
Correct |
164 ms |
4088 KB |
Output is correct |
14 |
Correct |
11 ms |
2304 KB |
Output is correct |
15 |
Correct |
108 ms |
4216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
256 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 |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 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 |
52 ms |
4472 KB |
Output is correct |
10 |
Correct |
39 ms |
3840 KB |
Output is correct |
11 |
Correct |
18 ms |
1664 KB |
Output is correct |
12 |
Correct |
19 ms |
1920 KB |
Output is correct |
13 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
54 ms |
4472 KB |
Output is correct |
10 |
Correct |
47 ms |
3832 KB |
Output is correct |
11 |
Correct |
23 ms |
1664 KB |
Output is correct |
12 |
Correct |
18 ms |
2024 KB |
Output is correct |
13 |
Correct |
6 ms |
640 KB |
Output is correct |
14 |
Correct |
85 ms |
5368 KB |
Output is correct |
15 |
Correct |
76 ms |
6008 KB |
Output is correct |
16 |
Correct |
12 ms |
1408 KB |
Output is correct |
17 |
Correct |
48 ms |
4216 KB |
Output is correct |
18 |
Correct |
32 ms |
2552 KB |
Output is correct |