## 小坏蛋_千千

I was caught in a heavy rain!

# Codeforces 834 D. The Bakery （线段树+dp）

## Description

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


## Examples output

2


## 题意

$n$$n$ 个数字分成 $k$$k$ 段，每一段的价值是其中不同数字的个数，求总的最大价值。

## 思路

• $k=1$$k=1$ 时， $dp\left[i\right]\left[1\right]$$dp[i][1]$ 为前 $i$$i$ 个数字的不同个数
• $k>1$$k>1$ 时， $dp\left[i\right]\left[k\right]=\underset{1\le j\le i}{max}dp\left[j-1\right]\left[k-1\right]+c\left[j\right]\left[i\right]$$dp[i][k]=\max_{1≤j≤i}dp[j-1][k-1]+c[j][i]$$c\left[j\right]\left[i\right]$$c[j][i]$ 代表区间 $\left[j,i\right]$$[j,i]$ 数字的不同个数）

## AC 代码

#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
#define default_setv -1

using namespace std;
typedef long long LL;
const int maxn = 1e5+10;

int sumv[maxn<<2];  //四倍空间
int minv[maxn<<2];
int maxv[maxn<<2];
int setv[maxn<<2];  //初始值：-1
int a[maxn];
int pos[maxn];
int pre[maxn];
int dp[maxn][55];

void maintain(int o,int L,int R)
{
int lc = o<<1,rc=o<<1|1;
sumv[o] = minv[o] = maxv[o] = 0;
if(setv[o] != default_setv)
{
sumv[o] = setv[o] * (R-L+1);
minv[o] = maxv[o] = setv[o];
}
else if(R>L)
{
sumv[o] = sumv[lc] + sumv[rc];
minv[o] = min(minv[lc],minv[rc]);
maxv[o] = max(maxv[lc],maxv[rc]);
}
}

void pushdown(int o)
{
int lc = o<<1,rc = o<<1|1;
if(setv[o] != default_setv)
{
setv[lc] = setv[rc] = setv[o];
setv[o] = default_setv;
}
{
}
}

/*
*/
{
int lc = o<<1,rc = o<<1|1;
if(y1<=L&&y2>=R)
else
{
pushdown(o);
int M = L + (R-L)/2;
if(y1<=M)
else
maintain(lc,L,M);
if(y2>M)
else
maintain(rc,M+1,R);
}
maintain(o,L,R);
}

/*
* o: 当前节点(1)，修改区间：[y1,y2] ，修改值：v_set，总区间：[L,R] (这里的L,R必须是2的幂次)
*/
void update_set(int o, int L, int R,int y1,int y2,int v_set)
{
int lc = o<<1, rc = o<<1|1;
if(y1 <= L && y2 >= R)
{
setv[o] = v_set;
}
else
{
pushdown(o);
int M = L + (R-L)/2;
if(y1 <= M)
update_set(lc, L, M,y1,y2,v_set);
else
maintain(lc, L, M);
if(y2 > M)
update_set(rc, M+1, R,y1,y2,v_set);
else
maintain(rc, M+1, R);
}
maintain(o, L, R);
}

/*
* o: 当前节点(1)，查询区间：[y1,y2] ，初始累加(0)，总区间：[L,R] (这里的L,R必须是2的幂次)
*/
int _min, _max, _sum;
void query(int o, int L, int R, int y1,int y2, int add)
{
if(setv[o] != default_setv)
{
}
else if(y1 <= L && y2 >= R)
{
_sum += sumv[o] + add * (R-L+1);
}
else
{
int M = L + (R-L)/2;
if(y1 <= M)
if(y2 > M)
}
}

/*
* o: 当前节点(1)，总区间：[L,R] (这里的L,R必须是2的幂次)
* 所有 setv 初始化为 -1，所有叶子节点 addv 为当前值
*/
void build(int o,int L,int R,int op)
{
if(L==R)
{
setv[o] = default_setv;
addv[o] = sumv[o] = maxv[o] = minv[o] = dp[L-1][op];
}
else
{
int M = L + (R-L)/2;
int lc = o<<1,rc = o<<1|1;
build(lc,L,M,op);
build(rc,M+1,R,op);

setv[o] = default_setv;
sumv[o] = sumv[lc] + sumv[rc];
maxv[o] = max(maxv[lc],maxv[rc]);
minv[o] = min(minv[lc],minv[rc]);
}
}

/*
* 测试函数，R 代表叶子节点个数
*/
void test(int R,int n)
{
cout<<"----------"<<endl;
auto init = [&]()
{
_sum = 0;
_min = INT_MAX;
_max = INT_MIN;
};
for(int i=1; i<=n; i++)
{
init();
query(1,1,R,i,i,0);
cout<<i<<" --> "<<_sum<<"\t "<<_min<<"\t "<<_max<<endl;
}
init();
query(1,1,R,1,R,0);
cout<<"total: "<<_sum<<endl;
cout<<"----------"<<endl;
}

int main()
{
IO;
int n,k;
cin>>n>>k;
int R = 1;
while(R<n)
R<<=1;
for(int i=1; i<=n; i++)
{
cin>>a[i];
pre[i] = pos[a[i]] + 1;
pos[a[i]] = i;
}
for(int i=1; i<=k; i++)
{
build(1,1,R,i-1);
for(int j=1; j<=n; j++)
{
_max = 0;
query(1,1,R,1,j,0);
dp[j][i] = _max;
}
}
cout<<dp[n][k]<<endl;
return 0;
}

#### Codeforces 834D The Bakery【Dp+线段树】好题~

2017-08-02 14:05:03

#### Codeforces-834D The Bakery（线段树+dp）

2017-08-01 11:12:59

#### codeforces834D - The Bakery

2017-12-02 00:13:18

#### Codeforces 834D - The Bakery 【DP+线段树】

2018-03-03 23:06:58

#### Codeforces 834D The Bakery【Dp+线段树】

2017-08-23 11:54:29

#### Codeforces-834D The Bakery 线段树优化DP

2017-08-14 21:15:12

#### CodeForces 834D The Bakery(dp+线段树)

2018-06-05 22:38:30

#### The Bakery CodeForces - 834D

2018-06-05 12:17:56

#### CodeForces - 834E 暴力枚举+数位dp

2018-06-08 17:49:24

#### Codeforces 19D(线段树+离散化)

2015-08-14 19:24:51