Tian Ji--The Horse Racing
Problem Descriptionhere is a famous stories in Chinese.
"That's about 2300 years ago." General Tian Ji is a high official in the country Qi. He likes to play horse racing with the king and others. "
"Both of Tian and the king had three horses in different classes, namely, regular, plus, and super. The rule is to has three rounds in a match; Each of the horses must is used in one round. The winner of a single round takes and hundred silver dollars from the loser. "
"The Being the most powerful man in the country, the king had so nice horses, and each class he horse is better than Tian ' S. As a result, each time the king takes six hundred silver dollars from Tian. "
"Tian Ji is not happy on that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home of hundred silver dollars and such a grace in the next match. "
"It is a rather simple trick. Using the He regular class horse race against the Super class from the king, they would certainly lose that round. But then he plus beat the King ' s regular, and his super beat the King's Plus. What's a simple trick. And how does your think of Tian Ji, the high ranked official in China? "
Were Tian Ji lives in nowadays, he'll certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he could discover that the horse racing problem can be simply Viewe D as finding the maximum matching in a bipartite graph. Draw Tian ' s horses on one side, and the king's horses on the other. Whenever one of Tian ' s horses can beat one from the king, we draw a edge between them, meaning we wish to establish this Pair. Then, the problem of winning as many rounds as possible are just to find the maximum matching in this graph. If There is ties, the problem becomes more complicated, he needs to assign weights 0, 1, or-1 to all the possible edges, And find a maximum weighted perfect matching ...
However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses---a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm are a too advanced tool to deal with the problem.
In this problem, you is asked to the write a program to solve this special case of matching problem.
Inputthe input consists of up to test cases. Each case starts with a positive an integer n (n <=) on the first line, which are the number of horses on each side. The next n integers on the second line is the speeds of Tian ' s horses. Then the next n integers in the third line is the speeds of the King ' s horses. The input ends with a line is has a single 0 after the last test case.
Outputfor each input case, output a line containing a single number, which are the maximum money Tian Ji'll get, in Silve R dollars.
Sample Input
392 83 7195 87 74220 2020 20220 1922 180
Sample Output
20000
The story of Tian Bogey horse racing. Greed, strategy:
1. Tian bogey fastest than the king fastest faster, than!
2. Tian bogey fastest than the king of the fastest slow, with Tian bogey the slowest with the king of the fastest ratio!
3. Tian Bogey the fastest and the king the fastest equal:
1). Tian bogey slowest than the king the slowest faster, than!
2). Tian bogey slowest than the king of the slowest fast, with Tian bogey the slowest with the King the fastest ratio!
The code is as follows:
#include <cstdio> #include <algorithm>using namespace Std;int main () {int n; while (scanf ("%d", &n) ==1&&n) {int t[1010],k[1010]; int t_low=0,k_low=0,t_fast=n-1,k_fast=n-1; for (int i=0;i<n;i++) scanf ("%d", &t[i]); for (int i=0;i<n;i++) scanf ("%d", &k[i]); Sort (t,t+n); Sort (k,k+n); int sum=0; while (T_low<=t_fast) {if (T[t_fast]>k[k_fast]) {//fastest faster than the King sum++; t_fast--; k_fast--; } else if (T[t_fast]<k[k_fast]) {//the fastest slower sum--than the king; t_low++; k_fast--; } else{//fastest as Fast if (T[t_low]<k[k_low]) {//slowest than King slowest slow sum--; t_low++; k_fast--; } else if (T[t_low]>k[k_low]) {//slowest than the king's slowest fast sum++; t_low++; k_low++; } else{//slowest as fast, with the slowest with the king fastest than if (T[t_low]<k[k_fast]) sum--; t_low++; k_fast--; }}} printf ("%d\n", sum*200); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1052.Tian Ji--The Horse Racing "very good greedy" "August 27"