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有几种走法.你只能 ...
随机推荐
- lua的通用print函数
1.前言 最近在做关于openresty方面的工作,涉及到lua脚本语言,经常需要打日志查看内容.普通的print函数遇到nil或table时,非常无力.而项目中的代码经常遇到参数为nil或table ...
- webpack 理解
目录 关于此文 在学习webpack之前,我们先去了解它的作用 它与其他其他前端工具(gulp,grunt)有什么差别呢 安装 webpack.config.js 配置结果 webpack 开始简单配 ...
- Docker(十三):OpenStack部署Docker集群
1.介绍 本教程使用Compose.Machine.Swarm工具把WordPress部署在OpenStack上. 本节采用Consul作为Swarm的Discovery Service模块,要利用C ...
- u3d之世界坐标系,屏幕坐标系,视口坐标系,如何获取物体距离摄像机的距离
世界坐标系就是unity的左手坐标系 屏幕坐标系是Game视图相机拍摄的场景坐标系,左下角(0,0),右上角(Screen.width,Screen.height),单位是像素.Z的位置是以相机的世界 ...
- K:线性表
1. 线性表在计算机中可以用顺序存储和链式存储两种存储结构来表示.其中用顺序存储结构表示的线性表成为顺序表,用链式存储结构表示的线性表称为链表,链表又有单链表,双向链表,循环链表之分. 2. 线性表是 ...
- SQL奇技淫巧
1.SQL行列转换 问题:假设有张学生成绩表(tb)如下:姓名 课程 分数张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94想变成(得到如下结果): 姓名 ...
- Qt创建停靠悬浮窗口
1.Qt实现窗口停靠和悬浮使用类QDockWidget,它有两个重要方法用来设置停靠特性以及停靠区域, dw1->setFeatures(QDockWidget::DockWidgetMovab ...
- Head First设计模式之外观模式
一.定义 外观模式提供了一个统一的接口,用来访问子系统中的一群接口.外观定义了一个高层接口,让子系统更容易使用. 外观模式不只是简化了接口,也将客户从组件的子系统中解耦. 外观和适配器可以包装许多类, ...
- longest valid parentheses方法归纳
题目大意见leetcode,下面我稍微介绍下想到的三种方法: 方法一:不用栈去找匹配 建立一个数组l2表示匹配,然后i从0开始,看到 ( 就把l2对应的数值记为-1,直到看到 ),找到)以后,从当前i ...
- 高效的CSS代码(1)
——阅读笔记,欢迎纠错 平时写CSS时常用的两个CSS文档(小杂感觉用的蛮喜欢,也是看了些书,尝试使用效果不错) /************CSS reset *********/ /******** ...