贪心法田忌赛马问题Java代码,HDU--1052 Tian Ji -- The Horse Racing田忌赛马(贪心)

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

6fa9aa2f0590001c7074c427f5048537.gif

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 Input3

92 83 71

95 87 74

2

20 20

20 20

2

20 19

22 18

0

Sample Output200

0

0

题意:

“田忌和齐王都有不同级别的三匹马,即一般,较好和超级三种马。规则是在一场比赛中进行三轮比赛:每一匹马必须在一轮比赛中使用。从失败者手中夺走了两百美元。

这题先给定一个n,代表双方各n匹马,然后下一行为田忌马的速度,再下一行为齐王马的速度。

求田忌能赚的最大钱数

思路:

1.如果齐王最快的马比田忌最快的马快,那直接让齐王最快的马和田忌最慢的马比,反正都是输200,何不留下田忌最快和齐王慢的马比呢。

2.如果齐王最快的马比田忌最快的马慢,那就这两匹马直接比了,把齐王最快的马消耗掉。

3. 如果齐王最快的马比田忌最快的马相等,1和2两点比较好想,但是这个情况稍微复杂点。看两组样例:

3

92 83 71

95 92 74

结果: 0

3

92 83 70

92 91 60

结果:200

那就出现的相等的时候比还是不比呢?

那这是我们从后往前分情况讨论

1).如果齐王最慢比田忌最慢的快,反正都是输,那就齐王最快的和田忌最慢的比,让田忌最快的保留下来

2).如果齐王最慢比田忌最慢的慢,那直接这样比了,反正都是赢,留下田忌最快的来。

3).如果齐王最慢比田忌最慢的相等,那就和情况1)一样,那就齐王最快的和田忌最慢的比,最差也是后面能打平。

测试数据:

3

92 83 71

95 92 74

0

4

92 92 83 71

95 92 92 74

0

3

92 83 70

92 91 60

200

#include

using namespace std;

int main(){

int n;

int a[10005],b[10005];

while(cin>>n&&n)

{

for(int i=0;i

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

for(int i=0;i

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

sort(a,a+n);

sort(b,b+n);

int sum=0,l=0,r=n-1;//l为田忌的最慢的马,r为田忌最快的马

for(int i=n-1,j=0;i>=j;)//i为齐王最慢的马,j为齐王最快的马

{

if(b[i]==a[r])//当双方马速度相同时,要分情况,难点

{

if(b[j]>=a[l])//如果齐王最慢和田忌最慢快或者相等,就齐王最快和田忌最慢的比

{

if(b[i]>a[l])//防止齐王最快的和田忌最慢的相等,那是平局,不用-200

sum-=200;

i--,l++;

}

else//如果齐王最慢和田忌最慢的慢,那直接比就行了

{

sum+=200;

j++,l++;

}

}

else if(b[i]>=a[r])//当齐王最快的马比田忌最快的马快,那就直接齐王最快的马比田忌最慢的马

{

sum-=200;

l++,i--;

}

else//当齐王最快的马比田忌最快的马慢,那就直接比,把齐王最快的马耗掉

{

sum+=200;

r--,i--;

}

}

cout<

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值