CodeForces 834D The Bakery(线段树优化DP)
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.
题意:给出一个数组,将数组分割成k份,使每份中不同数字的个数加起来最大,输出这个最大值
题解:这道题很好列出状态转移方程,dp[i][j]=max{dp[k][j-1]+cnt[k+1][i]}
dp[i][j]意为1~i之间分割j次所产生的最大值。cnt[i][j]表示i-j之间不同的颜色个数。
看到max可以考虑线段树优化,也就是如果我们有办法在O(logn)的时间内计算出cnt[i][j]的值就可以了,考虑一种颜色能够产生贡献的范围,为他上一次出现的位置到他当前的位置,产生贡献为1,可以使用线段树区间加,给pre[i]~i都加上1,从1到n进行枚举,逐渐进行上面的操作,每次换下一层的时候重构线段树,这样计算cnt[i][j]的复杂度就变成了logn的
代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson root<<1
#define rson root<<1|1
using namespace std; struct node
{
int l,r,sum,lazy;
}tr[];
int dp[][],pre[],pos[]; void init()
{
memset(tr,,sizeof(tr));
} void push_up(int root)
{
tr[root].sum=max(tr[lson].sum,tr[rson].sum);
} void push_down(int root)
{
tr[lson].sum+=tr[root].lazy;
tr[lson].lazy+=tr[root].lazy;
tr[rson].sum+=tr[root].lazy;
tr[rson].lazy+=tr[root].lazy;
tr[root].lazy=;
} void build(int root,int l,int r,int now)
{
if(l==r)
{
tr[root].l=l;
tr[root].r=r;
tr[root].sum=dp[now][l-];
return ;
}
tr[root].l=l;
tr[root].r=r;
int mid=(l+r)>>;
build(lson,l,mid,now);
build(rson,mid+,r,now);
push_up(root);
} void update(int root,int l,int r,int val)
{
if(tr[root].l==l&&tr[root].r==r)
{
tr[root].sum+=val;
tr[root].lazy+=val;
return ;
}
if(tr[root].lazy)
{
push_down(root);
}
int mid=(tr[root].l+tr[root].r)>>;
if(mid<l)
{
update(rson,l,r,val);
}
else
{
if(mid>=r)
{
update(lson,l,r,val);
}
else
{
update(lson,l,mid,val);
update(rson,mid+,r,val);
}
}
push_up(root);
} int query(int root,int l,int r)
{
if(tr[root].l==l&&tr[root].r==r)
{
return tr[root].sum;
}
if(tr[root].lazy)
{
push_down(root);
}
int mid=(tr[root].l+tr[root].r)>>;
if(mid<l)
{
return query(rson,l,r);
}
else
{
if(mid>=r)
{
return query(lson,l,r);
}
else
{
return max(query(lson,l,mid),query(rson,mid+,r));
}
}
} int main()
{
int n,k,t;
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%d",&t);
pre[i]=pos[t]+;
pos[t]=i;
}
for(int i=;i<=k;i++)
{
init();
build(,,n,i-);
for(int j=;j<=n;j++)
{
update(,pre[j],j,);
dp[i][j]=query(,,j);
}
}
printf("%d\n",dp[k][n]);
}
CodeForces 834D The Bakery(线段树优化DP)的更多相关文章
- Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP
D. The Bakery Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...
- D - The Bakery CodeForces - 834D 线段树优化dp···
D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...
- Codeforces Round #426 (Div. 2) D 线段树优化dp
D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)
Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...
- CF833B The Bakery 线段树,DP
CF833B The Bakery LG传送门 线段树优化DP. 其实这是很久以前就应该做了的一道题,由于颓废一直咕在那里,其实还是挺不错的一道题. 先考虑\(O(n^2k)\)做法:设\(f[i][ ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- BZOJ2090: [Poi2010]Monotonicity 2【线段树优化DP】
BZOJ2090: [Poi2010]Monotonicity 2[线段树优化DP] Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k]. ...
- [AGC011F] Train Service Planning [线段树优化dp+思维]
思路 模意义 这题真tm有意思 我上下楼梯了半天做出来的qwq 首先,考虑到每K分钟有一辆车,那么可以把所有的操作都放到模$K$意义下进行 这时,我们只需要考虑两边的两辆车就好了. 定义一些称呼: 上 ...
- 【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp
题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a varian ...
随机推荐
- Julia - 函数的参数传递
不定参数 不定参数的函数也称变参函数 函数的参数可以被定义成任意个 可以在最后一个参数后紧跟省略号“...”来定义变参函数 julia> bar(x, y, z...) = (x, y, z) ...
- 2.docker学习之linux安装
Docker CE is supported on CentOS 7.3 64-bit. 说明docker只能安装在centOS7以上 [root@hadoop-bigdata01 ~]# yum i ...
- 使用Spring表达式语言进行装配
1.1注入外部的值 Spring中,处理外部值的最简单方式就是声明属性源并通过Spring的Environment来检索属性.例如,程序清单3.7展现了一个基本的Spring配置类,它使用外部的属性来 ...
- Python开发丨这些面试题会不会难倒你
1:以下的代码的输出将是什么? 说出你的答案并解释. class Parent(object): x = 1 class Child1(Parent): pass cl ...
- ROS 禁止公网暴力破解SSH FTP
最简单的彻底禁止公网访问SSH FTP端口 1 2 /ip firewall filter add chain=input protocol=tcp dst-port=21-22 src-addres ...
- 用Pylint规范化Python代码,附PyCharm配置
Pylint一个可以检查Python代码错误,执行代码规范的工具.它还可以对代码风格提出建议. 官网:https://pylint.readthedocs.io pip install pylint ...
- 配置siebel捕捉SQL语句
C:\Siebel\15.0.0.0.0\Client\BIN\siebel.exe /c c:\Siebel\15.0.0.0.0\Client\bin\chs\siebel.cfg /B &quo ...
- Linux系统profile、bashrc、bash_profile等环境设置文件的使用
一.前言 关于bash的环境设置文件,分为系统设置和个人设置,一般来说建议用户直接修改个人的设置. 本文测试环境为:centos6.5. 二.系统设置值 1. /etc/sysconfig/i18n ...
- spring4-2-bean配置-1-依赖注入
配置 bean,本章节中主要介绍蓝色文字部分. 配置形式:基于 XML 文件的方式:基于注解的方式 Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & 实例工厂方法).F ...
- ssdb的高可用,源码分析
ssdb,一个高性能的支持丰富数据结构的 NoSQL 数据库, 用于替代 Redis.——这是其官网的自我介绍. ssdb在leveldb存储库的基础上进行改造和丰富,添加了类似redis操作的接口, ...