Love丶FFC's Blog
汉东省政法大学附属中学所在的光明区最近实施了名为“智慧光明”的智慧城市项目。具体到交通领域,通过“智慧光明”终端,可以看到光明区所有红绿灯此时此刻的状态。小明的学校也安装了“智慧光明”终端,小明想利用这个终端给出的信息,估算自己放学回到家的时间。
一次放学的时候,小明已经规划好了自己回家的路线,并且能够预测经过各个路段的时间。同时,小明通过学校里安装的“智慧光明”终端,看到了出发时刻路上经过的所有红绿灯的指示状态。请帮忙计算小明此次回家所需要的时间。
输入的第一行包含空格分隔的三个正整数 r、y、g,表示红绿灯的设置。这三个数均不超过 106。
输入的第二行包含一个正整数 n,表示小明总共经过的道路段数和路过的红绿灯数目。
接下来的 n 行,每行包含空格分隔的两个整数 k、t。k=0 表示经过了一段道路,将会耗时 t 秒,此处 t 不超过 106;k=1、2、3 时,分别表示出发时刻,此处的红绿灯状态是红灯、黄灯、绿灯,且倒计时显示牌上显示的数字是 t,此处 t 分别不会超过 r、y、g。
输出一个数字,表示此次小明放学回家所用的时间。
30 3 30
8
0 10
1 5
0 11
2 2
0 6
0 3
3 10
0 3
46
小明先经过第一段路,用时 10 秒。第一盏红绿灯出发时是红灯,还剩 5 秒;小明到达路口时,这个红绿灯已经变为绿灯,不用等待直接通过。接下来经过第二段路,用时 11 秒。第二盏红绿灯出发时是黄灯,还剩两秒;小明到达路口时,这个红绿灯已经变为红灯,还剩 11 秒。接下来经过第三、第四段路,用时 9 秒。第三盏红绿灯出发时是绿灯,还剩 10 秒;小明到达路口时,这个红绿灯已经变为红灯,还剩两秒。接下来经过最后一段路,用时 3 秒。共计 10+11+11+9+2+3 = 46 秒。
有些测试点具有特殊的性质:
* 前 2 个测试点中不存在任何信号灯。
测试点的输入数据规模:
* 前 6 个测试点保证 n ≤ 103。
* 所有测试点保证 n ≤ 105。
1.输入一次判断一次
2.首先判断道路情况
3.到达每个路灯时可能存在四种情况,画一个图进行逐个判断即可
4.遭遇黄灯时,还需要增加随后的红灯等待时间
5.遭遇绿灯,则不进行判断
6.使用%运算可以大大提高效率
Red, Yellow, Green = map(int, input().split()) # 输入红黄绿灯的秒数 Circle = Red + Yellow + Green # 路灯变化一轮需要的时间 NOR = int(input()) # 输入经过的路段和经过的灯的总数 TotalTime = 0 # 总共需要花费的时间 for i in range(NOR): RoadCondition, InitialRemainTime = map(int, input().split()) # 输入路段状态,初始剩余时间 WasteTime = TotalTime # 之前路段花费的时候会对后续的路灯状态和时间造成影响 if RoadCondition == 0: TotalTime += InitialRemainTime elif RoadCondition == 1: # 初始情况为红灯 if WasteTime <= InitialRemainTime: # 到达时仍为第一轮红灯 TotalTime += InitialRemainTime - WasteTime elif WasteTime > InitialRemainTime: # 到达时已变灯 WasteTime -= InitialRemainTime if Green < WasteTime <= Green + Yellow: # 变为黄灯 TotalTime += Green + Yellow - WasteTime + Red elif Green + Yellow < WasteTime: # 至少变化一轮 WasteTime = WasteTime - Green - Yellow # 去除第一轮剩余的黄绿灯时间 WasteTime %= Circle if 0 < WasteTime <= Red: # 变为红灯 TotalTime += Red - WasteTime elif Red + Green < WasteTime <= Red + Green + Yellow: # 变为黄灯 TotalTime += Circle - WasteTime + Red elif RoadCondition == 2: # 初始情况为黄灯 if WasteTime <= InitialRemainTime: # 到达时仍为第一轮黄灯 TotalTime += InitialRemainTime - WasteTime + Red elif WasteTime > InitialRemainTime: # 到达时已变灯 WasteTime -= InitialRemainTime if 0 < WasteTime <= Red: # 变为红灯 TotalTime += Red - WasteTime elif Red + Green < WasteTime: # 至少变化一轮 WasteTime = WasteTime - Green - Red WasteTime %= Circle if 0 < WasteTime <= Yellow: # 变为黄灯 TotalTime += Yellow - WasteTime + Red elif Yellow < WasteTime <= Yellow + Red: # 变为红灯 TotalTime += Yellow + Red - WasteTime elif RoadCondition == 3: # 初始时为绿灯 if WasteTime > InitialRemainTime: # 到达时已经变灯 WasteTime -= InitialRemainTime if 0 < WasteTime <= Yellow: # 变为黄灯 TotalTime += Yellow - WasteTime + Red elif Yellow < WasteTime <= Yellow + Red: # 变为红灯 TotalTime += Yellow + Red - WasteTime elif Yellow + Red < WasteTime: # 至少变化一轮 WasteTime = WasteTime - Yellow - Red WasteTime %= Circle if Green < WasteTime <= Green + Yellow: # 变为黄灯 TotalTime += Green + Yellow - WasteTime + Red elif Green + Yellow < WasteTime <= Green + Yellow + Red: # 变为红灯 TotalTime += Green + Yellow + Red - WasteTime print(TotalTime)
authentic cheap jordans
Read reviews and was a little hesitant since I had already inputted my order. or but thank god, I had no issues. similar to the received item in a timely matter, they are in new condition. you decide so happy I made the purchase. Will be definitely be purchasing again.
authentic cheap jordans https://www.realcheapretrojordanshoes.com/
original louis vuittons outlet
Read reviews and was a little hesitant since I had already inputted my order. or it may be but thank god, I had no issues. choose the received item in a timely matter, they are in new condition. in any event so happy I made the purchase. Will be definitely be purchasing again.
original louis vuittons outlet https://www.louisvuittonsoutletonline.com/
authentic cheap jordans
Read reviews and was a little hesitant since I had already inputted my order. perhaps but thank god, I had no issues. such as received item in a timely matter, they are in new condition. regardless so happy I made the purchase. Will be definitely be purchasing again.
authentic cheap jordans https://www.realjordansshoes.com/
FoorryNox
That said, veterinarians prescribe many drugs for non FDA approved uses buy generic cialis online
Preliaf
Optical microscopy adderall and viagra
liLzbbIoS
Garlic burn as self inflicted mucosal injury a case report and review of the literature buy cialis generic online Next time I have that upper right quadrant tenderness, I guess maybe I ll go see my gastroentrologist sooner rather than later