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+, M = 1e3+,inf = 2e9; int dp[][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] = ;
}
}
void build(int i,int ll,int rr,int p) {
lazy[i] = ;
mx[i] = ;
v[i] = ;
if(ll == rr) {
v[i] = dp[p][ll-];
mx[i] = v[i];
return ;
}
build(ls,ll,mid,p);
build(rs,mid+,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+,rr,x,y,c);
else update(ls,ll,mid,x,mid,c),update(rs,mid+,rr,mid+,y,c);
push_up(i);
}
int ask(int i,int ll,int rr,int x,int y)
{
if(x > y) return ;
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+,rr,x,y);
else return max(ask(ls,ll,mid,x,mid),ask(rs,mid+,rr,mid+,y));
push_up(i);
}
int mp[N],last[N],a[N];
int main() { scanf("%d%d",&n,&k);
for(int i = ; i <= n; ++i) {
scanf("%d",&a[i]);
last[i] = mp[a[i]];
mp[a[i]] = i;
}
for(int i = ; i <= k; ++i) {
build(,,n,i-);//(i-1)
for(int j = ; j <= n; ++j) {
update(,,n,max(last[j]+,),j, );
dp[i][j] = ask(,,n,,j);
}
}
cout<<dp[k][n]<<endl;
return ;
}
 

Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP的更多相关文章

  1. CodeForces 834D The Bakery(线段树优化DP)

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...

  2. Codeforces Round #426 (Div. 1) B The Bakery (线段树+dp)

    B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

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

  4. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  5. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

  6. Codeforces Round #587 (Div. 3) F. Wi-Fi(单调队列优化DP)

    题目:https://codeforces.com/contest/1216/problem/F 题意:一排有n个位置,我要让所有点都能联网,我有两种方式联网,第一种,我直接让当前点联网,花费为i,第 ...

  7. 【动态规划】【线段树】 Codeforces Round #426 (Div. 1) B. The Bakery

    给你一个序列,让你划分成K段,每段的价值是其内部权值的种类数,让你最大化所有段的价值之和. 裸dp f(i,j)=max{f(k,j-1)+w(k+1,i)}(0<=k<i) 先枚举j,然 ...

  8. Codeforces Round #546 (Div. 2) E 推公式 + 线段树

    https://codeforces.com/contest/1136/problem/E 题意 给你一个有n个数字的a数组,一个有n-1个数字的k数组,两种操作: 1.将a[i]+x,假如a[i]+ ...

  9. Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并

    D. Developing Game   Pavel is going to make a game of his dream. However, he knows that he can't mak ...

随机推荐

  1. Azure Storage Blob文件名区分大小写

    最近在使用Azure Storage的时候发现Storage的命名是区分大小写的,导致我们系统在更新图片的时候有时候更新不上,最终通过判断处理文件名解决. 因此我们在使用Storage需要注意一下文件 ...

  2. TeraTerm设定(窗体大小,字体字号)保存为默认值

    Tera Term是一款很好的SSH工具,大家经常遇到一个头疼的问题,每次打开的时候,都要自己重新设置一遍Font. 介绍一下把自己喜欢的字体,设置好后,保存到默认配置中的方法. 设置窗体大小: 设置 ...

  3. 【bzoj1299】[LLH邀请赛]巧克力棒 博弈+模拟

    Description TBL和X用巧克力棒玩游戏.每次一人可以从盒子里取出若干条巧克力棒,或是将一根取出的巧克力棒吃掉正整数长度.TBL先手两人轮流,无法操作的人输. 他们以最佳策略一共进行了10轮 ...

  4. LightOJ1106 Gone Fishing

    Gone Fishing John is going on a fishing trip. He has h hours available, and there are n lakes in the ...

  5. Linux 如何实现 VLAN

    LAN 表示 Local Area Network,本地局域网,通常使用 Hub 和 Switch 来连接 LAN 中的计算机.一般来说,两台计算机连入同一个 Hub 或者 Switch 时,它们就在 ...

  6. 「CodePlus 2018 4 月赛」最短路

    $n \leq 100000$,$m \leq 500000$的有向图,两点之间还可以以$a \ \ xor \ \ b$的代价从$a$到$b$,问$s$到$t$的最短路. 被自己蠢哭QAQ 首先两个 ...

  7. 字符串哈希hash

    题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转 ...

  8. spark学习(六)Java版RDD基本的基本操作

    1.map算子 private static void map() { //创建SparkConf SparkConf conf = new SparkConf() .setAppName(" ...

  9. BZOJ1016最小生成树计数 最小生成树 + 排列组合

    @[最小生成樹, 排列組合] Discription 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的 最小生成树.(如果两颗最小生成树中至少有一条边不 ...

  10. Windows Server 远程桌面报错:No Remote Desktop License Servers Available

    问题描述: 在用远程桌面访问Window Server服务器时,出现如下错误: The remote session was disconnected because there are no Rem ...