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

 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.

【题意】给你一个序列,然后在原来的顺序基础上将这些数分成K份,每一份至少一个数,然后对于每一份,将数的种数累加,求累加和最大值。

【分析】如果n小一些的话,我们可以n方DP做,DP[i][j]表示在i这个地方将前面分成k份的最大值,那么dp[i][j]=dp[k][j-1]+(k+1~j的数的种数),

但是N太大。。。于是我们想到线段树。线段树的维护这个区间内每一个位置的dp值+这个位置到我当前dp到的位置的数的种数,然后对这个区间取最大值。当我们dp到i时,我们将a[i]上一次出现的位置+1~i这个区间更新+1,然后查询1~i的最大值即可。

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 3e4+5500;;
const int M = 160009;
const int mod = 1e9+7;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,k,ans;
int a[N],mx[N*4],lazy[N*4];
int pre[N],pos[N],dp[N];
void pushUp(int rt){
    mx[rt]=max(mx[rt<<1],mx[rt<<1|1]);
}
void pushDown(int rt){
    if(lazy[rt]){
        lazy[rt<<1]+=lazy[rt];
        lazy[rt<<1|1]+=lazy[rt];
        mx[rt<<1]+=lazy[rt];
        mx[rt<<1|1]+=lazy[rt];
        lazy[rt]=0;
    }
}
void build(int l,int r,int rt){
    lazy[rt]=0;
    if(l==r){
        mx[rt]=dp[l-1];
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushUp(rt);
}
void upd(int L,int R,int l,int r,int x,int rt){
    if(L<=l&&r<=R){
        mx[rt]+=x;
        lazy[rt]+=x;
        return;
    }
    pushDown(rt);
    int mid=(l+r)>>1;
    if(L<=mid)upd(L,R,l,mid,x,rt<<1);
    if(R>mid) upd(L,R,mid+1,r,x,rt<<1|1);
    pushUp(rt);
}
int qry(int L,int R,int l,int r,int rt){
    if(L<=l&&r<=R){
        return mx[rt];
    }
    pushDown(rt);
    int ret=0,mid=(l+r)>>1;
    if(L<=mid)ret=max(ret,qry(L,R,l,mid,rt<<1));
    if(R>mid)ret=max(ret,qry(L,R,mid+1,r,rt<<1|1));
    return ret;
}
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        pre[i]=pos[a[i]];
        pos[a[i]]=i;
    }
    for(int i=1;i<=k;i++){
        build(1,n,1);
        for(int j=1;j<=n;j++){
            upd(pre[j]+1,j,1,n,1,1);
            dp[j]=qry(1,j,1,n,1);
        }
    }
    printf("%d\n",dp[n]);
}

 

posted @ 2017-07-31 20:44  贱人方  阅读(244)  评论(0编辑  收藏  举报