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. loj124 除数函数求和 1

    loj124 除数函数求和 1 https://loj.ac/problem/124 $\sum_{i=1}^n(\sum_{d|i}d^k)=\sum_{i=1}^n(i^k*{\lfloor}{\ ...

  2. 获取一段HTML文本中的第一张图片与截取内容摘要

    有时候我们获得到的数据是一段HTML文本,也许这段文本里面有许多图片,需要截取一张作为标题图片,这时就可以用到下面这个方法获取到第一张图片: #region 获取第一张图片 /// <summa ...

  3. oracle 触发器,序列,索引

    oracle 触发器,序列,索引 --1,触发器 ----trigger /*触发器是一种特殊的存储过程,它与数据表紧密联系,用于保护表中的数据, 当一个定义了特定类型触发器的基表执行插入.修改或删除 ...

  4. css 尺寸、边框、内边距、背景以及css Sprite

    上节课回顾: HTML标签: 格式排版 p 段落 双br 换行 单hr 分隔线 单h1~h6 标题 双pre 原样格式化输出 双div 标签,无任何特殊意义 HTML标签 :文本 <em> ...

  5. Arduino Ethernet W5100扩展板的指示灯含义

    Arduino Ethernet W5100扩展板是继承WIZnet W5100网络芯片的扩展板.将扩展板连接到Arduino后,可使Arduino具有网络功能.此扩展板上有多个指示灯,由于轻易查不到 ...

  6. Javaweb学习笔记3—Serverlet

    今天来讲javaweb的第三个阶段学习. 老规矩,首先先用一张思维导图来展现今天的博客内容. ps:我的思维是用的xMind画的,如果你对我的思维导图感兴趣并且想看到你们跟详细的备注信息,请点击下载 ...

  7. PMP项目管理学习笔记(11)——范围管理之定义范围

    定义范围过程组 定义范围包含将项目分解为团队成员要完成的具体工作之前你需要知道的所有一切. 输入:需求文档.项目章程.组织过程资产 工具:辅助工作室.产品分析.代理方案识别.专家判断 辅助工作室: 与 ...

  8. IDEA一些设置

    1. 设置字体为Consolas,Size:16, Line spacing: 1.1 2. 设置智能提示大小写不敏感 在设置中搜索sense, 找到配置节点 Editor->General-& ...

  9. Javascript异步编程的常用方法

    Javascript语言的执行环境是"单线程"(single thread).所谓"单线程",就是指一次只能完成一件任务.如果有多个任务,就必须排队,前面一个任 ...

  10. COGS 1743. 忠诚

    ★   输入文件:faithful.in   输出文件:faithful.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 老管家是一个聪明能干的人.他为财主工作了整整1 ...