日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

F - Tian Ji -- The Horse Racing

發布時間:2023/12/20 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 F - 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 thecountry 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 ofthe horses must be used in one round. The winner of a single round takes twohundred silver dollars from the loser."?

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

"Tian Ji was not happy about that, until he met Sun Bin, one of the mostfamous generals in Chinese history. Using a little trick due to Sun, Tian Jibrought home two hundred silver dollars and such a grace in the nextmatch."?

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

?

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 horseracing problem can be simply viewed as finding the maximum matching in abipartite graph. Draw Tian's horses on one side, and the king's horses on theother. Whenever one of Tian's horses can beat one from the king, we draw anedge between them, meaning we wish to establish this pair. Then, the problem ofwinning as many rounds as possible is just to find the maximum matching in thisgraph. If there are ties, the problem becomes more complicated, he needs toassign weights 0, 1, or -1 to all the possible edges, and find a maximumweighted 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 speedalways beat a vertex of lower speed. In this case, the weighted bipartitematching 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 ofmatching problem.?

---------------------------------------------------------------------------------------------------------------

輸入如下:

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

---------------------------------------------------------------------------------------------------------------

輸出如下:

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

---------------------------------------------------------------------------------------------------------------

測試輸入如下:

3

92 83 71

95 87 74

2

20 20

20 20

2

20 19

22 18

0

---------------------------------------------------------------------------------------------------------------

測試輸出如下

200

0

0

?---------------------------------------------------------------------------------------------------------------

思路就是我的那篇”田忌賽馬“,有興趣的可以看下,找一下就好了。如果只要代碼的話,那就算了吧!

---------------------------------------------------------------------------------------------------------------

代碼如下:

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std;int main() {int a[1000];int n;int b[1000]; int i,s,j,e,sum;while(scanf("%d",&n)!=EOF){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);i=s=0;j=e=n-1;sum=0;while(i<=j){if(a[i]>b[s]) {sum++;s++;i++;}else if(a[i]<b[s]){sum--;e--;i++;}else{if(a[j]>b[e]) {sum++;e--;j--;}else{if(a[i]<b[e])sum--;e--;i++;}}}printf("%d\n",sum*200);}return 0; } ---------------------------------------------------------------------------------------------------------------

總結

以上是生活随笔為你收集整理的F - Tian Ji -- The Horse Racing的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。