Codeforces Round #426 (Div. 2) - D
题目链接:http://codeforces.com/contest/834/problem/D
题意:给定一个长度为n的序列和一个k,现在让你把这个序列分成刚好k段,并且k段的贡献之和最大。对于每一段的贡献为该段有多少个不同的数字。
思路:考虑dp, dp[k][i]表示前i个数切k段的答案,那么dp[k][i]=max(dp[k-1][j]+color(j+1,i)) [1<=j<i], [color(l,r)表示区间[l,r]有多少个不同的数字] ,由于第k行的dp值只会影响到k+1行的dp值,所以我们可以把k这一维忽略掉。考虑转移dp[k][i+1],新增加的i+1这个点会影响到[pre[a[i]]+1,i]这段区间+1。
ftiasch的题解

我们在建线段树的时候,树上的结点(结点表示的区间为[l,r])维护的是g[k](即dp[k-1][r-1]),然后就是区间加和区间最大值了。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <bitset>
using namespace std;
typedef long long LL;
const int MAXN = + ; int n,k,pre[MAXN],dp[MAXN];
struct Color{
int val,l,r;
}c[MAXN]; //Segment Tree
#define L(x)(x<<1)
#define R(x)(x<<1|1)
struct Node{
int l,r,Lazy,maxval;
Node(int _l=,int _r=,double _val=){
l=_l; r=_r; maxval=_val;
}
}Seg[MAXN*];
void pushUp(int k){
Seg[k].maxval=max(Seg[L(k)].maxval,Seg[R(k)].maxval);
}
void pushDown(int k){
if(Seg[k].Lazy){
Seg[L(k)].Lazy+=Seg[k].Lazy;
Seg[L(k)].maxval+=Seg[k].Lazy;
Seg[R(k)].Lazy+=Seg[k].Lazy;
Seg[R(k)].maxval+=Seg[k].Lazy;
Seg[k].Lazy=;
}
}
void Build(int st,int ed,int k){
Seg[k].l=st; Seg[k].r=ed; Seg[k].Lazy=;
if(st==ed){
Seg[k].maxval=dp[st-];
return;
}
int mid=(st+ed)>>;
Build(st,mid,L(k)); Build(mid+,ed,R(k));
pushUp(k);
}
void Modify(int st,int ed,int k,int val){
if(Seg[k].l==st&&Seg[k].r==ed){
Seg[k].maxval+=val;
Seg[k].Lazy+=val;
return;
}
pushDown(k);
if(Seg[L(k)].r>=ed){
Modify(st,ed,L(k),val);
}else if(Seg[R(k)].l<=st){
Modify(st,ed,R(k),val);
}else{
Modify(st,Seg[L(k)].r,L(k),val); Modify(Seg[R(k)].l,ed,R(k),val);
}
pushUp(k);
}
int Query(int st,int ed,int k){
if(Seg[k].l==st&&Seg[k].r==ed){
return Seg[k].maxval;
}
pushDown(k);
int res;
if(Seg[L(k)].r>=ed){
res=Query(st,ed,L(k));
}else if(Seg[R(k)].l<=st){
res=Query(st,ed,R(k));
}else{
res=max(Query(st,Seg[L(k)].r,L(k)),Query(Seg[R(k)].l,ed,R(k)));
}
pushUp(k);
return res;
}
int main(){
#ifdef kirito
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
while(~scanf("%d%d",&n,&k)) {
memset(pre,,sizeof(pre));
for(int i=;i<=n;i++){
scanf("%d",&c[i].val);
c[i].r=i; c[i].l=pre[c[i].val]+; pre[c[i].val]=i;
}
for(int i=;i<=n;i++){
dp[i]=dp[i-]+(c[i].l==?:);
}
for(int j=;j<=k;j++){
Build(,n,);
for(int i=;i<=n;i++){
Modify(c[i].l,c[i].r,,);
dp[i]=Query(,i,);
}
}
printf("%d\n",dp[n]);
}
return ;
}
Codeforces Round #426 (Div. 2) - D的更多相关文章
- CodeForces 834C - The Meaningless Game | Codeforces Round #426 (Div. 2)
/* CodeForces 834C - The Meaningless Game [ 分析,数学 ] | Codeforces Round #426 (Div. 2) 题意: 一对数字 a,b 能不 ...
- Codeforces Round #426 (Div. 2)【A.枚举,B.思维,C,二分+数学】
A. The Useless Toy time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- Codeforces Round #426 (Div. 2)
http://codeforces.com/contest/834 A. The Useless Toy 题意: <,>,^,v这4个箭头符号,每一个都可以通过其他及其本身逆时针或者顺时针 ...
- Codeforces Round #426 (Div. 2) C. The Meaningless Game
C. The Meaningless Game 题意: 两个人刚刚开始游戏的时候的分数, 都是一分, 然后随机一个人的分数扩大k倍,另一个扩大k的平方倍, 问给你一组最后得分,问能不能通过游戏得到这样 ...
- Codeforces Round #426 (Div. 2)A B C题+赛后小结
最近比赛有点多,可是好像每场比赛都是被虐,单纯磨砺心态的作用.最近讲的内容也有点多,即便是点到为止很浅显的版块,刷了专题之后的状态还是~"咦,能做,可是并没有把握能A啊".每场网络 ...
- Codeforces Round #426 (Div. 2) A,B,C
A. The Useless Toy 题目链接:http://codeforces.com/contest/834/problem/A 思路: 水题 实现代码: #include<bits/st ...
- Codeforces Round #426 (Div. 2)A题&&B题&&C题
A. The Useless Toy:http://codeforces.com/contest/834/problem/A 题目意思:给你两个字符,还有一个n,问你旋转n次以后从字符a变成b,是顺时 ...
- 【Codeforces Round #426 (Div. 2) A】The Useless Toy
[Link]:http://codeforces.com/contest/834/problem/A [Description] [Solution] 开个大小为4的常量字符数组; +n然后余4,-n ...
- 【Codeforces Round #426 (Div. 2) B】The Festive Evening
[Link]:http://codeforces.com/contest/834/problem/B [Description] [Solution] 模拟水题; 注意一个字母单个出现的时候,结束和开 ...
- 【Codeforces Round #426 (Div. 2) C】The Meaningless Game
[Link]:http://codeforces.com/contest/834/problem/C [Description] 有一个两人游戏游戏; 游戏包括多轮,每一轮都有一个数字k,赢的人把自己 ...
随机推荐
- 【JQuery-02】事件绑定多次造成多次执行
http://blog.csdn.net/always_littlesun/article/details/52594548
- 深入理解php的输出缓冲区(output buffer)
这篇文章是翻译自Julien Pauli的博客文章PHP output buffer in deep,Julien是PHP源码的资深开发和维护人员.这篇文章从多个方面讲解了PHP中的输出缓冲区以及怎么 ...
- 和他人一起管理GitHub仓库
和他人一起管理 GitHub 仓库方法 打开一个项目的仓库 同意好之后在项目中要添加 README.md 文件(重要) 要让别人也可以使用 master 主线 在上传代码前,一定要使用 git 指令 ...
- python3 导入模块
python3导入模块和python2 有些不同 需要指定相对目录 如,在Project下有一个nlp目录里面有一个ltp模块,则 from n1.ltp import Clawer
- python - 代码调试的好帮手sys._getframe()
python 的调试,令人非常忧伤,通过将输出路径打印的方式,可以提高很大的方便性: import sys #coding=utf-8 def get_cur_info(): print sys._g ...
- 前端必须掌握的 nginx 技能(1)
概述 作为一个前端,我觉得必须要学会使用 nginx 干下面几件事: 代理静态资源 设置反向代理(添加https) 设置缓存 设置 log 部署 smtp 服务 设置 redis 缓存(选) 下面我按 ...
- winform带你玩转rabbitMQ
http://www.cnblogs.com/dubing/p/4017613.html
- 快速入门分布式消息队列之 RabbitMQ(2)
目录 目录 前文列表 RabbitMQ 的特性 Message Acknowledgment 消息应答 Prefetch Count 预取数 RPC 远程过程调用 vhost 虚拟主机 插件系统 最后 ...
- Linux_LVM、RAID_RHEL7
目录 目录 LVM逻辑卷管理 把物理分区初始化为物理卷 创建卷组 建立逻辑卷 格式化 挂载 vg拓展操作 lv扩展操作 RAID RAID 类型 RAID0条带化 RAID1镜像 RAID5条带冗余 ...
- Python Module_subprocess_子进程(程序调用)
目录 目录 前言 软件环境 认识subprocess Popen Constructor构造函数 Class Popen的参数 args 调用程序 调用Shell指令 stdinstdoutstder ...