专栏首页小樱的经验随笔Codeforces 834D The Bakery【dp+线段树维护+lazy】

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

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.

题目链接:http://codeforces.com/contest/834/problem/D

题意:把n个数分成k段,每段的价值等于这一段内不同数字的个数,求总的最大价值。

可以很快发现这是一个dp,dp[i][j]表示到第i个数字,已经分成了k段的最大价值。

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

可以发现转移不是那么容易,所以我们用到线段树去维护当前位置前面的最大价值。

对于状态i,j,线段树维护的是1~i-1的最大值

对于每一个位置,找到前面最后一个与它数字相同的的位置,把这之间线段树的值都加上1,然后dp[i][j]的值就是j-1到i-1的最大值。

最后答案就是dp[n][k]。

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

下面给出AC代码:【二维数组改写成一维数组(个人原因,不太喜欢高维度的)】

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define maxn 35010
 4 #define INF 0x3f3f3f3f
 5 int addv[maxn*4],Max[maxn*4];
 6 int dp[maxn],ql,qr;
 7 int pre[maxn], last[maxn], a[maxn];
 8 void build(int l,int r,int o)
 9 {
10     addv[o]=0;
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;
24     if(addv[o])
25     {
26         addv[lc]+=addv[o];
27         addv[rc]+=addv[o];
28         Max[lc]+=addv[o];
29         Max[rc]+=addv[o];
30         addv[o]=0;
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     {
39         addv[o]++;
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...

    Angel_Kitty
  • POJ 1018 Communication System

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

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

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

    Angel_Kitty
  • P2871 [USACO07DEC]手链Charm Bracelet

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

    attack
  • LeetCode Weekly Contest 46解题思路

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

    用户1147447
  • P2946 [USACO09MAR]牛飞盘队Cow Frisbee Team

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

    attack
  • LeetCode 122. Best Time to Buy and Sell Stock II

    题目:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/

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

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

    用户2965768
  • 基础练习 阶乘计算

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

    刘开心_1266679
  • 基数排序

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

    233333

扫码关注云+社区

领取腾讯云代金券