Hi,
I wanted to explain you the method I used to upsolve the problem of [problem:https://codeforces.net/contest/1478/problem/C].
First of all they define a new kind of thing called symmetric array
A[]
of where there aren
distinct positive integers and n additive inverse (multiplied by -1) of same integers. Whenever a problem define a new property or a thing , I find it better to make some specific and general example of it i.eA = {1, 2, 3, 4, -1, -2, -3, -4}
or{a, b, c, d, -a, -b, -c, -d}
Second thing they define are
D[]
where each i-th term is the sum of absolute differences betweena[i]
to all other possiblea[j], j \epsilon [1,n]
. For ex:n = 3, D[0] = |a[0] - a[0]| + |a[0] - (-a[0])| + |a[0] - a[1]| + |a[0] - (-a[1])| + |a[0] - a[2]| + |a[0] - (-a[2])|
Similarly for all other D[1], D[2], D[3], D[4], D[5]. It is easy to see that this D value would be same fora[i]
and-a[i]
.
Question is to tell whether its a valid D[]
or not ?
- I don't like absolute function but it has this property
|negative value| = -1*(negative_value)
(basically distances between values or locations) Lets call itdistance sum
for future reference. so I try to visualize all numbers in sorted order since order ofa[]
doesn't matter I can freely choose to look at them in sorted way (make a 2d graph) so I know which distance is going to have what value as it always equal to (larger value — smaller value) and its easy to easy what is bigger and smaller in a picture.
This is what we are working with in a generic example of n = 4 A[] = {a, b, c, d, -a, -b, -c, -d}
. I assumed a < b < c < d
- Now lets choose the smallest one and start calculating the total distances from
a
to every other number. Now from the picture, it might be easy to see why D (distance sum) value would be same fora[i]
and-a[i]
.
- Here you will see that distance sum for a is 2*(a + b + c + d) and same for -a. I drew the negative terms right below the positive terms for better clarity
- You will notice that distance sum terms are
2 * (a + b + c + d) , 2 * (b + b + c + d), 2 * (c + c + c + d), 2 * (d + d + d + d), 2 * (a + b + c + d) , 2 * (b + b + c + d), 2 * (c + c + c + d), 2 * (d + d + d + d)
They all are even and distinct (if you ignore the distances sum generated by -a[i]
)
looks great to me which means that the largest distance sum is 1. (d * 4) * 2
and in general let's say we sort all distance sum then D[2n] = 2 * n * a[2n] => a[2n] = D[2n] / (2n)
where (D[2n] % (2n))=0
otherwise a[n] will be in floating points which contradicts that a[] are positive distinct integers.
Let's get back to our convenient example of
{a, b, c, d, -a, -b, -c, -d}
and distance sums as above. 2nd largest distance sum is2 * (c + c + c + d)
and we knowd
now so we can easily calculatec = (D[6] - 2 * d) / 2 * (3)
but generallya[2n - 1] = (D[2n - 2] - 2 * a[2n] ) / 2 * (n - 1)
Note check if its perfectly dividing and positive after subtracting.You can get the whole
a[]
back like this. Lastly check that they is no zero created as it also contradicts the properties ofa[]
. If you want to check my code 105777562. Yeah this was a long tutorial but I hope its understandable. If anyone has a suggestion on how to improve the reasoning Please share it. Thank you.