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. Cloudera Manager是啥?主要是干啥的?

    简单来说,Cloudera Manager是一个拥有集群自动化安装.中心化管理.集群监控.报警功能的一个工具(软件),使得安装集群从几天的时间缩短在几个小时内,运维人员从数十人降低到几人以内,极大的提 ...

  2. 将tomcat添加到系统服务

    一.安装服务 执行“service.bat install 二.卸载服务 在命令行中进入/Tomcat路径/bin/,执行“service.bat remove”  

  3. div里面放img

    div里面放img的时候 会出现包裹不住的情况,这个时候 只要将img { width:100%,height:100%  },就可以解决问题了

  4. docker监控之cadvisor

    docker run -d \ --volume=/:/rootfs:ro \ --volume=/var/run:/var/run:rw \ --volume=/sys:/sys:ro \ --vo ...

  5. Codeforces 1107E(区间dp)

    用solve(l, r, prefix)代表区间l开始r结束.带了prefix个前缀str[l](即l前面的串化简完压缩成prefix-1个str[l],加上str[l]共有prefix个)的最大值. ...

  6. EXBSGS

    http://210.33.19.103/problem/2183 参考:https://blog.csdn.net/frods/article/details/67639410(里面代码好像不太对) ...

  7. 位运算 UEST 84 Binary Operations

    题目传送门 题意:所有连续的子序列的三种位运算计算后的值的和的期望分别是多少 分析:因为所有连续子序列的组数有n * (n + 1) / 2种,所以要将他们分类降低复杂度,以ai为结尾的分成一组,至于 ...

  8. 记忆化搜索(DFS+DP) URAL 1223 Chernobyl’ Eagle on a Roof

    题目传送门 /* 记忆化搜索(DFS+DP):dp[x][y] 表示x个蛋,在y楼扔后所需要的实验次数 ans = min (ans, max (dp[x][y-i], dp[x-1][i-1]) + ...

  9. JavaScript--数组常用方法总结

    JavaScript--数组常用方法总结 测试模板: var arr = ["a", "b", "c", "d", &q ...

  10. COPY, RETAIN, ASSIGN , READONLY , READWRITE,STRONG,WEAK,NONATOMIC整理--转

    copy:建立一个索引计数为1的对象,然后释放旧对象 对NSString 对NSString 它指出,在赋值时使用传入值的一份拷贝.拷贝工作由copy方法执行,此属性只对那些实行了NSCopying协 ...