# D. The Bakery

time limit per test:2.5 seconds

memory limit per test:256 megabytes

input:standard input

output:standard output

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.

dp[i][j] = max(dp[t][j-1]) (1<= t < i)

（注意线段树的区间范围是0~n，因为可以直接从0转移过来）

``` 1 #include <bits/stdc++.h>
2 using namespace std;
3 #define maxn 35010
4 #define INF 0x3f3f3f3f
6 int dp[maxn],ql,qr;
7 int pre[maxn], last[maxn], a[maxn];
8 void build(int l,int r,int o)
9 {
11     if(l == r)
12     {
13         Max[o]=dp[l];
14         return;
15     }
16     int mid=l+(r-l)/2;
17     build(l,mid,o*2);
18     build(mid+1,r,o*2+1);
19     Max[o]=max(Max[o*2],Max[o*2+1]);
20 }
21 void pushdown(int o)
22 {
23     int lc=o*2,rc=o*2+1;
25     {
31     }
32 }
33 void update(int l,int r,int o)
34 {
35     if(ql>qr)
36         return;
37     if(ql<=l&&qr>=r)
38     {
40         Max[o]++;
41         return;
42     }
43     pushdown(o);
44     int mid=l+(r-l)/2;
45     if(ql<=mid)
46        update(l,mid,o*2);
47     if(qr>mid)
48        update(mid+1,r,o*2+1);
49     Max[o]=max(Max[o*2],Max[o*2+1]);
50 }
51 int query(int l,int r,int o)
52 {
53     if(ql<=l&&qr>=r)
54     {
55         return Max[o];
56     }
57     pushdown(o);
58     int mid=l+(r-l)/2;
59     int best=-INF;
60     if(ql<=mid)
61        best=max(best,query(l,mid,o*2));
62     if(qr>mid)
63        best=max(best,query(mid+1,r,o*2+1));
64     return best;
65 }
66 int main()
67 {
68     int n,k;
69     scanf("%d%d",&n,&k);
70     memset(last,-1,sizeof(last));
71     int cnt=0;
72     for(int i=1;i<=n;i++)
73     {
74         scanf("%d",&a[i]);
75         pre[i]=last[a[i]];
76         last[a[i]]=i;
77         if(pre[i]==-1)
78            cnt++;
79         dp[i]=cnt;
80     }
81     for(int kk=2;kk<=k;kk++)
82     {
83         for(int i=1;i<kk-1;i++)
84             dp[i]=-INF;
85         build(1,n,1);
86         for(int i=kk;i<=n;i++)
87         {
88             ql=max(1,pre[i]),qr=i-1;
89             update(1,n,1);
90             ql=1,qr=i-1;
91             dp[i]=query(1,n,1);
92         }
93     }
94     printf("%d\n",dp[n]);
95     return 0;
96 }```

官方题解：

``` 1 #include <cstdio>
2 #include <cstring>
3 #include <map>
4
5 #define K first
6 #define V second
7
8 const int N = 35001;
9
10 int last[N], pre[N], dp[N];
11
12 int main()
13 {
14     int n, m;
15     while (scanf("%d%d", &n, &m) == 2) {
16         memset(last, 0, sizeof(last));
17         for (int i = 1, a; i <= n; ++ i) {
18             scanf("%d", &a);
19             pre[i] = last[a];
20             last[a] = i;
21         }
22         dp[0] = 0;
23         for (int i = 1; i <= n; ++ i) {
24             dp[i] = dp[i - 1] + !pre[i];
25         }
26         for (int k = 2; k <= m; ++ k) {
27             std::map<int, int> c;
28             c[0] = n + 1;
29             int last_dp = dp[k - 1];
30             for (int i = k; i <= n; ++ i) {
31                 int now = 0;
32                 while (now + c.rbegin()->V <= last_dp) {
33                     now += c.rbegin()->V;
34                     c.erase(c.rbegin()->K);
35                 }
36                 c.rbegin()->V += now - last_dp;
37                 c[i] = last_dp + 1;
38                 auto it = c.upper_bound(pre[i]);
39                 it --;
40                 it->V --;
41                 if (it->V == 0) {
42                     c.erase(it->K);
43                 }
44                 last_dp = dp[i];
45                 dp[i] = (n + 1) - c.begin()->V;
46             }
47         }
48         printf("%d\n", dp[n]);
49     }
50 }```

0 条评论

• ### POJ 1804 Brainman(5种解法，好题，【暴力】，【归并排序】，【线段树单点更新】，【树状数组】，【平衡树】)

Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1057...

• ### POJ 1018 Communication System

Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submi...

• ### 浅谈我对动态规划的一点理解---大家准备好小板凳，我要开始吹牛皮了~~~

前言 作为一个退役狗跟大家扯这些东西，感觉确实有点。。。但是，针对网上没有一篇文章能够很详细的把动态规划问题说明的很清楚，我决定还是拿出我的全部家当，来跟大家分...

• ### P2871 [USACO07DEC]手链Charm Bracelet

题目描述 Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of ...

• ### LeetCode Weekly Contest 46解题思路

版权声明：本文为博主原创文章，未经博主允许不得转载。 https://blog.csdn.n...

• ### P2946 [USACO09MAR]牛飞盘队Cow Frisbee Team

题目描述 After Farmer Don took up Frisbee, Farmer John wanted to join in the fun. He...

• ### Mato的文件管理 数据离散化+莫队+树状数组求逆序数

l变化时，区间左边元素，求和减去本身1为逆序数。 即本来有 sum(x) - 1个元素在我后面，现在我在第一个。

• ### 基础练习 阶乘计算

n!可能很大，而计算机能表示的整数范围有限，需要使用高精度计算的方法。使用一个数组A来表示一个大整数a，A[0]表示a的个位，A[1]表示a的十位，依次类推...

• ### 基数排序

基数排序的排序原理不难理解，但是在算法设计上，个人感觉还是比那些常见的排序要难的，耐心慢慢一步步理解，还是比较容易看懂的，注意基数排序有两种，一种是高位优先，一...