D. The Bakery

Codeforces
IDCF834D
Time2000ms
Memory256MB
Difficulty
data structuresdivide and conquerdp
English · Original
Chinese · Translation
Formal · Original
<center>![image](https://espresso.codeforces.com/7ed0e4643a5b272d27907c9a1f4628457b3331cf.png)</center>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 _a_1, _a_2, ..., _a__n_ (1 ≤ _a__i_ ≤ _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. [samples] ## 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.
不久前,Slastyona 甜点师决定开设自己的面包店!她购买了所需的原料和一台神奇的烤箱,这台烤箱可以烘烤多种蛋糕,并正式开业了。 很快,支出开始超过收入,因此 Slastyona 决定研究甜点市场。她发现将蛋糕装盒销售是盈利的,并且一个盒子中包含的蛋糕类型越**不同**(记该数量为盒子的 _值_),其售价就越高。 她需要改变生产技术!问题是烤箱会自行选择蛋糕类型,而 Slastyona 无法干预。然而,她知道今天烤箱将要烘烤的 #cf_span[n] 个蛋糕的类型及其顺序。Slastyona 必须今天恰好装满 #cf_span[k] 个盒子,每个盒子必须放入若干个(至少一个)烤箱连续生产的蛋糕(换句话说,每个盒子必须包含一个连续的蛋糕段)。 Slastyona 希望最大化所有盒子的总价值。请帮助她确定这个可能的最大总价值。 第一行包含两个整数 #cf_span[n] 和 #cf_span[k](#cf_span[1 ≤ n ≤ 35000],#cf_span[1 ≤ k ≤ min(n, 50)])——分别表示蛋糕数量和盒子数量。 第二行包含 #cf_span[n] 个整数 #cf_span[a1, a2, ..., an](#cf_span[1 ≤ ai ≤ n])——表示烤箱按顺序烘烤的蛋糕类型。 请输出一个整数——所有盒子的最大总价值。 在第一个例子中,Slastyona 只有一个盒子。她必须将所有蛋糕放入其中,因此盒中有两种不同类型的蛋糕,其值为 #cf_span[2]。 在第二个例子中,将前两个蛋糕放入第一个盒子,其余全部放入第二个盒子是有利的。第一个盒子中有两种不同类型的蛋糕,第二个盒子中有三种,因此总价值为 #cf_span[5]。 ## Input 第一行包含两个整数 #cf_span[n] 和 #cf_span[k](#cf_span[1 ≤ n ≤ 35000],#cf_span[1 ≤ k ≤ min(n, 50)])——分别表示蛋糕数量和盒子数量。第二行包含 #cf_span[n] 个整数 #cf_span[a1, a2, ..., an](#cf_span[1 ≤ ai ≤ n])——表示烤箱按顺序烘烤的蛋糕类型。 ## Output 请输出一个整数——所有盒子的最大总价值。 [samples] ## Note 在第一个例子中,Slastyona 只有一个盒子。她必须将所有蛋糕放入其中,因此盒中有两种不同类型的蛋糕,其值为 #cf_span[2]。 在第二个例子中,将前两个蛋糕放入第一个盒子,其余全部放入第二个盒子是有利的。第一个盒子中有两种不同类型的蛋糕,第二个盒子中有三种,因此总价值为 #cf_span[5]。
**Definitions** Let $ n, k \in \mathbb{Z} $ be the number of cakes and the number of boxes, respectively. Let $ A = (a_1, a_2, \dots, a_n) $ be a sequence of cake types, where $ a_i \in \{1, 2, \dots, n\} $. A *box* is defined as a non-empty contiguous subsequence of $ A $. The *value* of a box is the number of distinct cake types in that subsequence. **Constraints** 1. $ 1 \leq n \leq 35000 $ 2. $ 1 \leq k \leq \min(n, 50) $ 3. The $ n $ cakes must be partitioned into exactly $ k $ non-overlapping, contiguous, non-empty segments (boxes). **Objective** Maximize the sum of the values (distinct counts) over all $ k $ boxes: $$ \max_{\substack{1 = i_0 < i_1 < \dots < i_k = n+1 \\ i_j - i_{j-1} \geq 1}} \sum_{j=1}^{k} \left| \{ a_{i_{j-1}}, a_{i_{j-1}+1}, \dots, a_{i_j - 1} \} \right| $$
Samples
Input #1
4 1
1 2 2 1
Output #1
2
Input #2
7 2
1 3 3 1 4 4 4
Output #2
5
Input #3
8 3
7 7 8 7 7 8 1 7
Output #3
6
API Response (JSON)
{
  "problem": {
    "name": "D. The Bakery",
    "description": {
      "content": "<center>![image](https://espresso.codeforces.com/7ed0e4643a5b272d27907c9a1f4628457b3331cf.png)</center>Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredi",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF834D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "<center>![image](https://espresso.codeforces.com/7ed0e4643a5b272d27907c9a1f4628457b3331cf.png)</center>Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredi...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "不久前,Slastyona 甜点师决定开设自己的面包店!她购买了所需的原料和一台神奇的烤箱,这台烤箱可以烘烤多种蛋糕,并正式开业了。\n\n很快,支出开始超过收入,因此 Slastyona 决定研究甜点市场。她发现将蛋糕装盒销售是盈利的,并且一个盒子中包含的蛋糕类型越**不同**(记该数量为盒子的 _值_),其售价就越高。\n\n她需要改变生产技术!问题是烤箱会自行选择蛋糕类型,而 Slastyona 无...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, k \\in \\mathbb{Z} $ be the number of cakes and the number of boxes, respectively.  \nLet $ A = (a_1, a_2, \\dots, a_n) $ be a sequence of cake types, where $ a_i \\in \\{1, 2, \\d...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments