#P1650. Tian Ji's Horse Racing Strategy
Tian Ji's Horse Racing Strategy
Tian Ji's Horse Racing Strategy
In ancient China there is a famous story. Over 2300 years ago, General Tian Ji of the Qi state loved horse racing and often raced against the Qi king. Both had three horses: a regular, an upper-grade, and a superb horse. In a series of three races, the winner of each race would seize 200 coins from the loser. Because the Qi king's horses were slightly superior to Tian Ji's at the same levels, if they raced each horse in the corresponding order, Tian Ji would lose 600 coins overall.
Tian Ji's fortunes changed when he met the famous strategist Sun Bin. Sun Bin advised him to race his horses in a different order: use his regular horse against the Qi king's superb horse, his superb horse against the Qi king's upper-grade horse, and his upper-grade horse against the Qi king's regular horse. This strategy secured a 2-1 victory, netting Tian Ji 200 coins.
This problem extends the idea to a general situation where more than three horses are involved. Given the speeds of Tian Ji's horses and the Qi king's horses, you are to determine the maximum net coin gain (or loss) Tian Ji can achieve by optimally matching his horses against the opponent's. Winning a race yields +200 coins, a tie yields 0 coins, and losing costs 200 coins.
Note: When directly matching the horses in order, Tian Ji might lose overall. However, by reordering the races optimally, it is possible to improve the outcome. This problem can be modeled as a special bipartite matching problem. Although sophisticated algorithms (like min cost max flow) exist, a simple greedy algorithm is sufficient for this task.
Mathematical Formulation:
For each pair of horses:
If T_i > Q_j then gain = +200
If T_i = Q_j then gain = 0
If T_i < Q_j then gain = -200
The goal is to maximize the total gain.
inputFormat
The input consists of three lines:
- The first line contains a positive integer n (number of horses for each side).
- The second line contains n space-separated integers representing the speeds of Tian Ji's horses.
- The third line contains n space-separated integers representing the speeds of the Qi king's horses.
You may assume that 1 ≤ n ≤ 1000 and each speed is a nonnegative integer not greater than 10000.
outputFormat
Output a single integer which is the maximum coin difference (in coins) that Tian Ji can achieve by optimally matching his horses against the Qi king's horses. A positive number indicates a net gain, while a negative number indicates a net loss.
sample
3
92 83 71
95 87 74
200