P3628 [APIO2010]特别行动队(简单斜率优化)
发布日期:2021-06-30 10:32:42 浏览次数:2 分类:技术文章

本文共 2011 字,大约阅读时间需要 6 分钟。

题意

n n n名士兵,你需要把士兵分成连续的一段一段特别行动队

设其中一队的战斗力和为 x x x,那么修正战斗力是 a x 2 + b x + c ax^2+bx+c ax2+bx+c(当然 a < 0 a<0 a<0)

求一种分组方式,使得修正战斗力之和最大


转移方程为

f [ i ] = f [ j ] + a ∗ ( s u m i − s u m j ) 2 + b ∗ ( s u m i − s u m j ) + c f[i]=f[j]+a*(sum_i-sum_j)^2+b*(sum_i-sum_j)+c f[i]=f[j]+a(sumisumj)2+b(sumisumj)+c

f [ i ] = f [ j ] + a ∗ s u m i 2 + a ∗ s u m j 2 − 2 a ∗ s u m i ∗ s u m j + b ∗ s u m i − b ∗ s u m j + c f[i]=f[j]+a*sum_i^2+a*sum_j^2-2a*sum_i*sum_j+b*sum_i-b*sum_j+c f[i]=f[j]+asumi2+asumj22asumisumj+bsumibsumj+c

2 a ∗ s u m i ∗ s u m j + f [ i ] − a ∗ s u m i 2 − b ∗ s u m i − c = f [ j ] + a ∗ s u m j 2 − b ∗ s u m j 2a*sum_i*sum_j+f[i]-a*sum_i^2-b*sum_i-c=f[j]+a*sum_j^2-b*sum_j 2asumisumj+f[i]asumi2bsumic=f[j]+asumj2bsumj

2 ∗ a ∗ s u m i 2*a*sum_i 2asumi看作斜率, s u m j sum_j sumj看作自变量 x x x,等式右边看作 y y y

那么就是要最大化截距,维护一个上凸壳

s l o p e ( q h e a d , q h e a d + 1 ) > 2 ∗ a ∗ s u m i slope(q_{head},q_{head+1})>2*a*sum_i slope(qhead,qhead+1)>2asumi时, h e a d + + head++ head++

然后用队头更新 f [ i ] f[i] f[i]

我们加入点 i i i也需要维护上凸壳的斜率单调性

s l o p e ( q t a i l − 1 , q t a i l ) > = s l o p e ( q t a i l , i ) slope(q_{tail-1},q_{tail})>=slope(q_{tail,i}) slope(qtail1,qtail)>=slope(qtail,i)说明在上凸壳上,否则就踢出队列

#include 
using namespace std;#define int long longconst int maxn = 1e6+10;int n,a,b,c,sum[maxn],f[maxn];int head = 1, tail = 0, q[maxn];int x(int i){
return sum[i]; }int y(int i){
return f[i]+a*sum[i]*sum[i]-b*sum[i]; }double slope(int i,int j){
return ( 1.0*y(i)-y(j) )/( 1.0*x(i)-x(j) );}signed main(){
cin >> n >> a >> b >> c ; for(int i=1;i<=n;i++) cin >> sum[i]; for(int i=1;i<=n;i++) sum[i] += sum[i-1]; q[++tail] = 0; for(int i=1;i<=n;i++) {
while( head+1<=tail && slope(q[head],q[head+1])>2*a*sum[i] ) head++; int las = q[head]; f[i] = f[las]+a*sum[i]*sum[i]+a*sum[las]*sum[las]-2*a*sum[i]*sum[las]+b*sum[i]-b*sum[las]+c; while( tail-1>=head && slope(q[tail-1],q[tail])<=slope(q[tail],i) ) tail--; q[++tail] = i; } cout << f[n];}

转载地址:https://issue-is-vegetable.blog.csdn.net/article/details/116272325 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:WQS二分/带权二分/凸优化DP
下一篇:P4360 [CEOI2004]锯木厂选址(枚举+斜率优化)

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月05日 21时50分43秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【深度学习笔记】无参考图像质量评估指标解析及其matlab源码 2019-04-30
【论文阅读手札】MAMNet: Multi-path Adaptive Modulation Network for Image Super-Resolution 2019-04-30
【python学习笔记】获取某个文件夹下文件的总数 2019-04-30
【错误解决】cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\sr 2019-04-30
【python学习笔记】读取指定文件夹中的图片,结合边缘保留滤波EPF 2019-04-30
【工具和环境】Linux下安装pycharm 2019-04-30
【Accumulation】The last two sentences of the abstract 2019-04-30
【Accumulation】The definition of SISR 2019-04-30
【工具与环境】Windows下安装Sublime Text 3 2019-04-30
【解决错误】ValueError: some of the strides of a given numpy array are negative. 2019-04-30
【工具与环境】Excel中批量插入行 2019-04-30
【个人实验注意事项】 2019-04-30
【解决错误】ModuleNotFoundError: No module named ‘tqdm‘ 2019-04-30
【解决错误】ModuleNotFoundError: No module named ‘PIL‘ 2019-04-30
【深度学习笔记】生成requirements.txt文件 2019-04-30
【工具与环境】CSDN今晚十点竟然更换了Logo 2019-04-30
【Python学习笔记】深入剖析随机数种子 2019-04-30
【学习笔记】对vanilla的一些个人理解 2019-04-30
【解决错误】json.decoder.JSONDecodeError: Expecting value: line 11 column 14 (char 82) 2019-04-30
【解决错误】The size of tensor a (8) must match the size of tensor b (64) at non-singleton dimension 1 2019-04-30