#include "bits/stdc++.h"
using namespace std;
const int N = (int) 1e6 + 22;
mt19937 gen(22);
int p[N], mn[N];
int get(int v) {
return v == p[v] ? v : p[v] = get(p[v]);
}
void merge(int v, int u) {
v = get(v), u = get(u);
mn[u] = min(mn[u], mn[v]);
p[v] = u;
}
void solve() {
iota(p, p + N, 0);
iota(mn, mn + N, 0);
int n = gen() % 10 + 1;
cin >> n;
vector<int> a(n);
vector<int> cnt(N, 0), prv(N, -1);
for (int i = 0; i < n; i++) {
// a[i] = gen() % 10 + 1;
cin >> a[i];
cnt[a[i]]++;
}
for (int i = 0; i + 1 < N; i++) {
if (!cnt[i]) {
merge(i, i + 1);
}
}
// cout << n << endl;
// for (int i = 0; i < n; i++) {
// cout << a[i] << " ";
// }
// cout << endl;
{
auto b = a;
std::sort(b.begin(), b.end());
for (int i = 1; i < n; i++) {
if (b[i] != b[i - 1]) {
prv[b[i]] = b[i - 1];
}
}
}
int ans = 0;
while (!a.empty()) {
ans++;
int j = (int) a.size() - 1, c = 1;
bool is_first = true;
while (j > 0) {
// cout << j << endl;
if (a[j - 1] == a[j]) {
j--;
c++;
continue;
}
if (a[j - 1] != prv[a[j]]) {
break;
}
if (is_first) {
is_first = false;
j--;
c = 1;
continue;
}
if (c == cnt[a[j]]) {
j--;
c = 1;
} else {
break;
}
}
// cout << j << " " << a.size() << endl;
for (int i = j; i < (int) a.size(); i++) {
cnt[a[i]]--;
if (cnt[a[i]] == 0) {
merge(a[i], a[i] + 1);
int it = get(a[i]);
if (it != N - 1) {
if (mn[it] == 0) {
prv[it] = -1;
} else {
prv[it] = mn[it] - 1;
}
}
}
}
while ((int) a.size() > j) {
a.pop_back();
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
27828 KB |
Output is correct |
2 |
Correct |
24 ms |
27728 KB |
Output is correct |
3 |
Correct |
21 ms |
27680 KB |
Output is correct |
4 |
Correct |
31 ms |
27728 KB |
Output is correct |
5 |
Correct |
24 ms |
27728 KB |
Output is correct |
6 |
Correct |
25 ms |
27804 KB |
Output is correct |
7 |
Correct |
15 ms |
19544 KB |
Output is correct |
8 |
Correct |
14 ms |
19280 KB |
Output is correct |
9 |
Correct |
16 ms |
20996 KB |
Output is correct |
10 |
Correct |
19 ms |
20868 KB |
Output is correct |
11 |
Correct |
19 ms |
22864 KB |
Output is correct |
12 |
Correct |
15 ms |
19276 KB |
Output is correct |
13 |
Correct |
18 ms |
22864 KB |
Output is correct |
14 |
Correct |
19 ms |
21840 KB |
Output is correct |
15 |
Correct |
21 ms |
20304 KB |
Output is correct |
16 |
Correct |
17 ms |
20088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
27828 KB |
Output is correct |
2 |
Correct |
24 ms |
27728 KB |
Output is correct |
3 |
Correct |
21 ms |
27680 KB |
Output is correct |
4 |
Correct |
31 ms |
27728 KB |
Output is correct |
5 |
Correct |
24 ms |
27728 KB |
Output is correct |
6 |
Correct |
25 ms |
27804 KB |
Output is correct |
7 |
Correct |
15 ms |
19544 KB |
Output is correct |
8 |
Correct |
14 ms |
19280 KB |
Output is correct |
9 |
Correct |
16 ms |
20996 KB |
Output is correct |
10 |
Correct |
19 ms |
20868 KB |
Output is correct |
11 |
Correct |
19 ms |
22864 KB |
Output is correct |
12 |
Correct |
15 ms |
19276 KB |
Output is correct |
13 |
Correct |
18 ms |
22864 KB |
Output is correct |
14 |
Correct |
19 ms |
21840 KB |
Output is correct |
15 |
Correct |
21 ms |
20304 KB |
Output is correct |
16 |
Correct |
17 ms |
20088 KB |
Output is correct |
17 |
Correct |
20 ms |
20560 KB |
Output is correct |
18 |
Correct |
19 ms |
21072 KB |
Output is correct |
19 |
Correct |
16 ms |
19024 KB |
Output is correct |
20 |
Correct |
17 ms |
21488 KB |
Output is correct |
21 |
Correct |
19 ms |
19964 KB |
Output is correct |
22 |
Correct |
16 ms |
21152 KB |
Output is correct |
23 |
Correct |
11 ms |
18256 KB |
Output is correct |
24 |
Correct |
18 ms |
20260 KB |
Output is correct |
25 |
Correct |
13 ms |
18256 KB |
Output is correct |
26 |
Correct |
18 ms |
19428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
27828 KB |
Output is correct |
2 |
Correct |
24 ms |
27728 KB |
Output is correct |
3 |
Correct |
21 ms |
27680 KB |
Output is correct |
4 |
Correct |
31 ms |
27728 KB |
Output is correct |
5 |
Correct |
24 ms |
27728 KB |
Output is correct |
6 |
Correct |
25 ms |
27804 KB |
Output is correct |
7 |
Correct |
15 ms |
19544 KB |
Output is correct |
8 |
Correct |
14 ms |
19280 KB |
Output is correct |
9 |
Correct |
16 ms |
20996 KB |
Output is correct |
10 |
Correct |
19 ms |
20868 KB |
Output is correct |
11 |
Correct |
19 ms |
22864 KB |
Output is correct |
12 |
Correct |
15 ms |
19276 KB |
Output is correct |
13 |
Correct |
18 ms |
22864 KB |
Output is correct |
14 |
Correct |
19 ms |
21840 KB |
Output is correct |
15 |
Correct |
21 ms |
20304 KB |
Output is correct |
16 |
Correct |
17 ms |
20088 KB |
Output is correct |
17 |
Correct |
20 ms |
20560 KB |
Output is correct |
18 |
Correct |
19 ms |
21072 KB |
Output is correct |
19 |
Correct |
16 ms |
19024 KB |
Output is correct |
20 |
Correct |
17 ms |
21488 KB |
Output is correct |
21 |
Correct |
19 ms |
19964 KB |
Output is correct |
22 |
Correct |
16 ms |
21152 KB |
Output is correct |
23 |
Correct |
11 ms |
18256 KB |
Output is correct |
24 |
Correct |
18 ms |
20260 KB |
Output is correct |
25 |
Correct |
13 ms |
18256 KB |
Output is correct |
26 |
Correct |
18 ms |
19428 KB |
Output is correct |
27 |
Correct |
21 ms |
27720 KB |
Output is correct |
28 |
Correct |
24 ms |
27756 KB |
Output is correct |
29 |
Correct |
33 ms |
27720 KB |
Output is correct |
30 |
Correct |
30 ms |
27728 KB |
Output is correct |
31 |
Correct |
30 ms |
27728 KB |
Output is correct |
32 |
Correct |
20 ms |
19920 KB |
Output is correct |
33 |
Correct |
15 ms |
19792 KB |
Output is correct |
34 |
Correct |
18 ms |
19536 KB |
Output is correct |
35 |
Correct |
17 ms |
22096 KB |
Output is correct |
36 |
Correct |
25 ms |
22352 KB |
Output is correct |
37 |
Correct |
12 ms |
16464 KB |
Output is correct |
38 |
Correct |
13 ms |
16720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
27828 KB |
Output is correct |
2 |
Correct |
24 ms |
27728 KB |
Output is correct |
3 |
Correct |
21 ms |
27680 KB |
Output is correct |
4 |
Correct |
31 ms |
27728 KB |
Output is correct |
5 |
Correct |
24 ms |
27728 KB |
Output is correct |
6 |
Correct |
25 ms |
27804 KB |
Output is correct |
7 |
Correct |
15 ms |
19544 KB |
Output is correct |
8 |
Correct |
14 ms |
19280 KB |
Output is correct |
9 |
Correct |
16 ms |
20996 KB |
Output is correct |
10 |
Correct |
19 ms |
20868 KB |
Output is correct |
11 |
Correct |
19 ms |
22864 KB |
Output is correct |
12 |
Correct |
15 ms |
19276 KB |
Output is correct |
13 |
Correct |
18 ms |
22864 KB |
Output is correct |
14 |
Correct |
19 ms |
21840 KB |
Output is correct |
15 |
Correct |
21 ms |
20304 KB |
Output is correct |
16 |
Correct |
17 ms |
20088 KB |
Output is correct |
17 |
Correct |
20 ms |
20560 KB |
Output is correct |
18 |
Correct |
19 ms |
21072 KB |
Output is correct |
19 |
Correct |
16 ms |
19024 KB |
Output is correct |
20 |
Correct |
17 ms |
21488 KB |
Output is correct |
21 |
Correct |
19 ms |
19964 KB |
Output is correct |
22 |
Correct |
16 ms |
21152 KB |
Output is correct |
23 |
Correct |
11 ms |
18256 KB |
Output is correct |
24 |
Correct |
18 ms |
20260 KB |
Output is correct |
25 |
Correct |
13 ms |
18256 KB |
Output is correct |
26 |
Correct |
18 ms |
19428 KB |
Output is correct |
27 |
Correct |
21 ms |
27720 KB |
Output is correct |
28 |
Correct |
24 ms |
27756 KB |
Output is correct |
29 |
Correct |
33 ms |
27720 KB |
Output is correct |
30 |
Correct |
30 ms |
27728 KB |
Output is correct |
31 |
Correct |
30 ms |
27728 KB |
Output is correct |
32 |
Correct |
20 ms |
19920 KB |
Output is correct |
33 |
Correct |
15 ms |
19792 KB |
Output is correct |
34 |
Correct |
18 ms |
19536 KB |
Output is correct |
35 |
Correct |
17 ms |
22096 KB |
Output is correct |
36 |
Correct |
25 ms |
22352 KB |
Output is correct |
37 |
Correct |
12 ms |
16464 KB |
Output is correct |
38 |
Correct |
13 ms |
16720 KB |
Output is correct |
39 |
Correct |
69 ms |
27240 KB |
Output is correct |
40 |
Correct |
99 ms |
28652 KB |
Output is correct |
41 |
Correct |
57 ms |
27484 KB |
Output is correct |
42 |
Correct |
60 ms |
21840 KB |
Output is correct |
43 |
Correct |
40 ms |
20712 KB |
Output is correct |
44 |
Correct |
116 ms |
30536 KB |
Output is correct |
45 |
Correct |
108 ms |
30980 KB |
Output is correct |
46 |
Correct |
149 ms |
30792 KB |
Output is correct |
47 |
Correct |
110 ms |
36356 KB |
Output is correct |
48 |
Correct |
119 ms |
32260 KB |
Output is correct |
49 |
Correct |
166 ms |
30544 KB |
Output is correct |
50 |
Correct |
186 ms |
30536 KB |
Output is correct |
51 |
Correct |
178 ms |
30556 KB |
Output is correct |
52 |
Correct |
187 ms |
30536 KB |
Output is correct |
53 |
Correct |
184 ms |
30536 KB |
Output is correct |
54 |
Correct |
244 ms |
30516 KB |
Output is correct |
55 |
Correct |
237 ms |
30516 KB |
Output is correct |
56 |
Correct |
248 ms |
30536 KB |
Output is correct |
57 |
Correct |
247 ms |
30540 KB |
Output is correct |
58 |
Correct |
206 ms |
30520 KB |
Output is correct |
59 |
Correct |
220 ms |
30644 KB |
Output is correct |
60 |
Correct |
227 ms |
30648 KB |
Output is correct |
61 |
Correct |
237 ms |
30536 KB |
Output is correct |
62 |
Correct |
236 ms |
30508 KB |
Output is correct |