zoukankan      html  css  js  c++  java
  • Codeforces Round #426 (Div. 2) D. The Bakery (线段树)

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

    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=max{dp[i-1][x]+剩下数字的贡献|1<=x<j},其中剩下数字的贡献长度就是 p+1~x,p满足a[p]==a[x],用线段树剩下数字的贡献距离进行维护,不断的更新最大值即可。思路来自其他博客,详情见代码。

    AC代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <bits/stdc++.h>
     6 using namespace std;
     7 const int maxn=350005;
     8 int dp[55][maxn];
     9 int tree[maxn<<2],lazy[maxn];
    10 int pos[maxn],pre[maxn];
    11 int n,k;
    12 void push_down(int t)
    13 {
    14     if(tree[t])
    15     {
    16         tree[t<<1|1]+=lazy[t];
    17         tree[t<<1]+=lazy[t];
    18         lazy[t<<1|1]+=lazy[t];
    19         lazy[t<<1]+=lazy[t];
    20         lazy[t]=0;
    21     }
    22 }
    23 void push_up(int t)
    24 {
    25     tree[t]=max(tree[t<<1],tree[t<<1|1]);
    26 }
    27 void build(int k,int l,int r,int t)
    28 {
    29     lazy[t]=0;
    30     if(l==r)
    31     {
    32         tree[t]=dp[k][l-1];
    33 
    34         return ;
    35     }
    36     int mid=(l+r)/2;
    37     build(k,l,mid,t<<1);
    38     build(k,mid+1,r,t<<1|1);
    39     push_up(t);
    40 }
    41 
    42 void update(int L,int R,int l,int r,int t)
    43 {
    44     if(L<=l&&r<=R)
    45     {
    46         tree[t]++;
    47         lazy[t]++;
    48         return ;
    49     }
    50     int mid=(l+r)/2;
    51     push_down(t);
    52     if(L>=mid) update(L,R,l,mid,t<<1);
    53     if(R<mid) update(L,R,mid+1,r,t<<1|1);
    54     push_up(t);
    55 }
    56 int query(int L,int R,int l,int r,int t)
    57 {
    58     if(L<=l&&r<=R)
    59     {
    60         return tree[t];
    61     }
    62     int mid=(l+r)/2;
    63     push_down(t);
    64     int ans=0;
    65     if(L<=mid) ans=max(ans,query(L,R,l,mid,t<<1));
    66     if(R>mid) ans=max(ans,query(L,R,mid+1,r,t<<1|1));
    67     push_up(t);
    68     return ans;
    69 }
    70 int main()
    71 {
    72     while(~scanf("%d%d",&n,&k))
    73     {
    74         memset(pre,0,sizeof(pre));
    75         memset(pos,0,sizeof(pos));
    76         memset(tree,0,sizeof(tree));
    77         memset(lazy,0,sizeof(lazy));
    78         int x;
    79         for(int i=1;i<=n;i++)
    80         {
    81             scanf("%d",&x);
    82             pre[i]=pos[x]+1;
    83             pos[x]=i;
    84         }
    85         for(int i=1;i<=k;i++)
    86         {
    87             build(i-1,1,n,1);
    88             for(int j=1;j<=n;j++)
    89             {
    90                 update(pre[j],j,1,n,1);
    91                 dp[i][j]=query(1,j,1,n,1);
    92             }
    93         }
    94         printf("%d
    ",dp[k][n]);
    95     }
    96     return 0;
    97 }
    View Code
  • 相关阅读:
    dotnet 读 WPF 源代码笔记 了解 WPF 已知问题 用户设备上不存在 Arial 字体将导致应用闪退
    WPF 基础 2D 图形学知识 判断点是否在线段上
    WPF 实现自定义的笔迹橡皮擦
    WPF 加载诡异的字体无法布局
    dotnet 5 从 IL 层面分析协变返回类型新特性
    WPF 笔迹触摸点收集工具
    dotnet C# 序列化 XML 时进行自动格式化
    自定义systemctl服务
    maven之三大特性属性、profile、资源过滤
    dependencyManagement与dependencies
  • 原文地址:https://www.cnblogs.com/wang-ya-wei/p/7267260.html
Copyright © 2011-2022 走看看