Codeforces 834D The Bakery - 动态规划 - 线段树
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.
题目大意 给定一个序列,分成k个非空部分,使得每部分不同的数的个数之和最大。
显然你需要一发动态规划,用f[i][j]表示前i个位置,已经分成j个非空部分的最大总价值。转移很显然:
$f[i][j] = \max\left\{ f[k][j - 1] + w\left(k + 1, i \right )\right \}$
其中$w(i, j)$表示$[i, j]$这一段中不同的数的个数。如果这么做的话是$O(n^{2}k)$,会TLE。考虑如何维护$f[k][j - 1] + w(k + 1, i)$。
有注意到一堆后缀后面增加一个数,不同的数会发生改变的后缀的长度一定是连续的一段,并且这些后缀都不包含这个数。
因此,我们可以记录一个$last[x]$,表示在当前考虑的位置$i$之前,$x$上一次出现的位置在哪。然后就可以找到增加的这一段是哪了。
对于要支持区间求最大值,区间修改,我们想到了线段树。于是就用线段树去维护dp值对当前位置的贡献。转移的时候查一查最值就好了。
Code
/**
* Codeforces
* Problem#834D
* Accepted
* Time: 857ms
* Memory: 71900k
*/
#include<bits/stdc++.h>
using namespace std; typedef class SegTreeNode {
public:
int val;
int lazy;
SegTreeNode *l, *r; SegTreeNode():val(), lazy(), l(NULL), r(NULL) { }
// SegTreeNode(int val, SegTreeNode* l, SegTreeNode* r):val(val), lazy(lazy), l(l), r(r) { } inline void pushUp() {
val = max(l->val, r->val);
} inline void pushDown() {
l->val += lazy, l->lazy += lazy;
r->val += lazy, r->lazy += lazy;
lazy = ;
}
}SegTreeNode; #define Limit 4000000
SegTreeNode pool[Limit];
int top = ; SegTreeNode* newnode() {
if(top >= Limit) return new SegTreeNode();
pool[top] = SegTreeNode();
return pool + (top++);
} typedef class SegTree {
public:
SegTreeNode **rts; SegTree():rts(NULL) { }
SegTree(int k, int n) {
rts = new SegTreeNode*[(k + )];
for(int i = ; i <= k; i++)
build(rts[i], , n);
} void build(SegTreeNode*& node, int l, int r) {
node = newnode();
if(l == r) return;
int mid = (l + r) >> ;
build(node->l, l, mid);
build(node->r, mid + , r);
} void update(SegTreeNode*& node, int l, int r, int ql, int qr, int val) {
if(l == ql && r == qr) {
node->val += val;
node->lazy += val;
return;
}
if(node->lazy) node->pushDown();
int mid = (l + r) >> ;
if(qr <= mid) update(node->l, l, mid, ql, qr, val);
else if(ql > mid) update(node->r, mid + , r, ql, qr, val);
else {
update(node->l, l, mid, ql, mid, val);
update(node->r, mid + , r, mid + , qr, val);
}
node->pushUp();
} int query(SegTreeNode*& node, int l, int r, int ql, int qr) {
if(l == ql && r == qr) return node->val;
if(node->lazy) node->pushDown();
int mid = (l + r) >> ;
if(qr <= mid) return query(node->l, l, mid, ql, qr);
if(ql > mid) return query(node->r, mid + , r, ql, qr);
return max(query(node->l, l, mid, ql, mid), query(node->r, mid + , r, mid + , qr));
} SegTreeNode*& operator [] (int pos) {
return rts[pos];
}
}SegTree; int n, k;
int *arr;
int res = ; inline void init() {
scanf("%d%d", &n, &k);
arr = new int[(n + )];
for(int i = , x; i <= n; i++)
scanf("%d", arr + i); } SegTree st;
int f[][];
int last[];
inline void solve() {
st = SegTree(k, n);
memset(f, , sizeof(f[]) * (n + ));
memset(last, , sizeof(int) * (n + ));
for(int i = ; i <= n; i++) {
for(int j = ; j <= k && j <= i; j++) {
// printf("Segment [%d, %d] has been updated.", last[arr[i]] + 1, i - 1),
st.update(st[j - ], , n, last[arr[i]], i - , );
f[i][j] = st.query(st[j - ], , n, , i - );
st.update(st[j], , n, i, i, f[i][j]);
// cout << i << " " << j << " " << f[i][j] << endl;
}
last[arr[i]] = i;
}
printf("%d\n", f[n][k]);
} int main() {
init();
solve();
return ;
}
Codeforces 834D The Bakery - 动态规划 - 线段树的更多相关文章
- Codeforces 834D The Bakery 【线段树优化DP】*
Codeforces 834D The Bakery LINK 题目大意是给你一个长度为n的序列分成k段,每一段的贡献是这一段中不同的数的个数,求最大贡献 是第一次做线段树维护DP值的题 感觉还可以, ...
- Codeforces 833B The Bakery dp线段树
B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树
BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树 题意: 约翰的奶牛们从小娇生惯养,她们无法容忍牛棚里的任何脏东西.约翰发现,如果要使这群 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- 2019牛客多校第一场 I Points Division(动态规划+线段树)
2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...
- Codeforces 834D The Bakery【dp+线段树维护+lazy】
D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...
- Codeforces 834D - The Bakery(dp+线段树)
834D - The Bakery 思路:dp[i][j]表示到第j个数为止分成i段的最大总和值. dp[i][j]=max{dp[i-1][x]+c(x+1,j)(i-1≤x≤j-1)},c(x+1 ...
- CodeForces 834D The Bakery(线段树优化DP)
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- Codeforces 675E Trains and Statistic - 线段树 - 动态规划
题目传送门 快速的vjudge通道 快速的Codeforces通道 题目大意 有$n$个火车站,第$i$个火车站出售第$i + 1$到第$a_{i}$个火车站的车票,特殊地,第$n$个火车站不出售车票 ...
随机推荐
- 初识Vue,简单的todolist
vue开发源码:https://vuejs.org/js/vue.js todolist代码: <!DOCTYPE html> <html lang="en"&g ...
- LeetCode38.报数
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作 "one 1" ...
- EL语言表达式 (二)【EL对数据的访问】
一.访问方式: EL中访问数据和Java中访问数组的方式相同,即可以通过“[]”和“.”运算符进行访问.而且两种形式是等价的.如: 访问JavaBean对象userInfo中的id属性,可以写成下面两 ...
- Unity shader学习之菲涅耳反射
菲涅尔反射(Fresnel reflection),指光线照射物体表面时,一部分发生反射,一部分进入物体内部发生折射或散射,被反射的光和折射光之间存在一定的比率. 2个公式: 1. Schlick 菲 ...
- kali linux wmtools安装
1,选择挂载盘时选择自动检测 2,点击安裝vmware tools安裝 3.tar -xzf 壓縮包名 4../vmware-install.pl 5,reboot
- Robot framework selenium driver download
Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads http://npm.taobao.org/mirrors ...
- SQL性能优化前期准备-清除缓存、开启IO统计
文章来至:https://www.cnblogs.com/Ren_Lei/p/5669662.html 如果需要进行SQl Server下的SQL性能优化,需要准备以下内容: 一.SQL查询分析器设置 ...
- git克隆远程项目并创建本地对应分支
http://jingyan.baidu.com/article/19192ad83ea879e53e5707ce.html
- 【转】Tomcat 快速入门
本文转载自:https://www.cnblogs.com/jingmoxukong/p/8258837.html?utm_source=gold_browser_extension 目录 Tomca ...
- tensorflow--variable_scope
1.with tf.variable_scope(name , reuse = reuse) (1)创建variable scope with tf.variable_scope("foo& ...