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. AngularJS1.X版本双向绑定九问

    前言 由于工作的原因,使用angular1.x版本已经有一段时间了,虽然angualr2升级后就完全重构了,但每个版本存在也有一定的道理.话不多说,进入正题. 1.双向绑定的原理是什么? Angual ...

  2. Too Rich HDU - 5527 (贪心+dfs)

    Too Rich Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  3. java设计模式1--单例模式

    1:单例模式简介 单例模式是一种常用的软件设计模式,它确保某个类只有一个实例,而且自行实例化并向整个系统提供唯一的实例.总而言之就是在系统中只会存在一个对象,其中的数据是共享的 特点: 单例类只能有一 ...

  4. mysql 5.7初始化默认密码错误

    下载了一个mysql 5.7.17的安装包后,安装后怎么都启动不了,好在mysql安装是成功了,没办法只有使用命令行重新初始化设置了 我的mysql安装根目录为:C:\Program Files\My ...

  5. JZOJ 4757. 树上摩托

    Description Sherco是一位经验丰富的魔♂法师.Sherco在第零次圣杯战争中取得了胜利,并取得了王之宝藏——王の树.他想把这棵树砍去任意条边,拆成若干棵新树,并装饰在他的摩托上,让他的 ...

  6. python3.7 os模块

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 os模块 #os模块是与操作系统交互的一个接口 # os.get ...

  7. 初学js之qq聊天展开实例

    实现这样的效果. 直接看代码,html部分: <body> <div class="box"> <div class="lists" ...

  8. CentOS6.5生产环境系统安装

    CentOS 6.5系统安装 1-1 将预先准备的CentOS 6.5安装光盘插入光驱中,开机/重启系统时,系统会进行自检,自检完毕就会出现安装系统时的引导界面,如图1-1所示.1-2 使用键盘方向键 ...

  9. [jzoj5233]概率博弈(树形DP)

    Description 小A和小B在玩游戏.这个游戏是这样的: 有一棵

  10. Django 四——ModelForm用法

    内容概要: 1.新增数据库表中数据 2.更新数据库表中数据 Django的ModelForm Django中内置了Form和Model两个类,有时候页面的表单form类与Model类是一一对应,因此分 ...