Codeforces 834D The Bakery【dp+线段树维护+lazy】
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代码:【二维数组改写成一维数组(个人原因,不太喜欢高维度的)】
1 #include <bits/stdc++.h>
2 using namespace std;
3 #define maxn 35010
4 #define INF 0x3f3f3f3f
5 int addv[maxn*4],Max[maxn*4];
6 int dp[maxn],ql,qr;
7 int pre[maxn], last[maxn], a[maxn];
8 void build(int l,int r,int o)
9 {
10 addv[o]=0;
11 if(l == r)
12 {
13 Max[o]=dp[l];
14 return;
15 }
16 int mid=l+(r-l)/2;
17 build(l,mid,o*2);
18 build(mid+1,r,o*2+1);
19 Max[o]=max(Max[o*2],Max[o*2+1]);
20 }
21 void pushdown(int o)
22 {
23 int lc=o*2,rc=o*2+1;
24 if(addv[o])
25 {
26 addv[lc]+=addv[o];
27 addv[rc]+=addv[o];
28 Max[lc]+=addv[o];
29 Max[rc]+=addv[o];
30 addv[o]=0;
31 }
32 }
33 void update(int l,int r,int o)
34 {
35 if(ql>qr)
36 return;
37 if(ql<=l&&qr>=r)
38 {
39 addv[o]++;
40 Max[o]++;
41 return;
42 }
43 pushdown(o);
44 int mid=l+(r-l)/2;
45 if(ql<=mid)
46 update(l,mid,o*2);
47 if(qr>mid)
48 update(mid+1,r,o*2+1);
49 Max[o]=max(Max[o*2],Max[o*2+1]);
50 }
51 int query(int l,int r,int o)
52 {
53 if(ql<=l&&qr>=r)
54 {
55 return Max[o];
56 }
57 pushdown(o);
58 int mid=l+(r-l)/2;
59 int best=-INF;
60 if(ql<=mid)
61 best=max(best,query(l,mid,o*2));
62 if(qr>mid)
63 best=max(best,query(mid+1,r,o*2+1));
64 return best;
65 }
66 int main()
67 {
68 int n,k;
69 scanf("%d%d",&n,&k);
70 memset(last,-1,sizeof(last));
71 int cnt=0;
72 for(int i=1;i<=n;i++)
73 {
74 scanf("%d",&a[i]);
75 pre[i]=last[a[i]];
76 last[a[i]]=i;
77 if(pre[i]==-1)
78 cnt++;
79 dp[i]=cnt;
80 }
81 for(int kk=2;kk<=k;kk++)
82 {
83 for(int i=1;i<kk-1;i++)
84 dp[i]=-INF;
85 build(1,n,1);
86 for(int i=kk;i<=n;i++)
87 {
88 ql=max(1,pre[i]),qr=i-1;
89 update(1,n,1);
90 ql=1,qr=i-1;
91 dp[i]=query(1,n,1);
92 }
93 }
94 printf("%dn",dp[n]);
95 return 0;
96 }
官方题解:
1 #include <cstdio>
2 #include <cstring>
3 #include <map>
4
5 #define K first
6 #define V second
7
8 const int N = 35001;
9
10 int last[N], pre[N], dp[N];
11
12 int main()
13 {
14 int n, m;
15 while (scanf("%d%d", &n, &m) == 2) {
16 memset(last, 0, sizeof(last));
17 for (int i = 1, a; i <= n; ++ i) {
18 scanf("%d", &a);
19 pre[i] = last[a];
20 last[a] = i;
21 }
22 dp[0] = 0;
23 for (int i = 1; i <= n; ++ i) {
24 dp[i] = dp[i - 1] + !pre[i];
25 }
26 for (int k = 2; k <= m; ++ k) {
27 std::map<int, int> c;
28 c[0] = n + 1;
29 int last_dp = dp[k - 1];
30 for (int i = k; i <= n; ++ i) {
31 int now = 0;
32 while (now + c.rbegin()->V <= last_dp) {
33 now += c.rbegin()->V;
34 c.erase(c.rbegin()->K);
35 }
36 c.rbegin()->V += now - last_dp;
37 c[i] = last_dp + 1;
38 auto it = c.upper_bound(pre[i]);
39 it --;
40 it->V --;
41 if (it->V == 0) {
42 c.erase(it->K);
43 }
44 last_dp = dp[i];
45 dp[i] = (n + 1) - c.begin()->V;
46 }
47 }
48 printf("%dn", dp[n]);
49 }
50 }
- 一行代码,Pandas秒变分布式,快速处理TB级数据
- 机器学习算法实现解析——word2vec源码解析
- 从零开始创建一个基于Go语言的web service
- 转--Golang项目邮件发送模块代码分享
- 虚拟化平台cloudstack(8)——从UI开始
- 使用oracle的大数据工具ODCH访问HDFS数据文件
- UWP基础教程 - XAML开篇
- UWP基础教程 - XAML依赖属性和附加属性
- GO语言学习:动态Web
- Golang 连接mssql sql server
- UWP基础教程 - XAML对象元素和属性
- UWP基础教程 - 禁用&开启截取应用屏幕
- Android开发中应该避免的内存泄露
- UWP基础教程 - XAML资源
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- linux 平均负载 load average 的含义【转】
- zookeeper-01 概述
- zookeeper-02 部署
- zookeeper-03 命令行操作
- java连接zookeeper服务器出现“KeeperErrorCode = ConnectionLoss for ...”
- Linux 内存使用率
- CentOS7.4下编译Hadoop-2.7.6
- VMware安装CentOS6
- VMware安装CentOS7 3.1 主机名修改3.2 网卡名修改3.3 ifconfig安装
- VMware网络设置
- Destoon 6.0 guestbook.php 通用SQL注入漏洞
- Hadoop2.7.6_01_部署
- Hadoop2.7.6_02_HDFS常用操作
- Bottle HTTP 头注入漏洞探究
- Hadoop2.7.6_03_HDFS原理