Description:
Returns n tuples, such as (I, j, k ).
Defines the difference between a set of productkey, devicename, and devicesecret as follows:
D (TA, Tb) = max {Ia −ib, ja −jb, Ka −kb} −min {Ia −ib, ja −jb, Ka −kb}
Calculate the sum of all the differences between the three tuples (even if D (TA, Tb) is used, you do not need to calculate d (TB, Ta)
Data range:
N <= 500000
Time Limit: 1 s
[Question]
First, for any triple (A, B, C), we have max (a, B, c)-min (A, B, C) = (| a-B | + | a-c | + | B-c |)/2
Then, D (TA, Tb) = (| a-B | + | a-c | + | B-c |)/2, where A = Ia −ib, B = JA −jb, c = Ka −kb.
Take | a-B | as an example. A-B = (IA −ib)-(JA −jb) = (IA-ja)-(JA-JB ).
If X = Ia-ja, y = JA-ka, Z = ka-ia, then: D (TA, Tb) = (| x1-x2 | + | y1-y2 | + | Z1-Z2 |)/2
Consider X, Y, Z respectively. For a Xi, if he is larger than XJ, his contribution to the final difference is Xi, otherwise it is-xi.
Here,AlgorithmObviously, x, y, and z are sorted in ascending order. For each Xi, his contribution to the final answer is: XI * (I-(N-I + 1), Yi and Zi are the same. because every pair is counted twice, the answer should be divided by 2.
BecauseCodeVery NC, made three fast lines, the code is ugly. so I will not post code. in fact, because the operations on the three arrays are the same, you can simply use a pointer.
By QW
Reprinted please indicate the source