杭電acm 1052 田忌賽馬

Tian Ji -- The Horse Racing

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

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?"
?


image

?
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

Solution

田忌賽馬,首先用田忌最差的馬和國王最差的馬比較贏或輸都賽一場,若平局則用田忌最好的馬與國王最好的馬比若贏則賽一場若輸則用田忌最差的馬與國王最好的賽一場往踢,以這個順序依次較量构诚,得到最好的結(jié)果

Code

/**
 * date:2017.11.15
 * author:孟小德
 * function:杭電acm1052
 *  田忌賽馬
 */
 
 
 
import java.util.*;
 
public class acm1052
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
 
        int num;
 
        while((num = input.nextInt()) != 0)
        {
            int[] tian = new int[num];
            int[] king = new int[num];
 
            for (int i=0;i<num;i++)
            {
                tian[i] = input.nextInt();
            }
 
            for (int i=0;i<num;i++)
            {
                king[i] = input.nextInt();
            }
 
            Arrays.sort(tian);
            Arrays.sort(king);
 
            int win = 0;
            int tian_h = tian.length-1;
            int tian_l = 0;
            int king_h = king.length-1;
            int king_l = 0;
 
            while (tian_l <= tian_h)
            {
                if (tian[tian_l] > king[king_l])
                {
                    win ++;
                    tian_l++;
                    king_l++;
                }
                else if (tian[tian_l] < king[king_l])
                {
                    win--;
                    tian_l++;
                    king_h--;
                }
                else
                {
                    if (tian[tian_h] > king[king_h])
                    {
                        win++;
                        tian_h--;
                        king_h--;
                    }
                    else
                    {
                        if (tian[tian_l] < king[king_h])
                        {
                            win--;
                        }
                        tian_l++;
                        king_h--;
                    }
                }
            }
 
            System.out.println(win * 200);
 
        }
    }
 
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市呵俏,隨后出現(xiàn)的幾起案子堆缘,更是在濱河造成了極大的恐慌,老刑警劉巖普碎,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件吼肥,死亡現(xiàn)場離奇詭異,居然都是意外死亡麻车,警方通過查閱死者的電腦和手機(jī)缀皱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來动猬,“玉大人啤斗,你說我怎么就攤上這事≡娌欤” “怎么了争占?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵燃逻,是天一觀的道長。 經(jīng)常有香客問我臂痕,道長伯襟,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任握童,我火速辦了婚禮姆怪,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘澡绩。我一直安慰自己稽揭,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布肥卡。 她就那樣靜靜地躺著溪掀,像睡著了一般。 火紅的嫁衣襯著肌膚如雪步鉴。 梳的紋絲不亂的頭發(fā)上揪胃,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天,我揣著相機(jī)與錄音氛琢,去河邊找鬼喊递。 笑死,一個胖子當(dāng)著我的面吹牛阳似,可吹牛的內(nèi)容都是我干的骚勘。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼撮奏,長吁一口氣:“原來是場噩夢啊……” “哼俏讹!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起挽荡,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤藐石,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后定拟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體于微,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年青自,在試婚紗的時候發(fā)現(xiàn)自己被綠了株依。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡延窜,死狀恐怖恋腕,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情逆瑞,我是刑警寧澤荠藤,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布伙单,位于F島的核電站,受9級特大地震影響哈肖,放射性物質(zhì)發(fā)生泄漏吻育。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一淤井、第九天 我趴在偏房一處隱蔽的房頂上張望布疼。 院中可真熱鬧,春花似錦币狠、人聲如沸游两。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽贱案。三九已至,卻和暖如春渐行,著一層夾襖步出監(jiān)牢的瞬間轰坊,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工祟印, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人粟害。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓蕴忆,卻偏偏與公主長得像,于是被迫代替她去往敵國和親悲幅。 傳聞我的和親對象是個殘疾皇子套鹅,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容