Codeforces 833B The Bakery dp线段树
2.5 seconds
256 megabytes
standard input
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.
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.
Print the only integer – the maximum total value of all boxes with cakes.
4 1
1 2 2 1
2
7 2
1 3 3 1 4 4 4
5
8 3
7 7 8 7 7 8 1 7
6
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.
题意:
有n个蛋糕,k个盒子,蛋糕被连续的装进盒子中,每个盒子可以装无限多个蛋糕,每个盒子的价值是装进盒子中的蛋糕的种类数,问盒子的最大总价值是多少。每个数字代表一种蛋糕。
代码:
//容易想到dp式:dp[i][k]=max(dp[j][k-1]+num[j+1][i]),表示处理到第i个蛋糕时用了k个盒子的最大价值
//是由 第j(0<j<i)个蛋糕用k-1个盒子的价值+j+1到i中的种类数 中最大的那个转移得到。但是这样是O(nnm)
//的,显然不行。中间找最大值可以用线段树O(logn)处理,就可以了。其中每次更新 dp[j][k-1]+num[j+1][i]
//到线段树中,dp可以在建树时加入,num[j+1][i]就是求之间有多少不同的数,每加入一个a[i]时,这个点到
//上一次这个值出现的位置之间的都加1即可。
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int n,k,a[maxn+],now[maxn+],pre[maxn+];
int dp[maxn+],maxv[maxn*+],add[maxn*+];
void pushup(int rt){
maxv[rt]=max(maxv[rt<<],maxv[rt<<|]);
}
void pushdown(int rt){
if(add[rt]){
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt];
maxv[rt<<]+=add[rt];
maxv[rt<<|]+=add[rt];
add[rt]=;
}
}
void build(int l,int r,int rt){
add[rt]=; //记住!!!
if(l==r){
maxv[rt]=dp[l];
return;
}
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
pushup(rt);
}
void update(int ql,int qr,int v,int l,int r,int rt){
if(ql<=l&&qr>=r){
add[rt]+=v;
maxv[rt]+=v;
return;
}
pushdown(rt);
int mid=(l+r)>>;
if(ql<=mid)
update(ql,qr,v,l,mid,rt<<);
if(qr>mid)
update(ql,qr,v,mid+,r,rt<<|);
pushup(rt);
}
int query(int ql,int qr,int l,int r,int rt){
if(ql<=l&&qr>=r)
return maxv[rt];
pushdown(rt);
int mid=(l+r)>>,ans=;
if(ql<=mid) ans=max(ans,query(ql,qr,l,mid,rt<<));
if(qr>mid) ans=max(ans,query(ql,qr,mid+,r,rt<<|));
return ans;
}
int main()
{
scanf("%d%d",&n,&k);
memset(now,,sizeof(now));
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
pre[i]=now[a[i]];
now[a[i]]=i;
}
memset(dp,,sizeof(dp));
for(int j=;j<=k;j++){
build(,n,);
for(int i=;i<=n;i++){
update(pre[i],i-,,,n,);
dp[i]=query(,i-,,n,);
}
}
printf("%d\n",dp[n]);
return ;
}
Codeforces 833B The Bakery dp线段树的更多相关文章
- Codeforces 834D The Bakery 【线段树优化DP】*
Codeforces 834D The Bakery LINK 题目大意是给你一个长度为n的序列分成k段,每一段的贡献是这一段中不同的数的个数,求最大贡献 是第一次做线段树维护DP值的题 感觉还可以, ...
- Codeforces 834D The Bakery - 动态规划 - 线段树
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- Codeforces 833B The Bakery(主席树 + 决策单调性优化DP)
题目链接 The Bakery 题目大意:目标是把$n$个数分成$k$组,每个组的值为这个组内不同的数的个数,求$k$个组的值的和的最大值. 题目分析: 这道题我的解法可能和大众解法不太一样……我用主 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)
Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...
- cf834D(dp+线段树区间最值,区间更新)
题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...
- ZOJ 3349 Special Subsequence 简单DP + 线段树
同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- Codeforces.833B.The Bakery(线段树 DP)
题目链接 \(Description\) 有n个数,将其分为k段,每段的值为这一段的总共数字种类,问最大总值是多少 \(Solution\) DP,用\(f[i][j]\)表示当前在i 分成了j份(第 ...
随机推荐
- 记录一下自己申请并使用VPS的全过程
在学习REST API的时候,想要阅读一下谷歌爸爸的api design guide,无奈无情被墙,正好在学习云相关的技术,就想到申请一个VPS来用用. 这次我选择的是hostmybytes,原因有两 ...
- leetcode26_C++删除排序数组中的重复项
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1 ...
- Dilworth定理
来自网络的解释: 定理内容及其证明过程数学不好看不懂. 通俗解释: 把一个数列划分成最少的最长不升子序列的数目就等于这个数列的最长上升子序列的长度(LIS) EXAMPLE 1 HDU 1257 ...
- 01_基于TCP的循环为同一个客户端下载文件的下载器
原版: TCP分为客户端(client)和服务器(server),每次服务器只能为客户端提供一次的下载服务. 改良版: TCP分为客户端(client)和服务器(server), (1)每次服务器能为 ...
- 创建image
摘要: 本节演示如何通过 Web GUI 和 CLI 两种方法创建 Image. 本节演示如何通过 Web GUI 和 CLI 两种方法创建 Image. OpenStack 为终端用户提供了 Web ...
- 亚马逊6月18日发布惊世之作 或为3D智能手机
亚马逊将在 6 月 18 日举行一个产品发布会. 其内容可能是关于传闻已久的亚马逊智能手机.该公司在 YouTube 上公布了一段炫耀这款设备的视频.这段视频展示了很多人在这款产品前摇头晃脑,并且表现 ...
- 苹果全球营销高级副总裁Phil Schiller曾考虑炒掉长期创意代理商Media Arts Lab
来自<华尔街日报>消息,从去年开始,三星就利用广告来讽刺苹果产品.苹果全球营销高级副总裁菲尔•席勒(Phil Schiller)曾一度考虑炒掉该公司的长期创意代理商Media Arts L ...
- Sublime Text 2 - Unable to find git.exe 错误
今日打开 Sublime Text 2,随即弹出 Package Control - Unable to find git.exe 错误.如下, 原因:曾经通过 git clone 命令获取过 Sub ...
- 用js两张图片合并成一张图片
JS和canvas的合成方式 function drawAndShareImage(){ var canvas = document.createElement("canvas") ...
- HTML图片标签
<body> <!-- 使用img标签来向网页中引入外部的图片, img标签也是一个自结束标签 属性: src:设置一个外部图片的路径 alt:可以用来设置图片不能显示时,就会显示图 ...