Tian Ji horse racing (greedy)

Tian Ji -- The Horse Racing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34248    Accepted Submission(s): 10372


Problem Description
Here is a famous story in Chinese history.

"That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others."

"Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars from the loser."

"Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian's. As a result, each time the king takes six hundred silver dollars from Tian."

"Tian Ji was not happy about 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 two hundred silver dollars and such a grace in the next match."

"It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king's regular, and his super beat the king's plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China?"



Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed 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 an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are 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 is a too advanced tool to deal with the problem.

In this problem, you are asked to write a program to solve this special case of matching problem.
 

Input
The input consists of up to 50 test cases. Each case starts with a positive integer n (n <= 1000) on the first line, which is the number of horses on each side. The next n integers on the second line are the speeds of Tian’s horses. Then the next n integers on the third line are the speeds of the king’s horses. The input ends with a line that has a single 0 after the last test case.
 

Output
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.
 

Sample Input
 
  
3 92 83 71 95 87 74 2 20 20 20 20 2 20 19 22 18 0
 

Sample Output
 
  
200 0 0
 1. Every time a horse fails, it must reflect the maximum value of its failure, that is, a horse that is sure to lose must compete with the best horse of its King in order to realize the value of its failure! (That is, to strive for a greater chance of winning for the successor horses)
  2. The horses that win each time must reflect the maximization of their victory value, that is, they must win the relatively best horses in the king! (i.e. also give the following horses a greater chance of winning)
  3. Refuse to draw
#include<iostream>
#include<algorithm>
using namespace std;
int tian[1005],king[1005];
intmain()
{
	int n,m,i,minn,maxx,s;
	while(cin>>n&&n)
	{
		for(i=0;i<n;i++) cin>>tian[i];
		for(i=0;i<n;i++) cin>>king[i];
		sort(tian,tian+n);
		sort(king,king+n);
		minn=0; maxx=n-1; s=0;
		for(i=0;i<n;)
		{
			if (tian [i]> king [from]) s ++, from ++, i ++;
			else if(tian[n-1]>king[maxx]) s++,n--,maxx--;
			else
			{
				if(tian[i]<king[maxx]) s--;
				maxx--;//Including the case of a draw
				i++;
			}
		}
		cout<<s*200<<endl;
	}
}

The following content is reproduced:
This is a very interesting greedy question 
. The method of doing this question is like moving some parts of the data structure in class like pointers. . .
1. Compare the frog with Wang's fastest horse (by the way, remember to sort the speed from big to small first),
(1) If the frog's fastest horse is fast, then the silver coin +200, then the frog's fastest (2) If the fastest horse
of the king is faster than the fastest horse of the frog, then keep the fastest horse of the frog, and then take the slowest horse of the frog and the king Compare the slowest horses of , and at the same time, the number of silver coins is -200, then the fastest horse of the frog remains unchanged, the slowest horse++; the fastest horse of the king--. .
2. Compare the slowest horse of the frog with the slowest horse of the king
(1) The slowest horse of the frog is faster than the king's; Tian's slowest horse++, Wang's slowest horse++;
(2) Wang's slowest The horse is faster than the frog; then compare the slowest horse of the frog with the fastest horse of the king; the frog is the slowest horse++, and the king is the fastest horse---.
3. If the fastest and the slowest are the same, then compare the slowest horse of the frog with the fastest horse of the king; the slowest horse of the frog++, the fastest horse of the king--
#include<stdio.h>  
#include<iostream>  
#include<string.h>  
#include<algorithm>  
const int maxn=1005;  
using namespace std;  
  
intmain()  
{  
    int n,i,j,sum;  
    int tian[maxn],king[maxn];  
    while(~scanf("%d",&n)&n)  
    {  
        sum=0;  
        for(i=0; i<n; i++)  
            scanf("%d",&tian[i]);  
        for(i=0; i<n; i++)  
            scanf("%d",&king[i]);  
        sort(tian,tian+n);  
        sort(king,king+n);  
        int t_max,k_max,t_min,k_min;  
        t_max=k_max=n-1;  
        t_min = k_min = 0;  
        while(t_min<=t_max&&k_min<=k_max)  
        {  
            if(tian[t_max]>king[k_max])  
            {  
                sum+=200;  
                t_max--;  
                k_max--;  
            }  
            else if(king[k_max]>tian[t_max])  
            {  
                sum-=200;  
                t_min ++;  
                k_max--;  
            }  
            else  
            {  
                if(tian[t_min]>king[k_min])  
                {  
                    sum+=200;  
                    t_min ++;  
                    k_min ++;  
                }  
                else if(tian[t_min]<king[k_min])  
                {  
                    sum-=200;  
                    t_min ++;  
                    k_max--;  
                }  
                else  
                {  
                    if(tian[t_min] < king[k_max])  
                        sum-=200;  
                    t_min ++;  
                    k_max--;  
                }  
            }  
        }  
        printf("%d\n",sum);  
    }  
    return 0;  
}  



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325942658&siteId=291194637