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.
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.
Print the only integer – the maximum total value of all boxes with cakes.
4 1
1 2 2 1
2
7 2
1 3 3 1 4 4 4
5
8 3
7 7 8 7 7 8 1 7
6
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式:dp[i][k]=max(dp[j][k-1]+num[j+1][i]),表示处理到第i个蛋糕时用了k个盒子的最大价值 //是由 第j(0<j<i)个蛋糕用k-1个盒子的价值+j+1到i中的种类数 中最大的那个转移得到。但是这样是O(nnm) //的,显然不行。中间找最大值可以用线段树O(logn)处理,就可以了。其中每次更新 dp[j][k-1]+num[j+1][i] //到线段树中,dp可以在建树时加入,num[j+1][i]就是求之间有多少不同的数,每加入一个a[i]时,这个点到 //上一次这个值出现的位置之间的都加1即可。 #include<bits/stdc++.h> using namespace std; const int maxn=35000; int n,k,a[maxn+10],now[maxn+10],pre[maxn+10]; int dp[maxn+10],maxv[maxn*4+10],add[maxn*4+10]; void pushup(int rt){ maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]); } void pushdown(int rt){ if(add[rt]){ add[rt<<1]+=add[rt]; add[rt<<1|1]+=add[rt]; maxv[rt<<1]+=add[rt]; maxv[rt<<1|1]+=add[rt]; add[rt]=0; } } void build(int l,int r,int rt){ add[rt]=0; //记住!!! if(l==r){ maxv[rt]=dp[l]; return; } int mid=(l+r)>>1; build(l,mid,rt<<1); build(mid+1,r,rt<<1|1); pushup(rt); } void update(int ql,int qr,int v,int l,int r,int rt){ if(ql<=l&&qr>=r){ add[rt]+=v; maxv[rt]+=v; return; } pushdown(rt); int mid=(l+r)>>1; if(ql<=mid) update(ql,qr,v,l,mid,rt<<1); if(qr>mid) update(ql,qr,v,mid+1,r,rt<<1|1); pushup(rt); } int query(int ql,int qr,int l,int r,int rt){ if(ql<=l&&qr>=r) return maxv[rt]; pushdown(rt); int mid=(l+r)>>1,ans=0; if(ql<=mid) ans=max(ans,query(ql,qr,l,mid,rt<<1)); if(qr>mid) ans=max(ans,query(ql,qr,mid+1,r,rt<<1|1)); return ans; } int main() { scanf("%d%d",&n,&k); memset(now,0,sizeof(now)); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); pre[i]=now[a[i]]; now[a[i]]=i; } memset(dp,0,sizeof(dp)); for(int j=1;j<=k;j++){ build(0,n,1); for(int i=1;i<=n;i++){ update(pre[i],i-1,1,0,n,1); dp[i]=query(0,i-1,0,n,1); } } printf("%d ",dp[n]); return 0; }