首页 > 代码库 > Codeforces 834D The Bakery - 动态规划 - 线段树
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个非空部分的最大总价值。转移很显然:
其中w(i, j)表示[i, j]这一段中不同的数的个数。如果这么做的话是,会TLE。考虑如何维护。
有注意到一堆后缀后面增加一个数,不同的数会发生改变的后缀的长度一定是连续的一段,并且这些后缀都不包含这个数。
因此,我们可以记录一个last[x],表示在当前考虑的位置i之前,x上一次出现的位置在哪。然后就可以找到增加的这一段是哪了。
对于要支持区间求最大值,区间修改,我们想到了线段树。于是就用线段树去维护dp值对当前位置的贡献。转移的时候查一查最值就好了。
Code
1 /** 2 * Codeforces 3 * Problem#834D 4 * Accepted 5 * Time: 857ms 6 * Memory: 71900k 7 */ 8 #include<bits/stdc++.h> 9 using namespace std; 10 11 typedef class SegTreeNode { 12 public: 13 int val; 14 int lazy; 15 SegTreeNode *l, *r; 16 17 SegTreeNode():val(0), lazy(0), l(NULL), r(NULL) { } 18 // SegTreeNode(int val, SegTreeNode* l, SegTreeNode* r):val(val), lazy(lazy), l(l), r(r) { } 19 20 inline void pushUp() { 21 val = max(l->val, r->val); 22 } 23 24 inline void pushDown() { 25 l->val += lazy, l->lazy += lazy; 26 r->val += lazy, r->lazy += lazy; 27 lazy = 0; 28 } 29 }SegTreeNode; 30 31 #define Limit 4000000 32 SegTreeNode pool[Limit]; 33 int top = 0; 34 35 SegTreeNode* newnode() { 36 if(top >= Limit) return new SegTreeNode(); 37 pool[top] = SegTreeNode(); 38 return pool + (top++); 39 } 40 41 typedef class SegTree { 42 public: 43 SegTreeNode **rts; 44 45 SegTree():rts(NULL) { } 46 SegTree(int k, int n) { 47 rts = new SegTreeNode*[(k + 1)]; 48 for(int i = 0; i <= k; i++) 49 build(rts[i], 0, n); 50 } 51 52 void build(SegTreeNode*& node, int l, int r) { 53 node = newnode(); 54 if(l == r) return; 55 int mid = (l + r) >> 1; 56 build(node->l, l, mid); 57 build(node->r, mid + 1, r); 58 } 59 60 void update(SegTreeNode*& node, int l, int r, int ql, int qr, int val) { 61 if(l == ql && r == qr) { 62 node->val += val; 63 node->lazy += val; 64 return; 65 } 66 if(node->lazy) node->pushDown(); 67 int mid = (l + r) >> 1; 68 if(qr <= mid) update(node->l, l, mid, ql, qr, val); 69 else if(ql > mid) update(node->r, mid + 1, r, ql, qr, val); 70 else { 71 update(node->l, l, mid, ql, mid, val); 72 update(node->r, mid + 1, r, mid + 1, qr, val); 73 } 74 node->pushUp(); 75 } 76 77 int query(SegTreeNode*& node, int l, int r, int ql, int qr) { 78 if(l == ql && r == qr) return node->val; 79 if(node->lazy) node->pushDown(); 80 int mid = (l + r) >> 1; 81 if(qr <= mid) return query(node->l, l, mid, ql, qr); 82 if(ql > mid) return query(node->r, mid + 1, r, ql, qr); 83 return max(query(node->l, l, mid, ql, mid), query(node->r, mid + 1, r, mid + 1, qr)); 84 } 85 86 SegTreeNode*& operator [] (int pos) { 87 return rts[pos]; 88 } 89 }SegTree; 90 91 int n, k; 92 int *arr; 93 int res = 0; 94 95 inline void init() { 96 scanf("%d%d", &n, &k); 97 arr = new int[(n + 1)]; 98 for(int i = 1, x; i <= n; i++) 99 scanf("%d", arr + i);100 101 }102 103 SegTree st;104 int f[35001][51];105 int last[35001];106 inline void solve() {107 st = SegTree(k, n);108 memset(f, 0, sizeof(f[0]) * (n + 1));109 memset(last, 0, sizeof(int) * (n + 1));110 for(int i = 1; i <= n; i++) {111 for(int j = 1; j <= k && j <= i; j++) {112 // printf("Segment [%d, %d] has been updated.", last[arr[i]] + 1, i - 1),113 st.update(st[j - 1], 0, n, last[arr[i]], i - 1, 1);114 f[i][j] = st.query(st[j - 1], 0, n, 0, i - 1);115 st.update(st[j], 0, n, i, i, f[i][j]);116 // cout << i << " " << j << " " << f[i][j] << endl;117 }118 last[arr[i]] = i;119 }120 printf("%d\n", f[n][k]);121 }122 123 int main() {124 init();125 solve();126 return 0;127 }
Codeforces 834D The Bakery - 动态规划 - 线段树