Codeforces 834D The Bakery【dp+线段树维护+lazy】
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.
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.
题目链接:http://codeforces.com/contest/834/problem/D
题意:把n个数分成k段,每段的价值等于这一段内不同数字的个数,求总的最大价值。
可以很快发现这是一个dp,dp[i][j]表示到第i个数字,已经分成了k段的最大价值。
dp[i][j] = max(dp[t][j-1]) (1<= t < i)
可以发现转移不是那么容易,所以我们用到线段树去维护当前位置前面的最大价值。
对于状态i,j,线段树维护的是1~i-1的最大值
对于每一个位置,找到前面最后一个与它数字相同的的位置,把这之间线段树的值都加上1,然后dp[i][j]的值就是j-1到i-1的最大值。
最后答案就是dp[n][k]。
(注意线段树的区间范围是0~n,因为可以直接从0转移过来)
下面给出AC代码:【二维数组改写成一维数组(个人原因,不太喜欢高维度的)】
#include <bits/stdc++.h>
using namespace std;
#define maxn 35010
#define INF 0x3f3f3f3f
int addv[maxn*],Max[maxn*];
int dp[maxn],ql,qr;
int pre[maxn], last[maxn], a[maxn];
void build(int l,int r,int o)
{
addv[o]=;
if(l == r)
{
Max[o]=dp[l];
return;
}
int mid=l+(r-l)/;
build(l,mid,o*);
build(mid+,r,o*+);
Max[o]=max(Max[o*],Max[o*+]);
}
void pushdown(int o)
{
int lc=o*,rc=o*+;
if(addv[o])
{
addv[lc]+=addv[o];
addv[rc]+=addv[o];
Max[lc]+=addv[o];
Max[rc]+=addv[o];
addv[o]=;
}
}
void update(int l,int r,int o)
{
if(ql>qr)
return;
if(ql<=l&&qr>=r)
{
addv[o]++;
Max[o]++;
return;
}
pushdown(o);
int mid=l+(r-l)/;
if(ql<=mid)
update(l,mid,o*);
if(qr>mid)
update(mid+,r,o*+);
Max[o]=max(Max[o*],Max[o*+]);
}
int query(int l,int r,int o)
{
if(ql<=l&&qr>=r)
{
return Max[o];
}
pushdown(o);
int mid=l+(r-l)/;
int best=-INF;
if(ql<=mid)
best=max(best,query(l,mid,o*));
if(qr>mid)
best=max(best,query(mid+,r,o*+));
return best;
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
memset(last,-,sizeof(last));
int cnt=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
pre[i]=last[a[i]];
last[a[i]]=i;
if(pre[i]==-)
cnt++;
dp[i]=cnt;
}
for(int kk=;kk<=k;kk++)
{
for(int i=;i<kk-;i++)
dp[i]=-INF;
build(,n,);
for(int i=kk;i<=n;i++)
{
ql=max(,pre[i]),qr=i-;
update(,n,);
ql=,qr=i-;
dp[i]=query(,n,);
}
}
printf("%d\n",dp[n]);
return ;
}
官方题解:
#include <cstdio>
#include <cstring>
#include <map> #define K first
#define V second const int N = ; int last[N], pre[N], dp[N]; int main()
{
int n, m;
while (scanf("%d%d", &n, &m) == ) {
memset(last, , sizeof(last));
for (int i = , a; i <= n; ++ i) {
scanf("%d", &a);
pre[i] = last[a];
last[a] = i;
}
dp[] = ;
for (int i = ; i <= n; ++ i) {
dp[i] = dp[i - ] + !pre[i];
}
for (int k = ; k <= m; ++ k) {
std::map<int, int> c;
c[] = n + ;
int last_dp = dp[k - ];
for (int i = k; i <= n; ++ i) {
int now = ;
while (now + c.rbegin()->V <= last_dp) {
now += c.rbegin()->V;
c.erase(c.rbegin()->K);
}
c.rbegin()->V += now - last_dp;
c[i] = last_dp + ;
auto it = c.upper_bound(pre[i]);
it --;
it->V --;
if (it->V == ) {
c.erase(it->K);
}
last_dp = dp[i];
dp[i] = (n + ) - c.begin()->V;
}
}
printf("%d\n", dp[n]);
}
}
Codeforces 834D The Bakery【dp+线段树维护+lazy】的更多相关文章
- Codeforces 834D The Bakery 【线段树优化DP】*
Codeforces 834D The Bakery LINK 题目大意是给你一个长度为n的序列分成k段,每一段的贡献是这一段中不同的数的个数,求最大贡献 是第一次做线段树维护DP值的题 感觉还可以, ...
- Codeforces 834D The Bakery - 动态规划 - 线段树
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- Codeforces 833B The Bakery dp线段树
B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- [Codeforces]817F. MEX Queries 离散化+线段树维护
[Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...
- [动态dp]线段树维护转移矩阵
背景:czy上课讲了新知识,从未见到过,总结一下. 所谓动态dp,是在动态规划的基础上,需要维护一些修改操作的算法. 这类题目分为如下三个步骤:(都是对于常系数齐次递推问题) 1先不考虑修改,不考虑区 ...
- Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵
Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries ...
- DP+线段树维护矩阵(2019牛客暑期多校训练营(第二场))--MAZE
题意:https://ac.nowcoder.com/acm/contest/882/E 给你01矩阵,有两种操作:1是把一个位置0变1.1变0,2是问你从第一行i开始,到最后一行j有几种走法.你只能 ...
随机推荐
- Oracle安装步骤
1.在Oracle官网下载安装包: 2.非常重要:两个压缩包都要解压(不是分卷压缩的,不然安装过程中会报找不到文件的错误,被坑过!): 3.关闭所有安全相关软件(关闭杀毒软件.防火墙.windows ...
- vue vuex vue-rouert后台项目——权限路由(超详细简单版)
项目地址:vue-simple-template共三个角色:adan barbara carrie 密码全是:123456 adan 拥有 最高权限A 他可以看到 red , yellow 和 blu ...
- Charles 抓包工具使用部分问题总结
一. You may need to configure your browser or application to trust the Charles Root Certificate. See ...
- iOS Label 自适应高度
推荐第二个 测试一,只改变numberOfLines属性,label的高度不会自适应(会有text中的一部分内容称为......) NSString *str = @"jgreijgirje ...
- VR\AR 使用 SceneKit
VR\AR 使用 SceneKit http://www.jianshu.com/c/70d63e3941fd
- iOS 正则表达式使用(转)
1/ 教程一:认识正则表达式 .http://deerchao.net/tutorials/regex/regex.htm#mission 表7.尚未详细讨论的语法 代码/语法 说明 \a 报警字符( ...
- cocoapods安装说明,最快安装,以及使用
安装卸载更新新推荐 文章最后 其他问题总结: 1 添加taobao提供的镜像地址:http://ruby.taobao.org/ 移除命令:gem sources --remove https://r ...
- Python学习日记:day8-------文件操作
文件操作 1,文件路径:d:\xxxx.txt 绝对路径:从根目录到最后 相对路径:当前目录下的文件 2,编码方式:utf-8 3,操作方式:只读,只写,追加,读写,写读...... ...
- 创建一个可用的简单的SpringMVC项目,图文并茂
转载麻烦注明下来源:http://www.cnblogs.com/silentdoer/articles/7134332.html,谢谢. 最近在自学SpringMVC,百度了很多资料都是比较老的,而 ...
- KVM 初探
KVM 是业界最为流行的 Hypervisor,全称是 Kernel-based Virtual Machine.它是作为 Linux kernel 中的一个内核模块而存在,模块名为 kvm.ko,也 ...