您的位置:首页 > 其它

田忌赛马(较难贪心)

2014-10-06 10:04 232 查看






TianJi--TheHorseRacing

TimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)

TotalSubmission(s):18525AcceptedSubmission(s):5404



ProblemDescription

HereisafamousstoryinChinesehistory.

"Thatwasabout2300yearsago.GeneralTianJiwasahighofficialinthecountryQi.Helikestoplayhorseracingwiththekingandothers."

"BothofTianandthekinghavethreehorsesindifferentclasses,namely,regular,plus,andsuper.Theruleistohavethreeroundsinamatch;eachofthehorsesmustbeusedinoneround.Thewinnerofasingleroundtakestwohundredsilverdollarsfrom
theloser."

"Beingthemostpowerfulmaninthecountry,thekinghassonicehorsesthatineachclasshishorseisbetterthanTian's.Asaresult,eachtimethekingtakessixhundredsilverdollarsfromTian."

"TianJiwasnothappyaboutthat,untilhemetSunBin,oneofthemostfamousgeneralsinChinesehistory.UsingalittletrickduetoSun,TianJibroughthometwohundredsilverdollarsandsuchagraceinthenextmatch."

"Itwasarathersimpletrick.Usinghisregularclasshorseraceagainstthesuperclassfromtheking,theywillcertainlylosethatround.Butthenhisplusbeattheking'sregular,andhissuperbeattheking'splus.Whatasimpletrick.Andhowdoyou
thinkofTianJi,thehighrankedofficialinChina?"



WereTianJilivesinnowadays,hewillcertainlylaughathimself.Evenmore,werehesittingintheACMcontestrightnow,hemaydiscoverthatthehorseracingproblemcanbesimplyviewedasfindingthemaximummatchinginabipartitegraph.DrawTian's
horsesononeside,andtheking'shorsesontheother.WheneveroneofTian'shorsescanbeatonefromtheking,wedrawanedgebetweenthem,meaningwewishtoestablishthispair.Then,theproblemofwinningasmanyroundsaspossibleisjusttofind
themaximummatchinginthisgraph.Ifthereareties,theproblembecomesmorecomplicated,heneedstoassignweights0,1,or-1toallthepossibleedges,andfindamaximumweightedperfectmatching...

However,thehorseracingproblemisaveryspecialcaseofbipartitematching.Thegraphisdecidedbythespeedofthehorses---avertexofhigherspeedalwaysbeatavertexoflowerspeed.Inthiscase,theweightedbipartitematchingalgorithmisatoo
advancedtooltodealwiththeproblem.

Inthisproblem,youareaskedtowriteaprogramtosolvethisspecialcaseofmatchingproblem.



Input

Theinputconsistsofupto50testcases.Eachcasestartswithapositiveintegern(n<=1000)onthefirstline,whichisthenumberofhorsesoneachside.ThenextnintegersonthesecondlinearethespeedsofTian’shorses.Thenthenextnintegers
onthethirdlinearethespeedsoftheking’shorses.Theinputendswithalinethathasasingle0afterthelasttestcase.



Output

Foreachinputcase,outputalinecontainingasinglenumber,whichisthemaximummoneyTianJiwillget,insilverdollars.



SampleInput

3
928371
958774
2
2020
2020
2
2019
2218
0




SampleOutput

200
0
0




Source

2004AsiaRegionalShanghai

开始我自己做了几下,WA,后发现等于的情况时没考虑,哎,开始以为这题目简单,原来不是我想的

那样简单,后看如别人的写的思路和证明才模拟写的,现在吧别人的给大家看下(转载臭臭吧):


题目大意:田忌和国王赛马,给出马的数量n,然后是田忌的n匹马,国王的n匹马。问田忌最多可以赢得多少比赛(一场200块)。

这道题目以前看过,但是没有弄出来,思路是:田忌的最快的马比国王最快的马快,就用田忌最快的马跟国王最快的马比;如果田忌最快的马比国王最快的马慢,就用田忌最慢的马比。

因为只是知道要这样,但对于其他的情况,根本弄不明白要怎么比法,当马的速度一样快的时候,就











因为比赛的时候,是国王先出马,然后田忌再出,田忌有主动权上的优势。

先对田忌和国王的马进行排序。

贪心的策略:

一、当田忌最快的马比国王最快的马快时,用田忌最快的马赢国王最快的马。

二、当田忌最快的马比国王最快的马慢时,用田忌最慢的马输给国王最快的马。

三、当田忌最快的马跟国王最快的马一样快时,分情况。

证明一、:假设现在国王最快的马是K,田忌最快的马是T,如果存在一种更优的比赛策略,让T的对手不是K,而使得田忌赢更多的钱的话,那么设此时K的对手是t,T的对手是k:(T>K&&T>t&&K>k)

1、若t>K,则有T>k,t>K。这个结果和T>K,t>k是相同的。

2、若k<t≤K,则有T>k,t≤K。这个结果不如T>K,t>k来得优秀。

3、若t≤k≤K,则有T>k,t≤K。这个结果和T>K,t≤k是相同的。

