Gcj -- Minimum scalar product (2008 round1 aa)

Source: Internet
Author: User

Question:

Given the N numbers of the two groups, you can adjust the order of numbers in the same group at will, and find the minimum value of sum Xi * Yi I = 1 .. n.

Small: n <= 8

Abs xy, Yi <= 1000

Large: n <= 800

Abs xi and Yi <= 100000

 

If the sum of the two numbers is fixed, the product of the two numbers is the largest and only the two numbers are as close as possible.

Therefore, intuition tells us that X is sorted in ascending order, and Y is sorted in descending order, and the obtained product is the largest.

Because the order can be adjusted at will, we can fix the order of numbers in X, just thinking about the order of numbers in Y.

Verification:

When N = 2:

If there are only two numbers, x1, x2, Y1, and Y2, it is assumed that the numbers with large numbers are also large.

X1 * y2 + x2 * Y1-(x1 * Y1 + x2 * Y2)

X1 (Y2-Y1) + X2 (y1-y2)

(X1-x2) (y2-y1) <0

Obviously, n = 2 is correct.

When N> 2:

Assume that in the optimal solution, there are ya, Yb, B> A and ya> Yb (not arranged in descending order). Obviously, according to n = 2, their positions are exchanged, you will get a smaller answer.

Therefore, the assumption is correct.

In addition, it is very important to note that in larger data, Xi * Yi may overflow int (10 ^ 10) and data needs to be properly selected.

Gcj -- Minimum scalar product (2008 round1 aa)

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.