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. 第五章—if语句

    5-1 条件测试 :编写一系列条件测试:将每个测试以及你对其结果的预测和实际结果都打印出来.你编写的代码应类似于下面这样: car = 'subaru' print("Is car == ' ...

  2. javascript中的取反再取反~~

    操作符~, 是按位取反的意思,表面上~~(取反再取反)没有意义,实际上在JS中可以将浮点数变成整数. <html> <script> var myArray = new Arr ...

  3. 遗传算法框架GAFT优化小记

    前言 前段时间一直在用自己写的遗传算法框架测试算法在优化力场参数的效果,但是跑起来效率很慢,因为适应度函数需要调用多次力场程序计算能量,但是还是比我预想中的慢我也没有及时对程序进行profiling和 ...

  4. 模仿qq列表信息滑动删除效果

    这个效果的完成主要分为两个部分 自定义view作为listview的列表项 一个view里面包括 显示头像,名字,消息内容等的contentView和滑动才能显示出来的删除,置顶的右边菜单menuVi ...

  5. 敏捷开发与xp实践 实验报告

    20162315 敏捷开发与xp实践 实验报告 实验任务 1.在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单,找出一项让自己感觉最好 ...

  6. 软件工程 speedsnail 第二次冲刺10

    20150527 完成任务:蜗牛碰到线后速度方向的调整:已经基本实现多方向的反射: 遇到问题: 问题1 反射角的问题 解决1 利用tan()三角函数 明日任务: 大总结.找到新问题.布置下一次冲刺方案

  7. MVC、MVP、MVVM 模式

    一.前言 做客户端开发.前端开发对MVC.MVP.MVVM这些名词不了解也应该大致听过,都是为了解决图形界面应用程序复杂性管理问题而产生的应用架构模式.网上很多文章关于这方面的讨论比较杂乱,各种MV* ...

  8. 第181天:HTML5——视频、音频

    一.HTML5新增的video.source标签 <video width="320" height="240" controls="contr ...

  9. 第167天:canvas绘制柱状图

    canvas绘制柱状图 1.HTML <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  10. SPOJ3713——Primitive Root

    终于有一个SPOJ题目是我自己独立做出来的,ORZ,太感动了. 题目意思是给你一个素数,问你一个数r是否满足,r,r^2,r^3,……,r^p-1,全不相同. 以前做过这种类型的题目额.是这样的. 根 ...