AcWing 122 糖果传递
作者:互联网
-
假设第\(1\)个小朋友有\(a_1\)颗糖果,给第\(2\)个小朋友\(x_1\)颗糖果,从\(n\)获得\(x_n\)颗糖果,此时,他有\(a_1-x_1+x_n\)颗糖果,同理,第\(2\)个有\(a_2-x_2+x_1\),第\(3\)有...
-
每个小朋友的目标为平均数\(avg\),列出约束方程为
\[\large \left\{\begin{matrix} a_1-x_1+x_n=avg & \\ a_2-x_2+x_1=avg & \\ a_3-x_3+x_2=avg & \\ ... \\ a_n-x_n+x_{n-1}=avg \end{matrix}\right. \]我们的目标:
\[\large min(|x_1|+|x_2|+...+|x_n|) \]
下面,我们用\(x_n\)来表示上面的方程组:替代\(x_1,x_2,...,x_{n-1}\)
\[\large \left\{\begin{array}{l} x_1=a_1+x_n-avg \\ x_2=a_2+x_1-avg =(a_1+a_2)-2*avg-x_n & \\ x_3=a_3+x_2-avg =(a_1+a_2+a_3)-3*avg-x_n & \\ ... \\ x_{n-1}=(a_1+a_2+...+a_{n-1})-(n-1)*avg-x_n & \\ \end{array}\right. \]将\(x_k\)定为变量 , 常数定义为\(c_k\),则:
\[\large \displaystyle c_k=\sum_{i=1}^{k} -k*avg \]有:
\[\large \left\{\begin{array}{l} x_1=c_1-x_n \\ x_2=c_2-x_n \\ ... \\ x_{n-1}=c_{n-1}-x_n \end{array}\right. \]此时,我们的目标也就转化为:
$$
min(|c_1-x_n|+|c_2-x_n|+...+|c_{n-1}-x_n|)
标签:...,right,large,122,array,avg,糖果,AcWing 来源: https://www.cnblogs.com/littlehb/p/16437042.html