codeforces The Bakery(n个数划分k区间,权值为区间不同数,问总权值最大)

来源:互联网 发布:java truststore 编辑:程序博客网 时间:2024/05/22 14:38

B. The Bakery
time limit per test2.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

给你一个序列,让你划分成K段,每段的价值是其内部权值的种类数,让你最大化所有段的价值之和

很容易想到dp,那么 dp[i][j]=max(dp[i-1][k]+w[k~j]的权值)
i为划分组数 j为位置
首先枚举 划分的 组数i,求得 dp前面所有阶段的 dp[i-1][j] ,那么当前阶段的转移就是建造一棵线段树,上面是每个位置 划分i-1段的最大权值。
然后更新当前值给前面所有值带来的影响,其实影响就是 如果给所有分组能涉及到上一个j这个位置的数字的上一个位置的权值和+1,否则的话当前的值会和这个值上一个的位置分到一组,有重复,权值不加,然后再查询一下 以前的所有区间里面哪个最大,其实就是很常规的dp思想, 先分 j-1 组 再看加上当前权值的影响后,哪个权值大。

#include <bits/stdc++.h>using namespace std;const int maxn=35500;int dp[55][maxn];int pre[4*maxn],now[4*maxn];int sum[4*maxn];int add[4*maxn];void push_up(int x){    sum[x]=max(sum[x<<1],sum[x<<1|1]);}void push_down(int x){    if(add[x])    {        add[x<<1]+=add[x],add[x<<1|1]+=add[x];        sum[x<<1]+=add[x];sum[x<<1|1]+=add[x];        add[x]=0;    }}void build(int id,int l,int r,int rt){    sum[rt]=0;    add[rt]=0;    if(l==r)    {        sum[rt]=dp[id][l-1];        return ;    }    int mid=(l+r)>>1;    build(id,l,mid,rt<<1);    build(id,mid+1,r,rt<<1|1);    push_up(rt);}void update(int L,int R,int l,int r,int rt){    if(L<=l&&R>=r)    {        sum[rt]++;        add[rt]++;        return ;    }    push_down(rt);    int mid=(l+r)>>1;    if(L<=mid) update(L,R,l,mid,rt<<1);    if(R>mid) update(L,R,mid+1,r,rt<<1|1);    push_up(rt);}int query(int L,int R,int l,int r,int rt){    if(L<=l&&r<=R)    {        return sum[rt];    }    push_down(rt);    int mid=(l+r)>>1;    int res=0,res1=0;    if(L<=mid)    res=query(L,R,l,mid,rt<<1);    if(R>mid)    res1=query(L,R,mid+1,r,rt<<1|1);    push_up(rt);    return max(res,res1);}int main(){    int n,m;    scanf("%d%d",&n,&m);    for(int i=1;i<=n;i++)    {        int d;        scanf("%d",&d);        pre[i]=now[d]+1;        now[d]=i;    }    for(int i=1;i<=m;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[m][n] );}
阅读全文
0 0
原创粉丝点击