【CF833B】The Bakery

题目

题意翻译
将一个长度为n的序列分为k段

使得总价值最大一段区间的价值表示为区间内不同数字的个数

n<=35000,k<=50

Translated by @ysner @yybyyb

题目描述
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 n cakes the oven is going to bake today. Slastyona has to pack exactly k 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 n and k k ( 1<=n<=35000 1<=n<=35000 , 1<=k<=min(n,50) 1<=k<=min(n,50) ) – the number of cakes and the number of boxes, respectively.

The second line contains n n integers a_{1},a_{2},…,a_{n} a
1
​ ,a
2
​ ,…,a
n
​ ( 1<=a_{i}<=n 1<=a
i
​ <=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.

输入输出样例
输入样例#1: 复制
4 1
1 2 2 1
输出样例#1: 复制
2
输入样例#2: 复制
7 2
1 3 3 1 4 4 4
输出样例#2: 复制
5
输入样例#3: 复制
8 3
7 7 8 7 7 8 1 7
输出样例#3: 复制
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 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 5 .

思路

这道题很好列出状态转移方程,dp[i][j]=max{dp[k][j-1]+cnt[k+1][i]}

dp[i][j]意为1~i之间分割j次所产生的最大值。cnt[i][j]表示i-j之间不同的颜色个数。

看到max可以考虑线段树优化,也就是如果我们有办法在O(logn)的时间内计算出cnt[i][j]的值就可以了,考虑一种颜色能够产生贡献的范围,为他上一次出现的位置到他当前的位置,产生贡献为1,可以使用线段树区间加,给pre[i]~i都加上1,从1到n进行枚举,逐渐进行上面的操作,每次换下一层的时候重构线段树,这样计算cnt[i][j]的复杂度就变成了logn的 orz

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson root<<1
#define rson root<<1|1 
using namespace std;

struct node
{
    int l,r,sum,lazy;
}tr[140010];
int dp[60][40000],pre[35010],pos[35010];

void init()
{
    memset(tr,0,sizeof(tr));
}

void push_up(int root)
{
    tr[root].sum=max(tr[lson].sum,tr[rson].sum);
}

void push_down(int root)
{
    tr[lson].sum+=tr[root].lazy;
    tr[lson].lazy+=tr[root].lazy;
    tr[rson].sum+=tr[root].lazy;
    tr[rson].lazy+=tr[root].lazy;
    tr[root].lazy=0;
}

void build(int root,int l,int r,int now)
{
    if(l==r)
    {
        tr[root].l=l;
        tr[root].r=r;
        tr[root].sum=dp[now][l-1];
        return ;
    }
    tr[root].l=l;
    tr[root].r=r;
    int mid=(l+r)>>1;
    build(lson,l,mid,now);
    build(rson,mid+1,r,now);
    push_up(root);
}

void update(int root,int l,int r,int val)
{
    if(tr[root].l==l&&tr[root].r==r)
    {
        tr[root].sum+=val;
        tr[root].lazy+=val;
        return ;
    }
    if(tr[root].lazy)
    {
        push_down(root);
    }
    int mid=(tr[root].l+tr[root].r)>>1;
    if(mid<l)
    {
        update(rson,l,r,val);
    }
    else
    {
        if(mid>=r)
        {
            update(lson,l,r,val);
        }
        else
        {
            update(lson,l,mid,val);
            update(rson,mid+1,r,val);
        }
    }
    push_up(root);
}

int query(int root,int l,int r)
{
    if(tr[root].l==l&&tr[root].r==r)
    {
        return tr[root].sum;
    }
    if(tr[root].lazy)
    {
        push_down(root);
    }
    int mid=(tr[root].l+tr[root].r)>>1;
    if(mid<l)
    {
        return query(rson,l,r);
    }
    else
    {
        if(mid>=r)
        {
            return query(lson,l,r);
        }
        else
        {
            return max(query(lson,l,mid),query(rson,mid+1,r));
        }
    }
}

int main()
{
    int n,k,t;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&t);
        pre[i]=pos[t]+1;
        pos[t]=i;
    }
    for(int i=1;i<=k;i++)
    {
        init();
        build(1,1,n,i-1);
        for(int j=1;j<=n;j++)
        {
            update(1,pre[j],j,1);
            dp[i][j]=query(1,1,j);
        }
    }
    printf("%d\n",dp[k][n]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值