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.

Input

4 1
1 2 2 1

Output

2

题解


设\(dp[k][n]\)表示k个分割的情况下最大收益

设\(C[i][j]\)表示区间(i,j)不同蛋糕的数量

对于只有k=1的情况

\[dp[1][n]=C[1][n]
\]

对于k>1的情况

\[dp[k][n]=max\;dp[k-1][i-1]+C[i][n]
\]

如何求C数组?

对于每一个x,它能向左覆盖到上一个x的后一个位置,在这一部分,这个值都能提供贡献1。那么最终的value为所有点的覆盖中最大的值(即所有的x都有一个独立前缀,那么答案为这些独立前缀的交集最大者)

用线段树维护即可

import java.io.*;
import java.util.*; public class Main {
static final int N=35005;
static int dp[][]=new int[55][N];
static int a[]=new int[N];
static int n,k;
static int max[]=new int[N<<2],tag[]=new int[N<<2];
static void build(int pos,int rt,int l,int r) {
tag[rt]=0;
if(l==r) {
max[rt]=dp[pos][l-1];
return;
}
int mid=(l+r)>>1;
build(pos,rt<<1,l,mid);
build(pos,rt<<1|1,mid+1,r);
max[rt]=Math.max(max[rt<<1],max[rt<<1|1]);
}
static void pushdown(int rt) {
if(tag[rt]<=0) return;
tag[rt<<1]+=tag[rt];
tag[rt<<1|1]+=tag[rt];
max[rt<<1]+=tag[rt];
max[rt<<1|1]+=tag[rt];
tag[rt]=0;
}
static void update(int L,int R,int rt,int l,int r) {
if(L<=l&&r<=R) {
tag[rt]++;
max[rt]++;
return;
}
pushdown(rt);
int mid=(l+r)>>1;
if(L<=mid) update(L,R,rt<<1,l,mid);
if(R>mid) update(L,R,rt<<1|1,mid+1,r);
max[rt]=Math.max(max[rt<<1],max[rt<<1|1]);
}
static int query(int L,int R,int rt,int l,int r) {
if(L<=l&&r<=R) {
return max[rt];
}
pushdown(rt);
int mid=(l+r)>>1,ret=0;
if(L<=mid) ret=Math.max(ret,query(L,R,rt<<1,l,mid));
if(R>mid) ret=Math.max(ret, query(L,R,rt<<1|1,mid+1,r));
max[rt]=Math.max(max[rt<<1], max[rt<<1|1]);
return ret;
}
static int pos[]=new int[N];
static int pre[]=new int[N];
public static void main(String[] args){
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
n=in.nextInt();k=in.nextInt();
Arrays.fill(pos,0,n+1,0);
Arrays.fill(pre,0,n+1,0);
for(int i=1;i<=n;i++) {
a[i]=in.nextInt();
pre[i]=pos[a[i]]+1;
pos[a[i]]=i;
}
Arrays.fill(dp[0],0,n+1,0);
for(int i=1;i<=k;i++) {
build(i-1,1,1,n);
for(int j=1;j<=n;j++) {
update(pre[j],j,1,1,n);
dp[i][j]=query(1,j,1,1,n);
}
}
out.println(dp[k][n]);
out.flush();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}

【CodeForces 426】div1 B The Bakery的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 750C】New Year and Rating(做法2)

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 750C】New Year and Rating

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  5. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  6. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  7. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  8. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  9. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

随机推荐

  1. android 浏览器对图片加载高度渲染问题

    今天在开发有道汉语词典移动版的时候遇到了一个很奇怪的问题. 在android设备上访问的时候,总是发现有底部背景色不能完全渲染出来的情况(有时候又是正常的,一会儿出现一会儿不出现,iphone设备也是 ...

  2. icomoon字体使用

    如何灵活利用免费开源图标字体-IcoMoon篇 by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/w ...

  3. [BZOJ4064/Cerc2012]The Dragon and the knights

    Description 与当地鞋匠协会发生冲突的瓦维尔城堡的龙决定将它的狩猎场移出克拉科夫以减少敌对的邻居数量.现在他正在给和平而宁静的Bytes王国带来灾难与恐怖. 在Bytes王国有n条河流,每一 ...

  4. 洛谷 P4219 [BJOI2014]大融合

    查询,就相当于先删去这条边,然后查询边的两个端点所在连通块大小,乘起来得到答案,然后再把边加回去 可以用线段树分治做 #pragma GCC optimize("Ofast") # ...

  5. Python Unicode and str

    http://stackoverflow.com/questions/18034272/python-str-vs-unicode-types unicode is a character set. ...

  6. adb的含义

    ADB全名Andorid Debug Bridge. 是一个Debug工具.为何称之为Bridge呢?因为adb是一个标准的C/S结构的工具, 是要连接开发电脑和调试手机的.包含如下几个部分: 1.C ...

  7. 132 Palindrome Partitioning II 分割回文串 II

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 符合要求的的最少分割次数.例如,给出 s = "aab",返回 1 因为进行一次分割可以将字符串 s 分 ...

  8. Spark-SQL连接Hive

    第一步:修个Hive的配置文件hive-site.xml 添加如下属性,取消本地元数据服务: <property> <name>hive.metastore.local< ...

  9. php传json格式给C++时乱码解决方案

    今天在做给C++传json数据时,C++无法识别到中文 网上查下原因有json只支持utf-8,但是我的整个项目编码都是utf8的,没有出现过其它编码,所以问题还是抛给了C++解决,后来经一高手解答说 ...

  10. UVALive - 6428(扩展欧几里德)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=48388 前段时间偶然碰到的一道题,今天突然想到没把它记录下来. 比较不错的扩 ...