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.

【题意】给你一个序列,然后在原来的顺序基础上将这些数分成K份,每一份至少一个数,然后对于每一份,将数的种数累加,求累加和最大值。

【分析】如果n小一些的话,我们可以n方DP做,DP[i][j]表示在i这个地方将前面分成k份的最大值,那么dp[i][j]=dp[k][j-1]+(k+1~j的数的种数),

但是N太大。。。于是我们想到线段树。线段树的维护这个区间内每一个位置的dp值+这个位置到我当前dp到的位置的数的种数,然后对这个区间取最大值。当我们dp到i时,我们将a[i]上一次出现的位置+1~i这个区间更新+1,然后查询1~i的最大值即可。

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 3e4+;;
const int M = ;
const int mod = 1e9+;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,k,ans;
int a[N],mx[N*],lazy[N*];
int pre[N],pos[N],dp[N];
void pushUp(int rt){
mx[rt]=max(mx[rt<<],mx[rt<<|]);
}
void pushDown(int rt){
if(lazy[rt]){
lazy[rt<<]+=lazy[rt];
lazy[rt<<|]+=lazy[rt];
mx[rt<<]+=lazy[rt];
mx[rt<<|]+=lazy[rt];
lazy[rt]=;
}
}
void build(int l,int r,int rt){
lazy[rt]=;
if(l==r){
mx[rt]=dp[l-];
return;
}
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
pushUp(rt);
}
void upd(int L,int R,int l,int r,int x,int rt){
if(L<=l&&r<=R){
mx[rt]+=x;
lazy[rt]+=x;
return;
}
pushDown(rt);
int mid=(l+r)>>;
if(L<=mid)upd(L,R,l,mid,x,rt<<);
if(R>mid) upd(L,R,mid+,r,x,rt<<|);
pushUp(rt);
}
int qry(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R){
return mx[rt];
}
pushDown(rt);
int ret=,mid=(l+r)>>;
if(L<=mid)ret=max(ret,qry(L,R,l,mid,rt<<));
if(R>mid)ret=max(ret,qry(L,R,mid+,r,rt<<|));
return ret;
}
int main(){
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
pre[i]=pos[a[i]];
pos[a[i]]=i;
}
for(int i=;i<=k;i++){
build(,n,);
for(int j=;j<=n;j++){
upd(pre[j]+,j,,n,,);
dp[j]=qry(,j,,n,);
}
}
printf("%d\n",dp[n]);
}

Codeforces Round #426 (Div. 2) D The Bakery(线段树 DP)的更多相关文章

  1. Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP

    D. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

  2. Codeforces Round #587 (Div. 3) F Wi-Fi(线段树+dp)

    题意:给定一个字符串s 现在让你用最小的花费 覆盖所有区间 思路:dp[i]表示前i个全覆盖以后的花费 如果是0 我们只能直接加上当前位置的权值 否则 我们可以区间询问一下最小值 然后更新 #incl ...

  3. Codeforces Round #426 (Div. 1) B The Bakery (线段树+dp)

    B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

  4. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  5. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

  6. 【动态规划】【线段树】 Codeforces Round #426 (Div. 1) B. The Bakery

    给你一个序列,让你划分成K段,每段的价值是其内部权值的种类数,让你最大化所有段的价值之和. 裸dp f(i,j)=max{f(k,j-1)+w(k+1,i)}(0<=k<i) 先枚举j,然 ...

  7. Codeforces Round #546 (Div. 2) E 推公式 + 线段树

    https://codeforces.com/contest/1136/problem/E 题意 给你一个有n个数字的a数组,一个有n-1个数字的k数组,两种操作: 1.将a[i]+x,假如a[i]+ ...

  8. Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并

    D. Developing Game   Pavel is going to make a game of his dream. However, he knows that he can't mak ...

  9. Codeforces Round #275 Div.1 B Interesting Array --线段树

    题意: 构造一个序列,满足m个形如:[l,r,c] 的条件. [l,r,c]表示[l,r]中的元素按位与(&)的和为c. 解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的 ...

随机推荐

  1. Codeforces Round #411 (Div. 2) A-F

    比赛时候切了A-E,fst了A Standings第一页只有三个人挂了A题,而我就是其中之一,真™开心啊蛤蛤蛤 A. Fake NP time limit per test 1 second memo ...

  2. 【Luogu】P3927 SAC E#1 - 一道中档题 Factorial

    [题目]洛谷10月月赛R1 提高组 [题意]求n!在k进制下末尾0的个数,n<=1e18,k<=1e16. [题解]考虑10进制末尾0要考虑2和5,推广到k进制则将k分解质因数. 每个质因 ...

  3. 【转载】Quick-Cocos2d-x文件结构分析

    在上一章我们讲过了Quick-Cocos2d-x中的环境搭建,这章我们分析下quick中的文件结构吧!打开quick的文件夹,可以看到如下的这些目录和文件: bin:存放各种与引擎相关的脚本 comp ...

  4. hdu 1395 2^x mod n = 1(暴力题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1395 2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Oth ...

  5. Android控件——AutoCompleteTextView与MultiAutoCompleteTextView(实现自动匹配输入的内容)

    ------------------------------------AutoCompleteTextView----------------------

  6. netcat、nc工具随记

    netcat又称nc工具,其最主要的作用就是建立连接并返回两个数据流,剩下的就看各位的想象力了,想象力是很重要的,这也是这个工具的强大之处的所在,所以重要的东西才要说三遍,想象力! 具体参数如下: - ...

  7. 数组返回NULL绕过

    BUGKU:http://120.24.86.145:9009/19.php 还没看完源码,我就直接加了一个password[]=1结果就拿到flag了.然后再看源码我自己都搞不懂为什么可以得到源码. ...

  8. vim 常用的操作指令

    vim(vi improve).命令行下的一些常见操作: 移动光标: 0:将光标移动到该行的最前面: $:将光标移动到该行的最后面: G:移动到最后一行的开头: {/}:将光标移动到前面或者后面的{/ ...

  9. 2015多校第6场 HDU 5353 Average 贪心,细节处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5353 题意:有n个人围城一个环,每一个人手里都有一些糖果,第i个人有ai块.现在有三种操作:第i个人给 ...

  10. Vim配置Node.js开发工具

    ubuntu安装vim编辑器.默认情况下,vim在运行的时候会加载-/.vimrc文件里的配置文件,如果在-目录下不存在这个配置文件可以手动创建. 在-/.vim目录下是vim的插件加载的位置,可以在 ...