codeforces833B The Bakery
题目大意:将一个长度为n的序列分为k段,使得总价值最大,一段区间的价值表示为区间内不同数字的个数
思路:
显然的dp。
先想到一个朴素的状态转移方程 $dp[i][k]=max(dp[j][k-1]+val[j+1][i])$,$0<=j<i$ $dp[i][k]$表示到第i为,截取了k段的最大价值,val表示某一段区间的价值。
这样的时间复杂度是$n*n*k$,显然是不能接受的,这里面的一个n和一个k显然是不能优化的,那我们只需要把一个n优化成logn或者线性的就可以接受了,所以想到线段树优化。
那么我们就要维护$dp[j][k-1]+val[j+1][i]$这个的最大值,要怎么维护呢,当我们扫到一个数字x,这个数字会对哪些val造成影响呢?显然是前一个x后面的区间。加一就好了。
然后每次把$dp[i][k-1]$放入线段树,用滚动数组来优化空间。
#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
#define fpn() freopen("simple.in","r",stdin)
#define rd read()
using namespace std;
const int maxn=;
typedef long long ll;
int n,k,dp[][maxn],x,le[maxn],pos[maxn],sum[maxn<<],lazy[maxn<<];
void pushup(int o){
sum[o]=max(sum[o<<],sum[o<<|]);
}
void pushdown(int o,int l,int r){
int mid=(l+r)>>;
if(l==r)return;
if(lazy[o]){
sum[o<<]+=lazy[o];
sum[o<<|]+=lazy[o];
lazy[o<<]+=lazy[o];
lazy[o<<|]+=lazy[o];
lazy[o]=;
}
}
void update(int o,int l,int r,int ql,int qr,int val){
if(ql<=l&&r<=qr){
sum[o]+=val;
lazy[o]+=val;
return ;
}
int mid=(l+r)>>;
pushdown(o,l,r);
if(ql<=mid)update(o<<,l,mid,ql,qr,val);
if(qr>mid)update(o<<|,mid+,r,ql,qr,val);
pushup(o);
}
int query(int o,int l,int r,int ql,int qr){ if(ql<=l&&r<=qr){
return sum[o];
}
int mid=(l+r)>>;
pushdown(o,l,r);
int res=;
if(ql<=mid)res=query(o<<,l,mid,ql,qr);
if(qr>mid)res=max(res,query(o<<|,mid+,r,ql,qr));
return res;
}
int main(){
while(cin>>n>>k)
{
for(int i=;i<=n;i++)
{
scanf("%d",&x);
le[i]=pos[x];
pos[x]=i;
}
int p=;
for(int s=;s<=k;s++,p^=)
{
clr(sum,),clr(lazy,);
for(int i=;i<=n;i++)
{
update(,,n,le[i],i-,);
dp[p][i]=query(,,n,,n);
update(,,n,i,i,dp[p^][i]);
}
} printf("%d\n",dp[p^][n]);
}
}
codeforces833B The Bakery的更多相关文章
- 2019.03.09 codeforces833B. The Bakery(线段树优化dp)
传送门 线段树优化dpdpdp入门题. 要求把nnn个数分成kkk段,每段价值为里面不相同的数的个数,求所有段的价值之和最大值.n≤35000,k≤50n\le35000,k\le50n≤35000, ...
- Noip前的大抱佛脚----赛前任务
赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...
- 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 ...
随机推荐
- Win10 Tensorflow 配置Mask_RCNN
1.安装Anaconda3 下载地址 Anaconda 官网下载地址:https://www.continuum.io/downloads 下载以后,点击exe程序,开始安装,详细的安装过程(图片参 ...
- grid search 超参数寻优
http://scikit-learn.org/stable/modules/grid_search.html 1. 超参数寻优方法 gridsearchCV 和 RandomizedSearchC ...
- setnx()
setnx(key,value):当指定的key不存在时,为你设置指定的值
- Codeforces 429B B. Working out
题目意思: 给n*m的矩阵,每个格子有个数,A从(1,1)出发只能向下或右走,终点为(n,m),B从(n,1)出发只能向上或右走,终点为(1,m).两个人的速度不一样,走到的格子可以获的该格子的数,两 ...
- Reportng 的测试报告在 Jenkins 中显示不全
通过Jenkins执行接口测试生成测试报告,用Jenkins的web服务打开html显示不全. 环境: Jenkins版本:1.651.2 Jenkins发布在mac上面 使用的是testng管理测试 ...
- 设计模式11: Flyweight 享元模式(结构型模式)
Flyweight 享元模式(结构型模式) 面向对象的代价 面向对象很好的解决了系统抽象性的问题,同时在大多数情况下也不会损及系统的性能.但是,在某些特殊应用中,由于对象的数量太大,采用面向对象会给系 ...
- DataType--数值类型
SQL Server数值类型可以分为精确数字类型和近似数字类型精确数字BIT/SMALLINT/TINYINT/INT/BIGINTNUMERIC/DECIMAL/SMALLMONEY/MONEY 近 ...
- C# return、continue、break
return 终止当前进程 可用循环判断,验证,等功能 if (ew == v) { PublicControlLib.Class.PublicProperties.ShowSuccess(); re ...
- C#中return的两个作用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- WPF MultiSelect模式下ListBox 实现多个ListBoxItem拖拽
WPF 的ListBox不支持很多常见的用户习惯,如在Explorer中用鼠标可以选择多项Item,并且点击已经选择的Item,按住鼠标左键可以将所有已选择Item拖拽到指定的位置.本文简单的实现了这 ...