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. Mac中Eclipse安装和使用svn

    Eclipse版本为Neon Release (4.6.0) 安装svn 安装HomeBrew 在终端中输入 ruby -e "$(curl -fsSL https://raw.github ...

  2. iOS Label 自适应高度

    推荐第二个 测试一,只改变numberOfLines属性,label的高度不会自适应(会有text中的一部分内容称为......) NSString *str = @"jgreijgirje ...

  3. Centos7解决图形界面卡死问题

    经常会遇到图形界面卡死,搜了一搜,解决办法如下: killall -9 gnome-shell

  4. java.util.HashSet

    Operations Time Complexity Notes add, remove, contains, size O(1) assuming the hash functions has di ...

  5. Java自己动手写连接池四

    Java自己动手写连接池四 测试: package com.kama.cn; import java.sql.Connection; public class Test { public static ...

  6. chromedriver与chrome版本映射列表

    chromedriver与chrome版本映射列表: chromedriver版本 支持的Chrome版本 v2.30 v58-60 v2.29 v56-58 v2.28 v55-57 v2.27 v ...

  7. JS中金额转换以及格式化

    abs = function(val){ //金额转换 分->元 保留2位小数 并每隔3位用逗号分开 1,234.56 var str = (val/100).toFixed(2) + ''; ...

  8. vue2.0笔记《二》组件

    主要内容:如何注册组件.如何使用组件.父组件子组件之间值的传递 1.如何注册组件 第一步:通过import将子组件载入父组件的js中 // 第一步:通过import将子组件载入父组件的js中 impo ...

  9. Linux(Cent OS7.2)下启动停止memcached方法及ps命令使用讲解

    Linux下,以Cent OS7.2为例,安装memcached后的启动方法很简单,这里我们使用yum源安装. 首先查找yum源版本库的memchaced安装包, yum list | grep me ...

  10. onoffswitch-checkbox

    @foreach (EmailSubscription es in Model)   { if(true){ <div class="onoffswitch">     ...