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.

题目链接: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】的更多相关文章

  1. Codeforces 834D The Bakery 【线段树优化DP】*

    Codeforces 834D The Bakery LINK 题目大意是给你一个长度为n的序列分成k段,每一段的贡献是这一段中不同的数的个数,求最大贡献 是第一次做线段树维护DP值的题 感觉还可以, ...

  2. Codeforces 834D The Bakery - 动态规划 - 线段树

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...

  3. Codeforces 833B The Bakery dp线段树

    B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

  4. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  5. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  6. [Codeforces]817F. MEX Queries 离散化+线段树维护

    [Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...

  7. [动态dp]线段树维护转移矩阵

    背景:czy上课讲了新知识,从未见到过,总结一下. 所谓动态dp,是在动态规划的基础上,需要维护一些修改操作的算法. 这类题目分为如下三个步骤:(都是对于常系数齐次递推问题) 1先不考虑修改,不考虑区 ...

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

  9. DP+线段树维护矩阵(2019牛客暑期多校训练营(第二场))--MAZE

    题意:https://ac.nowcoder.com/acm/contest/882/E 给你01矩阵,有两种操作:1是把一个位置0变1.1变0,2是问你从第一行i开始,到最后一行j有几种走法.你只能 ...

随机推荐

  1. 【java设计模式】【行为模式Behavioral Pattern】策略模式Strategy Pattern

    package com.tn.策略模式; public class Client { private Strategy strategy; public void setStrategy(Strate ...

  2. win10 下 学习 xe10 误以为调试失效

    1. XE里面运行的有两个按扭,你点后面一个,就是debug 的了,前面一个是直接运行,不一样的!,被delphi7老思维导向错了  1)绿色箭头是直接运行,快捷键:ctrl+shfit+F9  2) ...

  3. 502 VS 504

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/89 首先看一下概念: 502:作为网关或者代理工作的服务器尝试执 ...

  4. 《Linux系统编程手册》读书笔记——第2章基本概念

    操作系统的核心--内核 内核的职责 进程调度:Linux属于抢占式多任务操作系统,多个进程可同时驻留于内存,且每个进程都能获得对CPU的使用权.哪些进程获得对CPU的使用,以及每个进程能使用多长时间 ...

  5. jQueryUI Autocomplete插件使用入门教程(最新版)---------转载

    前言: jQuery,无需多作介绍,相信各位读者都应该接触或使用过了.jQuery UI,简而言之,它是一个基于jQuery的前端UI框架.我们可以使用jQuery + jQuery UI非常简单方便 ...

  6. linux系统日常管理复习题讲解

    1. 如何看当前Linux系统有几颗物理CPU和每颗CPU的核数? 2. 查看系统负载有两个常用的命令,是哪两个?这三个数值表示什么含义呢? 3. vmstat r, b, si, so, bi, b ...

  7. Qt个人研究进展

    1:纯socket通信实现多线程邮件发送,支持多个收件人和附件,通用任何平台,包括ARM.2:纯串口通信AT命令实现多线程短信收发,支持多个收件人和长短信,通用任何平台,包括ARM.3:纯串口通信PO ...

  8. BCL和CoreFx的区别

    bcl是.netframework clr 的基础库corefx是.net core clr的基础库

  9. Centos7上安装使用locate

    centos7上默认没有locate命令,需先安装locate yum install mlocate 注意是mlocate,如果是yum install locate系统会提示没有安装包 安装完成后 ...

  10. Java的静态代码块是否会在类被加载时自动执行?

    JAVA静态代码块会在类被加载时自动执行? 一.先看Java静态方法,静态变量 http://www.cnblogs.com/winterfells/p/7906078.html 静态代码块 在类中, ...