#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
using vi = vector<int>;
void solve() {
int n;
cin >> n;
vi a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int ans = 0;
for (int i = 0;i <= n - 4;i++) {
unordered_map<int,int> mp;
mp[a[i]]++;
mp[a[i + 1]]++;
mp[a[i + 2]]++;
mp[a[i + 3]]++;
if (mp.size() == 2) {
int f = 0;
for (auto [x, y] : mp) {
f ^= y;
}
if (f == 2) {
i += 3;
ans++;
}
}
}
cout << ans << endl;
}
/*
*/
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int t = 1;
//cin >> t;
for (int i = 1; i <= t; i++) {
//cout << "----Test " << i << "----" << endl;
solve();
}
return 0;
}
枚举每一个长度为4的子数组即可,符合条件就跳到下一个位置,用umap判断,是否是两个数字,是否是三带一(1^3==2)

京公网安备 11010502036488号