CF833B The Bakery 线段树,DP
CF833B The Bakery
线段树优化DP。
其实这是很久以前就应该做了的一道题,由于颓废一直咕在那里,其实还是挺不错的一道题。
先考虑\(O(n^2k)\)做法:设\(f[i][j]\)表示\(1\)到\(i\)之间分割\(j\)次得到的最大值,\(g[i][j]\)表示\(i\)到\(j\)之间不同的颜色个数。
转移就是:
\]
但是时空都无法承受,考虑优化。
考虑使用线段树处理转移,我们不处理\(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的更多相关文章
- CF833B The Bakery (线段树+DP)
题目大意:给你一个序列(n<=35000),最多分不大于m块(m<=50),求每个块内不同元素的数量之和的最大值 考试的时候第一眼建图,没建出来,第二眼贪心 ,被自己hack掉了,又随手写 ...
- 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 ...
- Codeforces.833B.The Bakery(线段树 DP)
题目链接 \(Description\) 有n个数,将其分为k段,每段的值为这一段的总共数字种类,问最大总值是多少 \(Solution\) DP,用\(f[i][j]\)表示当前在i 分成了j份(第 ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- 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 ...
- CodeForces 834D The Bakery(线段树优化DP)
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- 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 ...
- 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 ...
随机推荐
- [翻译] LiquidFloatingActionButton
LiquidFloatingActionButton https://github.com/yoavlt/LiquidFloatingActionButton LiquidFloatingAction ...
- [UI] Article intro effects
Article intro effects http://freebiesbug.com/code-stuff/article-intro-effects/
- mysql创建表的注意事项
1 库名,表名,字段名必须使用小写字母,"_"分割. 2 库名,表名,字段名必须不超过12个字符. 3 库名,表名,字段名见名识意,建议使用名词而不是动词. 4 建议使用InnoD ...
- Outliner大纲式笔记软件介绍
简介 什么是Outliner An outliner (or outline processor) is a specialized type of word processor used to vi ...
- swift的类型系统及类型(内存)信息获取:接口、编译运行时、反射、内存布局
swift是静态语言,没有在运行时保存类型的结构信息(isa.class). 一.self.Self.Type.typeof extension Collection where Self.Eleme ...
- 写了一个web使用向导的小插件
运行效果: 引入插件: <link rel="stylesheet" href="ez-guide.css"> <script src=&qu ...
- etherlime-2-Etherlime Library API-deployer
Etherlime Library API 库API Deployer部署者 Deployer functionality The main functionality the deployer ex ...
- R函数-时间序列ETS参数说明
alpha\beta\gamma分别代表水平.趋势.季节分量的平滑参数α.β.γ.这三个参数我们希望接近于0,以便于更平滑,即越小越平滑.在乘法模型的情况下,参数需要非常低,否则模型会对噪声太敏感. ...
- jlink RTT 打印 BUG , FreeRTOS 在开启 tickless 模式下 无法使用的问题
一开始我以为是 jlink 的问题,后面发现是 tickless 模式搞鬼 tickless 模式下 ,内核 会 根据任务需求,会停止工作,这个时候 jlink rtt 打印就会失效!!! 不过 NR ...
- ASP.NET Core多语言 (转载)
ASP.NET Core中提供了一些本地化服务和中间件,可将网站本地化为不同的语言文化.ASP.NET Core中我们可以使用Microsoft.AspNetCore.Localization库来实现 ...