传送门
这个什么鬼证明直接看uoj的题解吧根本不会证明
首先方案一定是若干段等值的\(B\),然后对于一段,\(B\)的值应该是\(A\)的平均值.这个最优方案是可以线性构造的,也就是维护以区间平均值为权值的单调栈,每次在后面插入一个元素,不断弹栈并与最后一个合并,直到平均值单调递增
然后这个单调栈是可以两个区间的单调栈直接合并的,因为合并完后新单调栈的断点集合是原来两段的断点集合的子集.合并直接暴力就好了合并的话一定是前面那个的一段后缀的后面的一段前缀合并,然后后面的前缀位置(就是合并区间的右端点)是可以二分的,然后二分左端点使得合并的区间平均值最大,然后如果这个平均值比右端点右边的那一块的平均值小,那么最优的右端点就在在这个右端点以及其左边,否则在右边
然后用个可持久化线段树预处理一下每个前缀和后缀的单调栈就行了,注意从后往前和从前往后做都是一样的,能得到最优解
以上都是蒯的题解qwq,感性理解一下?
// luogu-judger-enable-o2
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include