Scales

InternationalOlympiadinInformatics2015
26thJuly-2ndAugust2015
Almaty,Kazakhstan
Day1
scales
Language:en-ISC
Scales
Aminahassixcoins,numberedfrom to .Sheknowsthatthecoinsallhavedifferentweights.She
wouldliketoorderthemaccordingtotheirweight.Forthispurposeshehasdevelopedanewkindof
balancescale.
Atraditionalbalancescalehastwopans.Tousesuchascale,youplaceacoinintoeachpanandthe
scalewilldeterminewhichcoinisheavier.
Amina’snewscaleismorecomplex.Ithasfourpans,labeled , , ,and .Thescalehasfour
differentsettings,eachofwhichanswersadifferentquestionregardingthecoins.Tousethescale,
Aminamustplaceexactlyonecoinintoeachofthepans , ,and .Additionally,inthefourth
settingshemustalsoplaceexactlyonecoinintopan .
Thefoursettingswillinstructthescaletoanswerthefollowingfourquestions:
1. Whichofthecoinsinpans , ,and istheheaviest?
2. Whichofthecoinsinpans , ,and isthelightest?
3. Whichofthecoinsinpans , ,and isthemedian?(Thisisthecointhatisneitherthe
heaviestnorthelightestofthethree.)
4. Amongthecoinsinpans , ,and ,consideronlythecoinsthatareheavierthanthecoinon
pan .Ifthereareanysuchcoins,whichofthesecoinsisthelightest?Otherwise,ifthereare
nosuchcoins,whichofthecoinsinpans , ,and isthelightest?
Task
WriteaprogramthatwillorderAmina’ssixcoinsaccordingtotheirweight.Theprogramcanquery
Amina’sscaletocompareweightsofcoins.Yourprogramwillbegivenseveraltestcasestosolve,
eachcorrespondingtoanewsetofsixcoins.
YourprogramshouldimplementthefunctionsinitandorderCoins.Duringeachrunofyour
program,thegraderwillfirstcallinitexactlyonce.Thisgivesyouthenumberoftestcasesand
allowsyoutoinitializeanyvariables.ThegraderwillthencallorderCoins()oncepertestcase.
init(T)
T:Thenumberoftestcasesyourprogramwillhavetosolveduringthisrun.Tisan
integerfromtherange
.
Thisfunctionhasnoreturnvalue.
orderCoins()
Thisfunctioniscalledexactlyoncepertestcase.
ThefunctionshoulddeterminethecorrectorderofAmina’scoinsbycallingthegrader
1/3
functionsgetHeaviest(),getLightest(),getMedian(),and/or
getNextLightest().
Oncethefunctionknowsthecorrectorder,itshouldreportitbycallingthegraderfunction
answer().
Aftercallinganswer(),thefunctionorderCoins()shouldreturn.Ithasnoreturn
value.
Youmayusethefollowinggraderfunctionsinyourprogram:
answer(W)—yourprogramshouldusethisfunctiontoreporttheanswerthatithasfound.
W:Anarrayoflength6containingthecorrectorderofcoins.W[0]throughW[5]should
bethecoinnumbers(i.e.,numbersfrom to )inorderfromthelightesttotheheaviest
coin.
YourprogramshouldonlycallthisfunctionfromorderCoins(),oncepertestcase.
Thisfunctionhasnoreturnvalue.
getHeaviest(A,B,C),getLightest(A,B,C),getMedian(A,B,C) —these
correspondtosettings1,2and3respectivelyforAmina’sscale.
A,B,C:Thecoinsthatareputinpans , ,and ,respectively.A,B,andCshouldbe
threedistinctintegers,eachbetween and inclusive.
EachfunctionreturnsoneofthenumbersA,B,andC:thenumberoftheappropriatecoin.
Forexample,getHeaviest(A,B,C)returnsthenumberoftheheaviestofthethree
givencoins.
getNextLightest(A,B,C,D) —thiscorrespondstosetting4forAmina’sscale
A,B,C,D:Thecoinsthatareputinpans , , ,and ,respectively.A,B,C,andD
shouldbefourdistinctintegers,eachbetween and inclusive.
ThefunctionreturnsoneofthenumbersA,B,andC:thenumberofthecoinselectedby
thescaleasdescribedaboveforsetting4.Thatis,thereturnedcoinisthelightestamongst
thosecoinsonpans , ,and thatareheavierthanthecoininpan ;or,ifnoneof
themisheavierthanthecoinonpan ,thereturnedcoinissimplythelightestofallthree
coinsonpans , ,and .
Scoring
Therearenosubtasksinthisproblem.Instead,yourscorewillbebasedonhowmanyweighings(total
numberofcallstograderfunctionsgetLightest(),getHeaviest(),getMedian()and/or
getNextLightest())yourprogrammakes.
Yourprogramwillberunmultipletimeswithmultipletestcasesineachrun.Let bethenumberof
runsofyourprogram.Thisnumberisfixedbythetestdata.Ifyourprogramdoesnotorderthecoins
correctlyinanytestcaseofanyrun,itwillget0points.Otherwise,therunsarescoredindividuallyas
follows.
Let bethesmallestnumbersuchthatitispossibletosortanysequenceofsixcoinsusing
weighingsonAmina’sscale.Tomakethetaskmorechallenging,wedonotrevealthevalueof
2/3
here.
Supposethelargestnumberofweighingsamongstalltestcasesofallrunsis
forsomeinteger
.Then,considerasinglerunofyourprogram.Letthelargestnumberofweighingsamongstall
testcasesinthisrunbe
forsomenon-negativeinteger .(Ifyouusefewerthan weighings
foreverytestcase,then
.)Then,thescoreforthisrunwillbe
,roundeddownto
twodigitsafterthedecimalpoint.
Inparticular,ifyourprogrammakesatmost weighingsineachtestcaseofeveryrun,youwillget
100points.
Example
Supposethecoinsareordered
Functioncall
getMedian(4,5,6)
getHeaviest(3,1,2)
getNextLightest(2,
3,4,5)
getNextLightest(1,
6,3,4)
getHeaviest(3,5,6)
getMedian(1,5,6)
getMedian(2,4,6)
answer([3,4,6,2,
1,5])
fromthelightesttotheheaviest.
Returns
Explanation
6
Coin isthemedianamongcoins , ,and .
1
Coin istheheaviestamongcoins , ,and .
Coins , ,and arealllighterthancoin ,sothelightestamong
3
them( )isreturned.
Coins and arebothheavierthancoin .Amongcoins and ,
6
coin isthelightestone.
5
Coin istheheaviestamongcoins , and .
1
Coin isthemedianamongcoins , and .
6
Coin isthemedianamongcoins , and .
Theprogramfoundtherightanswerforthistestcase.
Samplegrader
Thesamplegraderreadsinputinthefollowingformat:
line : —-thenumberoftestcases
eachofthelinesfrom to
:asequenceof distinctnumbersfrom to :theorderof
thecoinsfromthelightesttotheheaviest.
Forinstance,aninputthatconsistsoftwotestcaseswherethecoinsareordered
looksasfollows:
and
2
123456
346215
Thesamplegraderprintsthearraythatwaspassedasaparametertotheanswer()function.
3/3