Codeforces 834D The Bakery【dp+线段树】

本文介绍了一个关于将蛋糕按类型打包以最大化收益的问题。通过使用动态规划和线段树的方法,文章详细阐述了如何找到最优解的过程。



B. The Bakery


time limit per test 2.5seconds

memory limit per test     256megabytes

 

Some time ago Slastyona the Sweetmaid decided to open her ownbakery! She bought required ingredients and a wonder-oven which can bakeseveral types of cakes, and opened the bakery.

Soon the expenses started to overcome the income, so Slastyonadecided to study the sweets market. She learned it's profitable to pack cakesin boxes, and that the moredistinct cake types a box contains (let's denote this number as the value of the box), the higherprice it has.

She needs to change the production technology! The problem isthat the oven chooses the cake types on its own and Slastyona can't affect it.However, she knows the types and order ofn 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 oneright after another (in other words, shehas to put in a box a continuous segment of cakes).

Slastyona wants to maximize the total value of all boxes withcakes. Help her determine this maximum possible total value.

Input

The first line contains two integersn and k (1 ≤ n ≤ 35000,1 ≤ k ≤ min(n, 50)) –the number of cakes and the number of boxes, respectively.

The second line containsn integers a1, a2, ..., an (1 ≤ ai ≤ n) – the types of cakes in the order the oven bakes them.

Output

Print the only integer – the maximum total value of all boxes with cakes.

Examples

Input

4 1
1 2 2 1

Output

2

Input

7 2
1 3 3 1 4 4 4

Output

5

Input

8 3
7 7 8 7 7 8 1 7

Output

6

Note

In the first example Slastyona has only one box. She has to putall cakes in it, so that there are two types of cakes in the box, so the valueis equal to2.

In the second example it is profitable to put the first two cakes in the firstbox, and all the rest in the second. There are two distinct types in the firstbox, and three in the second box then, so the total value is5.

 

【题意】

把n个数分成k段,每段的价值为这一段数里不同数字的个数,问价值和最大为多少。


dp[i][j]. 前j个分成i段的最大值。 转移方程为 dp[i][j] = max(dp[i-1][j] + 剩余数的贡献,dp[i][j]);


用线段树来维护,求完每次的最大值后更新线段树,每个节点代表,第j个数之前的最大值(不包括j)然后进行下一轮的dp

#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <cmath>
using namespace std;
const int maxn = 350005;
int dp[55][maxn];
int tree[maxn<<2], lazy[maxn<<2];
void push_down(int root) {
    if (tree[root]) {
        tree[root<<1 | 1] += lazy[root];
        tree[root<<1] += lazy[root];
        
        lazy[root<<1 | 1] += lazy[root];
        lazy[root<<1] += lazy[root];
        lazy[root] = 0;
    }
}

void push_up(int root) {
    tree[root] = max(tree[root<<1], tree[root<<1 | 1]);
}

void build(int k, int left, int right, int root) {
    lazy[root] = 0;
    if (left == right) {
        tree[root] = dp[k][left - 1];
        return;
    }
    int mid = (left + right) >> 1;
    build(k, left, mid, root<<1);
    build(k, mid + 1, right, root<<1 | 1);
    push_up(root);
}

void update(int begin, int end, int left, int right, int root) {
    if (begin <= left && right <= end) {
        tree[root]++;
        lazy[root]++;
        return;
    }
    push_down(root);
    int mid = (left + right) >> 1;
    if (begin <= mid)
        update(begin, end, left, mid, root<<1);
    if (end > mid)
        update(begin, end, mid + 1, right, root<<1 | 1);
    
    push_up(root);
}

int query(int begin, int end, int left, int right, int root) {
    int res = -1;
    
    if (left >= begin && end >= right) {
        return tree[root];
    }
    
    push_down(root);
    int mid = (left + right) >> 1;
    if (mid >= begin)
        res = max(res, query(begin, end, left, mid, root<<1));
    if (mid < end)
        res = max(res, query(begin, end, mid + 1, right, root<<1 | 1));
    
    push_up(root);
    return res;
    
}
int pre[maxn], a[maxn];
int main() {
        //freopen("in.txt", "r", stdin);
    int n, k;
    while (cin >> n >> k) {
        memset(a, 0, sizeof(a));
        memset(dp, 0, sizeof(dp));
        memset(tree, 0, sizeof(tree));
        for (int i = 1; i <= n; ++i) {
            int x;
            cin >> x;
            pre[i] = a[x] + 1;
            a[x] = i;
        }
        
        for (int i = 1; i <= k; ++i) {
            build(i - 1, 1, n, 1);
            for (int j = 1; j <= n; ++j) {
                update(pre[j], j, 1, n, 1);
                dp[i][j] = query(1, j, 1, n, 1);
            }
        }
        cout << dp[k][n] << endl;
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值