NOIP2013 matches the line of the puzzle __ sort

Source: Internet
Author: User
Topic

Topic Description Description
Culvert has two boxes of matches, each containing n matches, and each match has a height. Now each match in each box in one row, the same height of the match is different, the distance between the two matches is defined as Σni=1 (AI−BI) 2 \sigma_{i=1}^{n} (a_i-b_i) ^2, where Ai a_i represents the first row of matches in the height of the I match, bi b_ I represents the height of the first match in the second row of matches.
The position of the two adjacent matches in each match can be exchanged, so that the distance between the two matches is minimized by exchanging. Please get this minimum distance, at least how many times you need to exchange. If this number is too large, output the result of the minimum exchange number of modulo 99,999,997.

input Description Input Description
A total of three rows, the first row contains an integer n, indicating the number of matches in each box.
The second row has n integers, separated by a space between each of the two integers, indicating the height of the first row of matches.
The third row has n integers, separated by a space between each two integers, representing the height of the second row of matches.

Output Description Description
The output is a row, containing an integer that represents the result of the minimum number of exchanges to modulo 99,999,997.

sample Input For example
[Sample 1]
4
2 3 1 4
3 2 1 4
[Sample 2]
4
1 3 4 2
1 7 2 4

Sample Output Sample
[Sample 1]
1
[Sample 2]
2

range of data and hints of the size & Hint
"Sample 1 description"
The minimum distance is 0, at least 1 times, such as swapping the first 2 matches in the 1th column or exchanging the first 2 matches in the 2nd column.
"Sample 2 description"
The minimum distance is 10, the minimum need to exchange 2 times, such as: Swap the 1th column in the middle of the 2 match position, and then swap the 2nd column in the position of 2 matches.
"Data Range"
For 10% of the data, 1≤n≤10;
For 30% of the data, 1≤n≤100;
For 60% of the data, 1≤n≤1,000;
For 100% of the data, 1≤n≤100,000,0≤ match height ≤231 2^{31}-1. Solving

It seems as if you want a and B to be "aligned", that is, a match in the high of a and a match in the B in the same position, proving the use of a sort inequality, as follows:
Note P is an arrangement, then the order of a is AP1,AP2,AP3,..., APN a_{p_1},a_{p_2},a_{p_3},\dots, A_{p_n}, at this point the distance is Σni=1 (api−bi) 2=σni=1 (a2pi+ B2I−2∗API∗BI) =σni=1a

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.