Description
将一个长度为n的序列分为k段使得总价值最大。一段区间的价值表示为区间内不同数字的个数
Solution
容易得到一个简单的DP,
d
p
i
,
j
dp_{i,j}
dpi,j表示到了第
i
i
i位、用了
j
j
j段的最大价值,那么有
d
p
i
,
j
=
max
(
d
p
k
,
j
−
1
+
v
a
l
u
e
(
k
+
1
,
i
)
)
dp_{i,j}=\max(dp_{k,j-1}+value(k+1,i))
dpi,j=max(dpk,j−1+value(k+1,i))。
我们可以先枚举
j
j
j,发现每次
i
+
1
i+1
i+1,影响到的
v
a
l
u
e
(
k
+
1
,
i
)
value(k+1,i)
value(k+1,i)只有该权值上一次出现的位置到
i
i
i这一段区间,用线段树维护即可。
Code
/************************************************
* Au: Hany01
* Prob: CF883B
* Email: hany01@foxmail.com
* Inst: Yali High School
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
static int _, __; static char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 35005;
int tr[maxn << 2], tag[maxn << 2], dp[maxn], n, las[maxn], pos[maxn];
#define lc (t << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
inline void pushdown(int t) {
if (tag[t]) tag[lc] += tag[t], tag[rc] += tag[t], tr[lc] += tag[t], tr[rc] += tag[t], tag[t] = 0;
}
void build(int t, int l, int r) {
tag[t] = 0;
if (l == r) tr[t] = dp[l];
else build(lc, l, mid), build(rc, mid + 1, r), tr[t] = max(tr[lc], tr[rc]);
}
void update(int t, int l, int r, int x, int y) {
if (x <= l && r <= y) ++ tag[t], ++ tr[t];
else {
pushdown(t);
if (x <= mid) update(lc, l, mid, x, y);
if (y > mid) update(rc, mid + 1, r, x, y);
tr[t] = max(tr[lc], tr[rc]);
}
}
int query(int t, int l, int r, int x, int y) {
if (x <= l && r <= y) return tr[t];
pushdown(t);
if (y <= mid) return query(lc, l, mid, x, y);
if (x > mid) return query(rc, mid + 1, r, x, y);
return max(query(lc, l, mid, x, y), query(rc, mid + 1, r, x, y));
}
int main()
{
#ifdef hany01
freopen("cf833b.in", "r", stdin);
freopen("cf833b.out", "w", stdout);
#endif
static int t, T;
n = read(), T = read();
For(i, 1, n) t = read(), las[i] = pos[t], pos[t] = i;
while (T --) {
build(1, 0, n);
For(i, 1, n) update(1, 0, n, las[i], i - 1), dp[i] = query(1, 0, n, 0, i - 1);
}
printf("%d\n", dp[n]);
return 0;
}