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$个火车站不出售车票 ...
随机推荐
- sqli-labs(九)_COOKIE处注入
第二十关: 这关是一个Cookie处的注入,输入正确的账号密码后,会跳到index.php页面,如下图 这个时候再访问登陆页面的时候http://localhost/sqli-labs-master/ ...
- 字符串转化为int数组
String a = "1,2,3,4,5,6" String str[] = a.split(","); int array[] = new int[str. ...
- 发布网站配置文件和SSL
1.将cert下新建一个文件将所有证书文件放在新建的文件下 例如:cert/medcard 2.配置网站的.conf文件 <VirtualHost *:443> ServerName ww ...
- Self hosted OWIN 绑定地址127.0.0.1,外网无法访问
static void Main() { string baseAddress = "http://localhost:4004/"; ...
- C# Mongo Client 2.4.2创建索引
static async Task CreateIndex() { var client = new MongoClient(); var database = client.GetDatabase( ...
- date的用法
date -d "-1 month" "+%T" 当前时间减少一个月 +%T 简便表示时分秒 +%F 简便表示年月日 date +%Y 四位年份 date + ...
- html5-label标签
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- html5-绝对路径/相对路径
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 随机模拟(MCMC)
http://cos.name/2013/01/lda-math-mcmc-and-gibbs-sampling/ http://blog.csdn.net/lin360580306/article/ ...
- 对SQLite 数据库的一点点了解
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它的设计目标是嵌入式的,它占用资源非常低,在嵌入式设备中,可能只需要几百k的内存就够了. SQLit ...