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.

题意:

有n个蛋糕,k个盒子,蛋糕被连续的装进盒子中,每个盒子可以装无限多个蛋糕,每个盒子的价值是装进盒子中的蛋糕的种类数,问盒子的最大总价值是多少。每个数字代表一种蛋糕。

代码:

//容易想到dp式:dp[i][k]=max(dp[j][k-1]+num[j+1][i]),表示处理到第i个蛋糕时用了k个盒子的最大价值
//是由 第j(0<j<i)个蛋糕用k-1个盒子的价值+j+1到i中的种类数 中最大的那个转移得到。但是这样是O(nnm)
//的,显然不行。中间找最大值可以用线段树O(logn)处理,就可以了。其中每次更新 dp[j][k-1]+num[j+1][i]
//到线段树中,dp可以在建树时加入,num[j+1][i]就是求之间有多少不同的数,每加入一个a[i]时,这个点到
//上一次这个值出现的位置之间的都加1即可。
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int n,k,a[maxn+],now[maxn+],pre[maxn+];
int dp[maxn+],maxv[maxn*+],add[maxn*+];
void pushup(int rt){
maxv[rt]=max(maxv[rt<<],maxv[rt<<|]);
}
void pushdown(int rt){
if(add[rt]){
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt];
maxv[rt<<]+=add[rt];
maxv[rt<<|]+=add[rt];
add[rt]=;
}
}
void build(int l,int r,int rt){
add[rt]=; //记住!!!
if(l==r){
maxv[rt]=dp[l];
return;
}
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
pushup(rt);
}
void update(int ql,int qr,int v,int l,int r,int rt){
if(ql<=l&&qr>=r){
add[rt]+=v;
maxv[rt]+=v;
return;
}
pushdown(rt);
int mid=(l+r)>>;
if(ql<=mid)
update(ql,qr,v,l,mid,rt<<);
if(qr>mid)
update(ql,qr,v,mid+,r,rt<<|);
pushup(rt);
}
int query(int ql,int qr,int l,int r,int rt){
if(ql<=l&&qr>=r)
return maxv[rt];
pushdown(rt);
int mid=(l+r)>>,ans=;
if(ql<=mid) ans=max(ans,query(ql,qr,l,mid,rt<<));
if(qr>mid) ans=max(ans,query(ql,qr,mid+,r,rt<<|));
return ans;
}
int main()
{
scanf("%d%d",&n,&k);
memset(now,,sizeof(now));
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
pre[i]=now[a[i]];
now[a[i]]=i;
}
memset(dp,,sizeof(dp));
for(int j=;j<=k;j++){
build(,n,);
for(int i=;i<=n;i++){
update(pre[i],i-,,,n,);
dp[i]=query(,i-,,n,);
}
}
printf("%d\n",dp[n]);
return ;
}

Codeforces 833B The Bakery dp线段树的更多相关文章

  1. Codeforces 834D The Bakery 【线段树优化DP】*

    Codeforces 834D The Bakery LINK 题目大意是给你一个长度为n的序列分成k段,每一段的贡献是这一段中不同的数的个数,求最大贡献 是第一次做线段树维护DP值的题 感觉还可以, ...

  2. Codeforces 834D The Bakery - 动态规划 - 线段树

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...

  3. Codeforces 833B The Bakery(主席树 + 决策单调性优化DP)

    题目链接 The Bakery 题目大意:目标是把$n$个数分成$k$组,每个组的值为这个组内不同的数的个数,求$k$个组的值的和的最大值. 题目分析: 这道题我的解法可能和大众解法不太一样……我用主 ...

  4. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  5. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

  6. cf834D(dp+线段树区间最值,区间更新)

    题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...

  7. ZOJ 3349 Special Subsequence 简单DP + 线段树

    同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...

  8. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  9. Codeforces.833B.The Bakery(线段树 DP)

    题目链接 \(Description\) 有n个数,将其分为k段,每段的值为这一段的总共数字种类,问最大总值是多少 \(Solution\) DP,用\(f[i][j]\)表示当前在i 分成了j份(第 ...

随机推荐

  1. Paper Reading - Attention Is All You Need ( NIPS 2017 ) ★

    Link of the Paper: https://arxiv.org/abs/1706.03762 Motivation: The inherently sequential nature of ...

  2. varnish squid nginx比较

    linux运维中,web cache server方案的部署是一个很重要的环节,选择也有很多种比如:varnish.squid.nginx.下面就对当下常用的这几个web cache server做一 ...

  3. mysql 性能优化 20 条建议

    MySQL性能优化的最佳20+条经验 2009年11月27日陈皓发表评论阅读评论100,946 人阅读   今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性 ...

  4. erlang节点互相ping,一个能ping通,另外一个不行。

    今天发现一个问题,2个erlang节点,1个主动ping另外一个不通,然后等待另外一个ping过来,2个节点才连通.记录一下. 首先,erlang节点的cookie是一致的.查了文档,cookie一致 ...

  5. 模拟jq的设置样式

    //需求,创建一个div,添加到页面上,给div添加属性 //面向对象开发,构造函数创建类 function divTag(){ this.div1=document.createElement('d ...

  6. PAT 甲级 1040 Longest Symmetric String

    https://pintia.cn/problem-sets/994805342720868352/problems/994805446102073344 Given a string, you ar ...

  7. 在线webservice

    腾讯QQ在线状态 WEB 服务Endpoint: http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx Disco: http:// ...

  8. 剖析Vue原理&实现双向绑定MVVM-2

    vue.js 最核心的功能有两个,一是响应式的数据绑定系统,二是组件系统.本文仅探究双向绑定是怎样实现的.先讲涉及的知识点,再用简化得不能再简化的代码实现一个简单的 hello world 示例. 一 ...

  9. form 表单提交类型

    multipart/form-data与x-www-form-urlencoded区别 multipart/form-data:既可以上传文件等二进制数据,也可以上传表单键值对,只是最后会转化为一条信 ...

  10. hdu-题目1421:搬寝室

    http://acm.hdu.edu.cn/showproblem.php?pid=1421 搬寝室 Time Limit: 2000/1000 MS (Java/Others)    Memory ...