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

题意:给你一个长度为n的序列 现在可以分成k部分 求每部分不同数的个数的和的最大值

题解:线段树优化dp

dp[i][j] 表示前j个分成i部分的最大值

 #pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
#define LL long long
#define mod 1000000007
using namespace std;
int n,k;
int a[];
int last[];
int dp[][];
map<int,int> mp;
struct node{
int l,r;
int maxn;
int add;
} tree[];
void buildtree(int root ,int left,int right,int p){
tree[root].l=left;
tree[root].r=right;
tree[root].add=;
tree[root].maxn=;
if(left==right){
tree[root].maxn=dp[p][left-];//将当前结点的最大值 初始为 前(left-1)个分成p部分的最大值
return ;
}
int mid=(left+right)>>;
buildtree(root<<,left,mid,p);
buildtree(root<<|,mid+,right,p);
tree[root].maxn=max(tree[root<<].maxn,tree[root<<|].maxn);
}
void pushdown(int root)
{
if(tree[root].add==) return ;
tree[root<<].add+=tree[root].add;
tree[root<<|].add+=tree[root].add;
tree[root<<].maxn+=tree[root].add;
tree[root<<|].maxn+=tree[root].add;
tree[root].add=;
}
void update(int root,int left,int right,int c)
{
if(tree[root].l==left&&tree[root].r==right)
{
tree[root].add+=c;
tree[root].maxn+=c;
return ;
}
pushdown(root);
int mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
{
update(root<<,left,right,c);
}
else
{
if(left>mid)
update(root<<|,left,right,c);
else
{
update(root<<,left,mid,c);
update(root<<|,mid+,right,c); }
}
tree[root].maxn=max(tree[root<<].maxn,tree[root<<|].maxn);
}
int query(int root,int left,int right)
{
if(left>right)
return ;
if(tree[root].l==left&&tree[root].r==right)
{
return tree[root].maxn;
}
pushdown(root);
int mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
return query(root<<,left,right);
else
{
if(left>mid)
return query(root<<|,left,right);
else
return max(query(root<<,left,mid),query(root<<|,mid+,right));
}
}
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++){
buildtree(,,n,i-);//
for(int j=;j<=n;j++){
update(,max(last[j]+,),j,); //第j个只能在 (max(last[j]+1,1),j) 做贡献
dp[i][j]=query(,,j);
}
}
printf("%d\n",dp[k][n]);
return ;
}

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

  1. Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)

    https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...

  2. Codeforces #426 Div2 D(线段树优化 DP )

    #426 Div2 D 题意 给出 \(n\) 个数字,将这些数字隔成 \(k\) 个部分(相对位置不变),统计每个部分有几个不同数字,然后全部加起来求和,问和最大是多少. 分析 很容易想到 \(DP ...

  3. Please, another Queries on Array?(Codeforces Round #538 (Div. 2)F+线段树+欧拉函数+bitset)

    题目链接 传送门 题面 思路 设\(x=\prod\limits_{i=l}^{r}a_i\)=\(\prod\limits_{i=1}^{n}p_i^{c_i}\) 由欧拉函数是积性函数得: \[ ...

  4. Nastya Hasn't Written a Legend(Codeforces Round #546 (Div. 2)E+线段树)

    题目链接 传送门 题面 题意 给你一个\(a\)数组和一个\(k\)数组,进行\(q\)次操作,操作分为两种: 将\(a_i\)增加\(x\),此时如果\(a_{i+1}<a_i+k_i\),那 ...

  5. CodeCraft-19 and Codeforces Round #537 (Div. 2) E 虚树 + 树形dp(新坑)

    https://codeforces.com/contest/1111/problem/E 题意 一颗有n个点的树,有q个询问,每次从树挑出k个点,问将这k个点分成m组,需要保证在同一组中不存在一个点 ...

  6. CodeForces 834C - The Meaningless Game | Codeforces Round #426 (Div. 2)

    /* CodeForces 834C - The Meaningless Game [ 分析,数学 ] | Codeforces Round #426 (Div. 2) 题意: 一对数字 a,b 能不 ...

  7. D - The Bakery CodeForces - 834D 线段树优化dp···

    D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...

  8. Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...

  9. BZOJ2090: [Poi2010]Monotonicity 2【线段树优化DP】

    BZOJ2090: [Poi2010]Monotonicity 2[线段树优化DP] Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k]. ...

随机推荐

  1. unload没有用

    今天下午测试了unload这个事件包括beforeunload <script type="text/javascript"> window.addEventListe ...

  2. Daily Scrum8 11.12

    昨天的任务已完成. 今日任务: 徐钧鸿:个人作业 张艺:构建带有用户管理的框架,并将后端移植好的代码连结. 黄可嵩:完成搜索移植 徐方宇:研究httpclient如何运作,如何利用它实现服务器和客户端 ...

  3. 2018-2019-20172321 《Java软件结构与数据结构》第九周学习总结

    2018-2019-20172321 <Java软件结构与数据结构>第九周学习总结 教材学习内容总结 第15章 图 无向图 图由顶点和边组成. 顶点由名字或标号来表示,如:A.B.C.D: ...

  4. 特别好用的eclipse快捷键

    alt+/ 提示 alt+shift+r重命名 alt+shift+j添加文档注释 Ctrl+shift+y小写 Ctrl+shift+x大写 ctrl+shift+f格式化代码(需要取消输入法的简繁 ...

  5. JavaBean 与 EJB 的区别

    JavaBean在一般情况下指的是实体类,在大部分情况下和POJO是同义词,基本构成就是一些字段和与之对应的 setter.getter方法,如果一个JavaBean需要在不同的JVM的进程中进行传递 ...

  6. 配置ssh免密码登入

    首先要设置好主机名hostnamectl,然后编辑文件/etc/hosts 192.168.43.9 node0 192.168.43.10 node1 192.168.43.11 node2     ...

  7. linux下 XGCOM串口助手的安装

    源码下载:http://code.google.com/p/xgcom/     也可以自己搜索下载 首先先安装依赖库,直接运行命令 #sudo apt-get install libglib2.0- ...

  8. Asp.Net Core实现文件上传

    1. Asp.Net Core Mvc方式 public class UploadController : Controller { private IHostingEnvironment _host ...

  9. php三种方法从控制结构或脚本中跳出

    PHP中,如果希望停止一段代码,根据需要达到的效果不同,可以有三种方法实现: 1. break: 如果在循环中使用了break语句,脚本就会从循环体后面的第一条语句开始执行: 2. continue: ...

  10. HDU4791_Alice's Print Service

    全场最水题. 保留打印a[i]份分别需要的钱,从后往前扫一遍,保证取得最优解. 查找的时候,二分同时判断最小值即可. 注意初值的设定应该设定为long long 的无穷大. #include < ...