codeforce div2 426 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

——————————————————————————————————

这道题很容易想到一个O(nnk)的暴力 

就是外层枚举k第二层枚举n 内层枚举断点就可以了 是个非常常见的dp

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=45007; 
int sum,n,m,c[M],cnt;
int f[M][55],q[M];
struct node{int v,pos;}e[M];
bool cmp(node a,node b){return a.v<b.v;}
void clear(){memset(q,0,sizeof(q));}
int main()
{
    freopen("camp.in","r",stdin);
    freopen("camp.out","w",stdout);
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&e[i].v),e[i].pos=i;
    sort(e+1,e+1+n,cmp);
    c[e[1].pos]=++cnt;
    for(int i=2;i<=n;i++){
        if(e[i].v!=e[i-1].v) cnt++;
        c[e[i].pos]=cnt;
    }
    for(int i=1;i<=n;i++){
        if(!q[c[i]]) sum++,q[c[i]]=1;
        f[i][1]=sum;
    }
    clear();
    for(int k=2;k<=m;k++){
        clear();
        for(int i=k;i<=n;i++){
            sum=1; q[c[i]]=i;
            for(int j=i-1;j>=k-1;j--){
                f[i][k]=max(f[i][k],f[j][k-1]+sum);
                if(q[c[j]]!=i) sum++,q[c[j]]=i;
            }
        }
    }printf("%d\n",f[n][m]);
    return 0;
}
View Code

但是很明显这样只能水五十分

很明显外两层不能去掉 因为他们枚举的是所有的状态

而内层的枚举很明显可以用线段树优化

每次找到一个i 他的颜色是x 将x的上一个位置+1到当前位置的区间+1 然后找一下最大值

更新当前的答案f【i】 表示将1-i 分成当前状态的k段的最优情况

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=45007; 
int sum,n,m,c[M],cnt;
int f[M][55],q[M];
struct node{int v,pos;}e[M];
bool cmp(node a,node b){return a.v<b.v;}
void clear(){memset(q,0,sizeof(q));}
int main()
{
    freopen("camp.in","r",stdin);
    freopen("camp.out","w",stdout);
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&e[i].v),e[i].pos=i;
    sort(e+1,e+1+n,cmp);
    c[e[1].pos]=++cnt;
    for(int i=2;i<=n;i++){
        if(e[i].v!=e[i-1].v) cnt++;
        c[e[i].pos]=cnt;
    }
    for(int i=1;i<=n;i++){
        if(!q[c[i]]) sum++,q[c[i]]=1;
        f[i][1]=sum;
    }
    clear();
    for(int k=2;k<=m;k++){
        clear();
        for(int i=k;i<=n;i++){
            sum=1; q[c[i]]=i;
            for(int j=i-1;j>=k-1;j--){
                f[i][k]=max(f[i][k],f[j][k-1]+sum);
                if(q[c[j]]!=i) sum++,q[c[j]]=i;
            }
        }
    }printf("%d\n",f[n][m]);
    return 0;
}
View Code

 

 

转载于:https://www.cnblogs.com/lyzuikeai/p/7326076.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值