题目链接: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. join的源码

    long base = System.currentTimeMills();  long now = 0; if(millis < 0){  throw new IllegalArgumentE ...

  2. 大数据笔记(二十五)——Scala函数式编程

    ===================== Scala函数式编程 ======================== 一.Scala中的函数 (*) 函数是Scala中的头等公民,就和数字一样,可以在变 ...

  3. 初始化String,System,OutOfMemoryError

    对于String, System而言,其初始化过程和 Thread一致.而对于OutOfMemoryError而言,其继承结构如下: 因此对于OutOfMemoryError,会首先将状态改为4,然后 ...

  4. listen( ) accept( )

    服务器端,创建socket,bind绑定套接字后,还需要使用listen()函数让套接字进入被动监听状态,再调用accept()函数,就可以随时响应客户端的请求了 listen() 函数 通过 lis ...

  5. JavaScript 文件拖拽上传插件 dropzone.js 介绍

    http://www.renfei.org/blog/dropzone-js-introduction.html

  6. Detectron系统实现了最先进的物体检测算法https://github.com/facebookresearch/Detectron

    ,包括Mask R-CNN. 它是用Python编写的,支持Caffe2深度学习框架. 不久前,FAIR才开源了语音识别的工具wav2letter,戳这里看大数据文摘介绍<快讯 | Facebo ...

  7. nodejs 文件操作

    前言: nodejs 自带的文件操作的模块  fs 就是对文件的增删查改: 就像我们用的服务器,我们没有办法在运行的文件上进行一直的修改,因为他不向浏览器,刷新后我们的文件会自己修改: 如果想要更改我 ...

  8. linux测试某进程占用oi、cpu、内存的使用情况

    pidstat 概述 pidstat是sysstat工具的一个命令,用于监控全部或指定进程的cpu.内存.线程.设备IO等系统资源的占用情况.pidstat首次运行时显示自系统启动开始的各项统计信息, ...

  9. Fresnel integral菲涅尔积分的一丢丢探讨

    起因源于导师的关于回旋曲线的一点问题 其中最后得到的曲率公式中的c,s’和s定义不明确 于是开始从头从(2.1)式中的积分入手探究 维基百科中Fresnel integral的S(x)与C(x)的定义 ...

  10. linux查看硬盘信息

    1. 通过内核查看硬盘信息 cat /sys/block/sda/device/model cat /sys/block/sda/device/vendor 2. 用过hdparm命令查看 hdpar ...