版本比较
CF1580D
正文
增加 21 行删除 21 行
17 行未改动18192021222324252627282930313233343536373839404142434445464748495051525354555617 行未改动$$```cpp#include <bits/stdc++.h>using namespace std;typedef long long i64;inline void ckmax(i64 &x, i64 t) { if (x < t) x = t; }int n, m, rt;int a[4005], ls[4005], rs[4005], st[4005], siz[4005];i64 f[4005][4005];bool v[4005];void dfs(int x) {f[x][1] = 1ll * (m - 1) * a[x]; siz[x] = 1;function<void(int)> work = [&] (int y) {dfs(y);for (int i = siz[x]; i >= 0; --i)for (int j = 0; j <= siz[y]; ++j)ckmax(f[x][i + j], f[x][i] + f[y][j] - 2ll * a[x] * i * j);siz[x] += siz[y];};if (ls[x]) work(ls[x]); if (rs[x]) work(rs[x]);}int main(void) {scanf("%d%d", &n, &m);for (int i = 1, cur = 0, tot = 0; i <= n; ++i) {scanf("%d", a + i); cur = tot;while (cur && a[st[cur]] > a[i]) --cur;if (cur) rs[st[cur]] = i;if (cur < tot) ls[i] = st[cur + 1];st[++cur] = i; tot = cur;}for (int i = 1; i <= n; ++i) v[ls[i]] = v[rs[i]] = 1;for (int i = 1; i <= n; ++i) if (!v[i]) dfs(i), printf("%lld\n", f[i][m]);return 0;}```17 行未改动18192021222324252627282930313233343536373839404142434445464748495051525354555617 行未改动$$```cpp#include <bits/stdc++.h>using namespace std;typedef long long i64;inline void ckmax(i64 &x, i64 t) { if (x < t) x = t; }int n, m, rt;int a[4005], ls[4005], rs[4005], st[4005], siz[4005];i64 f[4005][4005];bool v[4005];void dfs(int x) {f[x][1] = 1ll * (m - 1) * a[x]; siz[x] = 1;function<void(int)> work = [&] (int y) {dfs(y);for (int i = siz[x]; i >= 0; --i)for (int j = 0; j <= siz[y]; ++j)ckmax(f[x][i + j], f[x][i] + f[y][j] - 2ll * a[x] * i * j);siz[x] += siz[y];};if (ls[x]) work(ls[x]); if (rs[x]) work(rs[x]);}int main(void) {scanf("%d%d", &n, &m);for (int i = 1, cur = 0, tot = 0; i <= n; ++i) {scanf("%d", a + i); cur = tot;while (cur && a[st[cur]] > a[i]) --cur;if (cur) rs[st[cur]] = i;if (cur < tot) ls[i] = st[cur + 1];st[++cur] = i; tot = cur;}for (int i = 1; i <= n; ++i) v[ls[i]] = v[rs[i]] = 1;for (int i = 1; i <= n; ++i) if (!v[i]) dfs(i), printf("%lld\n", f[i][m]);return 0;}```