Codeforces Round #426 (Div. 1):B. The Bakery
来源:互联网 发布:淘宝网男手提包 编辑:程序博客网 时间:2024/06/06 02:42
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 11 2 2 1
2
7 21 3 3 1 4 4 4
5
8 37 7 8 7 7 8 1 7
6
题意:n个连续的数要分成m段,每段的价值为不同的数字个数,求最大价值和
dp[c][d]表示前c个数分成d段的最大价值,val(a, b)表示区间[a, b]中不同数的个数
转移:dp[c][d] = max(dp[i][d-1]+val(i+1, c)) (1<=i<=c-1)
如果暴力转移的话复杂度是O(kn²)肯定会超时
可以用线段树保存dp[i][d-1]+val(i+1, c)的值,这样每次查询就是log(n)了
保存dp[i][d-1]非常简单,可val(i+1, c)怎么加上去呢?
假设当前遍历完前c个数,那么你只需要知道在这c个数中所有不同的数最后一次出现的位置就好了
对于数字x最后一次出现的位置是y,那么所有的dp[i][d-1] (1<=i<=y-1)都要加上1!
因为数字x对所有的val(i, c) (1<=i<=y)都有贡献!(数字x在y往后的位置都没有再出现过)
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[35005], pre[35005], pos[35005], dp[35005][53], tre[138830], temp[138830];void Lazy(int l, int r, int x){int m;m = (l+r)/2;tre[x*2] += temp[x];tre[x*2+1] += temp[x];if(l!=m)temp[x*2] += temp[x];if(r!=m+1)temp[x*2+1] += temp[x];temp[x] = 0;}void Update(int l, int r, int x, int a, int b, int c){int m;if(l>=a && r<=b){tre[x] += c;if(l!=r)temp[x] += c;return;}if(temp[x])Lazy(l, r, x);m = (l+r)/2;if(a<=m)Update(l, m, x*2, a, b, c);if(b>=m+1)Update(m+1, r, x*2+1, a, b, c);tre[x] = max(tre[x*2+1], tre[x*2]);}int Query(int l, int r, int x, int a, int b){int m, now;now = 0;if(l>=a && r<=b)return tre[x];if(temp[x])Lazy(l, r, x);m = (l+r)/2;if(a<=m)now = max(now, Query(l, m, x*2, a, b));if(b>=m+1)now = max(now, Query(m+1, r, x*2+1, a, b));return now;}int main(void){int n, k, i, j;scanf("%d%d", &n, &k);for(i=1;i<=n;i++){scanf("%d", &a[i]);pre[i] = pos[a[i]];pos[a[i]] = i;}for(j=1;j<=k;j++){memset(tre, 0, sizeof(tre));memset(temp, 0, sizeof(temp));for(i=1;i<=n;i++){Update(0, n, 1, i, i, dp[i][j-1]);if(i>=j){Update(0, n, 1, pre[i], i-1, 1);dp[i][j] = Query(0, n, 1, j-1, i-1);}}}printf("%d\n", dp[n][k]);return 0;}
- Codeforces Round #426 (Div. 1):B. The Bakery
- Codeforces Round #426 (Div. 1) B. The Bakery (DP + 线段树)
- Codeforces Round #426 (Div. 1) B. The Bakery(dp+线段树)
- Codeforces Round #368 (Div. 2)B Bakery
- Codeforces Round #368 (Div. 2) B. Bakery
- Codeforces Round #368 (Div. 2) B. Bakery
- Codeforces Round #426 (Div. 2) The Bakery 线段树+dp
- Codeforces Round #426 (Div. 2) D. The Bakery
- Codeforces Round #368 (Div. 2)(B. Bakery 贪心)
- Codeforces Round #368 (Div. 2) B. Bakery (水题)
- Codeforces Round #368 (Div. 2) B Bakery(水)
- Codeforces Round #368 (Div. 2) B. Bakery (水题)
- Codeforces Round #368 (Div. 2) 707B. Bakery
- Codeforces Round #368 (Div. 2) B. Bakery(简单图)
- Codeforces 833B (Codeforces Round #426) The Bakery DP+线段树
- Codeforces Round #426 (Div. 2) C. The Meaningless Game 思维 D. The Bakery dp
- Codeforces Round #426 (Div. 2) D. The Bakery(DP+线段树) 好题
- Codeforces Round #426 (Div. 2) D. The Bakery(DP+线段树)
- grep sed awk 文本处理 题目及答案
- svn服务用svnsync命令双机热备
- Unable to inflate view tag without class attribute
- 根据不同访问设备跳转到PC页面或手机页面
- input file 选择文件之后的校验
- Codeforces Round #426 (Div. 1):B. The Bakery
- Hibernate生命周期
- echarts x轴文字高度
- c++实现哈夫曼树编码
- Java学习中关于容器
- idea创建第一个hibernate遇到的问题
- 数据类型转换与自定义函数
- 重磅消息:OpenBSD 将会在每次重启后都使用和之前不同的内核
- LUA 基础语法相关