[CF833B Round#426 Div.1]The Bakery——[线段树+DP]

【原题】
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.

【题目翻译】
将一个长度为n的序列分为k段,使得总价值最大。

一段区间的价值表示为区间内不同数字的个数

n<=35000,k<=50

【输入格式】
第一行两个数n,k
第二行n个数表示这个序列

S a m p l e    I n p u t Sample~~Input Sample  Input

4 1
1 2 2 1

S a m p l e    O u t p u t Sample~~Output Sample  Output

2

【题意分析】

朴素dp很好想:dp[i][j]表示前i个分成j段的最大价值,那么枚举断点k

d p [ i ] [ j ] = max ⁡ ( d p [ i ] [ j ] , d p [ k ] [ j − 1 ] + p [ k + 1 ] [ i ] ) dp[i][j]=\max(dp[i][j],dp[k][j-1]+p[k+1][i]) dp[i][j]=max(dp[i][j],dp[k][j1]+p[k+1][i])

其中p[i][j]表示i到j有几种颜色。

这样显然布星,我们发现这个max()貌似可以用线段树优化,那么这个p[i][j]怎么搞呢?

我们弄一棵区间最大值的线段树,考虑每个点的贡献,它只可能在它上一次出现的位置到它当前的位置这一段产生一单位的贡献,那么又可以用线段树搞了。

注意每次建树清零。

Code:

#include 
#include 
#include 
#include 
#include 
#include 
#define INF 1 << 28
#define MAXN 100000
using namespace std;

int pos[MAXN], pre[MAXN], dp[MAXN][60], res, n, k; 

struct segtree {
    
    int tree[MAXN << 2], lazy[MAXN << 2];
    
    void clear () {
        memset (tree, 0, sizeof tree);
        memset (lazy, 0, sizeof lazy);
    }
    
    inline void pushup (int now) {
        tree[now] = max (tree[now << 1], tree[now << 1 | 1]);
    }
    
    inline void pushdown (int now, int l, int r) {
        if (lazy[now]) {
            int mid = l + r >> 1;
            tree[now << 1] += lazy[now];
            tree[now << 1 | 1] += lazy[now];
            lazy[now << 1] += lazy[now];
            lazy[now << 1 | 1] += lazy[now];
            lazy[now] = 0;
        }
    }
    
    void build (int now, int l, int r, int o) {
        if (l == r) {
            tree[now] = dp[l - 1][o];
            return;
        }
        int mid = l + r >> 1;
        build (now << 1, l, mid, o);
        build (now << 1 | 1, mid + 1, r, o);
        pushup (now);
    }
    
    void update (int now, int l, int r, int L, int R, int o) {
        if (R < l || r < L) return;
        if (L <= l && r <= R) {
            tree[now] += o, lazy[now] += o;
            return;
        }
        pushdown (now, l, r);
        int mid = l + r >> 1;
        update (now << 1, l, mid, L, R, o);
        update (now << 1 | 1, mid + 1, r, L, R, o);
        pushup (now);
    }
    
    void query (int now, int l, int r, int L, int R) {
        if (R < l || r < L) return;
        if (L <= l && r <= R) {
            res = max (res, tree[now]);
            return;
        }
        pushdown (now, l, r);
        int mid = l + r >> 1;
        query (now << 1, l, mid, L, R);
        query (now << 1 | 1, mid + 1, r, L, R);
    }
    
}tree;

int main () {
    scanf ("%d%d", &n, &k);
    for (register int i = 1; i <= n; i++) {
        int x; scanf ("%d", &x);
        pre[i] = pos[x], pos[x] = i;
    }
    for (register int i = 1; i <= k; i++) {
        tree.clear (), tree.build (1, 1, n, i - 1);
        for (register int j = 1; j <= n; j++) {
            tree.update (1, 1, n, pre[j] + 1, j, 1);
            res = -INF; tree.query (1, 1, n, 1, j);
            dp[j][i] = res;
        }
    }
    printf ("%d\n", dp[n][k]);
    return 0;
} 

你可能感兴趣的:(动态规划,线段树,构造)