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.

Input

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.

Output

Print the only integer – the maximum total value of all boxes with cakes.

Examples
Input
4 1 1 2 2 1
Output
2
Input
7 2 1 3 3 1 4 4 4
Output
5
Input
8 3 7 7 8 7 7 8 1 7
Output
6
Note

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 ;
}

Codeforces 834D The Bakery - 动态规划 - 线段树的更多相关文章

  1. Codeforces 834D The Bakery 【线段树优化DP】*

    Codeforces 834D The Bakery LINK 题目大意是给你一个长度为n的序列分成k段,每一段的贡献是这一段中不同的数的个数,求最大贡献 是第一次做线段树维护DP值的题 感觉还可以, ...

  2. Codeforces 833B The Bakery dp线段树

    B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

  3. BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树

    BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树 题意:  约翰的奶牛们从小娇生惯养,她们无法容忍牛棚里的任何脏东西.约翰发现,如果要使这群 ...

  4. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  5. 2019牛客多校第一场 I Points Division(动态规划+线段树)

    2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...

  6. Codeforces 834D The Bakery【dp+线段树维护+lazy】

    D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...

  7. Codeforces 834D - The Bakery(dp+线段树)

    834D - The Bakery 思路:dp[i][j]表示到第j个数为止分成i段的最大总和值. dp[i][j]=max{dp[i-1][x]+c(x+1,j)(i-1≤x≤j-1)},c(x+1 ...

  8. CodeForces 834D The Bakery(线段树优化DP)

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...

  9. Codeforces 675E Trains and Statistic - 线段树 - 动态规划

    题目传送门 快速的vjudge通道 快速的Codeforces通道 题目大意 有$n$个火车站,第$i$个火车站出售第$i + 1$到第$a_{i}$个火车站的车票,特殊地,第$n$个火车站不出售车票 ...

随机推荐

  1. shiro loginUrl拦截无效

    logUrl不拦截 或者 只跳转到/login.jsp 不跳到自己设置登录链接 在springmvc或事务那里 开启spring的显示代理(即cglib),并将shiro的安全管理器交给spring管 ...

  2. ASP.NET Core 启动流程图

    简洁描述: 一   WebHostBuilder.Build() =>1注入公共的实例 2创建WebHost实例 3注入自定义实例 4创建IServer 5添加中间件(_components集合 ...

  3. react native 中使用react-native-vector-icons

    1.引入依赖 cnpm install react-native-vector-icons --save 2.LINK原生 react-native link react-native-vector- ...

  4. mint-ui Picker的使用

    <template> <div v-bind:style="{minHeight:clientHeight + 'px'}" id="recive-mi ...

  5. mysql----------局域网数据库:如何让navicat链接局域网其他的数据库。

    1.找到被链接的数据库,打开以后有一个自带的mysql数据库,打开以后下面有一个user表,把里面的第一条数据的第一个字段改成% 百分号,然后保存,重启数据库,搞定 2.如果是linux下的话,记得把 ...

  6. EF性能优化

    下面总结了一些在使用EF的过程中应当特别注意的地方,避免大家再走弯路. 1.分清真分页和假分页 大家都知道分页分为真分页和假分页,并且假分页是特别耗费性能的.我们在使用的过程中也是以真分页为主,但是在 ...

  7. jQuery文档操作--append()、prepend()、after()和before()

       append(content|fn)  概述 向每个匹配的元素内部追加内容,这个操作与对指定的元素执行appendChild方法,将它们添加到文档中的情况类似 参数    content  要追 ...

  8. 多线程:Operation(一)

    1. 进程和线程 1.1 进程 进程:正在运行的应用程序叫进程 进程之间都是独立的,运行在专用且受保护的内存空间中 两个进程之间无法通讯 通俗的理解,手机上同时开启了两个App.这两个App肯定是在不 ...

  9. 【Linux学习十】负载均衡带来tomcat的session不一致问题

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 tomcat7 jdk7 session不一致是指web服务器(tom ...

  10. flask模板应用-空白控制

    模板应用实践 空白控制 在实际输出的HTML文件中,模板中的jinja2语句.表达式和注释会保留移除后的空行. 例如下面的代码: {% set user.age = 23 %} {% if urer. ...