[Triple] Question

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.