1. 程式人生 > >1052 Tian Ji -- The Horse Racing

1052 Tian Ji -- The Horse Racing

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

#include<stdio.h>

#include<algorithm>

#include<cstring>

using namespace std;

int main()

{

    int n,i,sum=0;

    int a[1008],b[1008];

    while(scanf("%d",&n)!=EOF)

    {

        sum=0;

        if(n==0)

            break;

        for(i=0;i<n;i++)

        {

            scanf("%d",&a[i]);

        }

        for(i=0;i<n;i++)

        {

            scanf("%d",&b[i]);

        }

        sort(a,a+n);

        sort(b,b+n);

        int am=0,bm=0,ak=n-1,bk=n-1;

        while(am<=ak&&bm<=bk)

        {

          if(a[ak]>b[bk]) //田忌的最快與齊王最快比,田忌快則PK掉;  

            {

                sum=sum+200;

                ak--;

                bk--;

            }

            else if(a[ak]<b[bk]) //田忌慢則用田忌最慢的PK齊王最快;  

            {

                sum=sum-200;

                am++;

                bk--;

            }

            else//快馬相等的話,用田忌的最慢與齊王最慢比,田忌快則PK掉,田忌慢則PK齊王最快;

            {

                if(a[am]>b[bm])

                {

                    sum=sum+200;

                    am++;

                    bm++;

                }

                else if(a[am]<b[bm])

                {

                    sum=sum-200;

                    am++;

                    bk--;

                }

                else//最快與最慢都相等,則用田忌最慢PK掉齊王最快;  

                {

                    if(a[am]<b[bk])

                    {

                        sum=sum-200;

                    }

                    am++;

                    bk--;

                }

            }

        }

        printf("%d\n",sum);

    }

    return 0;

}

 附上大神思路:

題意:田忌和齊王各有N匹馬,判斷怎樣比賽,使田忌淨勝場數最多。
我感覺這題的精髓就是,不管怎麼比賽,都要讓田忌的馬發揮最大價值。當然,馬的第一要務是用來贏得比賽,而且要最大效益的贏,也就是要贏對方僅次於自己的馬。當他不能完成這個任務的時候就要去輸,並拉對方最快的馬下水,給自己後面的隊友創造更大的勝利機會。
解題思路:1.若田忌最慢的馬可以戰勝齊王最慢的馬,那麼就讓它戰勝那匹慢馬,勝利場次加1。(田忌最慢馬 > 齊王最慢馬)2.若田忌最慢的馬不能戰勝齊王最慢的馬,那麼它更加不能戰勝其他的馬,那就讓它輸,而且輸給齊王最快馬,失敗場次加1。(田忌最慢馬 < 齊王最快馬)3.若田忌最慢的馬與齊王最慢的馬速度相等。此時,不能簡單地認為與它打成平手就是最好情況,相反,打平是下下策,為什麼呢?因為自己後面的隊友很有可能戰勝此時對方的這匹慢馬,所以就算自己輸一場,隊友也能幫忙贏回一場,而勝一場,輸一場的收益和打平一場的收益是一樣的,而且自己輸的時候可以拉對方最快的馬下水,給己方最快的馬創造更大的勝利機會(因為它失去了一個強勁的對手),也就是說己方最快的馬很可能因為自己的犧牲再勝利一場,從這個角度看,還是自己故意輸掉比較好。
但是,還有一點需要注意,當自己放水前,如果己方最快的馬原本就比對方最快的馬快,然後還輸給對方最快的馬,那麼己方最快的馬的才華就浪費了,為什麼?很簡單,它原本就能贏,需要你放水麼?- -!換句話說,這種情況下,自己的犧牲沒有一點價值。所以,在放水時,一定要保證己方最快馬不快於對方最快馬。滿足此條件後,讓己方最慢馬與對方最快馬去比賽(有可能平局),這樣,田忌的馬就得到了充分的利用。

相關推薦

HDU 1052 Tian Ji -- The Horse Racing

