LuoGu: CF1082E Increasing Frequency
CF: E. Increasing Frequency
将问题转化为找到一个区间使得这个区间中出现次数最多的次数减去 $c$ 出现的次数最大。考虑每添加一个数,找到以这个数为右端点的最大值,考虑前缀和,需要找到最小的左端点,每次更新答案即可。
查看代码
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
| #include <cstdio> using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } template <class Type> void chkmin (Type &x, Type k) {k < x && (x = k);} template <class Type> void chkmax (Type &x, Type k) {k > x && (x = k);} const int N = 5e5 + 10; int n, m; int cnt[N], mn[N]; int main () { read(n), read(m); int res = 0; for (int i = 1, a; i <= n; i++) { read(a); chkmin(mn[a], cnt[a] - cnt[m]); chkmax(res, ++cnt[a] - cnt[m] - mn[a]); } write(res + cnt[m]); return 0; }
|