// 頻度配列 top K だけ見る #include using namespace std; int main() { int n; cin >> n; assert(1 <= n and n <= 300000); vector C(n); vector A(n); for (int i = 0; i < n; i++) { cin >> A[i]; assert(1 <= A[i] and A[i] <= n); A[i]--; C[A[i]]++; } vector ind(n); iota(ind.begin(), ind.end(), 0); sort(ind.begin(), ind.end(), [&](int i, int j) { return C[i] > C[j]; }); int ans = 1 << 30; int b = min(n, 1000); for (int i = 0; i < b; i++) { int p = ind[i]; int s = -1; for (int j = 0; j < n; j++) { if (A[j] == p) { s = j; break; } } if (s == -1) break; int dp0 = 0; int dp1 = 0; int tot = 0; s = (s + 1) % n; for (int _ = 0; _ < n; _++) { if (A[s] == p) { tot += min(dp0, dp1); dp0 = 0; dp1 = 0; } else { tot++; int ndp0, ndp1; if (A[s] > p) { ndp0 = dp0; ndp1 = min(dp0, dp1) + 1; } else { ndp0 = dp0 + 1; ndp1 = min(dp0, dp1); } dp0 = ndp0; dp1 = ndp1; } s++; if (s == n) s = 0; } ans = min(ans, tot); } cout << ans << endl; }