Codeforces Round #426 (Div. 2)-The Bakery(线段树+DP)

来源:互联网 发布:益玩网络 编辑:程序博客网 时间:2024/06/07 05:48
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 ≤ 350001 ≤ 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 11 2 2 1
output
2
input
7 21 3 3 1 4 4 4
output
5
input
8 37 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.

题意:给你n个蛋糕,编号相同代表属于同一种类,现在有k个盒子,每个盒子中装的蛋糕的种类数代表该盒子的价值,问你所有盒子加在一起的总价值最大为多少。

题解:dp[i][j]:前i个蛋糕分在j个盒子中的最大价值,一个盒子中从加入第一个蛋糕开始到最后一个可以看做该区间的和,因为你当前加的蛋糕加入放在已经有该种类蛋糕的盒子中没什么意义,因此标记该种类蛋糕上次出现在哪里,放在他后边即可。。。建一个线段树保存1到n分成i段的最大价值即可。。

#include<map>    #include<stack>    #include<queue>  #include<vector>    #include<math.h>    #include<stdio.h>  #include<iostream>#include<string.h>    #include<stdlib.h>    #include<algorithm>    using namespace std;    typedef __int64 ll;    #define inf 1000000000    #define mod 1000000007   #define maxn  35005#define lowbit(x) (x&-x)    #define eps 1e-10  int g[maxn*4],n,k,lazy[maxn*4],flag[maxn],a[maxn],p[maxn],dp[maxn][55];void pushdown(int id){if(lazy[id]){g[id*2]+=lazy[id];g[id*2+1]+=lazy[id];lazy[id*2]+=lazy[id];lazy[id*2+1]+=lazy[id];lazy[id]=0;}}void updata(int id,int l,int r,int a,int b,int val){if(r<a || l>b)return;if(l>=a && r<=b){g[id]+=val;lazy[id]+=val;return;}pushdown(id);int m=(l+r)/2;if(a<=m)updata(id*2,l,m,a,b,val);if(b>m)updata(id*2+1,m+1,r,a,b,val);g[id]=max(g[id*2],g[id*2+1]);}int query(int id,int l,int r,int a,int b){if(r<a || l>b)return 0;if(l>=a && r<=b)return g[id];pushdown(id);int m=(l+r)/2,res=0;if(a<=m)res=max(res,query(id*2,l,m,a,b));if(r>m)res=max(res,query(id*2+1,m+1,r,a,b));g[id]=max(g[id*2],g[id*2+1]);return res;}int  main(void){int i,j;scanf("%d%d",&n,&k);for(i=1;i<=n;i++){scanf("%d",&a[i]);p[i]=flag[a[i]];//标记当前种类上一次出现的位置flag[a[i]]=i;//更新该种类当前位置}for(j=1;j<=k;j++){memset(g,0,sizeof(g));memset(lazy,0,sizeof(lazy));for(i=1;i<=n;i++){updata(1,1,n,p[i]+1,i,1);dp[i][j]=max(1,query(1,1,n,1,i));updata(1,1,n,i+1,i+1,dp[i][j-1]);}}printf("%d\n",dp[n][k]);return 0;}


阅读全文
1 0