codeforces#426(div1) B - The Bakery (线段树 + dp)
B. 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
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 个数划分成 m 段,要求每组数不相等的数的数量最大之和。
思路:
dp方程 : dp[i][j] = max( dp[k][j-1] + v(k, i) );( j<=k<i , k = j, j+1, +...+ i-1)
dp[i][j]表示第 i 个数分到第 j 段的最大值。
v(k, i) 表示k~i中不同数的个数,此处用hash记录每个数上一次出现的位置,从上一次出现的位置到当前位置的 dp[i][j-1] 值均可+1。
线段树优化:维护 dp[k~i][j-1]的最大值。
此时时间复杂度 O(n*m*log(n))。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn = 35010, maxk = 55, INF = 0x3f3f3f3f; int a[maxn], dp[maxn][maxk], last[maxn], now[maxn];
int maxv[maxn<<2], lazy[maxn<<2]; push_down(int rt)
{
if(lazy[rt])
{
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
maxv[rt<<1]+=lazy[rt];
maxv[rt<<1|1]+=lazy[rt];
lazy[rt]=0;
}
} push_up(int rt)
{
maxv[rt]=max(maxv[rt<<1], maxv[rt<<1|1]);
} void update(int l, int r, int rt, int ul, int ur, int v)
{
if(ul<=l && r<=ur)
{
maxv[rt]+=v;
lazy[rt]+=v;
return;
}
push_down(rt);
int m=(l+r)/2;
if(ul<=m) update(lson, ul, ur, v);
if(m<ur) update(rson, ul, ur, v);
push_up(rt);
} int query(int l, int r, int rt, int ql, int qr)
{
if(ql<=l && r<=qr)
return maxv[rt];
push_down(rt);
int m=(l+r)/2, res=-INF;
if(ql<=m) res=max(res, query(lson, ql, qr));
if(qr>m) res=max(res, query(rson, ql, qr));
push_up(rt);
return res;
} int main()
{
int n, m;
scanf("%d%d", &n, &m);
for(int i=1; i<=n; ++i)
scanf("%d", &a[i]);
for(int i=1; i<=n; ++i)
{
last[i]=now[a[i]];
now[a[i]]=i;
}
for(int j=1; j<=m; ++j)
{
if(j!=1)
{
memset(maxv, 0, sizeof maxv);
memset(lazy, 0, sizeof lazy);
for(int i=j-1; i<=n; ++i)
{
update(0, n, 1, i, i, dp[i][j-1]);
}
}
update(0, n, 1, max(j-1, last[j]), j-1, 1);
dp[j][j]=j;
for(int i=j+1; i<=n; ++i)
{
update(0, n, 1, max(j-1, last[i]), i-1, 1);
dp[i][j]=query(0, n, 1, j-1, i-1);
}
}
printf("%d\n", dp[n][m]);
return 0;
}
codeforces#426(div1) B - The Bakery (线段树 + dp)的更多相关文章
- Codeforces.833B.The Bakery(线段树 DP)
题目链接 \(Description\) 有n个数,将其分为k段,每段的值为这一段的总共数字种类,问最大总值是多少 \(Solution\) DP,用\(f[i][j]\)表示当前在i 分成了j份(第 ...
- CF833B The Bakery (线段树+DP)
题目大意:给你一个序列(n<=35000),最多分不大于m块(m<=50),求每个块内不同元素的数量之和的最大值 考试的时候第一眼建图,没建出来,第二眼贪心 ,被自己hack掉了,又随手写 ...
- codeforces E. Trains and Statistic(线段树+dp)
题目链接:http://codeforces.com/contest/675/problem/E 题意:你可以从第 i 个车站到 [i + 1, a[i]] 之间的车站花一张票.p[i][j]表示从 ...
- codeforces 675E E. Trains and Statistic(线段树+dp)
题目链接: E. Trains and Statistic time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
随机推荐
- IDEA2020.1使用LeetCode插件运行并调试本地样例
环境: idea2020.1 插件: LeetCode-editor 6.7 一.IDEA安装LeetCode插件 安装完成重启idea 打开插件 URL可以选择国服和世界服.LoginName和Pa ...
- 关于java for循环常见练习题
使用for循环方式计算2+4+6+…+100的值 package day02; /** * 使用for循环方式计算2+4+6+…+100的值 * @author mu * */ public clas ...
- zabbix-自动发现并监控
创建自动发现规则 添加 ip 范围 客户端安装zabbix-agent [root@node1 ~]# docker load -i zabbix_agent.tar.gz 23f7bd114e4a: ...
- archaius(1) 概述
archaius作为配置管理工具,内部主要定义了下几个模块: 配置源 配置源的主要功能是将配置从目标位置加载到内存中.详见:archaius源码分析之配置源 配置管理器 配置管理器的主要功能是管理内存 ...
- RXJAVA源码之多线程
在不指定线程的情况下, RxJava 遵循的是线程不变的原则,即:在哪个线程调用 subscribe(),就在哪个线程生产事件:在哪个线程生产事件,就在哪个线程消费事件.如果需要切换线程,就需要用到 ...
- Elasticsearch安装、原理学习总结
ElasticSearch ElasticSearch概念 Elasticsearch是Elastic Stack核心的分布式搜索和分析引擎. 什么是Elastic Stack Elastic Sta ...
- 容器云平台No.5~企业级私有镜像仓库Harbor V2.02
镜像仓库 仓库,顾名思义,就是存放东西的地方,Docker仓库,理所当然,就是存放docker镜像的地方了. Docker仓库分公有仓库和私有仓库.共有仓库有hub.docker.com.gcr.io ...
- 针对于Java的35 个代码性能优化总结
针对于Java的35 个代码性能优化总结前言代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的 ...
- MySQL: 2、SQL语言
一.SQL的简介: 1.SQL的概念: SQL就是结构化查询语言,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理关系数据库系统 2.SQL的作用: - ...
- 13.深入k8s:Pod 水平自动扩缩HPA及其源码分析
转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 源码版本是1.19 Pod 水平自动扩缩 Pod 水平自动扩缩工作原理 Pod 水平自动 ...