Codeforces Round #426 (Div. 2) D. 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 ≤ 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 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.


题意:把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转移过来

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>


using namespace std;


const int maxn = 45000;
int maxv[maxn*4];
int add[maxn*4];


void PushUp(int rt)
{
    maxv[rt] = max(maxv[rt*2],maxv[rt*2+1]);
}
void PushDown(int l,int r,int rt)
{
    if(add[rt])
    {
        int m = (l+r)/2;
        add[rt*2] += add[rt];
        add[rt*2+1] += add[rt];
        maxv[rt*2] += add[rt];
        maxv[rt*2+1] += add[rt];
        add[rt] = 0;
    }
}


void update(int x,int y,int c, int l, int r, int rt)
{
    if (x<=l && y>=r)
    {
        maxv[rt] += c;
        add[rt] += c;
        return;
    }
    PushDown(l,r,rt);
    int m = (l + r) / 2;
    if (x <= m)
        update(x, y, c, l, m, rt*2);
    if(y > m)
        update(x, y, c, m + 1, r, rt*2+1);


    PushUp(rt);
}


int query(int ll, int rr, int l, int r, int rt)
{
    if (ll <= l && rr >= r) return maxv[rt];
    PushDown(l,r,rt);
    int m = (l + r) / 2;
    int ret = 0;
    if (ll <= m)
        ret = max(ret,query(ll, rr, l, m, rt*2));
    if (rr > m)
        ret = max(ret,query(ll, rr, m + 1, r, rt*2+1));
    return ret;
}


int a[maxn],last[maxn],now[maxn];
int dp[maxn][55];


int main(void)
{
    int n,k,i,j;
    while(scanf("%d%d",&n,&k)==2)
    {
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(now,0,sizeof(now));
        for(i=1;i<=n;i++)
        {
            last[i] = now[a[i]];
            now[a[i]] = i;
        }
        memset(dp,0,sizeof(dp));
        for(j=1;j<=k;j++)
        {
            memset(maxv,0,sizeof(maxv));
            memset(add,0,sizeof(add));
            for(i=1;i<=n;i++)
            {
                update(i,i,dp[i][j-1],0,n,1);
            }
            for(i=j;i<=n;i++)
            {
                update(max(last[i],j-1),i-1,1,0,n,1);
                dp[i][j] = query(j-1,i-1,0,n,1);
            }
        }
        printf("%d\n",dp[n][k]);
    }


    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值