Codeforces834D - The Bakery
Description
给出一个\(n(n\leq35000)\)个数的数列\(\{a_i\}\)和\(m(m\leq50)\)。将原数列划分成\(m\)个连续的部分,每个部分的权值等于其中不同的数的个数。求所有划分方案中,所有部分权值和中的最大值。
Solution
线段树优化DP。
记录\(f[k][i]\)表示将前\(i\)个数划分为\(k\)段的最大权值和,\(w(i,j)\)表示\([L,R]\)的权值,那么容易列出转移方程:
考虑一下如何简化$w$。记录$a_x$上一次出现的位置为$pre_x$,则$a_x$为$pre_x+1\leq i \leq x$的$w(i,x)$提供了$1$的贡献。那么我们如果想从$w(i,x-1)$转移到$w(i,x)$,只需对区间$[pre_x+1,x]$加$1$即可。
那么我们要做的就是维护$f[k-1][j]+w(j+1,i)$的区间最值,用线段树即可。第二维由$i$变为$i+1$时,对线段树进行一次区间加即可。
> 时间复杂度$O(nk\cdot logn)$。
##Code
```cpp
//The Bakery
#include <cstdio>
inline char gc()
{
static char now[1<<16],*s,*t;
if(s==t) {t=(s=now)+fread(now,1,1<<16,stdin); if(s==t) return EOF;}
return *s++;
}
inline int read()
{
int x=0; char ch=gc();
while(ch<'0'||'9'<ch) ch=gc();
while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
return x;
}
inline int max(int x,int y) {return x>y?x:y;}
const int N=4e4;
int n,m,a[N];
int pre[N],pre1[N];
#define Ls (p<<1)
#define Rs (p<<1|1)
const int N1=N<<2;
int rt,val[N1]; int add[N1];
void update(int p) {val[p]=max(val[Ls],val[Rs]);}
void addV(int p,int v) {add[p]+=v,val[p]+=v;}
void pushdw(int p) {if(add[p]) addV(Ls,add[p]),addV(Rs,add[p]),add[p]=0;}
int optL,optR;
void ins(int p,int L0,int R0,int v,int type)
{
if(optL<=L0&&R0<=optR)
{
if(type==1) addV(p,v);
else val[p]=v;
return;
}
pushdw(p);
int mid=L0+R0>>1;
if(optL<=mid) ins(Ls,L0,mid,v,type);
if(mid<optR) ins(Rs,mid+1,R0,v,type);
update(p);
}
int query(int p,int L0,int R0)
{
if(optL<=L0&&R0<=optR) return val[p];
pushdw(p);
int mid=L0+R0>>1; int r=0;
if(optL<=mid) r=max(r,query(Ls,L0,mid));
if(mid<optR) r=max(r,query(Rs,mid+1,R0));
return r;
}
int f[N];
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
{
a[i]=read();
pre[i]=pre1[a[i]],pre1[a[i]]=i;
}
rt=1;
for(int k=1;k<=m;k++)
{
for(int i=k;i<=n;i++)
{
optL=pre[i],optR=i-1,ins(rt,0,n,1,1);
optL=0,optR=i-1,f[i]=query(rt,0,n);
}
if(k==m) break;
for(int i=0;i<=n;i++) optL=optR=i,ins(rt,0,n,f[i],2);
}
printf("%d\n",f[n]);
return 0;
}
```\]
Codeforces834D - The Bakery的更多相关文章
- Codeforeces 707B Bakery(BFS)
B. Bakery time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- 信号量和PV操作写出Bakery算法的同步程序
面包店烹制面包及蛋糕,由n个销售员卖出.当有顾客进店购买面包或蛋糕时,应先在取号机上取号,然后等待叫号,若有销售员空闲时便叫下一号,试用信号量和PV操作写出Bakery算法的同步程序. 设计要求 1) ...
- Codeforces 834D The Bakery【dp+线段树维护+lazy】
D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...
- Codeforces 834D The Bakery - 动态规划 - 线段树
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- 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 Round #368 (Div. 2) B. Bakery 水题
B. Bakery 题目连接: http://www.codeforces.com/contest/707/problem/B Description Masha wants to open her ...
- Codeforces 834D - The Bakery(dp+线段树)
834D - The Bakery 思路:dp[i][j]表示到第j个数为止分成i段的最大总和值. dp[i][j]=max{dp[i-1][x]+c(x+1,j)(i-1≤x≤j-1)},c(x+1 ...
- CF833B The Bakery 线段树,DP
CF833B The Bakery LG传送门 线段树优化DP. 其实这是很久以前就应该做了的一道题,由于颓废一直咕在那里,其实还是挺不错的一道题. 先考虑\(O(n^2k)\)做法:设\(f[i][ ...
随机推荐
- Android(java)学习笔记129:对ListView等列表组件中数据进行增、删、改操作
1. ListView介绍 解决大量的相似的数据显示问题 采用了MVC模式: M: model (数据模型) V: view (显示的视图) C: controller 控制器 入门案例: aci ...
- XManager 远程连接Netbackup图形用户界面
XManager远程连接Netbackup图形用户界面 目标: 在自己的Windows桌面打开Linux的Netbackup图形用户界面 工具: Windows: Xmanager,Xshell, ...
- Contour Features 边界特征
查找轮廓 findContours cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]]) → image ...
- groupmod - 修 改 群 组
总览 SYNOPSIS groupmod [-g gid [-o]] [-n group_name ] group 描述 DESCRIPTION groupmod 命 令 会 参 照 你 命 令 列 ...
- xpath定位和css定位对比
xpath定位和css定位对比 实际项目中使用较多的是xpath定位和css定位.XPath是XML文档中查找结点的语法,换句话就是通过元素的路径来查找这个元素.xpath比较强大,而css选择器 ...
- Acronis.Disk.Director磁盘分区管理
Acronis.Disk.Director分为for 专业版和服务器版的,我在生产环境中调整Windows2003跳板机使用的是Acronis.Disk.Director Server 10.0.20 ...
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
XML配置文件代码如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- odoo10 api 装饰器
http://www.cnblogs.com/kfx2007/p/3894297.html one:装饰record-style方法中的self为单一实例,被装饰的方法将会在每一条记录中循环调用,返回 ...
- 头文件string与string.h的区别
在C++中,#include<iostream>与#include<iostream.h>的区别,前者要使用更新的编译器(其实大部分编译器多比较前卫了,出了有些搞嵌入式的用变态 ...
- Spring Boot 应用 快速发布到linux服务器的脚本代码示例
前提说明:spring boot 应用打包成jar包之后要部署到Linux服务器上面运行,我用的nohup java -jar 命令,但是代码更新之后重新部署的时候覆盖原来的项目,又要手动运行ps - ...