CF-833B The Bakery(线段树优化Dp)

Posted blog-dr-j

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF-833B The Bakery(线段树优化Dp)相关的知识,希望对你有一定的参考价值。

 
技术分享图片

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.

解题思路:

这道题是DP,一个数做出贡献仅在上一次到这次之间,线段树维护区间最大值,查询O(logn)

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define lll spc<<1
 5 #define rrr spc<<1|1
 6 using std::max;
 7 struct trnt{
 8     int lzt;
 9     int mxs;
10 }tr[10000000],str;
11 int dp[100000];
12 int lst[100000];
13 int nw[100000];
14 int n,K;
15 void pushup(int spc)
16 {
17     tr[spc].mxs=max(tr[lll].mxs,tr[rrr].mxs);
18     return ;
19 }
20 void add(int spc,int v)
21 {
22     tr[spc].lzt+=v;
23     tr[spc].mxs+=v;
24     return ;
25 }
26 void pushdown(int spc)
27 {
28     if(tr[spc].lzt)
29     {
30         add(lll,tr[spc].lzt);
31         add(rrr,tr[spc].lzt);
32         tr[spc].lzt=0;
33     }
34     return ;
35 }
36 void build(int l,int r,int spc)
37 {
38     tr[spc]=str;
39     if(l==r)
40     {
41         tr[spc].mxs=dp[l-1];
42         return ;
43     }
44     int mid=(l+r)>>1;
45     build(l,mid,lll);
46     build(mid+1,r,rrr);
47     pushup(spc);
48     return ;
49 }
50 void update(int l,int r,int ll,int rr,int spc,int v)
51 {
52     if(l>rr||ll>r)
53         return ;
54     if(ll<=l&&r<=rr)
55     {
56         add(spc,v);
57         return ;
58     }
59     pushdown(spc);
60     int mid=(l+r)>>1;
61     update(l,mid,ll,rr,lll,v);
62     update(mid+1,r,ll,rr,rrr,v);
63     pushup(spc);
64     return ;
65 }
66 int query(int l,int r,int ll,int rr,int spc)
67 {
68     if(l>rr||ll>r)
69         return -0x3f3f3f3f;
70     if(ll<=l&&r<=rr)
71         return tr[spc].mxs;
72     pushdown(spc);
73     int mid=(l+r)>>1;
74     return max(query(l,mid,ll,rr,lll),query(mid+1,r,ll,rr,rrr));
75 }
76 int main()
77 {
78     scanf("%d%d",&n,&K);
79     for(int i=1;i<=n;i++)
80     {
81         int tmp;
82         scanf("%d",&tmp);
83         lst[i]=nw[tmp];
84         nw[tmp]=i;
85     }
86     for(int i=1;i<=K;i++)
87     {
88         build(1,n,1);
89         for(int j=0;j<=n;j++)
90             dp[j]=0;
91         for(int j=i;j<=n;j++)
92         {
93             update(1,n,lst[j]+1,j,1,1);
94             dp[j]=query(1,n,i,j,1);
95         }
96     }
97     printf("%d
",dp[n]);
98     return 0;
99 }

 

以上是关于CF-833B The Bakery(线段树优化Dp)的主要内容,如果未能解决你的问题,请参考以下文章

CF833B The Bakery

cf 833B 线段树优化dp

题解CF#426(Div. 1) B.The Bakery

codeforces#426(div1) B - The Bakery (线段树 + dp)

Codeforces 834D The Bakery - 动态规划 - 线段树

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