Codeforces 833B - The Bakery(DP+线段树)

D. The Bakery
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains two integers n and k (1 ≤ n ≤ 350001 ≤ 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.

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 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.

【题意】

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


【思路】

显然需要用到动态规划。我们用dp[i][j]表示前j个数分成i段价值和的最大值。容易得到状态转移方程为

dp[i][j]=max{dp[i-1][x]+剩下数的贡献} {1<=x< j}

过程用线段树维护一下区间最大值即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 35005;
struct SegTree
{
    int left, right, val, lazy;
}seg[maxn<<2];
int dp[55][maxn], pos[maxn], pre[maxn], a[maxn];
void pushup(int rt)
{
    seg[rt].val = max(seg[rt<<1].val, seg[rt<<1|1].val);
    return ;
}
void pushdown(int rt)
{
    seg[rt<<1].val += seg[rt].lazy;
    seg[rt<<1|1].val += seg[rt].lazy;
    seg[rt<<1].lazy += seg[rt].lazy;
    seg[rt<<1|1].lazy += seg[rt].lazy;
    seg[rt].lazy = 0;
}
void build(int pos, int left, int right, int rt)
{
    seg[rt].lazy = 0;
    if(left == right)
    {
        seg[rt].val = dp[pos][left-1];
        return ;
    }
    int mid = (left + right) >> 1;
    build(pos, left, mid, rt<<1);
    build(pos, mid+1, right, rt<<1|1);
    pushup(rt);
}
void update(int l, int r, int left, int right, int rt)
{
    if(l <= left && r >= right)
    {
        seg[rt].val++;
        seg[rt].lazy++;
        return ;
    }
    pushdown(rt);
    int mid = (left + right) >> 1;
    if(l <= mid)
        update(l, r, left, mid, rt<<1);
    if(r > mid)
        update(l, r, mid+1, right, rt<<1|1);
    pushup(rt);
}
int query(int l, int r, int left, int right, int rt)
{
    if(l <= left && r >= right)
    {
        return seg[rt].val;
    }
    pushdown(rt);
    int mid = (left + right) >> 1;
    int ans = 0;
    if(l <= mid)
        ans = max(ans, query(l, r, left, mid, rt<<1));
    if(r > mid)
        ans = max(ans, query(l, r, mid+1, right, rt<<1|1));
    pushup(rt);
    return ans;
}
int main()
{
    int n, k;
    while(scanf("%d%d", &n, &k) != EOF)
    {
        memset(pos, 0, sizeof(pos));
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            pre[i] = pos[a[i]]+1;
            pos[a[i]] = 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);
            }
        }
        printf("%d\n", dp[k][n]);
    }
    return 0;
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C题。在这个题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 题: CodeForces - 982C 树形DP是什么题?如何解决? 回答: CodeForces - 982C是一个树形动态规划题。在这个题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值