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

Copy
4 1
1 2 2 1
output

Copy
2
input

Copy
7 2
1 3 3 1 4 4 4
output

Copy
5
input

Copy
8 3
7 7 8 7 7 8 1 7
output

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

思路:

dp[i][j]代表: 前j个数,分成i块的最大价值。

那么有状态转移方程: dp[i][j] = max(dp[i-1][k]+sum[k+1][j],dp[i][j])

记录下每个点上次出现的位置,存到last[]数组。

更新dp[i-1][1-n],将上一个状态的值存进线段树维护,因为每次状态变化的范围都是  last[j]-j,区间更新下就好了

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 2e5+;
//jmqayxtl
int mx[M<<],lazy[M<<],dp[][M],a[M],last[M],vis[M]; void pushup(int rt){
mx[rt] = max(mx[rt<<],mx[rt<<|]);
} void pushdown(int rt){
if(lazy[rt]){
lazy[rt<<] += lazy[rt];
lazy[rt<<|] += lazy[rt];
mx[rt<<] += lazy[rt];
mx[rt<<|] += lazy[rt];
lazy[rt] = ;
}
} void build(int p,int l,int r,int rt){
lazy[rt] = ; mx[rt] = ;
if(l == r){
mx[rt] = dp[p][l-];
return ;
}
mid;
build(p,lson);
build(p,rson);
pushup(rt);
} void update(int L,int R,int c,int l,int r,int rt){
if(L <= l&&R >= r){
mx[rt] += c; lazy[rt] += c;
return ;
}
pushdown(rt);
mid;
if(L <= m) update(L,R,c,lson);
if(R > m) update(L,R,c,rson);
pushup(rt);
} int query(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r){
return mx[rt];
}
pushdown(rt);
mid;
int ret = ;
if(L <= m) ret = max(query(L,R,lson),ret);
if(R > m) ret = max(query(L,R,rson),ret);
return ret;
} int main()
{
ios::sync_with_stdio();
cin.tie(); cout.tie();
int n,k;
cin>>n>>k;
for(int i = ;i <= n;i ++){
cin>>a[i];
last[i] = vis[a[i]];
vis[a[i]] = i;
}
for(int i = ;i <= k;i ++){
build(i-,,n,);
for(int j = ;j <= n;j ++){
update(last[j]+,j,,,n,);
dp[i][j] = query(,j,,n,);
cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
}
}
cout<<dp[k][n]<<endl;
}

Codeforces Round #426 (Div. 1) B The Bakery (线段树+dp)的更多相关文章

  1. 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 req ...

  2. Codeforces Round #587 (Div. 3) F Wi-Fi(线段树+dp)

    题意:给定一个字符串s 现在让你用最小的花费 覆盖所有区间 思路:dp[i]表示前i个全覆盖以后的花费 如果是0 我们只能直接加上当前位置的权值 否则 我们可以区间询问一下最小值 然后更新 #incl ...

  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 #426 (Div. 1) B. The Bakery

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

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

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

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

  9. Codeforces Round #275 Div.1 B Interesting Array --线段树

    题意: 构造一个序列,满足m个形如:[l,r,c] 的条件. [l,r,c]表示[l,r]中的元素按位与(&)的和为c. 解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的 ...

随机推荐

  1. 六,ESP8266 TCP Client(基于Lua脚本语言)

    今天不知道是不是让我姐挺失望.......很多时候都不知道自己努力的方向对不对,,以后能不能带给家人最美好的期盼...... Init.lua 没啥改变,,就改了一下加载Client.lua gpio ...

  2. 探讨CAN总线的抗干扰能力

    探讨CAN总线的抗干扰能力 CAN总线经近20年的发展已步入壮年期,它不仅在汽车领域的应用占据一定优势,在其他工业应用上也生机勃勃.枝繁叶茂.究竟是什么原因使它这么成功?当人们发现它的局限性,又面临新 ...

  3. Android Studio常用快捷键 - 转

    Android Studio常用快捷键 1. Ctrl+D: 集合了复制和粘贴两个操作,如果有选中的部分就复制选中的部分,并在选中部分的后面粘贴出来,如果没有选中的部分,就复制光标所在的行,并在此行的 ...

  4. Android病毒家族及行为(一)

    1病毒名称:a.remote.GingerMaste中文名:病毒家族:GingerMast病毒类别:远程控制恶意行为:获取root权限,同时连接远端服务器,在其指令控制下静默下载其它恶意软件,给用户手 ...

  5. 20155213免考项目——简易的HIDAttack

    20155213免考项目--简易的HIDAttack 听5214说他做不出来自己的免考项目,于是就转向bof进阶,并且成功做出了64位的ROP攻击...... 既然如此,那我就再做一个吧,但都已经期末 ...

  6. 2017-2018-2 20155230《网络对抗技术》实验5:MSF基础应用

    基础问题回答 用自己的话解释什么是exploit,payload,encode. exploit 就是运行该模块吧,在msf的模块中配置好各项属性后exploit一下就开始运行使用该模块了 paylo ...

  7. python 回溯法 子集树模板 系列 —— 17、找零问题

    问题 有面额10元.5元.2元.1元的硬币,数量分别为3个.5个.7个.12个.现在需要给顾客找零16元,要求硬币的个数最少,应该如何找零?或者指出该问题无解. 分析 元素--状态空间分析大法:四种面 ...

  8. Docker-compose部署gitlab中文版

    目录 Docker-compose部署gitlab 1.安装Docker 2.安装Docker-compose 3.安装Gitlab Docker-compose部署gitlab 1.安装Docker ...

  9. JavaScript实现选项卡(三种方法)

    本文实例讲述了js选项卡的实现方法. 一.html代码: <div id="div1"> <input class="active" type ...

  10. centos 7部署ELK

    一.ELK介绍 Elasticsearch 是基于 JSON 的分布式搜索和分析引擎,专为实现水平扩展.高可用和管理便捷性而设计.Logstash 是动态数据收集管道,拥有可扩展的插件生态系统,能够与 ...