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 ...
随机推荐
- 铁乐学python_Day44_IO多路复用
目录 IO模型介绍 阻塞IO(blocking IO) 非阻塞IO(non-blocking IO) 多路复用IO(IO multiplexing) 异步IO(Asynchronous I/O) IO ...
- November 15th 2016 Week 47th Tuesday
Success is finding satisfaction in giving a little more than you take. 成功就是付出比得到多,仍然心满意足. Can I find ...
- Anaconda 包管理工具 conda 进行虚拟环境管理入门
在基于 python 进行数据分析.机器学习等领域的实践和学习时,由于代码的更迭和更新,运行他人实现的代码或尝试安装新的工具库时往往需要指定特定版本的其他工具库,以满足特定环境的构建条件.而将同一工具 ...
- 嵌套的ng-repeat双层循环,内层如何获取外层的$index?
html代码: <div> <ul ng-repeat="row in table track by $index"> <li ng-repeat=& ...
- (转)em重建全过程
该问题遇到N次,被郁闷N次,特此记录以备不时之需 由于n久不用em,而本机在公司使用dhcp自动获取ip,导致ip变化,而使em启动报出ora-12514 DBD ERROR: OCIServerAt ...
- Java基础知识强化107:DecimalFormat
1. 引入: 如何控制输出数据的精度? >1. 使用Math.round方法 (1)Java如何把一个float(double)四舍五入到小数点后2位,4位,或者其它指定位数 ? 答:比如,如下 ...
- java web开发环境配置系列(一)安装JDK
在今天,读书有时是件“麻烦”事.它需要你付出时间,付出精力,还要付出一份心境.--仅以<java web开发环境配置系列>来祭奠那逝去的…… 1.下载JDK文件(http://www.or ...
- Sequelize-nodejs-9-Scopes
Scopes作用域 Scoping allows you to define commonly used queries that you can easily use later. Scopes c ...
- 鴻雁 Anser cygnoides
鴻雁 Anser cygnoides,其中 Anser 是屬名.雁屬的模式種是 Anser anser 灰雁,在中國也有分佈,但不如鴻雁和中國人關係密切.中國人所說的「大雁」一般指鴻雁,偶爾指灰雁或是 ...
- C#调试含有源代码的动态链接库遇见there is no source code available for the current location提示时的解决方案
C#调试含有源代码的动态链接库遇见there is no source code available for the current location提示时的解决方案: 1.首先试最常规的方法:Cle ...