The Bakery CodeForces - 834D (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.

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[i-1][k]+v[k,j])表示将前j个数,放入i个篮子里的最大价值。

如果直接做的话,会超时。

首先这个类似于将j个数分成i段的问题,先考虑滚动数组,将数组将为1为,dp[j]表示前j个数的最大价值,在更新dp[i][j]时,相当于在dp[i-1][j]里在多加一个篮子,所以考虑找前j个中最大的dp[k]即可。对于a[j],它能影响到的范围只是,前面没有它的这一部分,所以每当加入a[j],只要更新pre[j]到j就行(pre[j]为上一个出现a[j]的位置)。更新后,直接查(1,j)的最大值,就行dp[j]的最大值。

做这样的dp,首先要能完全的理解这个dp转移的进行过程,其次,经验非常重要。

代码:

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
#define Lson l,m,rt<<1
#define Rson m+1,r,rt<<1|1
#define inf 0x3f3f3f3f
int n,k;
int a[maxn];
int pre[maxn],last[maxn];
int dp[maxn];
struct Tree
{
    int l,r,Max,lazy;
}tree[maxn<<2];
void push_up(int rt)
{
    tree[rt].Max=max(tree[rt<<1].Max,tree[rt<<1|1].Max);
}
void Build(int l,int r,int rt)
{
    tree[rt].l=l,tree[rt].r=r;
    tree[rt].lazy=0;
    if(l==r)
    {
        tree[rt].Max=dp[l];//把前一轮的dp值,作为基础
        return;
    }
    int m=(l+r)>>1;
    Build(Lson);
    Build(Rson);
    push_up(rt);
}
void push_down(int rt)
{
    if(tree[rt].lazy)
    {
        int tmp=tree[rt].lazy;
        tree[rt<<1].Max+=tmp;
        tree[rt<<1|1].Max+=tmp;
        tree[rt<<1].lazy+=tmp;
        tree[rt<<1|1].lazy+=tmp;
        tree[rt].lazy=0;
    }
}
void updata(int L,int R,int v,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        tree[rt].Max+=v;
        tree[rt].lazy+=v;
        return;
    }
    push_down(rt);
    int m=(l+r)>>1;
    if(L<=m) updata(L,R,v,Lson);
    if(R>m) updata(L,R,v,Rson);
    push_up(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return tree[rt].Max;
    }
    push_down(rt);
    int m=(l+r)>>1;
    int ans1=0,ans2=0;
    if(R>m) ans1=query(L,R,Rson);
    if(L<=m) ans2=query(L,R,Lson);
    push_up(rt);
    return max(ans1,ans2);
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    scanf("%d%d",&n,&k);
    memset(last,-1,sizeof(last));
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        pre[i]=last[a[i]];
        last[a[i]]=i;
        if(pre[i]==-1)
        {
            cnt++;
        }
        dp[i]=cnt;
    }
    for(int i=2;i<=k;i++)
    {
        for(int j=1;j<i-1;j++) dp[j]=-inf;
        Build(1,n,1);//每次都要重新建树,因为每次都新加一个篮子
        for(int j=i;j<=n;j++)
        {
            int l=max(1,pre[j]),r=j-1;//找到a[j]影响的区间
            updata(l,r,1,1,n,1);
            l=1,r=j-1;
            dp[j]=query(l,r,1,n,1);
        }
    }
    printf("%d\n",dp[n]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/82085857