CF833B The Bakery
Posted oi-forever
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF833B The Bakery相关的知识,希望对你有一定的参考价值。
题意翻译
将一个长度为n的序列分为k段
使得总价值最大一段区间的价值表示为区间内不同数字的个数
n<=35000,k<=50
Translated by @ysner @yybyyb
题目描述
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 nn cakes the oven is going to bake today. Slastyona has to pack exactly kk 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 nn and kk ( 1<=n<=350001<=n<=35000 , 1<=k<=min(n,50)1<=k<=min(n,50) ) – the number of cakes and the number of boxes, respectively.
The second line contains nn integers a_{1},a_{2},...,a_{n}a1?,a2?,...,an? ( 1<=a_{i}<=n1<=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.
输入输出样例
说明
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 22 .
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 55 .
总结:
线段树优化dp
dp方程很好推
设dp[i][j]表示前i个数分成j组的最大价值
dp[i][j] = max(dp[k][j - 1] + calc(k + 1, j))
calc(i, j)表示i到j中不重复的数的个数
然后暴力dp的话就是O(n * n * k), 炸飞了
然后可以滚掉第二维
考虑优化转移, 将上一阶段的dp数组建立线段树
第k个节点表示dp[k][i - 1]即dp[k]
然后考虑如何计算calc(k + 1, j)
lst[i]表示a[i]上一次出现的位置
显然枚举到j时,只有(lst[j] + 1, j).....(j, j)的calc才会改变,即值加1,线段树区间加法
然后就是线段树区间查询最大值啦
#include <bits/stdc++.h> using namespace std; const int maxn = 4e5 + 7; int a[maxn], val[maxn], tag[maxn], n, k; int id[maxn], lst[maxn], dp[40000]; int mmax(int a, int b) {return a > b ?a :b;} void update(int o) { val[o] = mmax(val[o << 1], val[o << 1 | 1]); } void pushdown(int o, int l, int r) { if(tag[o] != 0) { tag[o << 1] += tag[o]; tag[o << 1 | 1] += tag[o]; val[o << 1] += tag[o]; val[o << 1 | 1] += tag[o]; tag[o] = 0; } } void Build(int o, int l, int r) { tag[o] = 0; if(l == r) { val[o] = dp[l]; return; } int mid = (l + r) >> 1; Build(o << 1, l, mid); Build(o << 1 | 1, mid + 1, r); update(o); } void Modify(int o, int l, int r, int ql, int qr) { if(ql <= l && r <= qr) { val[o]++; tag[o]++; return; } int mid = (l + r) >> 1; pushdown(o, l, r); if(ql <= mid) Modify(o << 1, l, mid, ql, qr); if(qr > mid) Modify(o << 1 | 1, mid + 1, r, ql, qr); update(o); } int Query(int o, int l, int r, int ql, int qr) { if(ql <= l && r <= qr) return val[o]; int res = 0, mid = (l + r) >> 1; pushdown(o, l, r); if(ql <= mid) res = mmax(res, Query(o << 1, l, mid, ql, qr)); if(qr > mid) res = mmax(res, Query(o << 1 | 1, mid + 1, r, ql, qr)); return res; } int main() { scanf("%d%d",&n, &k); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); lst[i] = id[a[i]]; id[a[i]] = i; } for (int i = 1; i <= k; ++i) { Build(1, 0, n); for (int j = i; j <= n; ++j) { Modify(1, 0, n, lst[j], j - 1); dp[j] = Query(1, 0, n, 0, j - 1); } } printf("%d ", dp[n]); return 0; }
以上是关于CF833B The Bakery的主要内容,如果未能解决你的问题,请参考以下文章