This page is still under construction.

Parts of this page are still being built. What you see may change.

Team Battle

Interview

Time limit1sMemory limit1024 MB

Summary
Given two n-member teams with integer skills, compute the expected value of A's score minus B's score over a uniformly random pairing, where the winner of a match scores the squared skill difference.
Level

Medium6 of 10

Topics
Math, Sorting, Prefix sum, Probability
Solved
No attempts yet

Problem

Two teams A and B, each consisting of n people (1 ≤ n ≤ 50,000), hold a team battle. In a team battle, the n people each compete one-on-one against a member of the opposing team. Each person's opponent is chosen at random, and every possible pairing occurs with equal probability. For example, if the two teams are (A1, A2) and (B1, B2), the possible pairings are (A1:B1, A2:B2) and (A1:B2, A2:B1), and each occurs with probability 50%.

Each person's skill is given as a nonnegative integer. Skill is absolute, so the person with the larger integer always wins. The winner receives points equal to the square of the difference between the two skills, and the loser receives 0 points. If the two skills are equal, the match is a draw and both receive 0 points. A team's score is the sum of the points its members receive.

Given the integers representing the skills of the team members, write a program that computes the expected value (average) of [(A's score)-(B's score)].

For example, suppose team A has skills (3, 7) and team B has skills (1, 5). The possible pairings are (3:1, 7:5) and (3:5, 7:1). In the first, team A receives (3 - 1)2 + (7 - 5)2 = 8 points and team B receives 0, so (A's score) - (B's score) = 8. In the second, team A receives (7 - 1)2 = 36 points and team B receives (5 - 3)2 = 4 points, so (A's score) - (B's score) = 32. The average is therefore (8 + 32) / 2 = 20 points.

Input

The first line contains the integer n. The next n lines contain the integers representing the skills of team A's members, and the following n lines contain the integers representing the skills of team B's members. Each skill is a nonnegative integer no greater than 50,000.

Output

Print the expected value on the first line. An absolute or relative error of up to 10-6 is allowed.

Examples1

  1. Example 1

    Input
    2
    1
    7
    3
    5
    
    Expected output
    0.0000