#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define il inline
#define fup(x, l, r) for ( int x = (l), eNd = (r); x <= eNd; ++ x )
#define fdw(x, r, l) for ( int x = (r), eNd = (l); x >= eNd; -- x )
typedef pair<int, int> PII;
typedef long long LL;
typedef long double LD;
const int N = 1e6 + 10;
const int M = 2e6 + 10;
const int INF = 2e9, P = 998244353;
const double eps = 1e-6;
il int Max(int a, int b) { return a > b ? a : b; }
il int Min(int a, int b) { return a < b ? a : b; }
il int read() { int x = 0; bool f = true; char c = getchar(); while (c < 48 || c > 57) { if (c == '-') f = false; c = getchar(); } while (c >= 48 && c <= 57) x = (x << 3) + (x << 1) + c - 48, c = getchar(); return f ? x : -x; }
il void write(int x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + 48); }
il void wel(int x) { write(x), putchar('\n'); }
il void wsp(int x) { write(x), putchar(' '); }
int n, a[N];
vector<int> A, B;
int ne[N]; // i 后第一次出现 a[i] 的位置,若没有则为 n + 1
int lst[N], res;
signed main()
{
n = read();
fup (i, 1, n) a[i] = read();
fdw (i, n, 1)
{
if (!lst[a[i]]) ne[i] = n + 1;
else ne[i] = lst[a[i]];
lst[a[i]] = i;
}
ne[0] = n + 1;
A.push_back(0), B.push_back(0);
fup (i, 1, n)
{
if (a[i] == a[A.back()]) A.push_back(i);
else if (a[i] == a[B.back()]) B.push_back(i);
else
{
++ res;
if (ne[A.back()] > ne[B.back()]) A.push_back(i);
else B.push_back(i);
}
}
wel(res);
return 0;
}剪贴板
剪贴板 zomqilsr
剪贴板信息
剪贴板信息
- 收录
- 更新