题解 CF939E 【Maximize!】
gapeqi
·
·
题解
贪心好题。
因为这道题比较数学,所以要用数学的方式证一下。
(证明下方都有证明的结论,不想看证明的可以直接往下看)
用到的符号:
A/B:A/B=\{x|x\in A,x \notin B\}
S_{\max}:{\exists} S_{\max}\in S,{\forall} x_0\in S/\{S_{\max}\},x_0\le S_{\max}
\displaystyle S_{len}:S_{len}=\sum^{x\in S}1
\displaystyle S_{sum}:S_{sum}=\sum^{x\in S}x
\displaystyle S_{avg}:S_{avg}=\dfrac{\sum^{x\in S}x}{\sum^{x\in S}1}
猜想一:f(S)=S_{\max}-S_{avg},f(S)>f(S/{S_{\max}})
证:
设 \Delta=S_{\max}-(S/{S_{\max}})_{\max}
{\because}f(S)=S_{\max}-S_{avg}
=\dfrac{S_{len}((S/{S_{\max}})_{\max}+\Delta)-((S/{S_{\max}})_{sum}+\Delta)}{S_{len}}
=\left((S/{S_{\max}})_{\max}-\dfrac{(S/{S_{\max}})_{sum}}{(S/{S_{\max}})_{len}+1}\right )+\dfrac{S_{len}-1}{S_{len}}\times \Delta
{\because}(S/{S_{\max}})_{\max}-\dfrac{(S/{S_{\max}})_{sum}}{(S/{S_{\max}})_{len}+1}\gt f(S/{S_{\max}}),S_{len}\ge 1
{\therefore}f(S)>f(S/{S_{\max}})
结论一:一定要选新加入的数
猜想二:T=S/{S_{\max}},{\forall} x_{0}\in Z,x_{0} \le T_{\max},f(T\cup \{x_{0}\})>f(T)\Rightarrow x_{0}<T_{avg}
证:
{\because}f(T)=T_{\max}-\dfrac{T_{sum}}{T_{len}}
{\because}f(T\cup \{x_{0}\})=T_{\max}-\dfrac{T_{sum}+x_{0}}{T_{len}+1}
{\therefore}f(T\cup \{x_{0}\})>f(T)\Leftrightarrow f(T\cup \{x_{0}\})-f(T)\ge 0
\Leftrightarrow\dfrac{T_{sum}}{T_{len}}-\dfrac{T_{sum}+x_{0}}{T_{len}+1}\ge 0
\Leftrightarrow \dfrac{T_{sum}-T_{len} x_{0}}{T_{len}(T_{len}+1)}>0
\Leftrightarrow x_0<T_{avg}
结论二:一定要选比集合的平均数小的数加进来
这样的话,就可以使用尺取法了,主要代码:
if(ch==1)
{
cnt++;
cin>>a[cnt];
sum+=a[cnt]-maxn;
maxn=a[cnt];
while(poi<cnt&&(poi+1)*a[poi+1]<sum)
{
poi++;
sum+=a[poi];
}
}
else
printf("%.10lf\n",(double)maxn-(double)sum/((double)poi+1.00000));