codeforces 833B The Bakery

http://www.elijahqi.win/2018/01/07/codeforces-833-b-the-bakery/
B. 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.

orz icefox太强啦 我到先在写的都是巨佬暑假的题 看了他的方法写完后又 膜了眼代码 发现他的有点小瑕疵 不管啦 反正他巨啊 orz

题意:把n个数字分成k段然后呢 每段里有多少不同数字就是这段的贡献求 把n个数字分成k段的贡献和最大是多少 那么首先来看 蒟蒻我也想到的朴素dp

dp[i][j]表示1~i个分成j段最大的贡献是多少 num表示一段区间内不同数字的个数有多少个

那显然dp[i][j]=max(dp[z][j-1]+num[z+1][i],dp[i][j]){z=j-1~i) 这样显然是会tle的 那么想一想如何去优化

大概有一种线段树的做法 那就是我把dp[z][j-1]+num[z+1][i] 这个式子看作一个叶子结点放在线段树中 那么显然我每次去转移的时候都是在k-1~i-1当中选一个最大的来转移 那么每次转移之前我该怎样把num[z-1][j-1]调整成为num[z-1][j]呢 因为我可以知道一定只是增加了j这一个位置的数那么它显然是对pre[j]+1~j这些位置的叶子节点都产生了+1的贡献 那么我直接在线段树上区间加1即可了

那么dp变成每次我枚举k表示我一共分了k段我的最有代价分别是什么 每次枚举到这个kk的时候我都需要用上一层的dp值来重新建一下线段树 然后每次一边转移一边更新 ,这个pre数组就是我当前i号位置的数字上一次出现的地方在哪里 注意k=1的情况我用set单独预处理了一下然后再dp的时候就直接从2开始就可以了

#include<set>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
#define N 40000
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S)return EOF;}
    return *S++;
}
inline int read(){
    int x=0;char ch=gc();
    while(ch<'0'||ch>'9') ch=gc();
    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
    return x;
}
struct node{
    int left,right,max,lazy;
}tree[N<<2];
int dp[N][55],num,n,k,pos[N],pre[N],root;set<int>s;
inline void pushdown(int x){
    if (!tree[x].lazy) return;
    int l=tree[x].left,r=tree[x].right;
    tree[l].max+=tree[x].lazy;tree[r].max+=tree[x].lazy;
    tree[l].lazy+=tree[x].lazy;tree[r].lazy+=tree[x].lazy;tree[x].lazy=0;
}
inline void update(int x){
    int l=tree[x].left,r=tree[x].right;
    tree[x].max=max(tree[l].max,tree[r].max);
}
inline void insert1(int x,int l,int r,int l1,int r1){
    if(l1<=l&&r1>=r){++tree[x].max;++tree[x].lazy;return;}
    int mid=l+r>>1;pushdown(x);
    if (l1<=mid) insert1(tree[x].left,l,mid,l1,r1);
    if (r1>mid) insert1(tree[x].right,mid+1,r,l1,r1);update(x);
}
inline int query(int x,int l,int r,int l1,int r1){
    if (l1<=l&&r1>=r) return tree[x].max;
    int mid=l+r>>1;pushdown(x);int tmp=0;
    if (l1<=mid) tmp=max(tmp,query(tree[x].left,l,mid,l1,r1));
    if (r1>mid) tmp=max(tmp,query(tree[x].right,mid+1,r,l1,r1));return tmp; 
}
inline void build(int &x,int l,int r,int kk){
    x=++num;tree[x].lazy=0;if(l==r) {tree[x].max=dp[l][kk];return;}
    int mid=l+r>>1;build(tree[x].left,l,mid,kk);build(tree[x].right,mid+1,r,kk);update(x);
}
int main(){
    freopen("cf.in","r",stdin);
    n=read();k=read();
    for (int i=1;i<=n;++i) {
        int a=read();s.insert(a);pre[i]=pos[a];pos[a]=i;dp[i][1]=s.size();
    }
    for (int kk=2;kk<=k;++kk){
        num=0;build(root,1,n,kk-1);
        for (int i=kk;i<=n;++i){
            insert1(root,1,n,pre[i],i-1);
            dp[i][kk]=query(root,1,n,kk-1,i-1);
        }
    }printf("%d\n",dp[n][k]);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值