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

  1. CodeForces 834C - The Meaningless Game | Codeforces Round #426 (Div. 2)

    /* CodeForces 834C - The Meaningless Game [ 分析,数学 ] | Codeforces Round #426 (Div. 2) 题意: 一对数字 a,b 能不 ...

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

  3. Codeforces Round #426 (Div. 2)

    http://codeforces.com/contest/834 A. The Useless Toy 题意: <,>,^,v这4个箭头符号,每一个都可以通过其他及其本身逆时针或者顺时针 ...

  4. Codeforces Round #426 (Div. 2) C. The Meaningless Game

    C. The Meaningless Game 题意: 两个人刚刚开始游戏的时候的分数, 都是一分, 然后随机一个人的分数扩大k倍,另一个扩大k的平方倍, 问给你一组最后得分,问能不能通过游戏得到这样 ...

  5. Codeforces Round #426 (Div. 2)A B C题+赛后小结

    最近比赛有点多,可是好像每场比赛都是被虐,单纯磨砺心态的作用.最近讲的内容也有点多,即便是点到为止很浅显的版块,刷了专题之后的状态还是~"咦,能做,可是并没有把握能A啊".每场网络 ...

  6. Codeforces Round #426 (Div. 2) A,B,C

    A. The Useless Toy 题目链接:http://codeforces.com/contest/834/problem/A 思路: 水题 实现代码: #include<bits/st ...

  7. Codeforces Round #426 (Div. 2)A题&&B题&&C题

    A. The Useless Toy:http://codeforces.com/contest/834/problem/A 题目意思:给你两个字符,还有一个n,问你旋转n次以后从字符a变成b,是顺时 ...

  8. 【Codeforces Round #426 (Div. 2) A】The Useless Toy

    [Link]:http://codeforces.com/contest/834/problem/A [Description] [Solution] 开个大小为4的常量字符数组; +n然后余4,-n ...

  9. 【Codeforces Round #426 (Div. 2) B】The Festive Evening

    [Link]:http://codeforces.com/contest/834/problem/B [Description] [Solution] 模拟水题; 注意一个字母单个出现的时候,结束和开 ...

  10. 【Codeforces Round #426 (Div. 2) C】The Meaningless Game

    [Link]:http://codeforces.com/contest/834/problem/C [Description] 有一个两人游戏游戏; 游戏包括多轮,每一轮都有一个数字k,赢的人把自己 ...

随机推荐

  1. 【TIL】today i learned

    20191115 JSON解析网站 https://www.json.cn/    方便简洁,左侧放JSON表达式,右侧自动解析 联系英文盲打网站 https://www.keybr.com/  字母 ...

  2. vscode-php代码提升及函数跳转

    安装插件,php intellisense 安装后还要配置一下PHP的运行路径 打开扩展     输入 PHP IntelliSense     安装     文件 - 首选项 - 设置 - 扩展 - ...

  3. 启动Maven项目时报错Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) on project **-web: Failed to clean project: Failed to delete E:\**\target\tomcat\logs\access_lo

    这类错误 出现这种错误,通常是由于您已启动了另一个tomcat 进程或者运行的javaw.exe进程,导致报错. 解决方法: 1. 鼠标点击 X 进行关闭运行失败的 Console页,(如果运行多次, ...

  4. Powershell + HTA

    众所周知,Powershell早已被集成到了windows的环境中,国外大牛玩得不亦乐乎,而国内圈子却很少听到讨论Powershell的,HTA更不用说了,不是学计算机的或许根本不知道这是什么鬼 Li ...

  5. RequestMapping 注解的解析、匹配、注册

    RequestMapping 注解的解析.匹配.注册 1)创建 RequestMappingHandlerMapping 实例时会触发 afterPropertiesSet 调用. 2)读取容器中所有 ...

  6. Microsoft:Team Foundation Server 20XX Release Notes

    ylbtech-Microsoft:Team Foundation Server 2017 Release Notes 1.返回顶部 1. https://docs.microsoft.com/en- ...

  7. Vue知识整理9:class与style绑定

    1.v-bind:class:绑定class样式.通过控制isActive变量值来实现是否显示:通过.active样式设置背景颜色. 2.支持普通的class与v-bind绑定样式混合使用: v-bi ...

  8. java数字加密算法

    数字加密在项目中时常会遇到,如手机号,身份证号信息等,下面小白将自己手写的数字加密算法分享给大家,可在项目中直接运用.加密规则,入参时传递一个字段时间戳 time:* 1.以字母代替数字,0-9分别为 ...

  9. 4.1.k8s.svc(Service)

    #Service Service为Pod提供4层负载均衡, 访问 -> Service -> Pod组 #4种类型 ClusterIP 默认,分配一个VIP,只能内部访问 NodePort ...

  10. 网易云课堂_C++程序设计入门(下)_第8单元:年年岁岁花相似– 运算符重载_第8单元 - 作业2:OJ编程 - 重载数组下标运算符

    第8单元 - 作业2:OJ编程 - 重载数组下标运算符 查看帮助 返回   温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提 ...