【CodeForces 426】div1 B The Bakery
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的情况
\]
对于k>1的情况
\]
如何求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的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 750C】New Year and Rating(做法2)
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 750C】New Year and Rating
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- Beta版本发布!
该作业所属课程:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业地址:https://edu.cnblogs.com/c ...
- 转-sql之left join、right join、inner join的区别
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...
- Myisamchk使用
Myisam损坏的情况: . 服务器突然断电导致数据文件损坏;强制关机,没有先关闭mysql 服务;mysqld 进程在写表时被杀掉.因为此时mysql可能正在刷新索引. . 磁盘损坏. . 服务器死 ...
- oracle 触发器,序列,索引
oracle 触发器,序列,索引 --1,触发器 ----trigger /*触发器是一种特殊的存储过程,它与数据表紧密联系,用于保护表中的数据, 当一个定义了特定类型触发器的基表执行插入.修改或删除 ...
- AJPFX总结线程创建的两种方法
创建线程的第一种方式:继承Thread ,由子类复写run方法.步骤:1,定义类继承Thread类:2,目的是复写run方法,将要让线程运行的代码都存储到run方法中:3,通过创建Thread类的子类 ...
- 上交oj1219 重要的逆序数对
题意: https://acm.sjtu.edu.cn/OnlineJudge/problem/1219 思路: 在经典的归并排序求逆序数对算法基础上稍作修改. 实现: #include <io ...
- 一个SAP开发人员的双截棍之路
由于种种原因,Jerry最近加入了SAP成都研究院的一个演讲俱乐部,这个俱乐部主要是提高大家的英语演讲能力. 说来Jerry也是大一下期和大二上期一次性高分通过四六级考试的,但是当毕业进入SAP成都研 ...
- java 面试题整理
java面试题 1.接口和抽象类的区别 抽象类 接口 抽象类中可以有默认方法 在java8之前,不能有默认方法 extends implements 抽象类中可以有构造器 接口中不能有构造器 抽象类中 ...
- 事件冒泡 & 阻止事件冒泡
事件冒泡 : 当一个元素接收到事件的时候,会把他接收到的所有传播给他的父级,一直到顶层window.事件冒泡机制 阻止冒泡 : 当前要阻止冒泡的事件函数中调用 event.cancelBubble = ...
- vue 高度 动态更新计算 calcHeight watch $route
vue 高度 动态更新计算 calcHeight () { // this.tableHeight = window.innerHeight - 210 } }, mounted () { // co ...