CF833B The Bakery

LG传送门

线段树优化DP。

其实这是很久以前就应该做了的一道题,由于颓废一直咕在那里,其实还是挺不错的一道题。

先考虑\(O(n^2k)\)做法:设\(f[i][j]\)表示\(1\)到\(i\)之间分割\(j\)次得到的最大值,\(g[i][j]\)表示\(i\)到\(j\)之间不同的颜色个数。

转移就是:

\[f[i][j]=max \{f[t][j-1]+g[t+1][i] \} \qquad t \in [1,i)
\]

但是时空都无法承受,考虑优化。

考虑使用线段树处理转移,我们不处理\(g\)数组(\(O(n^2)\)的空间开不下),对于每一个点处理它对从前一个同颜色位置到当前位置的贡献,即在线段树上把这些位置的权值\(++\),维护一个区间最大值,转移时直接查询前缀最大值就可以了。事实上,这里的线段树只是把\(f\)和\(g\)拿来一起转移从而优化了时间复杂度,转移的本质是没有变的。

可以滚动数组(不用也没关系),具体看代码。

#include<cstdio>
#include<cctype>
#define B 1000000
#define R register
#define I inline
using namespace std;
const int S=35003,M=140003;
char buf[B],*p1,*p2;
I char gc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,B,stdin),p1==p2)?EOF:*p1++;}
I int rd(){
R int f=0; R char c=gc();
while(c<48||c>57) c=gc();
while(c>47&&c<58) f=f*10+(c^48),c=gc();
return f;
}
int h[S],p[S],b[M],c[M],f[S],n;
I int max(int x,int y){return x>y?x:y;}
I void upd(int k,int v){c[k]+=v,b[k]+=v;}
I void psu(int k,int p,int q){c[k]=max(c[p],c[q]);}
I void psd(int k,int p,int q){if(b[k]) upd(p,b[k]),upd(q,b[k]),b[k]=0;}
void bld(int k,int l,int r){
b[k]=0;
if(l==r){
c[k]=f[l-1];
return ;
}
R int p=k<<1,q=p|1,m=l+r>>1;
bld(p,l,m),bld(q,m+1,r),psu(k,p,q);
}
void mdf(int k,int l,int r,int x,int y){
if(x<=l&&r<=y){
upd(k,1);
return ;
}
R int p=k<<1,q=p|1,m=l+r>>1;
psd(k,p,q);
if(x<=m)
mdf(p,l,m,x,y);
if(m<y)
mdf(q,m+1,r,x,y);
psu(k,p,q);
}
int qry(int k,int l,int r,int x,int y){
if(x<=l&&r<=y)
return c[k];
R int m=l+r>>1,p=k<<1,q=p|1,o=0;
psd(k,p,q);
if(x<=m)
o=max(o,qry(p,l,m,x,y));
if(m<y)
o=max(o,qry(q,m+1,r,x,y));
return o;
}
int main(){
R int k,i,j,x;
n=rd(),k=rd();
for(i=1;i<=n;++i)
x=rd(),p[i]=h[x]+1,h[x]=i;
for(i=1;i<=k;++i){
bld(1,1,n);
for(j=1;j<=n;++j)
mdf(1,1,n,p[j],j),f[j]=qry(1,1,n,1,j);
}
printf("%d",f[n]);
return 0;
}

CF833B The Bakery 线段树,DP的更多相关文章

  1. CF833B The Bakery (线段树+DP)

    题目大意:给你一个序列(n<=35000),最多分不大于m块(m<=50),求每个块内不同元素的数量之和的最大值 考试的时候第一眼建图,没建出来,第二眼贪心 ,被自己hack掉了,又随手写 ...

  2. codeforces#426(div1) B - The Bakery (线段树 + dp)

    B. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

  3. Codeforces.833B.The Bakery(线段树 DP)

    题目链接 \(Description\) 有n个数,将其分为k段,每段的值为这一段的总共数字种类,问最大总值是多少 \(Solution\) DP,用\(f[i][j]\)表示当前在i 分成了j份(第 ...

  4. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  5. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

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

  7. CodeForces 834D The Bakery(线段树优化DP)

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...

  8. Codeforces Round #426 (Div. 2) D The Bakery(线段树 DP)

     The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard input ...

  9. Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP

    D. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

随机推荐

  1. django 板块动态切换

    需求:在同一页面的不同板块上可以实现动态切换,使用一个view实现,具体如下图所示,点击phy显示物理机列表,点击vm显示虚机列表,phy.vm对应的url均是动态生成:               ...

  2. 汉字转拼音工具类java

    package com.baihui.core.utils; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge. ...

  3. MyISAM和innoDB对比,覆盖索引简单回顾

    MyISAM Myisam是Mysql的默认存储引擎,当create创建新表时,未指定新表的存储引擎时,默认使用Myisam. 它不支持事务,也不支持外键,尤其是访问速度快,对事务完整性没有要求或者以 ...

  4. JS代码高亮编辑器 ace.js

    JS代码高亮编辑器 ace.js 字数254 阅读2 评论0 喜欢0 瞎扯 ace 是 js 实现的代码编辑器 编译打包之后的 ACE 代码 官网,未提供编译好的文件 ACE 拥有的特点 语法高亮超过 ...

  5. Maven实战(十一)eclipse下载依赖jar包出问题

    问题描述 在pom.xml中配置了依赖,但是提示依赖不成功,或在本地仓库找不到相关依赖 大致错误如下: ArtifactDescriptorException: Failed to read arti ...

  6. Mina使用总结(一)MinaServer

    我们先看一个最简单的Mina Server服务端代码,该段代码实现了服务端Server启动并监听客户端请求 package com.bypay.mina.server; import java.io. ...

  7. Spring 源代码阅读之声明式事务

    事务控制流程 例如对如下代码进行事务控制 class service1{ method1(){ method2(); } } class service2{ method2(); } 原理:建立一个m ...

  8. Oracle 数据库创建(图形界面操作)

    Oracle 创建数据库图文分解: 1. 选择所有程序->Oracle-OraDb11g_home1->Configuration and Migration Tools -> Da ...

  9. 安装配置maven私服-nexus

    1.ubuntu下的Bundle安装方式 1.1. 去官网下载安装包:http://www.sonatype.org/nexus/ 我这里下载的是:nexus-2.8.1-01-bundle.zip, ...

  10. 20165318 2017-2018-2 《Java程序设计》第六周学习总结

    20165318 2017-2018-2 <Java程序设计>第六周学习总结 教材内容学习总结 第八章 常用实用类 1.String类 String类用来处理字符序列,在java.lang ...