Roller Coaster Railroad

advertisement
InternationalOlympiadinInformatics2016
12-19thAugust2016
Kazan,Russia
day1_2
railroad
Country:ISC
RollerCoasterRailroad
Annaisworkinginanamusementparkandsheisinchargeofbuildingtherailroadfora
newrollercoaster.Shehasalreadydesigned n specialsections(conveniently
numberedfrom 0 to n
− 1 )thataffectthespeedofarollercoastertrain.Shenow
hastoputthemtogetherandproposeafinaldesignoftherollercoaster.Forthe
purposeofthisproblemyoumayassumethatthelengthofthetrainiszero.
Foreach i between 0 and n
− 1 ,inclusive,thespecialsection i hastwoproperties:
whenenteringthesection,thereisaspeedlimit:thespeedofthetrainmustbe
lessorequalto si km/h(kilometersperhour),
whenleavingthesection,thespeedofthetrainisexactly ti km/h,regardlessof
thespeedatwhichthetrainenteredthesection.
Thefinishedrollercoasterisasinglerailroadlinethatcontainsthe n specialsections
insomeorder.Eachofthe n sectionsshouldbeusedexactlyonce.Consecutive
sectionsareconnectedwithtracks.Annashouldchoosetheorderofthe n sections
andthendecidethelengthsofthetracks.Thelengthofatrackismeasuredinmeters
andmaybeequaltoanynon-negativeinteger(possiblyzero).
Eachmeterofthetrackbetweentwospecialsectionsslowsthetraindownby 1 km/h.
Atthebeginningoftheride,thetrainentersthefirstspecialsectionintheorder
selectedbyAnna,goingat 1 km/h.
Thefinaldesignmustsatisfythefollowingrequirements:
thetraindoesnotviolateanyspeedlimitwhenenteringthespecialsections;
thespeedofthetrainispositiveatanymoment.
Inallsubtasksexceptsubtask3,yourtaskistofindtheminimumpossibletotallength
oftracksbetweensections.Insubtask3youonlyneedtocheckwhetherthereexists
avalidrollercoasterdesign,suchthateachtrackhaszerolength.
Implementationdetails
Youshouldimplementthefollowingfunction(method):
int64plan_roller_coaster(int[]s,int[]t).
s:arrayoflength n ,maximumallowedentryspeeds.
t:arrayoflength n ,exitspeeds.
Inallsubtasksexceptsubtask3,thefunctionshouldreturntheminimum
1/3
possibletotallengthofalltracks.Insubtask3thefunctionshouldreturn 0 if
thereexistsavalidrollercoasterdesignsuchthateachtrackhaszero
length,andanypositiveintegerifitdoesnotexist.
FortheClanguagethefunctionsignatureisslightlydifferent:
int64plan_roller_coaster(intn,int[]s,int[]t).
n:thenumberofelementsinsandt(i.e.,thenumberofspecialsections),
theotherparametersarethesameasabove.
Example
plan_roller_coaster([1,4,5,6],[7,3,8,6])
Inthisexampletherearefourspecialsections.Thebestsolutionistobuildtheminthe
order 0, 3, 1, 2 ,andtoconnectthembytracksoflengths 1, 2, 0 respectively.Thisis
howatraintravelsalongthisrailroadtrack:
Initiallythespeedofthetrainis 1 km/h.
Thetrainstartstheridebyenteringspecialsection 0 .
Thetrainleavessection 0 goingat 7 km/h.
Thenthereisatrackoflength 1 m.Whenthetrainreachestheendofthetrack,
itsspeedis 6 km/h.
Thetrainentersspecialsection 3 goingat 6 km/handleavesitatthesame
speed.
Afterleavingsection 3 ,thetraintravelsalonga 2 mlongtrack.Itsspeed
decreasesto 4 km/h.
Thetrainentersspecialsection 1 goingat 4 km/handleavesitgoingat 3
km/h.
Immediatelyafterspecialsection 1 thetrainentersspecialsection 2 .
Thetrainleavessection 2 .Itsfinalspeedis 8 km/h.
Thefunctionshouldreturnthetotallengthoftracksbetweenthespecialsections:
1 + 2 + 0 = 3.
Subtasks
≤ si ≤ 109 and 1 ≤ ti ≤ 109 .
(11points): 2 ≤ n ≤ 8 ,
(23points): 2 ≤ n ≤ 16 ,
(30points): 2 ≤ n ≤ 200 000 .Inthissubtaskyourprogramonlyneedstocheck
Inallsubtasks 1
1.
2.
3.
whethertheansweriszeroornot.Iftheanswerisnotzero,anypositiveinteger
answerisconsideredcorrect.
4. (36points): 2
≤ n ≤ 200 000 .
Samplegrader
2/3
Thesamplegraderreadstheinputinthefollowingformat:
line1:integer n .
line2+i,for i between 0 and n
− 1 :integers si and ti .
3/3
Download