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. 刷题总结——table(ssoi)

    题目: 题目背景 SOURCE:NOIP2016-RZZ-2 T2 题目描述 给定一个 n×m 的矩阵,行列均从 1 开始标号. 一个矩阵被认为是稳定的,当且仅当对于任意的 2≤i≤n,第 i 行的数 ...

  2. [无趣]bit reverse

    真不想承认啊,因为年轻而犯下的错误! inline void _BR(int* a,int r){ for(int i=0,j=1;i<r;++i,j<<=1){ for(int k ...

  3. 接口自动化测试框架--http请求的get、post方法的实现

    已知两种方法.一种是通过httpclient实现(貌似很简单,以后看一下),一种是以下方法: Client实现: package common; import com.alibaba.fastjson ...

  4. Selenium+Chrome+PhantomJS 爬取淘宝

    https://github.com/factsbenchmarks/taobao-jingdong 一 简单铺垫 Selenium负责驱动浏览器与python对接 PhantomJS负责渲染解析Ja ...

  5. JavaScript 的时间消耗--摘抄

    JavaScript 的时间消耗 2017-12-24 dwqs 前端那些事儿 随着我们的网站越来越依赖 JavaScript, 我们有时会(无意)用一些不易追踪的方式来传输一些(耗时的)东西. 在这 ...

  6. 使用 ftrace 调试 Linux 内核,第 3 部分

    内核头文件 include/linux/kernel.h 中描述了 ftrace 提供的工具函数的原型,这些函数包括 trace_printk.tracing_on/tracing_off 等.本文通 ...

  7. mysql合并和时间函数

    sql:利用group_concat()方法,参数为需要合并的字段,合并的字段分隔符默认为逗号,可通过参数separator指定,该方法往往配合group by 一起使用.利用group_concat ...

  8. intellij idea 和 myeclipse 转换

    原文出处:http://chinaxxren.iteye.com/blog/893970 当只用 intellij idea 建立 工程 1.首先是new project--->create p ...

  9. (12)centos之stmp服务器

    yum remove sendmail #卸载sendmail  

  10. 【Java】NIO中Selector的select方法源码分析

    该篇博客的有些内容和在之前介绍过了,在这里再次涉及到的就不详细说了,如果有不理解请看[Java]NIO中Channel的注册源码分析, [Java]NIO中Selector的创建源码分析 Select ...