zoukankan      html  css  js  c++  java
  • codeforces CF834D CF833B The Bakery 线段树优化DP

    $ Rightarrow $ 戳我进CF原题

    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 $ a_1, a_2, ..., a_n (1 ≤ a_i ≤ 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

    input1

     4 1
     1 2 2 1
    

    output1

     2
    

    input2

     7 2
     1 3 3 1 4 4 4
    

    output2

     5
    

    input3

     8 3
     7 7 8 7 7 8 1 7
    

    output3

    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段

    • 使得总价值最大一段区间的价值表示为区间内不同数字的个数

    • $ n le 35000,k le 50 $

    Translated by @ysner @yybyyb
     

    思路

    • 一个显然的DP方程 $ dp[i][j]=max(dp[k-1][j-1]+w[k][i]) $ 。

    • $ dp[i][j] $ 表示前 $ i $ 个数划分成 $ j $ 段的最大价值, $ w[i][j] $ 表示区间 $ [L,R] $ 的权值,
      则我们最终要的答案就是 $ dp[n][k] $ ,暴力转移复杂度为 $ O(kn^2) $ 。

    • 考虑一下如何简化 $ w $ 。记录 $ a_x $ 上一次出现的位置为 $ pre_x $ ,
      则 $ a_x $ 为 $ pre_x+1 le i le x $ 的 $ w[i][x] $ 提供了 $ 1 $ 的贡献。
      那么我们如果想从 $ w[i][x-1] $ 转移到 $ w[i][x] $ ,只需对区间 $ [pre_x+1, x] $ 加 $ 1 $ 即可。

    • 那么我们要做的就是维护 $ f[k-1][j]+w[k][i] $ 的区间最值,用线段树即可。
      第二维由 $ i $ 变为 $ i+1 $ 时,对线段树进行一次区间加即可。

    • 时间复杂度 $ O(n imes k imes log_n ) $
       

    代码

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    #define int long long
    #define N 35005
    inline int read() {
        register char ch;
        while(!isdigit(ch=getchar()));
        register int x=ch^'0';
        while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
        return x;
    }
    int n,k,sum[N<<2],lzy[N<<2],pre[N],pos[N],f[N][51];
    void build(int o,int l,int r,int k){
    	lzy[o]=0; 
    	if(l==r){
    		sum[o]=f[l-1][k];
    		return;
    	}
    	int mid=l+r>>1;
    	build(o<<1,l,mid,k); build(o<<1|1,mid+1,r,k);
    	sum[o]=max(sum[o<<1],sum[o<<1|1]);
    }
    inline void pushdown(int o){
    	sum[o<<1]+=lzy[o];
    	sum[o<<1|1]+=lzy[o];
    	lzy[o<<1]+=lzy[o];
    	lzy[o<<1|1]+=lzy[o];
    	lzy[o]=0;
    }
    void updata(int o,int l,int r,int L,int R){
    	if(L<=l&&r<=R){
    		sum[o]+=1;
    		lzy[o]+=1;
    		return;
    	}
    	if(lzy[o]) pushdown(o);
    	int mid=l+r>>1;
    	if(L>mid) updata(o<<1|1,mid+1,r,L,R);
    	else if(R<=mid) updata(o<<1,l,mid,L,R);
    	else{
    		updata(o<<1,l,mid,L,R);
    		updata(o<<1|1,mid+1,r,L,R);
    	}
    	sum[o]=max(sum[o<<1],sum[o<<1|1]);
    }
    int query(int o,int l,int r,int L,int R){
    	if(L<=l&&r<=R) return sum[o];
    	if(lzy[o]) pushdown(o);
    	int mid=l+r>>1;
    	if(L>mid) return query(o<<1|1,mid+1,r,L,R);
    	else if(R<=mid) return query(o<<1,l,mid,L,R);
    	else return max(query(o<<1,l,mid,L,R),query(o<<1|1,mid+1,r,L,R));
    }
    signed main(){
    	n=read(); k=read();
    	for(int i=1;i<=n;++i){
    		int a;
    		scanf("%lld",&a);
    		pre[i]=pos[a]+1;
    		pos[a]=i;
    	}
    	for(int j=1;j<=k;++j){
    		build(1,1,n,j-1);
    		for(int i=1;i<=n;++i){
    			updata(1,1,n,pre[i],i);
    			f[i][j]=query(1,1,n,1,i);
    		}
    	}
    	printf("%lld",f[n][k]);
    	return 0;
    }
    /*
    #        42697971
    When     2018-09-09 11:37:17 
    Who      PotremZ 
    Problem  D - The Bakery 
    Lang     GNU C++11 
    Verdict  Accepted
    Time     1154 ms
    Memory   16700 KB
    */
    
  • 相关阅读:
    两个list,并集、交集、差集
    关于apt-get install
    异步进程池
    获取cpu信息
    print重定向
    使用pandas操作excel
    排列组合,取所有可能性
    list_反转,切片,删除,升序降序
    抠图
    enumerate()函数的用法
  • 原文地址:https://www.cnblogs.com/PotremZ/p/CF834D.html
Copyright © 2011-2022 走看看