zoukankan      html  css  js  c++  java
  • 【CodeForces 426】div1 B The Bakery


    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.

    Input

    4 1
    1 2 2 1
    

    Output

    2
    

    题解


    (dp[k][n])表示k个分割的情况下最大收益
    (C[i][j])表示区间(i,j)不同蛋糕的数量
    对于只有k=1的情况

    [dp[1][n]=C[1][n] ]

    对于k>1的情况

    [dp[k][n]=max;dp[k-1][i-1]+C[i][n] ]

    如何求C数组?
    对于每一个x,它能向左覆盖到上一个x的后一个位置,在这一部分,这个值都能提供贡献1。那么最终的value为所有点的覆盖中最大的值(即所有的x都有一个独立前缀,那么答案为这些独立前缀的交集最大者)
    用线段树维护即可

    import java.io.*;
    import java.util.*;
    
    public class Main {
    	static final int N=35005;
    	static int dp[][]=new int[55][N];
    	static int a[]=new int[N];
    	static int n,k;
    	static int max[]=new int[N<<2],tag[]=new int[N<<2];
    	static void build(int pos,int rt,int l,int r) {
    		tag[rt]=0;
    		if(l==r) {
    			max[rt]=dp[pos][l-1];
    			return;
    		}
            int mid=(l+r)>>1;
    	    build(pos,rt<<1,l,mid);
    	    build(pos,rt<<1|1,mid+1,r);
    	    max[rt]=Math.max(max[rt<<1],max[rt<<1|1]);
    	}
    	static void pushdown(int rt) {
    		if(tag[rt]<=0) return;
    		tag[rt<<1]+=tag[rt];
    		tag[rt<<1|1]+=tag[rt];
    		max[rt<<1]+=tag[rt];
    		max[rt<<1|1]+=tag[rt];
    		tag[rt]=0;
    	}
    	static void update(int L,int R,int rt,int l,int r) {
    		if(L<=l&&r<=R) {
    			tag[rt]++;
    			max[rt]++;
    			return;
    		}
    		pushdown(rt);
    		int mid=(l+r)>>1;
    		if(L<=mid) update(L,R,rt<<1,l,mid);
    		if(R>mid) update(L,R,rt<<1|1,mid+1,r);
    	    max[rt]=Math.max(max[rt<<1],max[rt<<1|1]);
    	}
    	static int query(int L,int R,int rt,int l,int r) {
    		if(L<=l&&r<=R) {
    			return max[rt];
    		}
    		pushdown(rt);
    		int mid=(l+r)>>1,ret=0;
    		if(L<=mid) ret=Math.max(ret,query(L,R,rt<<1,l,mid));
    		if(R>mid)  ret=Math.max(ret, query(L,R,rt<<1|1,mid+1,r));
    		max[rt]=Math.max(max[rt<<1], max[rt<<1|1]);
    		return ret;
    	}
    	static int pos[]=new int[N];
    	static int pre[]=new int[N];
    	public static void main(String[] args){
    		InputReader in = new InputReader(System.in);
    	    PrintWriter out = new PrintWriter(System.out);
    		n=in.nextInt();k=in.nextInt();
    		Arrays.fill(pos,0,n+1,0);
    		Arrays.fill(pre,0,n+1,0);
    		for(int i=1;i<=n;i++) {
    			a[i]=in.nextInt();
    			pre[i]=pos[a[i]]+1;
    			pos[a[i]]=i;
    		}
    		Arrays.fill(dp[0],0,n+1,0);
    		for(int i=1;i<=k;i++) {
    			build(i-1,1,1,n);
    			for(int j=1;j<=n;j++) {
    				update(pre[j],j,1,1,n);
    				dp[i][j]=query(1,j,1,1,n);
    			}
    		}
    		out.println(dp[k][n]);
    		out.flush();
    	}
    	static class InputReader {
            public BufferedReader reader;
            public StringTokenizer tokenizer;
            public InputReader(InputStream stream) {
                reader = new BufferedReader(new InputStreamReader(stream), 32768);
                tokenizer = null;
            }
            public String next() {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    try {
                        tokenizer = new StringTokenizer(reader.readLine());
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                return tokenizer.nextToken();
            }
            public int nextInt() {
                return Integer.parseInt(next());
            }
        }
    }
    
    
  • 相关阅读:
    scss-数据类型
    scss-@import
    scss-&父选择器标识符
    scss-嵌套属性
    Python之NumPy(axis=0 与axis=1)区分
    Java map 详解
    java之JDBC多条语句执行
    p-value值的认识
    numpy.random之常用函数
    Python random模块sample、randint、shuffle、choice随机函数
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/7273460.html
Copyright © 2011-2022 走看看