Codeforces 834D The Bakery【Dp+线段树】好题~

探讨了如何通过优化蛋糕装箱策略来最大化收益的问题,利用DP算法结合线段树优化实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ≤ 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.

题目大意:

给你长度为N的一个序列,让你将其分成连续的k段,每段的价值为其中数字种类的个数,求最大价值总和。


思路:


显然我们很容易写出一个n^2k的一个Dp.

设定Dp【i】【j】表示到位子j,分成了i段的最大价值总和。


那么有:Dp【i】【j】=max(Dp【i】【j】,Dp【i-1】【k】+val【k+1,j】);

这里我们可以用线段树加速转移。

那么有:Dp【i】【j】=Query(0,j-1);但是直接这样跑需要建K棵线段树,然而我们可以滚动一下数组,那么一颗线段树就够了。


接下来考虑val【k+1,j】入树。

我们遍历到第j个位子的时候,我们显然树上第k个位子表示的是Dp【i-1】【k】+Val【k+1,j】,那么考虑第j个数,它会对区间【last[a[j]],j-1】区间内的树上的位子有所影响。

那么我们遍历到第j个位子的时候,将树上区间【last[a[j]],j】的值都加一。

这里last[a[j]]表示的是a【j】这个数上一次出现的位子。

过程更新一下就行。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<map>
#include<algorithm>
using namespace std;
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
int tree[350050*8];
int flag[350050*8];
int a[350050];
int last[350050];
int dp[350050];
int n,k;
void build(int l,int r,int rt){
    flag[rt]=0;
    if(l==r)
    {
        tree[rt]=dp[l];
        flag[rt]=0;
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
void pushdown(int l,int r,int rt)//向下维护树内数据
{
    if(flag[rt])//如果贪婪标记不是0(说明需要向下进行覆盖区间(或点)的值)
    {
        int m=(l+r)/2;
        flag[rt*2]+=flag[rt];
        flag[rt*2+1]+=flag[rt];
        tree[rt*2]+=flag[rt];//千万理解如何覆盖的区间值(对应线段树的图片理解(m-l)+1)是什么意识.
        tree[rt*2+1]+=flag[rt];
        flag[rt]=0;
    }
}
void pushup(int rt)
{
    tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
int Query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return tree[rt];
    }
    else
    {
        pushdown(l,r,rt);
        int m=(l+r)>>1;
        int ans=0;
        if(L<=m)
        {
            ans=max(ans,Query(L,R,lson));
        }
        if(m<R)
        {
            ans=max(ans,Query(L,R,rson));
        }
        pushup(rt);
        return ans;
    }
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)//覆盖的是区间~
    {
        tree[rt]+=c;//覆盖当前点的值
        flag[rt]+=c;//同时懒惰标记~!
        return ;
    }
    pushdown(l,r,rt);
    int m=(l+r)/2;
    if(L<=m)
    {
        update(L,R,c,lson);
    }
    if(m<R)
    {
        update(L,R,c,rson);
    }
    pushup(rt);
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=k;i++)
        {
            build(0,n,1);
            for(int j=1;j<=n;j++)dp[j]=0,last[a[j]]=0;
            for(int j=1;j<=n;j++)
            {
                update(last[a[j]],j-1,1,0,n,1);
                last[a[j]]=j;
                dp[j]=Query(0,j-1,0,n,1);
            }
        }
        printf("%d\n",dp[n]);
    }
}m 










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值