HDOJ Tian Ji Horse Racing--Greedy Algorithm

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.

how are you

This greedy algorithm is really difficult to think about. You can refer to the big cow on poj http://poj.org/showmessage?message_id=164719, the excerpt is as follows:

The essence of greed is that Tian only takes out his fast horses and Wang fights when he is sure to win, otherwise he will use the slowest horse to defeat the king's fast horse to the greatest extent possible to weaken the king's combat effectiveness.

*
Greedy strategy:
1. If Tian Ji's fastest horse is faster than Qi Wang's fastest horse, then compare the two.
(Because if Tian Ji's other horses are likely to win, so the two are compared)
2. If Tian Ji's fastest horse is slower than King Qi's fastest horse, use the ratio of Tian Ji's slowest horse and King Qi's fastest horse.
(Because all the horses can't win the fastest horse of King Qi, so use the one with the least loss and compare with him the slowest)
3. If they are equal, compare the slowest horse of Tian Ji and the slowest horse of King Qi.
3.1, if Tian Ji's slowest horse is faster than Qi's slowest horse, the ratio of the two.
(If Tian Ji's slowest horse can win one, he will win, and King Qi's slowest horse must also have a competition with him, so it is faster to choose the smallest horse than him.)
3.2, and others, compare Tian Ji's slowest horse with Qi Wang's fastest horse.
(Anyway, all the horses are faster than Tian Ji's slowest horse, so this horse will lose, choose the fastest horse that contributes the most and kill King Qi)

#include <iostream> #include<algorithm> #include<string.h> #include<stdio.h> using namespace std; intmain() {     int n;     while(cin>>n&&n!=0)     {         int tj[1001],king[1001],count=0;         int tj_min=0,tj_max=n-1;         int king_min=0,king_max=n-1;         for(int i=0; i<n; i++)             cin>>tj[i];         for(int i=0; i<n; i++)             cin>>king[i];         sort(tj,tj+n);         sort(king,king+n);         while(n--)         {             if(tj[tj_max]>king[king_max])             {                 count++;                 tj_max--;                 king_max--;             }             else if(tj[tj_max]<king[king_max])             {                 count--;                 tj_min ++;                 king_max--;             }             else             {                 if(tj[tj_min]>king[king_min])                 {                     count++;                     tj_min ++;                     king_min ++;                 }                 else                 {                     if(tj[tj_min]<king[king_max])                     count--;                     tj_min ++;                     king_max--;                 }             }

        }          cout<<count*200<<endl;

    }           return 0;

}

Haha, Tian Ji understands the idea of ​​horse racinglaughing out loud

Guess you like

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