每周一题3_杭电ACM_Tian Ji -- The Horse Racing
Tian Ji – The Horse Racing
文章目錄
- Tian Ji -- The Horse Racing
- 原題描述
- 測試代碼
- 思路
- 附:
題目來源:杭電ACM 1.3.1
原題描述
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
·
Source
2004 Asia Regional Shanghai
·
Recommend
JGShining
測試代碼
/* 作者:童話 環境:Win 10 & Visual Studio 2019 時間:2021-3-27 */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h>//冒泡排序函數、升序 void BubbleSort(int* src, int len);int main(void) {int n; // 馬的數量int tianhorses[1000]; // 田忌的馬int kinghorses[1000]; // 國王的馬while ((void)scanf("%d", &n), n){//輸入for (int i = 0; i < n; i++){(void)scanf("%d", &tianhorses[i]);}for (int i = 0; i < n; i++){(void)scanf("%d", &kinghorses[i]);}//升序排序BubbleSort(tianhorses, n);BubbleSort(kinghorses, n);//賽馬int win = 0; // 勝場變量int tBeg = 0, tEnd = n - 1; // 田忌、tBeg表示最次的馬、tEnd表示最棒的馬int kBeg = 0, kEnd = n - 1; // 國王、同上while (tBeg <= tEnd){if (tianhorses[tBeg] > kinghorses[kBeg]){/*田忌最次的馬比國王最次的馬快、勝場加一*/win++;tBeg++;kBeg++;}else if (tianhorses[tEnd] > kinghorses[kEnd]){/*田忌最棒的馬比國王最棒的馬快、勝場加一*/win++;tEnd--;kEnd--;}else if (tianhorses[tBeg] < kinghorses[kEnd]){/*以上兩個條件都不滿足若田忌最次的馬比國王最棒的馬,硬上、勝場減一*/win--;tBeg++;kEnd--;}else{/*以上都不滿足,那就最次的馬打個平局*/tBeg++;kBeg++;}}printf("%d\n", win * 200);}return 0; }void BubbleSort(int* src, int len) {for (int i = 0; i < len - 1; i++){for (int j = 0; j < len - 1 - i; j++){if (src[j] > src[j + 1]){int temp = src[j];src[j] = src[j + 1];src[j + 1] = temp;}}} }思路
輸入進來的馬的速度需要先排序!
先比較最次的馬:
-
如果田忌能贏,那就直接跟國王比,勝場加一
-
如果田忌不能贏,再看最棒的馬
-
如果最棒的馬田忌能贏,也是直接跟國王比,勝場加一
-
如果不能贏,就用最次的馬跟國王最棒的比(反正都是輸),勝場減一
-
如果最次的、最棒的都相同,最次的馬比一場,不影響勝場
-
附:
附上之前寫的一種代碼,自己試了幾組數據沒問題,但是系統沒讓我過。。
/* 作者:童話 環境:Win 10 & Visual Studio 2019 時間:2021-3-27 */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h>//降序排序函數 void ArraySort(int* src, int len);int main(void) {int n; // the number of horses on each sideint tianhorses[1000]; // the speeds of Tian’s horsesint kinghorses[1000]; // the speeds of the king’s horseswhile (((void)scanf("%d", &n)), n){//輸入馬的速度for (int i = 0; i < n; i++){(void)scanf("%d", &tianhorses[i]);}for (int i = 0; i < n; i++){(void)scanf("%d", &kinghorses[i]);}//數組由大到小排序ArraySort(tianhorses, n);ArraySort(kinghorses, n);int win = 0, lose = 0; // 勝負場變量int tBegin = 0, tEnd = n - 1; // 田忌的馬的下標,Begin為上等馬int kBegin = 0, kEnd = n - 1; // 國王的馬的下標,Begin為上等馬//循環耗盡田忌的馬while (tBegin <= tEnd){if (tianhorses[tBegin] > kinghorses[kBegin]){/*田忌的上等馬 快于 國王的上等馬那就直接用田忌的上等馬和國王的上等馬比賽結果就是:勝場加一、兩人均損失一匹上等馬*/win++;tBegin++;kBegin++;}else if (tianhorses[tBegin] < kinghorses[kBegin]){/*田忌的上等馬 慢于 國王的上等馬可以用田忌的下等馬和國王的上等馬比賽結果就是:敗場加一、田忌損失一匹下等馬、國王損失一匹上等馬*/lose++;tEnd--;kBegin++;}else{/*田忌的上等馬 和 國王的上等馬一樣快現在看下等馬的情況*/if (tianhorses[tEnd] > kinghorses[kEnd]){/*田忌的下等馬 快于 國王的下等馬那就直接用田忌的下等馬和國王的下等馬比賽結果就是:勝場加一、田忌損失一匹下等馬、國王損失一匹下等馬*/win++;tEnd--;kEnd--;}else{if (tianhorses[tEnd] < kinghorses[kEnd]){/*田忌的下等馬 慢于 國王的下等馬可以用田忌的下等馬和國王的上等馬比賽,反正都是輸結果就是:敗場加一、田忌損失一匹下等馬、國王損失一匹上等馬*/lose++;tEnd--;kBegin++;}if (tianhorses[tEnd] == kinghorses[kEnd]){/*田忌的最上等的馬和最下等的馬都跟國王的一樣快無勝負,只損失馬匹*/tBegin++;kBegin++;tEnd--;kEnd--;}}}}printf("%d\n", (win - lose) * 200);}return 0; }void ArraySort(int* src, int len) {/*排序函數,從大到小*/int i, j, temp;for (i = 0; i < len - 1; i++){for (j = i + 1; j < len; j++){if (src[i] < src[j]){temp = src[i];src[i] = src[j];src[j] = temp;}}} }初步分析,應該是我第二種方法數組排序反了,應該是先比較最次的馬的
而且當田忌最次的馬比不過國王最次的馬的時候,應該是先比較最棒的馬。
等最棒的馬比較完了,最棒的馬假如比的贏國王,那
數組下標變化之后,原來次棒的馬就是最棒的馬了,
這個時候如果最棒的馬比不過就用最次的馬去消耗國王。
大概就是這樣。
總結
以上是生活随笔為你收集整理的每周一题3_杭电ACM_Tian Ji -- The Horse Racing的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java事物传播行为,Spring事务传
- 下一篇: nginx配置-优化静态资源