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份(第 ...
随机推荐
- Gradle初使用
我以前一直使用Maven来构建工程,这两天突然发现gradle也非常好用,记录一下自己使用gradle的过程. Gradle的下载与配置 本次选择下载的是gradle3.5版本,没选最新的gradle ...
- hadoop之mapper类妙用
1. Mapper类 首先 Mapper类有四个方法: (1) protected void setup(Context context) (2) Protected void map(KEYIN k ...
- Docker 快速入门教程
本文目的是给几乎从未接触过docker,或者仅仅是听说或者通过新闻了解过Docker的同学 通过一个已有的Docker仓库构建和提交自己的Docker 镜像 这里会涉及到一些概念,但是不单独介绍 这里 ...
- C++进阶训练——停车收费系统设计
一.简介 经过一段时间的c++基础学习,是时候做一个较为全面的.运用c++功能的较复杂的项目练练手了. 运用软件:Visual Studio (VS). 题目:c++停车收费系统设计(某本编程书进 ...
- 获取label标签内for的属性值-js
<body> <div class="row_2" id="ass"> <label for="aaa"> ...
- 请教Amazon FBA里面Label Service, Stickerless, Commingled Inventory是什么意思?
Accept Label Service接受标签服务,选择了以后下面的操作中会有一个让您打印标签的流程,您就可以按照FBA流程提示进行每一步标签服务的操作. Accept Stickless, Com ...
- centos 7.2 安装apache,mysql,php5.6
安装Apache.PHP.Mysql.连接Mysql数据库的包: yum -y install httpd yum -y install php yum -y install php-fpm yum ...
- [python][odlboy]设置字符串打印的颜色
格式:\033[显示方式;前景色;背景色m 说明:前景色 背景色 颜色---------------------------------------30 ...
- C++ 名字空间namespace的使用
A namespace is a scope.C++ provides namespaces to prevent name conflicts.A namespace is a mechanism ...
- snprintf vs sprintf
#include <stdio.h> int printf(const char *format, ...); int fprintf(FILE *stream, const char * ...