#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define X first
#define Y second
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T i, U ...j) {
cout << i << ' ', abc(j...);
}
template <typename T> void printv(T l, T r) {
for (; l != r; ++l)
cout << *l << " \n"[l + 1 == r];
}
#define test(x...) abc("[" + string(#x) + "]", x);
const int N = 1000001;
int dsu[N];
int Find(int x) {
if (dsu[x] == x)
return x;
return dsu[x] = Find(dsu[x]);
}
void Union(int u, int v) {
dsu[Find(u)] = Find(v);
}
int main () {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector <int> a(n), cnt(N, 0);
for (int i = 0; i < n; ++i) {
cin >> a[i], --a[i], cnt[a[i]]++;
}
for (int i = 1; i < N; ++i) if (!cnt[i])
Union(i, i - 1);
iota(dsu, dsu + N, 0);
reverse(all(a));
int ans = 0;
for (int i = 0, j = 0; i < n; i = j) {
ans++;
if (i + 1 == n) {
break;
}
while (j < n && a[i] == a[j])
cnt[a[j]]--, j++;
if (!cnt[a[i]])
Union(a[i], a[i] - 1);
int x = Find(a[i] - 1);
while (j < n) {
if (x != a[j])
break;
cnt[a[j]]--, j++;
if (!cnt[x])
Union(x, x - 1), x = Find(x);
}
}
cout << ans << endl;
}
/*
6
3 6 4 5 1 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8140 KB |
Output is correct |
2 |
Correct |
10 ms |
8140 KB |
Output is correct |
3 |
Correct |
10 ms |
8140 KB |
Output is correct |
4 |
Correct |
12 ms |
8140 KB |
Output is correct |
5 |
Correct |
10 ms |
8096 KB |
Output is correct |
6 |
Correct |
11 ms |
8140 KB |
Output is correct |
7 |
Incorrect |
12 ms |
8152 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8140 KB |
Output is correct |
2 |
Correct |
10 ms |
8140 KB |
Output is correct |
3 |
Correct |
10 ms |
8140 KB |
Output is correct |
4 |
Correct |
12 ms |
8140 KB |
Output is correct |
5 |
Correct |
10 ms |
8096 KB |
Output is correct |
6 |
Correct |
11 ms |
8140 KB |
Output is correct |
7 |
Incorrect |
12 ms |
8152 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8140 KB |
Output is correct |
2 |
Correct |
10 ms |
8140 KB |
Output is correct |
3 |
Correct |
10 ms |
8140 KB |
Output is correct |
4 |
Correct |
12 ms |
8140 KB |
Output is correct |
5 |
Correct |
10 ms |
8096 KB |
Output is correct |
6 |
Correct |
11 ms |
8140 KB |
Output is correct |
7 |
Incorrect |
12 ms |
8152 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8140 KB |
Output is correct |
2 |
Correct |
10 ms |
8140 KB |
Output is correct |
3 |
Correct |
10 ms |
8140 KB |
Output is correct |
4 |
Correct |
12 ms |
8140 KB |
Output is correct |
5 |
Correct |
10 ms |
8096 KB |
Output is correct |
6 |
Correct |
11 ms |
8140 KB |
Output is correct |
7 |
Incorrect |
12 ms |
8152 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |