D. 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

——————————————————————————————————

这道题很容易想到一个O(nnk)的暴力

就是外层枚举k第二层枚举n 内层枚举断点就可以了 是个非常常见的dp

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=;
int sum,n,m,c[M],cnt;
int f[M][],q[M];
struct node{int v,pos;}e[M];
bool cmp(node a,node b){return a.v<b.v;}
void clear(){memset(q,,sizeof(q));}
int main()
{
freopen("camp.in","r",stdin);
freopen("camp.out","w",stdout);
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&e[i].v),e[i].pos=i;
sort(e+,e++n,cmp);
c[e[].pos]=++cnt;
for(int i=;i<=n;i++){
if(e[i].v!=e[i-].v) cnt++;
c[e[i].pos]=cnt;
}
for(int i=;i<=n;i++){
if(!q[c[i]]) sum++,q[c[i]]=;
f[i][]=sum;
}
clear();
for(int k=;k<=m;k++){
clear();
for(int i=k;i<=n;i++){
sum=; q[c[i]]=i;
for(int j=i-;j>=k-;j--){
f[i][k]=max(f[i][k],f[j][k-]+sum);
if(q[c[j]]!=i) sum++,q[c[j]]=i;
}
}
}printf("%d\n",f[n][m]);
return ;
}

但是很明显这样只能水五十分

很明显外两层不能去掉 因为他们枚举的是所有的状态

而内层的枚举很明显可以用线段树优化

每次找到一个i 他的颜色是x 将x的上一个位置+1到当前位置的区间+1 然后找一下最大值

更新当前的答案f【i】 表示将1-i 分成当前状态的k段的最优情况

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=;
int sum,n,m,c[M],cnt;
int f[M][],q[M];
struct node{int v,pos;}e[M];
bool cmp(node a,node b){return a.v<b.v;}
void clear(){memset(q,,sizeof(q));}
int main()
{
freopen("camp.in","r",stdin);
freopen("camp.out","w",stdout);
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&e[i].v),e[i].pos=i;
sort(e+,e++n,cmp);
c[e[].pos]=++cnt;
for(int i=;i<=n;i++){
if(e[i].v!=e[i-].v) cnt++;
c[e[i].pos]=cnt;
}
for(int i=;i<=n;i++){
if(!q[c[i]]) sum++,q[c[i]]=;
f[i][]=sum;
}
clear();
for(int k=;k<=m;k++){
clear();
for(int i=k;i<=n;i++){
sum=; q[c[i]]=i;
for(int j=i-;j>=k-;j--){
f[i][k]=max(f[i][k],f[j][k-]+sum);
if(q[c[j]]!=i) sum++,q[c[j]]=i;
}
}
}printf("%d\n",f[n][m]);
return ;
}

codeforce div2 426 D. The Bakery的更多相关文章

  1. codeforce div2 C 树状数组

    http://codeforces.com/contest/362 题目大意:给你一个序列,用冒泡排序法让他变为非递减的序列最少需要几次.在冒泡交换之间,你有一个swap操作,该swap操作是交换任意 ...

  2. Codeforce Div-2 985 C. Liebig's Barrels

    http://codeforces.com/contest/985/problem/C C. Liebig's Barrels time limit per test 2 seconds memory ...

  3. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

  4. Codeforces #426 Div2 D(线段树优化 DP )

    #426 Div2 D 题意 给出 \(n\) 个数字,将这些数字隔成 \(k\) 个部分(相对位置不变),统计每个部分有几个不同数字,然后全部加起来求和,问和最大是多少. 分析 很容易想到 \(DP ...

  5. Codeforce Round #643 #645 #646 (Div2)

    codeforce Round #643 #645 #646 div2 Round #643 problem A #include<bits/stdc++.h> using namespa ...

  6. Codeforce Round #216 Div2

    e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...

  7. Codeforce Round #211 Div2

    真的是b到不行啊! 尼玛C题一个这么简单的题目没出 aabbccddee 正确的是aabccdee 我的是   aabcdee 硬是TM的不够用,想半天还以为自己的是对的... A:题... B:题. ...

  8. 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 ...

  9. codeforce 192 div2解题报告

    今天大家一起做的div2,怎么说呢,前三题有点坑,好多特判.... A. Cakeminator 题目的意思是说,让你吃掉cake,并且是一行或者一列下去,但是必须没有草莓的存在.这道题目,就是判断一 ...

随机推荐

  1. JavaScript 日期权威指南

    简介 JavaScript通过强大的对象为我们提供日期处理功能:日期. 本文确实_不是_谈论 Moment.js ,我认为它是处理日期的最佳库,你应该在处理日期时几乎总是使用它. Date对象 Dat ...

  2. php订单号的生成

    来自ECSHOP订单号生成函数:/includes/lib_order.php文件中的get_order_sn() /** * 得到新订单号 * @return string */ function ...

  3. 经典dfs(depth-first search)

    DFS主要在于参数的改变; 样例输入: n=4                //给定n个数字 a={1,2,4,7}    //输入n个数据 k=15              //目标数字 样例输 ...

  4. 常用模块之 re shutil configparser hashlib xldt和xlwd

    shutil 高级文件处理模块 封装的更简单了 主要是文件的复制,移动,压缩解压缩 需要保证目标文件已经存在shutil.copymode('test.txt','testcopy4.txt') 压缩 ...

  5. Python9-MySQL-Homework-day43

    表结构 SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure f ...

  6. 7、python中的字典

    字典是python内置的一种无序.可变的数据结构. 字典也叫哈希表.什么是哈希表?哈希表就是会对表中的键(key)执行哈希计算,并根据计算结果在内存中分配一个区域来储存该键所对应的值(value).这 ...

  7. “帮你APP”团队冲刺3

    1.整个项目预期的任务量 (任务量 = 所有工作的预期时间)和 目前已经花的时间 (所有记录的 ‘已经花费的时间’),还剩余的时间(所有工作的 ‘剩余时间’) : 所有工作的预期时间:88h 目前已经 ...

  8. 48、android代码架构总结

    之前是按功能模块进行分类,现在随着功能模块越来越多,代码层次不再清晰,所以修改了工程结构: 之前: 经过修改现在: 1.更严谨的遵循mvc架构 bean目录存放的是数据模型 ui存储的是activit ...

  9. IIS6.0,Apache低版本,PHP CGI 解析漏洞

    IIS6.0解析漏洞 在IIS6.0下存在这样的文件"名字.asp;名字.jpg" 代表了jpg文件可以以asp脚本类型的文件执行. 根据这个解析漏洞我们可以上传这种名字类型的图片 ...

  10. MCMC 浅谈

    # MCMC 浅谈 1. 采样(sampling)是什么 MCMC在采样算法中有着举足轻重的地位,那么什么是采样?采样就是根据某种分布生成样本.举个例子,线性同余发生器就是根据均匀分布生成样本,这就很 ...