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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| void solvebig(int col) { for (int i = 1; i <= n; ++ i) cnt[i] = cnt[i - 1] + (a[i] == col); int tres = 0; for (int i = 1; i <= sz; ++ i) { int ed = app[i].size(); for (int j = 0; j < ed; ++ j) pre[j] = cnt[app[i][j] - 1] - j + 1, suf[j] = cnt[n] - cnt[app[i][j]] + j; for (int j = 1; j < ed; ++ j) chkmax(pre[j], pre[j - 1]); for (int j = ed - 2; ~j; -- j) chkmax(suf[j], suf[j + 1]); for (int j = 0; j < ed; ++ j) chkmax(tres, pre[j] + suf[j]); } if (chkmax(res, tres)) ans.clear(); if (res == tres) ans.push_back(col); for (int i = 1; i <= sz; ++ i) { int ed = app[i].size(), tres = 0; for (int j = 0; j < ed; ++ j) pre[j] = j - cnt[app[i][j]] + 1, suf[j] = cnt[app[i][j] - 1] + ed - j; chkmax(pre[0], 0), chkmax(suf[ed - 1], cnt[n]); for (int j = 1; j < ed; ++ j) chkmax(pre[j], pre[j - 1]); for (int j = ed - 2; ~j; -- j) chkmax(suf[j], suf[j + 1]); for (int j = 0; j < ed - 1; ++ j) chkmax(tres, pre[j] + suf[j + 1]); if (chkmax(res, tres)) ans.clear(); if (res == tres) ans.push_back(i); } }
void solvesmall(int col) { auto &ap = app[col]; app[col].insert(app[col].begin(), 0); int ed = ap.size(), tres = 0; for (int j = 0, cur; j < ed; ++ j) { cur = j; for (int del = 1; del <= lim; ++ del) { if (mx[ap[j] + 1][del] > n + 1) break; while (cur < ed && ap[cur] <= mx[ap[j] + 1][del]) cur ++; chkmax(tres, ed - cur + j + del); } } app[col].erase(app[col].begin()); if (chkmax(res, tres)) ans.clear(); if (res == tres) ans.push_back(col); }
void work() { std::cin >> n; lim = std::max(std::sqrt(n) + 1, 1.), res = 0; for (int i = 1; i <= n; ++ i) app[i].clear(); for (int i = 1; i <= n; ++ i) scanf("%d", a + i); std::vector<int> ws(n); for (int i = 0; i < n; ++ i) ws[i] = a[i + 1]; std::sort(ws.begin(), ws.end()); ws.erase(std::unique(ws.begin(), ws.end()), ws.end()); sz = ws.size(); for (int i = 1; i <= n; ++ i) a[i] = std::lower_bound(ws.begin(), ws.end(), a[i]) - ws.begin() + 1; std::cerr << a[n - 6] << std::endl; for (int i = 1; i <= n; ++ i) app[a[i]].push_back(i); for (int i = 1; i <= n + 1; ++ i) for (int j = 1; j <= lim; ++ j) mx[i][j] = INF; for (int i = 1; i <= sz; ++ i) { if ((int) app[i].size() > lim) continue; int ed = app[i].size(); for (int j = 0; j < ed; ++ j) for (int del = 1; del <= lim && j + del <= ed; ++ del) chkmin(mx[app[i][j]][del], app[i][j + del - 1]); } for (int i = n - 1; i; -- i) for (int j = 1; j <= lim; ++ j) chkmin(mx[i][j], mx[i + 1][j]); for (int i = 1; i <= sz; ++ i) if ((int) app[i].size() > lim) solvebig(i); else solvesmall(i); printf("%d\n", res); std::sort(ans.begin(), ans.end()); ans.erase(std::unique(ans.begin(), ans.end()), ans.end()); for (int x : ans) printf("%d\n", ws[x - 1]); }
|