Codeforces Round #426 (Div. 1) B The Bakery (线段树+dp)
2.5 seconds
256 megabytes
standard input
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.
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.
Print the only integer – the maximum total value of all boxes with cakes.
4 1
1 2 2 1
2
7 2
1 3 3 1 4 4 4
5
8 3
7 7 8 7 7 8 1 7
6
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)的更多相关文章
- 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 ...
- Codeforces Round #587 (Div. 3) F Wi-Fi(线段树+dp)
题意:给定一个字符串s 现在让你用最小的花费 覆盖所有区间 思路:dp[i]表示前i个全覆盖以后的花费 如果是0 我们只能直接加上当前位置的权值 否则 我们可以区间询问一下最小值 然后更新 #incl ...
- 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 ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- 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 ...
- 【动态规划】【线段树】 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,然 ...
- Codeforces Round #546 (Div. 2) E 推公式 + 线段树
https://codeforces.com/contest/1136/problem/E 题意 给你一个有n个数字的a数组,一个有n-1个数字的k数组,两种操作: 1.将a[i]+x,假如a[i]+ ...
- 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 ...
- Codeforces Round #275 Div.1 B Interesting Array --线段树
题意: 构造一个序列,满足m个形如:[l,r,c] 的条件. [l,r,c]表示[l,r]中的元素按位与(&)的和为c. 解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的 ...
随机推荐
- zabbix(2-server-agent)
注意:以下步骤都是在LAMP配置之后进行的. 关于LAMP环境的简单快速搭建,见博客:http://afterdawn.blog.51cto.com/7503144/1923139 下面开始介绍在Ce ...
- windows linux hosts文件的配置,开发项目中域名跳转等。
我们通常都知道Windows中hosts文件(C:\Windows\System32\drivers\etc),用来映射域名的.linux上当然也有,一般在/etc/hosts下. 当工作的项目,在开 ...
- MyBatis在Oracle中插入数据并返回主键的问题解决
引言: 在MyBatis中,希望在Oracle中插入数据之时,同一时候返回主键值,而非插入的条数... 环境:MyBatis 3.2 , Oracle. Spring 3.2 SQL Snipp ...
- 领域驱动设计理解&总结
领域驱动设计理解&总结 这篇文章主要是通读<实现领域驱动设计>之后自己的理解和总结(同时也参照一些博文的分析来加深自己的理解): 有些疑问是自定义内容,虽然有自己的理解,但依然感觉 ...
- 查看Oracle数据库中的,已经连接好的..当前用户状况
参考: http://stackoverflow.com/questions/1043096/how-to-list-active-open-connections-in-oracle 以sys身份连 ...
- 使用 vi/vim 时,粘贴进新创建文件或空文件的首行内容丢失的解决方法
只需要进入插入模式后,回车空一行或几行,再粘贴,再把上面的几个空行back回去,就不会丢失首行的内容了.
- 异常 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 解决方案
原来是因为 AssetsMapper.xml 不知道为什么不见了,导致这个异常,在启动项目时的启动任务里调用到了它,然后因为没有这个xml,所以抛出异常 启动信息: C:\extend\Develop ...
- C# ConcurrentBag的实现原理
目录 一.前言 二.ConcurrentBag类 三. ConcurrentBag线程安全实现原理 1. ConcurrentBag的私有字段 2. 用于数据存储的TrehadLocalList类 3 ...
- Category Theory: 01 One Structured Family of Structures
Category Theory: 01 One Structured Family of Structures 这次看来要放弃了.看了大概三分之一.似乎不能够让注意力集中了.先更新吧. 群的定义 \( ...
- RPG游戏开发基础教程
RPG游戏开发基础教程 第一步 下载RPG Maker 开发工具包 1.RPG Maker 是什么? RPG Maker 是由Enterbrain公司推出的RPG制作工具. 中文译名为RPG制作大师. ...