可以列出一个比较简单的 dp:,其中 是 的个数。
选取的每段长度都一样,并且我们的贡献是 ,也就是说当 时,一定比两个 更优。
换而言之,我们可以通过只维护 ,并按字典序取 。可以说明的是 (显然 ,平衡得证)。
并且如果只动态维护 个 具体值显然空间是 的。
时间 ,空间 。
Code
#define first x
#define second y
#include <bits/stdc++.h>
#undef first
#undef second
using i32 = int;
using i64 = long long;
using namespace std;
#define eb emplace_back
#define vi vector < i32 >
#define pii pair < i32, i32 >
#define For(i, l, r) for (i32 i = (l); i <= (r); i++)
#define Fep(i, r, l) for (i64 i = (r); i >= (l); i--)
#define ALL(S) S.begin(), S.end()
#define sz(S) (i32)(S.size())
inline void work() {
#define Bnum vector < pii >
i32 n, m;
cin >> n >> m;
vi a(n + 1); For (i, 1, n) cin >> a[i];
auto add = [&](Bnum& f, i32 c) {
i32 n = f.size();
For (i, 0, n - 1) if (f[i].x == c) {
if (f[i].y != c) break;
auto R = f.begin() + i + 1;
auto L = prev(R);
while (1) {
++c;
if (L == f.begin()) break;
L = prev(L);
if ((*L).x != c || (*L).y != (*L).x) {
L = next(L); break;
}
}
f.erase(L, R);
}
n = f.size();
For (i, 0, n - 1) if (f[i].x == c) return f[i].y++, void();
if (f.empty() || c > f[0].x) return f.insert(f.begin(), {c, 1}), void();
Fep (i, n - 1, 0) if (f[i].x > c) return f.insert(f.begin() + i + 1, {c, 1}), void();
assert(0);
};
auto cmp = [&](const Bnum& f, const Bnum& g) -> bool { //f > g
i32 n = f.size(), m = g.size();
for (i32 i = 0; ; i++) {
if (i >= n && i >= m) return 0;
if (i >= n) return 0;
if (i >= m) return 1;
if (f[i].x > g[i].x) return 1;
if (f[i].x < g[i].x) return 0;
if (f[i].y > g[i].y) return 1;
if (f[i].y < g[i].y) return 0;
}
assert(0);
};
vector < Bnum > f(m);
vi cnt(m + 1); i32 now = 0, c = 1;
For (i, 1, n) {
if (a[i]) now += (++cnt[a[i]]) == 2;
c += a[i] == 0;
if (i < m) continue;
c -= a[i - m] == 0;
if (a[i - m]) now -= (--cnt[a[i - m]]) == 1;
i32 j = i % m, lst = (j + m - 1) % m;
if (!now) {
add(f[j], max(c, 1));
if (m > 1 && cmp(f[lst], f[j])) f[j] = f[lst];
} else f[j] = f[lst];
}
const i32 mod = 998244353;
i32 fac = 1; now = 0; auto& ans = f[n % m];
n = ans.size(); i64 sum = 0;
Fep (i, n - 1, 0) {
while (now < ans[i].x) fac = 1ll * fac * (++now) % mod;
sum += 1ll * fac * ans[i].y;
}
sum %= mod; auto ksm = [&](i32 x, i32 y, i32 res = 1) {
for (; y; y >>= 1, x = 1ll * x * x % mod) if (y & 1)
res = 1ll * res * x % mod; return res;
}; cout << ksm(m, mod - 1 - m, sum) << '\n';
}
signed main() { work(); };玛格是我的妈妈
当然了我对于玛格小姐的喜欢,并不是因为我是短发控,也不是因为玛格的高攻纸防特别戳我,也不是因为玛格的伟大玛格的设定让我万分动容,更不是因为我自见到玛格小姐的那一刻,就被她深深吸引。
而是因为在我的内心深处,喜欢宝生玛格这件事,就像是鱼生活在水里,驴没有生活在水里,人需要呼吸,这种如同世间千万常理一样,无法割舍无法自拔,要是作为一个人能和玛格小姐交往,那我就是被玛格小姐骗得倾家荡产我也愿意。
我的天哪玛格大人,玛格大人你带我走吧,玛格简直就是我的妈妈,妈妈怎么了,15岁正是当妈妈的好年纪,宝生玛格就是我的妈妈,妈妈妈妈妈妈妈妈妈妈妈妈妈妈妈妈!

