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. vue2.0项目中 localhost改成ip地址访问

    这里 你可以写成你的ip  那你的项目只能ip访问了,但是写成0.0.0.0的话 你既可已localhost 访问也可以ip访问 也可以写成 127.0.0.1也可以,也能local访问了和ip访问( ...

  2. cocos2d JS-(JavaScript) 几种循环遍历对象的比较

    通常我们会用循环的方式来遍历数组.但是循环是 导致js 性能问题的原因之一.一般我们会采用下几种方式来进行数组的遍历: 方式1: for in 循环: var arr = [1,2,3,4,5]; v ...

  3. UGUI 打图集

    using UnityEngine; using System.Collections; using UnityEditor; using System.Collections.Generic; us ...

  4. Bootstrap-全局样式的文本颜色和背景颜色

    .text-五种颜色   文本颜色.text-info文本浅蓝颜色-提示.text-warning文本黄色-警告颜色.text-success文本绿色-成功颜色.text-primary文本深蓝色-警 ...

  5. Ecust DIV3 k进制 【暴力不断优化】

    K进制 Description 给定一个正整数n,请你判断在哪些进制下n的表示恰好有2位是1,其余位都是0. Input 输入第一行为整数TT,表示有TT组数据(1 \le T \le 50)(1≤T ...

  6. Codeforces Round #323

    div1 C 这题的是给了一个无限循环的子数组,问有多少个 (l,s)l代表起点s代表长度的循环串,使得所有的在原串中的每位都小于等于另外这个串(l<=n,1<=s<n) 像这样,我 ...

  7. WebAppInitializer类,代替web.xml

    package com.ssm.yjblogs.config; import javax.servlet.MultipartConfigElement; import javax.servlet.Se ...

  8. arc 093 C – Traveling Plan

    题意: 给出横坐标上一系列的点,一个人从0出发按照下标顺序访问每一个点,再回到0点. 问每次如果去掉一个点,那么访问的距离变为多少. 思路: 去掉这个点,那么就减去这个点到上一点到这一点的距离,减去这 ...

  9. 【JavaScript 6连载】三、构造函数

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  10. JS中对象与数组(大括号{}与中括号[])

    一.{ } 大括号,表示定义一个对象,大部分情况下要有成对的属性和值,或是函数. 如:var LangShen = {"Name":"Langshen",&qu ...