Tian Ji -- The Horse Racing

花了一天时间写出两种做法的题目

尽管自己没想到枚举法居然真的能做,感谢老爸提醒,嘿嘿嘿。
题目:
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?”

这里有一个中国历史上著名的故事。

“那是2300年前的事了。田忌将军是齐国的高官。他喜欢和国王以及其他人一起赛马。”

“天王两人都有三匹不同等级的马,分别是普通马、加号马和超级马。规则是一场比赛分三轮,每匹马必须用一轮。一轮胜出者从失败者手中夺走两百银元。”

“作为全国最有权势的人,国王的马非常好,每一级的马都比田的好,因此,国王每次从田忌那里拿走六百银元。”

“田忌对此并不高兴,直到他遇到了中国历史上最著名的将军之一孙膑。由于孙膑的一个小把戏,田忌在下一场比赛中赢了两百块银元和一个这样的恩典。”

“这是一个相当简单的把戏。用他平时的班马与国王的超级班马比赛,他们肯定会输掉那一轮。但是他的加号打败了国王的常规赛,他的超级赛打败了国王的加号赛。多简单的把戏啊。你觉得中国的高官田吉怎么样?”

If 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.

如果田忌生活在现在,他一定会嘲笑自己。更重要的是,如果他现在参加ACM竞赛,他可能会发现赛马问题可以简单地看作是在一个二分图中找到最大匹配。一边画田的马,一边画王的马。只要田忌的一匹马能打败国王的一匹马,我们就在它们之间划一条边,这意味着我们希望建立一对。那么,赢得尽可能多轮的问题就是在这个图中找到最大匹配。如果有联系,问题会变得更复杂,他需要给所有可能的边分配0、1或-1的权重,并找到一个最大权重的完美匹配。。。

然而,赛马问题是一个非常特殊的情况下,双方匹配。图是由马的速度决定的——速度越快的顶点总是比速度越慢的顶点强。在这种情况下,加权二部匹配算法是一个太先进的工具来处理这个问题。

在这个问题中,要求您编写一个程序来解决这个特殊情况下的匹配问题。

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.

输入由多达50个测试用例组成。每种情况都以第一行的正整数n(n<=1000)开始,这是每边的马数。第二行接下来的n个整数是田忌的马的速度。接下来第三行的n个整数是国王的马的速度。输入以最后一个测试用例后有一个0的行结束

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 <cstdio>
#include <algorithm>
#include <<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值