分享圖片 open sil inner sig ber trick red tdi Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276

hdu 1052 Tian Ji -- The Horse Racing (田忌賽馬)

Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 37

HDU 1052 Tian Ji -- The Horse Racing (貪心)(轉載有修改)

Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11

hdu 1052 Tian Ji -- The Horse Racing

題目真的是有點長。 題意: 兩組相同數量的數字,兩兩對比不能重複使用,如果第一組的一個大於第二組的一個則加200分,小於減200分,等於不加不減。 思路: 先將兩組都按照從小到大進行排列,貪心的思路記下來第一組比第二組大的數量。具體就是,當成兩個指標,分別表示當前比較

HDOJ-1052 Tian Ji -- The Horse Racing(貪心演算法)

題目描述:就是田忌賽馬,但是要注意兩邊存在馬速度相同的情況。 分析: (看了各路大佬的題解Orz,自我總結一下) 先排序,這裡從快到慢排序; 1.田忌最快馬>王最快馬(貪心,毫無疑問,進行比賽後獲勝) 2.田忌最快馬<王最快馬(同樣的,既然贏不了

hdu 1052 Tian Ji -- The Horse Racing 可惡的貪心-------也算是經典貪心題吧,對於一般人來說,不看題解,應該很難做出來吧

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 ra

【貪心】HDU 1052 Tian Ji -- The Horse Racing n匹馬的“田忌賽馬”

題意:田忌賽馬。田忌和齊王各有n匹馬,輸入田忌的馬的速度和齊王的馬的速度。每一輪田忌贏了就得200兩銀子,平就得0兩,輸了就失去200兩銀子。問田忌最多能得到多少。題目的策略是貪心,分析見leokan大牛的blog: http://hi.baidu.com/leokan/b

1052 Tian Ji -- The Horse Racing

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 li

LA 3266&HDU 1052 Tian Ji -- The Horse Racing(田忌賽馬,貪心)

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 r

HDU-1052 POJ-2287 Tian Ji -- The Horse Racing(田忌賽馬)

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 ra

poj 2287 Tian Ji -- The Horse Racing

!= space 好的 賽馬 content main 題意 一場 void poj 2287 Tian Ji -- The Horse Racing 題意: 田忌賽馬 假設3匹馬變成1000匹。齊王仍然讓他的馬按從優到劣的順序出賽,田忌能夠按隨意順序選

POJ 2287 - Tian Ji -- The Horse Racing(田忌賽馬) 題解

ons for amp 匹配 void 代碼 div line 比賽 此文為博主原創題解,轉載時請通知博主,並把原文鏈接放在正文醒目位置。 題目鏈接:http://poj.org/problem?id=2287 題目大意: 田忌賽馬的故事就不用多說了吧.... 輸

Tian Ji -- The Horse Racing

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

3266 Tian Ji -- The Horse Racing

題目傳送門 #include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <algorithm> #in

2018北大暑校acm演算法訓練課程 Tian Ji -- The Horse Racing 貪心

總時間限制: 5000ms 記憶體限制: 65536kB 描述 Here is a famous story in Chinese history. That was about 2300 years ago. General Tian Ji was a

POJ 2287 Tian Ji -- The Horse Racing&&浙江科技學院第十三屆程式設計競賽1006 田忌賽馬後傳(貪心)

思路:如果田忌最慢的比齊王最慢的快,或者田忌最快的比齊王最快的快,那麼就比,否則讓田忌最慢的和齊王最快的比。 #include<map> #include<queue> #in

Tian Ji -- The Horse Racing(田忌賽馬模擬)+貪心

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 ra

杭電oj1052題:Tian Ji -- The Horse Racing

題目: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 li

UVa LA 3266 - Tian Ji -- The Horse Racing 貪心,不只處理一端,也處理另一端以理清局面 難度: 2

優先 i++ pen repos repo esp 解決 無法 pos 題目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr

hdu1052Tian Ji -- The Horse Racing(貪心,細節多)

Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 37