#include <iostream> #include <vector> #include <algorithm> #include <set> int cnt[500001]; int first[500001]; int main() { int n; std::cin >> n; std::vector<int> v(n); int max = -1; std::set<int>s; long long current = -1; long long intervals = 1; for (int i = 0; i < n; i++) { std::cin >> v[i]; v[i]--; cnt[v[i]]++; first[v[i]] = i; max = std::max(max, cnt[v[i]]); } for (int i = 0; i < n; i++) { first[v[i]] = std::min(first[v[i]], i); if (cnt[v[i]] == max){ s.insert(v[i]); } } int maxcnt = s.size(); if (maxcnt == 1){ std::cout<<"1"; return 0; } for (int i = 0; i < n; i++){ cnt[v[i]]--; if (cnt[v[i]] == 0){ if (current == -1){ current = v[i]; } else { current = -1; intervals++; } } if (cnt[v[i]] == max - 1){ maxcnt--; if (maxcnt == 1){ std::cout<<intervals+1; return 0; } } } std::cout<<intervals; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include <iostream> #include <vector> #include <algorithm> #include <set> int cnt[500001]; int first[500001]; int main() { int n; std::cin >> n; std::vector<int> v(n); int max = -1; std::set<int>s; long long current = -1; long long intervals = 1; for (int i = 0; i < n; i++) { std::cin >> v[i]; v[i]--; cnt[v[i]]++; first[v[i]] = i; max = std::max(max, cnt[v[i]]); } for (int i = 0; i < n; i++) { first[v[i]] = std::min(first[v[i]], i); if (cnt[v[i]] == max){ s.insert(v[i]); } } int maxcnt = s.size(); if (maxcnt == 1){ std::cout<<"1"; return 0; } for (int i = 0; i < n; i++){ cnt[v[i]]--; if (cnt[v[i]] == 0){ if (current == -1){ current = v[i]; } else { current = -1; intervals++; } } if (cnt[v[i]] == max - 1){ maxcnt--; if (maxcnt == 1){ std::cout<<intervals+1; return 0; } } } std::cout<<intervals; return 0; } |