Codeforces Round #426 (Div. 2) D. 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.
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
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个盒子,n个数,将连续的一段数放到盒子里,使得每个盒子不同数个数加起来,总和最大
题解:
设定dp[i][j],在前j个数,分成i块的 最大价值
那么 dp[i][j] = max(dp[i-1][k] + sum[k+1][j])
记录每个位这个数 上一次出现的位置last[i]
更新当前层,先把上一层即 dp[i-1][1~n] 的值更新到线段树,每次相当于加入一个a[j], 与前(j-1)个后缀形成新的 后缀,但是有些后缀的不同个数不会增加
就利用last,使得last[j] ~ j-1 这一段位置 +1,就是当前贡献的答案,最后线段树查询即可
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 5e5+, M = 1e3+,inf = 2e9; int dp[][N],k;
int lazy[N];
int mx[N],v[N],n;
void push_up(int i) {
mx[i] = max(mx[ls],mx[rs]);
}
void push_down(int i,int ll,int rr) {
if(ll == rr) return ;
if(lazy[i]) {
lazy[ls] += lazy[i];
lazy[rs] += lazy[i];
mx[ls] += lazy[i];
mx[rs] += lazy[i];
lazy[i] = ;
}
}
void build(int i,int ll,int rr,int p) {
lazy[i] = ;
mx[i] = ;
v[i] = ;
if(ll == rr) {
v[i] = dp[p][ll-];
mx[i] = v[i];
return ;
}
build(ls,ll,mid,p);
build(rs,mid+,rr,p);
push_up(i);
}
void update(int i,int ll,int rr,int x,int y,int c) {
if(x > y) return ;
push_down(i,ll,rr);
if(ll == x && rr == y) {
mx[i] += c;
lazy[i] += c;
return ;
}
if(y <= mid) update(ls,ll,mid,x,y,c);
else if(x > mid) update(rs,mid+,rr,x,y,c);
else update(ls,ll,mid,x,mid,c),update(rs,mid+,rr,mid+,y,c);
push_up(i);
}
int ask(int i,int ll,int rr,int x,int y)
{
if(x > y) return ;
push_down(i,ll,rr);
if(ll == x && rr == y) {
return mx[i];
}
if(y <= mid) return ask(ls,ll,mid,x,y);
else if(x > mid) return ask(rs,mid+,rr,x,y);
else return max(ask(ls,ll,mid,x,mid),ask(rs,mid+,rr,mid+,y));
push_up(i);
}
int mp[N],last[N],a[N];
int main() { scanf("%d%d",&n,&k);
for(int i = ; i <= n; ++i) {
scanf("%d",&a[i]);
last[i] = mp[a[i]];
mp[a[i]] = i;
}
for(int i = ; i <= k; ++i) {
build(,,n,i-);//(i-1)
for(int j = ; j <= n; ++j) {
update(,,n,max(last[j]+,),j, );
dp[i][j] = ask(,,n,,j);
}
}
cout<<dp[k][n]<<endl;
return ;
}
Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP的更多相关文章
- CodeForces 834D The Bakery(线段树优化DP)
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- 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 ...
- Codeforces Round #426 (Div. 2) D The Bakery(线段树 DP)
The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq
B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...
- Codeforces Round #587 (Div. 3) F. Wi-Fi(单调队列优化DP)
题目:https://codeforces.com/contest/1216/problem/F 题意:一排有n个位置,我要让所有点都能联网,我有两种方式联网,第一种,我直接让当前点联网,花费为i,第 ...
- 【动态规划】【线段树】 Codeforces Round #426 (Div. 1) B. The Bakery
给你一个序列,让你划分成K段,每段的价值是其内部权值的种类数,让你最大化所有段的价值之和. 裸dp f(i,j)=max{f(k,j-1)+w(k+1,i)}(0<=k<i) 先枚举j,然 ...
- Codeforces Round #546 (Div. 2) E 推公式 + 线段树
https://codeforces.com/contest/1136/problem/E 题意 给你一个有n个数字的a数组,一个有n-1个数字的k数组,两种操作: 1.将a[i]+x,假如a[i]+ ...
- Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并
D. Developing Game Pavel is going to make a game of his dream. However, he knows that he can't mak ...
随机推荐
- hdu 1907 尼姆博弈
John Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- 多重部分和 poj1742
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- 九度oj 题目1104:整除问题
题目描述: 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除. 输入: 两个整数n(2<=n<=1000),a(2<=a<=1000) 输出: 一个整数. ...
- POJ 3468 线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- 标准C程序设计七---03
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- 使用plantuml生成uml图
主要包括以下三步: 一.到http://plantuml.com/download 下载plantuml.jar ,我将这个软件放置到home的/home/munication/WORKM/Progr ...
- raspi串口、python串口模块pyserial
一.安装 1.下载软件包pyserial-2.7.tar.gz 网址:https://pypi.python.org/pypi/pyserial 2.8uftp上传至/usr/local/src/ ...
- 体验Windows 2008 R2的RemoteApp
[说明]这是<中小企业虚拟机解决方案大全>一书中部分章节的摘抄.该书预计于2009年12月初由<电子工业出版社>出版,敬请期待! 通过远程桌面服务,组织可以为用户提供随时随 ...
- sklearn 特征选择
1.移除低方差的特征(Removing features with low variance) VarianceThreshold 是特征选择中的一项基本方法.它会移除所有方差不满足阈值的特征.默认设 ...
- PS 如何把大嘴变小嘴
Photoshop整容教程:让MM美唇大嘴变小嘴 2009-06-17 14:15作者:佚名出处:天极网软件频道责任编辑:王健 下面就开始实际操作了. 1.首先从Photosh ...