由此可知,交换各自对手后,一定不会使得结果变劣,那么假设是不成立的。

得证!!


证明二、:因为田忌最快的马比国王最快的马慢,所以田忌所有的马都比国王最快的马慢,也就是说此时田忌的所有马都赢不了国王的马,而出于贪心的思想,应该保留相比之下更快的马,因此用最慢的马去输一定不会比用别的马去输来得劣。

得证!!


其实上面这两个很容易就想得到,最难的是相等的时候。因为不可以直接让田忌最快的马跟国王最快的马打平,或者直接用最慢的马去输给国王最快的马。(存在反例)

1、如果选择全部打平,那么对于田忌1234,国王1234,这组数据,田忌什么黄金也得不到。但是如果选择1->4,4->3,3->2,2->1田忌可以得到400两黄金。(大雄想的)

2、如果选择用最慢的马输掉比赛的话,对于田忌34,国王14,这组数据,田忌一胜一负,什么黄金也得不到,但是如果田忌选择3->1,4->4,一胜一平,田忌可以得到200两黄金。

所以:对于情况三,我们应该从最慢的马开始考虑了

1、当田忌最慢的马比国王最慢的马快,那么用田忌最慢的马赢国王最慢的马

2、当田忌最慢的马比国王最慢的马慢,那么用田忌最慢的马输给国王最快的马

3、当田忌最慢的马跟国王最慢的马相等的时候,用田忌最慢的马跟国王最快的马比

证明1、:假设现在国王最慢的马是K,田忌最慢的马是T,如果存在一种更优的比赛策略,让T的对手不是K,而使得田忌赢更多的钱的话,那么设此时K的对手是t,T的对手是k:(T>K&&t>T&&k>K)

1、若T>k,则有T>k,t>K。这个结果和T>K,t>k是相同的。

2、若T<k≤t,则有T<k,t>K。。这个结果不如T>K,t>k来得优秀。

3、若K≤t≤k,则有T>k,t≤K。这个结果和T>K,t≤k是相同的.

由此可知,交换各自对手后,一定不会使得结果变劣,那么假设是不成立的。

得证!!


证明2、:因为田忌最慢的马比国王最慢的马慢,所以田忌最慢的马都比国王所有的马慢,也就是说此时田忌最慢的马赢不了国王的任何一匹马,而出于贪心的思想,应该去掉国王最快的马,因此输给国王最快的马一定不会比输给其他的马来得劣。

得证!!


证明3、:因为田忌最快T和最慢t的马与国王最快K和最慢k的马都相等,如果用最快的马跟国王最快的马比,最慢的马和国王最慢的马比,那么田忌什么收获也没有。此时是T->K,t->k,其实这个时候我们可以换个角度想,上面这种情况跟T->k,t->k,没有什么区别,都是双方少了最快和最慢的马。但是从证明二可知,我们必须让最慢的马输给国王最快的马,而在田忌的马中找任何一匹比最慢的马快一点的马去赢国王最慢的马都行,这样田忌在同样一胜一负的情况下,保留了跑得更快的马,会比把最快的马拿去比赛来得更优些。

得证!!




代码:


#include<iostream>

#include<algorithm>

usingnamespacestd;

#defineN1010

intT
,K
;

boolcmp(inta,intb)

{

returna>b;

}

intmain()

{

intti,tj,ki,kj,win,n;

while(scanf("%d",&n)&&n!=0)

{

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

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

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

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

sort(T,T+n,cmp);

sort(K,K+n,cmp);

ti=ki=0;

tj=kj=n-1;

for(i=0,win=0;i<n;i++)

{

if(T[ti]>K[ki])

{

ti++;

ki++;

win++;

}

elseif(T[ti]<T[ki])

{

tj--;

ki++;

win--;

}

else

{

if(T[tj]>K[kj])

{

tj--;

kj--;

win++;

}

elseif(T[tj]<K[kj])

{

tj--;

ki++;

win--;

}

else

{

if(T[tj]<K[ki])

{

tj--;

ki++;

win--;

}

}

}

}

printf("%d\n",win*200);

}

return0;

}



mycode:

#include<iostream>

#include<algorithm>

#include<cstdlib>

usingnamespacestd;

intt[1111],k[1111],n;

boolcmp(inta,intb)

{

returna>b;

}

intmain()

{

intts,te,ks,ke,i,ans;

while(scanf("%d",&n)!=EOF&&n)

{

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

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

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

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

sort(t,t+n,cmp);

sort(k,k+n,cmp);

ans=0;

ts=ks=0;

te=ke=n-1;

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

{

if(t[ts]<k[ks])

{

te--;

ks++;

ans-=200;

}

elseif(t[ts]>k[ks])

{

ts++;

ks++;

ans+=200;

}

else

{

if(t[te]>k[ke])

{

te--;

ke--;

ans+=200;

}

elseif(t[te]<k[ke])

{

te--;

ks++;

ans-=200;

}

else

{

if(t[te]<k[ks])

{

te--;

ks++;

ans-=200;

}

}

}

}

printf("%d\n",ans);

}

return0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: