码迷,mamicode.com
首页 > 编程语言 > 详细

贪心算法之ian Ji -- The Horse Racing

时间:2015-08-15 18:26:06      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:c++   贪心算法   

 

Tian Ji -- The Horse Racing

Description

Here is a famousstory in Chinese history. 

That was about2300 years ago. General Tian Ji was a high official in the country Qi. He likesto 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 horses that ineach class his horse is better than Tian‘s. As a result, each time the kingtakes six hundred silver dollars from Tian. 

Tian Ji was not happy about that, until he met Sun Bin, one of the most famousgenerals in Chinese history. Using a little trick due to Sun, Tian Ji broughthome 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 thesuper class from the king, they will certainly lose that round. But then hisplus beat the king‘s regular, and his super beat the king‘s plus. What a simpletrick. 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 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 maximum weightedperfect 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.

Input

The input consistsof 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. Thenext n integers on the second line are the speeds of Tian‘s horses. Then thenext n integers on the third line are the speeds of the king‘s horses. Theinput ends with a line that has a single `0‘ after the last test case.

Output

For each inputcase, output a line containing a single number, which is the maximum money TianJi 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

 

题目意思:田忌赛马。给出田忌和齐王的马匹数目以及马的速度,找出田忌赢下比赛的最大数目,然后乘上200,就是田忌的最终能赢的钱数。

解题思路:可以采用贪心算法。

贪心策略:能赢就赢一场,不能赢就消耗齐王马匹最快的一匹

比赛思路:

先对田忌和齐王的马匹速度按从小到大排序

先比较田忌和齐王最慢的马匹速度。

a、         如果田忌最慢的马比齐王最慢的马快,则用田忌最慢的马与齐王最慢的马比,赢一场

b、         如果田忌最慢的马比齐王最慢的马的速度慢,则无论怎么样,田忌都会输一场,因此,用田忌最慢的马和齐王最快的马比赛,输一场,消耗齐王最快的马

c、          如果田忌最慢的马与齐王最慢的马速度相等,则分一下两种情况讨论:

C1、如果田忌最快的马比齐王最快的马快,则用田忌最慢的马对上齐王最慢的马,用田忌最快的马对上齐王最快的马,这样能保证一胜一平

C2、如果田忌最快的马不比齐王最快的马快,则用田忌最慢的马和齐王最快的马比,输一场,消耗齐王最快的马

 

以上情况除了C2都好理解,下面详细讲解C2。在C2中,田忌最快的马和齐王最快的马可以分为两种情况,田忌最快的马比齐王最快的马慢和相等。如果田忌最快的马比齐王最快的马慢,显然C2能找到最优解。下面证明剩余的一种情况

证明:田忌最快的马和齐王最快的马相等时拿最慢的马来和齐王最快的马比有最优解。

1)假设他们有n匹马,看n=2的时候.

a1 a2
b1 b2

因为 田忌最快的马和齐王最快的马相等所以a1=b1,a2=b2 所以这种情况有2种比赛方式,易得这两种方式得分相等。

2)当数列a和数列b全部相等等时(a1=b1,a2=b2...an=bn),显然最慢的马来和齐王最快的马比有最优解,可以赢n-1长,输1场,找不到更好的方

法了。
3)当数列a和数列b元素全部相等时(a1=b1=a2=b2...=an=bn),无法赢也不输。

现在假设n匹马时拿最慢的马来和齐王最快的马比有最优解,证明有n+1匹马时拿最慢的马来和齐王最快的马比也有最优解。

数列
a1 a2 a3 a4...an an+1
b1 b2 b3 b4...bn bn+1

其中ai>=ai-1,bi>=bi-1

数列a和数列b不全部相等时,拿最慢的马来和齐王最快的马比数列得到数列
(a1) a2 a3 a4...an an+1
b1 b2 b3 b4...bn (bn+1)

分4种情况讨论 
1.b1=b2,an=an+1
则有
a2 a3 a4...an
b2 b3 b4...bn
其中a2>=a1,a1=b1,b1=b2,得a2>=b2(此后这种大小关系不再论述),an>=bn.
此时若a2=b1,根据归纳假设,有最优解,否则a2>根据前面“公理”论证有最优解。
当且仅当a数列,b数列元素全部相等时有an+1=b1,已证得,所以an+1>b1,赢回最慢的马来和齐王最快的马比输的那一场。

2.b1<=b2,an=an+1
交换 b1,b2的位置,
数列
(a1) a2 a3 a4...an an+1
b2 b1 b3 b4...bn (bn+1)
此时 a2>=a1,an>=bn,
对于子表
a2 a3 a4...an
b1 b3 b4...bn

有最优解。
an+1>=b2, 当且仅当b2=b3=b4=..=bn+1时有an+1=b2,这种情况,a中其它元素<=b1,b2,b3,b4..bn,对于这部分来说,能赢 x盘(x<=n),假如不拿最慢的马来和齐王最快的马比则拿最快的马来和齐王最快的马比,此时平一盘,能赢x-1盘,而拿最慢的马来和齐王最快的马比,输一盘能赢x盘,总的来说,还是X这个数,没有亏。

3.b1=b2,an<=an+1
4.b1<=b2,an<=an+1证明方法类似,不再重复。

以证得当有n+1匹马的时候,田忌和齐王最快最慢的马速度相等时,拿最慢的马来和齐王最快的马比有最优解,已知当n=2时成立,所以对于n>2且为整数时也成立。

 

程序代码为:

// 2287.cpp : 定义控制台应用程序的入口点。

//

 

 

#include<iostream>

#include<algorithm>

using namespacestd;

 

#define maxHourses1005 //最大马匹数目

intTianJihourses[maxHourses];//田忌马匹速度

intKingHourses[maxHourses];//齐王马匹速度

int n;//输入的马匹数目

 

void result();

int main()

{

       cin>>n;

       while(n!=0)

       {

              result();

              cin>>n;

       }

       return 0;

}

 

void result()

{

       for(int i=0;i<n;i++)

              cin>>TianJihourses[i];

       for(int i=0;i<n;i++)

              cin>>KingHourses[i];

       sort(TianJihourses,TianJihourses+n);//排序

       sort(KingHourses,KingHourses+n);//排序

       int loss=0;

       int win=0;

       int tLeft=0,tRight=n-1;

       int kLeft=0,kRight=n-1;

       while(tLeft<=tRight)

       {

              if(TianJihourses[tLeft]>KingHourses[kLeft])

              {

                     //田忌最慢的马比齐王最慢的马快

                     win+=1;

                     tLeft+=1;

                     kLeft+=1;

              }

              elseif(TianJihourses[tLeft]<KingHourses[kLeft])

              {

                     //田忌最慢的马比齐王最慢的马慢

                     loss+=1;

                     tLeft+=1;

                     kRight-=1;//消耗最快的马

              }

              else

              {

                     //田忌最慢的马比齐王最慢的马速度相等

                     if(TianJihourses[tRight]>KingHourses[kRight])

                     {

                            //田忌最快的马速度比齐王最快的马速度快

                            win+=1;

                            tRight-=1;

                            kRight-=1;

                     }

                     else

                     {

                            if(TianJihourses[tLeft]<KingHourses[kRight])

                            {

                                   //田忌最快的马速度比齐王最快的马速度慢

                                   loss+=1;

                            }

                            tLeft+=1;

                            kRight-=1;//消耗最快的马

                     }

              }

       }

       cout<<200*(win-loss)<<endl;

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

贪心算法之ian Ji -- The Horse Racing

标签:c++   贪心算法   

原文地址:http://blog.csdn.net/hqf1992/article/details/47683231

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!