Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP

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

Examples
input
4 1
1 2 2 1
output
2
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个盒子,n个数,将连续的一段数放到盒子里,使得每个盒子不同数个数加起来,总和最大

题解:

  设定dp[i][j],在前j个数,分成i块的 最大价值

  那么 dp[i][j]  = max(dp[i-1][k] + sum[k+1][j]) 

  记录每个位这个数 上一次出现的位置last[i]

  更新当前层,先把上一层即 dp[i-1][1~n] 的值更新到线段树,每次相当于加入一个a[j], 与前(j-1)个后缀形成新的 后缀,但是有些后缀的不同个数不会增加

  就利用last,使得last[j] ~ j-1 这一段位置 +1,就是当前贡献的答案,最后线段树查询即可

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 5e5+10, M = 1e3+20,inf = 2e9;

int dp[60][N],k;
int lazy[N];
int mx[N],v[N],n;
void push_up(int i) {
    mx[i] = max(mx[ls],mx[rs]);
}
void push_down(int i,int ll,int rr) {
    if(ll == rr) return ;
    if(lazy[i]) {
        lazy[ls] += lazy[i];
        lazy[rs] += lazy[i];
        mx[ls] += lazy[i];
        mx[rs] += lazy[i];
        lazy[i] = 0;
    }
}
void build(int i,int ll,int rr,int p) {
    lazy[i] = 0;
    mx[i] = 0;
    v[i] = 0;
    if(ll == rr) {
        v[i] = dp[p][ll-1];
        mx[i] = v[i];
        return ;
    }
    build(ls,ll,mid,p);
    build(rs,mid+1,rr,p);
    push_up(i);
}
void update(int i,int ll,int rr,int x,int y,int c) {
    if(x > y) return ;
    push_down(i,ll,rr);
    if(ll == x && rr == y) {
        mx[i] += c;
        lazy[i] += c;
        return ;
    }
    if(y <= mid) update(ls,ll,mid,x,y,c);
    else if(x > mid) update(rs,mid+1,rr,x,y,c);
    else update(ls,ll,mid,x,mid,c),update(rs,mid+1,rr,mid+1,y,c);
    push_up(i);
}
int ask(int i,int ll,int rr,int x,int y)
{
    if(x > y) return 0;
    push_down(i,ll,rr);
    if(ll == x && rr == y) {
        return mx[i];
    }
    if(y <= mid) return ask(ls,ll,mid,x,y);
    else if(x > mid) return ask(rs,mid+1,rr,x,y);
    else return max(ask(ls,ll,mid,x,mid),ask(rs,mid+1,rr,mid+1,y));
    push_up(i);
}
int mp[N],last[N],a[N];
int main() {

    scanf("%d%d",&n,&k);
    for(int i = 1; i <= n; ++i) {
        scanf("%d",&a[i]);
        last[i] = mp[a[i]];
        mp[a[i]] = i;
    }
    for(int i = 1; i <= k; ++i) {
        build(1,1,n,i-1);//(i-1)
        for(int j = 1; j <= n; ++j) {
            update(1,1,n,max(last[j]+1,1),j, 1);
            dp[i][j] = ask(1,1,n,1,j);
        }
    }
    cout<<dp[k][n]<<endl;
    return 0;
}

 

 

posted @ 2017-07-31 11:02  meekyan  阅读(314)  评论(0编辑  收藏  举报