只过了Pretest,可能有误,欢迎批评指正
已AC,请放心食用
题面:https://codeforces.com/contest/1478/problem/C#
题意:
a数组有2n个元素,两两不同,且每个数和它的绝对值同时出现。如[-3,3,-1,1]。
根据公式 d i = ∑ j = 1 2 n ∣ a i − a j ∣ d_{i}=\sum_{j=1}^{2 n}\left|a_{i}-a_{j}\right| di=∑j=12n∣ai−aj∣ 求得d数组。即:d数组中每个元素,为a数组相应元素和其他所有元素绝对值的和。
已知d数组,求是否存在合法的a数组。
思路:
显然,d数组和a数组中,元素的顺序没什么卵用
假设a数组从大到小排好了序,只考虑a数组中的正数(也就是前n个)
容易发现
d i = ∑ j = 1 n m a x ( 2 a i , 2 a j ) d_i= \sum_{j=1}^{n} max(2a_i,2a_j) di=∑j=1nmax(2ai,2aj)
看图!
举个例子
例如a=[6,3,1,-1,-3,-6]
d 1 = 2 ∗ 6 + 2 ∗ 6 + 2 ∗ 6 = 36 d_1=2*6+2*6+2*6=36 d1=2∗6+2∗6+2∗6=36,因为 m a x ( 6 , 6 ) = 6 , m a x ( 6 , 3 ) = 6 , m a x ( 6 , 1 ) = 6 max(6,6)=6, max(6,3)=6, max(6,1)=6 max(6,6)=6,max(6,3)=6,max(6,1)=6
d 2 = 2 ∗ 6 + 2 ∗ 3 + 2 ∗ 3 = 24 d_2=2*6+2*3+2*3=24 d2=2∗6+2∗3+2∗3=24,因为 m a x ( 3 , 6 ) = 6 , m a x ( 3 , 3 ) = 3 , m a x ( 3 , 1 ) = 3 max(3,6)=6, max(3,3)=3, max(3,1)=3 max(3,6)=6,max(3,3)=3,max(3,1)=3
d 3 = 2 ∗ 6 + 2 ∗ 3 + 2 ∗ 1 = 20 d_3=2*6+2*3+2*1=20 d3=