Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredients and a wonder-oven which can bake several types of cakes, and opened the bakery.
Soon the expenses started to overcome the income, so Slastyona decided to study the sweets market. She learned it's profitable to pack cakes in boxes, and that the more distinct cake types a box contains (let's denote this number as the value of the box), the higher price it has.
She needs to change the production technology! The problem is that the oven chooses the cake types on its own and Slastyona can't affect it. However, she knows the types and order of n cakes the oven is going to bake today. Slastyona has to pack exactly k boxes with cakes today, and she has to put in each box several (at least one) cakes the oven produced one right after another (in other words, she has to put in a box a continuous segment of cakes).
Slastyona wants to maximize the total value of all boxes with cakes. Help her determine this maximum possible total value.
The first line contains two integers n and k (1 ≤ n ≤ 35000, 1 ≤ k ≤ min(n, 50)) – the number of cakes and the number of boxes, respectively.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the types of cakes in the order the oven bakes them.
Print the only integer – the maximum total value of all boxes with cakes.
4 1 1 2 2 1
2
7 2 1 3 3 1 4 4 4
5
8 3 7 7 8 7 7 8 1 7
6
In the first example Slastyona has only one box. She has to put all cakes in it, so that there are two types of cakes in the box, so the value is equal to 2.
In the second example it is profitable to put the first two cakes in the first box, and all the rest in the second. There are two distinct types in the first box, and three in the second box then, so the total value is 5.
题目大意 给定一个序列,分成k个非空部分,使得每部分不同的数的个数之和最大。
显然你需要一发动态规划,用f[i][j]表示前i个位置,已经分成j个非空部分的最大总价值。转移很显然:
$f[i][j] = \max\left\{ f[k][j - 1] + w\left(k + 1, i \right )\right \}$
其中$w(i, j)$表示$[i, j]$这一段中不同的数的个数。如果这么做的话是$O(n^{2}k)$,会TLE。考虑如何维护$f[k][j - 1] + w(k + 1, i)$。
有注意到一堆后缀后面增加一个数,不同的数会发生改变的后缀的长度一定是连续的一段,并且这些后缀都不包含这个数。
因此,我们可以记录一个$last[x]$,表示在当前考虑的位置$i$之前,$x$上一次出现的位置在哪。然后就可以找到增加的这一段是哪了。
对于要支持区间求最大值,区间修改,我们想到了线段树。于是就用线段树去维护dp值对当前位置的贡献。转移的时候查一查最值就好了。
Code
/**
* Codeforces
* Problem#834D
* Accepted
* Time: 857ms
* Memory: 71900k
*/
#include<bits/stdc++.h>
using namespace std; typedef class SegTreeNode {
public:
int val;
int lazy;
SegTreeNode *l, *r; SegTreeNode():val(), lazy(), l(NULL), r(NULL) { }
// SegTreeNode(int val, SegTreeNode* l, SegTreeNode* r):val(val), lazy(lazy), l(l), r(r) { } inline void pushUp() {
val = max(l->val, r->val);
} inline void pushDown() {
l->val += lazy, l->lazy += lazy;
r->val += lazy, r->lazy += lazy;
lazy = ;
}
}SegTreeNode; #define Limit 4000000
SegTreeNode pool[Limit];
int top = ; SegTreeNode* newnode() {
if(top >= Limit) return new SegTreeNode();
pool[top] = SegTreeNode();
return pool + (top++);
} typedef class SegTree {
public:
SegTreeNode **rts; SegTree():rts(NULL) { }
SegTree(int k, int n) {
rts = new SegTreeNode*[(k + )];
for(int i = ; i <= k; i++)
build(rts[i], , n);
} void build(SegTreeNode*& node, int l, int r) {
node = newnode();
if(l == r) return;
int mid = (l + r) >> ;
build(node->l, l, mid);
build(node->r, mid + , r);
} void update(SegTreeNode*& node, int l, int r, int ql, int qr, int val) {
if(l == ql && r == qr) {
node->val += val;
node->lazy += val;
return;
}
if(node->lazy) node->pushDown();
int mid = (l + r) >> ;
if(qr <= mid) update(node->l, l, mid, ql, qr, val);
else if(ql > mid) update(node->r, mid + , r, ql, qr, val);
else {
update(node->l, l, mid, ql, mid, val);
update(node->r, mid + , r, mid + , qr, val);
}
node->pushUp();
} int query(SegTreeNode*& node, int l, int r, int ql, int qr) {
if(l == ql && r == qr) return node->val;
if(node->lazy) node->pushDown();
int mid = (l + r) >> ;
if(qr <= mid) return query(node->l, l, mid, ql, qr);
if(ql > mid) return query(node->r, mid + , r, ql, qr);
return max(query(node->l, l, mid, ql, mid), query(node->r, mid + , r, mid + , qr));
} SegTreeNode*& operator [] (int pos) {
return rts[pos];
}
}SegTree; int n, k;
int *arr;
int res = ; inline void init() {
scanf("%d%d", &n, &k);
arr = new int[(n + )];
for(int i = , x; i <= n; i++)
scanf("%d", arr + i); } SegTree st;
int f[][];
int last[];
inline void solve() {
st = SegTree(k, n);
memset(f, , sizeof(f[]) * (n + ));
memset(last, , sizeof(int) * (n + ));
for(int i = ; i <= n; i++) {
for(int j = ; j <= k && j <= i; j++) {
// printf("Segment [%d, %d] has been updated.", last[arr[i]] + 1, i - 1),
st.update(st[j - ], , n, last[arr[i]], i - , );
f[i][j] = st.query(st[j - ], , n, , i - );
st.update(st[j], , n, i, i, f[i][j]);
// cout << i << " " << j << " " << f[i][j] << endl;
}
last[arr[i]] = i;
}
printf("%d\n", f[n][k]);
} int main() {
init();
solve();
return ;
}