codeforce div2 426 D. The Bakery
2.5 seconds
256 megabytes
standard input
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.
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
——————————————————————————————————
这道题很容易想到一个O(nnk)的暴力
就是外层枚举k第二层枚举n 内层枚举断点就可以了 是个非常常见的dp
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=;
int sum,n,m,c[M],cnt;
int f[M][],q[M];
struct node{int v,pos;}e[M];
bool cmp(node a,node b){return a.v<b.v;}
void clear(){memset(q,,sizeof(q));}
int main()
{
freopen("camp.in","r",stdin);
freopen("camp.out","w",stdout);
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&e[i].v),e[i].pos=i;
sort(e+,e++n,cmp);
c[e[].pos]=++cnt;
for(int i=;i<=n;i++){
if(e[i].v!=e[i-].v) cnt++;
c[e[i].pos]=cnt;
}
for(int i=;i<=n;i++){
if(!q[c[i]]) sum++,q[c[i]]=;
f[i][]=sum;
}
clear();
for(int k=;k<=m;k++){
clear();
for(int i=k;i<=n;i++){
sum=; q[c[i]]=i;
for(int j=i-;j>=k-;j--){
f[i][k]=max(f[i][k],f[j][k-]+sum);
if(q[c[j]]!=i) sum++,q[c[j]]=i;
}
}
}printf("%d\n",f[n][m]);
return ;
}
但是很明显这样只能水五十分
很明显外两层不能去掉 因为他们枚举的是所有的状态
而内层的枚举很明显可以用线段树优化
每次找到一个i 他的颜色是x 将x的上一个位置+1到当前位置的区间+1 然后找一下最大值
更新当前的答案f【i】 表示将1-i 分成当前状态的k段的最优情况
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=;
int sum,n,m,c[M],cnt;
int f[M][],q[M];
struct node{int v,pos;}e[M];
bool cmp(node a,node b){return a.v<b.v;}
void clear(){memset(q,,sizeof(q));}
int main()
{
freopen("camp.in","r",stdin);
freopen("camp.out","w",stdout);
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&e[i].v),e[i].pos=i;
sort(e+,e++n,cmp);
c[e[].pos]=++cnt;
for(int i=;i<=n;i++){
if(e[i].v!=e[i-].v) cnt++;
c[e[i].pos]=cnt;
}
for(int i=;i<=n;i++){
if(!q[c[i]]) sum++,q[c[i]]=;
f[i][]=sum;
}
clear();
for(int k=;k<=m;k++){
clear();
for(int i=k;i<=n;i++){
sum=; q[c[i]]=i;
for(int j=i-;j>=k-;j--){
f[i][k]=max(f[i][k],f[j][k-]+sum);
if(q[c[j]]!=i) sum++,q[c[j]]=i;
}
}
}printf("%d\n",f[n][m]);
return ;
}
codeforce div2 426 D. The Bakery的更多相关文章
- codeforce div2 C 树状数组
http://codeforces.com/contest/362 题目大意:给你一个序列,用冒泡排序法让他变为非递减的序列最少需要几次.在冒泡交换之间,你有一个swap操作,该swap操作是交换任意 ...
- Codeforce Div-2 985 C. Liebig's Barrels
http://codeforces.com/contest/985/problem/C C. Liebig's Barrels time limit per test 2 seconds memory ...
- ACM思维题训练 Section A
题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...
- Codeforces #426 Div2 D(线段树优化 DP )
#426 Div2 D 题意 给出 \(n\) 个数字,将这些数字隔成 \(k\) 个部分(相对位置不变),统计每个部分有几个不同数字,然后全部加起来求和,问和最大是多少. 分析 很容易想到 \(DP ...
- Codeforce Round #643 #645 #646 (Div2)
codeforce Round #643 #645 #646 div2 Round #643 problem A #include<bits/stdc++.h> using namespa ...
- Codeforce Round #216 Div2
e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...
- Codeforce Round #211 Div2
真的是b到不行啊! 尼玛C题一个这么简单的题目没出 aabbccddee 正确的是aabccdee 我的是 aabcdee 硬是TM的不够用,想半天还以为自己的是对的... A:题... B:题. ...
- Codeforces Round #426 (Div. 1) B The Bakery (线段树+dp)
B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- codeforce 192 div2解题报告
今天大家一起做的div2,怎么说呢,前三题有点坑,好多特判.... A. Cakeminator 题目的意思是说,让你吃掉cake,并且是一行或者一列下去,但是必须没有草莓的存在.这道题目,就是判断一 ...
随机推荐
- C/C++程序基础 (二)常用知识点
使用宏实现max 注意括号在宏内的使用 #define MAX(x, y) ( ( (x) > (y) ) ? (x) : (y) ) 宏参数连接 a##e##b 转化为字符串 #a const ...
- 数据库sql语句的exists和in的区别
性能变化的关键: #1 执行的先后顺序 谁是驱动表,谁先执行查询,谁后执行查询 #2 执行过程 exists的优点是:只要存在就返回了,这样的话很有可能不需要扫描整个表. in需要扫描完整个表,并 ...
- (83)zabbix Less than 25% free in the configuration cache解决
在zabbix server默认配置下,出现告警:Less than 25% free in the configuration cache,字面意思是:可用的配置缓存少于25%. 报错如下图: 增加 ...
- ASP.NET Core模块化前后端分离快速开发框架介绍之1、开篇
源码地址 GitHub:https://github.com/iamoldli/NetModular 演示地址 地址:https://nm.iamoldli.com 账户:admin 密码:admin ...
- LeetCode948-令牌放置
问题:令牌放置 你的初始能量为 P,初始分数为 0,只有一包令牌. 令牌的值为 token[i],每个令牌最多只能使用一次,可能的两种使用方法如下: 如果你至少有 token[i] 点能量,可以将令牌 ...
- vue layui
关于 vue中使用layui插件,个人一些小小的心得. 我是全局的引入,在static文件夹里存放layui的完整代码 在index页面中标签引入 <link rel="stylesh ...
- MTCNN学习资源
MTCNN pytorch版本的实现 TropComplique/mtcnn-pytorch https://github.com/TropComplique/mtcnn-pytorch MTCNN实 ...
- C++ 虚函数实例
#include <iostream> using namespace std; //线 class Line { public: Line(float len); ; ; protect ...
- Codeforces Round #464 (Div. 2) D. Love Rescue
D. Love Rescue time limit per test2 seconds memory limit per test256 megabytes Problem Description V ...
- HDU:3336-Count the string(next数组理解)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pr ...