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. Oracle分页抽数存储过程

    --outTotal是需要返回的总数,v_loginUserId是传入的登录人ID,抽取他的客户,v_CurrPage是传入的第几页,v_pageSize传入的每页数据条数. ) FROM tb_cu ...

  2. c语言中--typeof--关键字用法

    C语言中 typeof 关键字是用来定义变量数据类型的.在linux内核源代码中广泛使用. 下面是Linux内核源代码中一个关于typeof实例: #define min(x, y) ({ \ typ ...

  3. 【NTT】loj#6261. 一个人的高三楼

    去年看过t老师写这题博客:以为是道神仙题 题目大意 求一个数列的$k$次前缀和.$n\le 10^5$. 题目分析 [计数]cf223C. Partial Sums 加强版.注意到最后的式子是$f_i ...

  4. Mybatis 插入一条或批量插入 返回带有自增长主键记录

    首先讲一下,  插入一条记录返回主键的 Mybatis 版本要求低点,而批量插入返回带主键的 需要升级到3.3.1版本,3.3.0之前的都不行, <dependency> <grou ...

  5. jQuery的select2下拉框的搜索功能(使用select2插件,方便简单)

    第一步: 引入我们用使用的插件 jquery: select2: css: js: 第二步: 创建一个html页面,body内容: <div> <select class=" ...

  6. oop 单例模式

  7. 第1-5章 慕课网微信小程序开发学习笔记

    第1章 前言:不同的时代,不同的Web --微信小程序商城构建全栈应用 http://note.youdao.com/noteshare?id=a0e9b058853dbccf886c1a890594 ...

  8. Python入门必学:递归函数正确的操作使用方法,案例详解

    递归函数,在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact(n)表示,可以 ...

  9. python3.7 random模块

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 random模块 import random #随机模块 # r ...

  10. Nordic Collegiate Programming Contest (NCPC) 2016

    A Artwork B Bless You Autocorrect! C Card Hand Sorting D Daydreaming Stockbroker 贪心,低买高卖,不要爆int. #in ...