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

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;

}

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

时间: 2024-10-13 11:42:42

贪心算法之ian Ji -- The Horse Racing的相关文章

贪心/hdu 1052 Tian Ji -- The Horse Racing

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int n; 6 int tian[1010],king[1010]; 7 bool cmp(int x,int y) 8 { 9 return x>y; 10 } 11 int main() 12 { 13 scanf("%d",&n); 14 while (n!=

HDU 1052 Tian Ji -- The Horse Racing【贪心在动态规划中的运用】

算法分析: 这个问题很显然可以转化成一个二分图最佳匹配的问题.把田忌的马放左边,把齐王的马放右边.田忌的马A和齐王的B之间,如果田忌的马胜,则连一条权为200的边:如果平局,则连一条权为0的边:如果输,则连一条权为-200的边. 然而我们知道,二分图的最佳匹配算法的复杂度很高,无法满足N=2000的要求. 我们不妨用贪心思想来分析一下问题.因为田忌掌握有比赛的“主动权”,他总是根据齐王所出的马来分配自己的马,所以这里不妨认为齐王的出马顺序是按马的速度从高到低出的.由这样的假设,我们归纳出如下贪心

HDU1052 Tian Ji -- The Horse Racing 贪心

Tian Ji -- The Horse Racing Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1052 Description Here is a famous story in Chinese history. "That was about 2300 years ago. General Tian Ji was a

ACM--田忌赛马--贪心--HDOJ 1052--Tian Ji -- The Horse Racing

HDOJ题目地址:传送门 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25520    Accepted Submission(s): 7506 Problem Description Here is a famous story in Chinese history. "Th

HDU-1052-Tian Ji -- The Horse Racing(C++ &amp;&amp; 简单贪心)

Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21782    Accepted Submission(s): 6374 Problem Description Here is a famous story in Chinese history. "That was about

hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】

思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較,二等于在推断田的最慢的是不是比king的最快的慢,三小于则与king的最快的比較: Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe

Tian Ji -- The Horse Racing(杭电1052)(贪心)

Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18925    Accepted Submission(s): 5530 Problem Description Here is a famous story in Chinese history. "That was about

HDU 1052 Tian Ji -- The Horse Racing(贪心)(2004 Asia Regional Shanghai)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 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

杭电 1052 Tian Ji -- The Horse Racing(贪心)

http://acm.hdu.edu.cn/showproblem.php?pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18058    Accepted Submission(s): 5239 Problem Description Here is a fa