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. Visual Studio 2017 初次体验

    在初次体验中遇到以下问题以及技巧 1. 在出现红色波浪线时为出现错误语法,将鼠标移动到相应位置可以获得相关错误信息 2.在编写代码过程中,行号上出现的小黄灯可以有提示信息 3.List 与 Array ...

  2. Vue -computed传参数

    vue 中computed想传递参数怎么办? 闭包在这里起到的重要的作用 <input v-model="newItem(key,val)" type="text& ...

  3. 【JS】实时监控页面,input框数值自动求和

    需求: 有一个页面需要将input框填入的各个费用自动相加,添加到“合计费用”里. 解决方案: 使用jquery的blur实践,每个费用的Input框检测到失去焦点时,将所有的input框数值相加求和 ...

  4. DeepFaceLab报错,OOM如何解决?

    DeepFaceLab出错,虽然错误提示好几个屏幕,但是无非两种情况,一种是驱动没装好,一种是显存配置不够.上一篇文章说了驱动的问题,这一篇就说说配置不够的问题. 这个问题的表现形式,往往是各种OOM ...

  5. JZOJ 3461. 【NOIP2013模拟联考5】小麦亩产一千八(kela)

    3461. [NOIP2013模拟联考5]小麦亩产一千八(kela) (Standard IO) Time Limits: 1000 ms  Memory Limits: 262144 KB  Det ...

  6. 大数据小项目之电视收视率企业项目08--》MapReduce编写之Wordcount

    编程规范 (1)用户编写的程序分成三个部分:Mapper,Reducer,Driver(提交运行mr程序的客户端) (2)Mapper的输入数据是KV对的形式(KV的类型可自定义) (3)Mapper ...

  7. 离线安装 Visual Studio Express 而不下载整个镜像文件的方法(转载)

    转 visual studio 2010 express 全序列号 phone开发工具YDK44-2WW9W-QV7PM-8P8G8-FTYDF VC# 2010 Express: PQT8W-68Y ...

  8. ACM模板

    #include <iostream> //万能头文件#include<bits/stdc++.h> 方便时用 #include <algorithm> #incl ...

  9. Diycode开源项目 LoginActivity分析

    1.首先看一下效果 1.1.预览一下真实页面 1.2.分析一下: 要求输入Email或者用户名,点击编辑框,弹出键盘,默认先进入输入Email或用户名编辑框. 点击密码后,密码字样网上浮动一段距离,E ...

  10. Java之基于Apache jar包的FTPClient上传

    首先,准备工作: http://pan.baidu.com/s/1dD1Utwt 从以上链接下载Apache的jar包,并将其复制到工程的WEB-INF下的lib包里,在此,准备工作就已经完成了. 具 ...