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 ...
随机推荐
- 【bzoj3689】异或之 可持久化Trie树+堆
题目描述 给定n个非负整数A[1], A[2], ……, A[n].对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A[i] xor A[j],这样共有n*(n ...
- Chapter 4-5
1.切片对象 sequence[起始索引:结束索引:步进值] 对象身份的比较 is /is not 2.eval()参数是一个字符串, 可以把这个字符串当成表达式来求值. >>>x ...
- @Java web程序员,在保留现场,服务不重启的情况下,执行我们的调试代码(JSP 方式)
一.前言 类加载器实战系列的第六篇(悄悄跟你说,这篇比较水),前面5篇在这里: 实战分析Tomcat的类加载器结构(使用Eclipse MAT验证) 还是Tomcat,关于类加载器的趣味实验 了不得, ...
- 了解Chrome扩展程序开发--摘抄
了解Chrome扩展程序开发 2018-01-11 边城到此莫若 鸡蛋君说前端 首先,我尝试来用简单几句话描述一下Chrome扩展程序: Chrome扩展主要用于对浏览器功能的增强,它强调与浏览器相结 ...
- 如何用Eclipse将普通的JavaWeb项目转为Maven项目
最新自己的第一个项目差不多稳定运行之后 想着将项目转为Maven项目.于是参考网上成功的将自己的普通的项目转为了maven项目,现在记录一下: 0.普通的java项目的结构如下: 1.接下来开始进行正 ...
- mysql合并和时间函数
sql:利用group_concat()方法,参数为需要合并的字段,合并的字段分隔符默认为逗号,可通过参数separator指定,该方法往往配合group by 一起使用.利用group_concat ...
- mongodb的入门学习
mongodb的入门学习 简介: MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库 ...
- MySql常用函数积累
--MySql查看表结构 select column_name,data_type,CHARACTER_MAXIMUM_LENGTH,column_comment from information_s ...
- 用 jQuery实现图片等比例缩放大小
原文:http://www.open-open.com/code/view/1420975773093 <script type="text/javascript"> ...
- 【hibernate/JPA】注解方式实现 复合主键【spring boot】
1>hibernate/JPA实现复合主键的思路:是将所有的主键属性封装在一个主键类中,提供给需要复合主键的实体类使用. 2>主键类的几点要求: . 使用复合主键的实体类必须实现Seria ...