Love丶FFC's Blog

CCF-CSP_20181202:小明放学

2019-12-05 09:00:34
阅读:600   •   评论:6
标签:,

题目背景

汉东省政法大学附属中学所在的光明区最近实施了名为“智慧光明”的智慧城市项目。具体到交通领域,通过“智慧光明”终端,可以看到光明区所有红绿灯此时此刻的状态。小明的学校也安装了“智慧光明”终端,小明想利用这个终端给出的信息,估算自己放学回到家的时间。

问题描述

一次放学的时候,小明已经规划好了自己回家的路线,并且能够预测经过各个路段的时间。同时,小明通过学校里安装的“智慧光明”终端,看到了出发时刻路上经过的所有红绿灯的指示状态。请帮忙计算小明此次回家所需要的时间。

输入格式

输入的第一行包含空格分隔的三个正整数 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

编程语言:Python

解题思想:

1.输入一次判断一次

2.首先判断道路情况

3.到达每个路灯时可能存在四种情况,画一个图进行逐个判断即可

4.遭遇黄灯时,还需要增加随后的红灯等待时间

5.遭遇绿灯,则不进行判断

6.使用%运算可以大大提高效率

时间复杂度:O(n)

IDE代码如下:

  1. Red, Yellow, Green = map(int, input().split()) # 输入红黄绿灯的秒数
  2. Circle = Red + Yellow + Green # 路灯变化一轮需要的时间
  3. NOR = int(input()) # 输入经过的路段和经过的灯的总数
  4.  
  5. TotalTime = 0 # 总共需要花费的时间
  6. for i in range(NOR):
  7. RoadCondition, InitialRemainTime = map(int, input().split()) # 输入路段状态,初始剩余时间
  8. WasteTime = TotalTime # 之前路段花费的时候会对后续的路灯状态和时间造成影响
  9. if RoadCondition == 0:
  10. TotalTime += InitialRemainTime
  11. elif RoadCondition == 1: # 初始情况为红灯
  12. if WasteTime <= InitialRemainTime: # 到达时仍为第一轮红灯
  13. TotalTime += InitialRemainTime - WasteTime
  14. elif WasteTime > InitialRemainTime: # 到达时已变灯
  15. WasteTime -= InitialRemainTime
  16. if Green < WasteTime <= Green + Yellow: # 变为黄灯
  17. TotalTime += Green + Yellow - WasteTime + Red
  18. elif Green + Yellow < WasteTime: # 至少变化一轮
  19. WasteTime = WasteTime - Green - Yellow # 去除第一轮剩余的黄绿灯时间
  20. WasteTime %= Circle
  21. if 0 < WasteTime <= Red: # 变为红灯
  22. TotalTime += Red - WasteTime
  23. elif Red + Green < WasteTime <= Red + Green + Yellow: # 变为黄灯
  24. TotalTime += Circle - WasteTime + Red
  25. elif RoadCondition == 2: # 初始情况为黄灯
  26. if WasteTime <= InitialRemainTime: # 到达时仍为第一轮黄灯
  27. TotalTime += InitialRemainTime - WasteTime + Red
  28. elif WasteTime > InitialRemainTime: # 到达时已变灯
  29. WasteTime -= InitialRemainTime
  30. if 0 < WasteTime <= Red: # 变为红灯
  31. TotalTime += Red - WasteTime
  32. elif Red + Green < WasteTime: # 至少变化一轮
  33. WasteTime = WasteTime - Green - Red
  34. WasteTime %= Circle
  35. if 0 < WasteTime <= Yellow: # 变为黄灯
  36. TotalTime += Yellow - WasteTime + Red
  37. elif Yellow < WasteTime <= Yellow + Red: # 变为红灯
  38. TotalTime += Yellow + Red - WasteTime
  39. elif RoadCondition == 3: # 初始时为绿灯
  40. if WasteTime > InitialRemainTime: # 到达时已经变灯
  41. WasteTime -= InitialRemainTime
  42. if 0 < WasteTime <= Yellow: # 变为黄灯
  43. TotalTime += Yellow - WasteTime + Red
  44. elif Yellow < WasteTime <= Yellow + Red: # 变为红灯
  45. TotalTime += Yellow + Red - WasteTime
  46. elif Yellow + Red < WasteTime: # 至少变化一轮
  47. WasteTime = WasteTime - Yellow - Red
  48. WasteTime %= Circle
  49. if Green < WasteTime <= Green + Yellow: # 变为黄灯
  50. TotalTime += Green + Yellow - WasteTime + Red
  51. elif Green + Yellow < WasteTime <= Green + Yellow + Red: # 变为红灯
  52. TotalTime += Green + Yellow + Red - WasteTime
  53. print(TotalTime)

评论板

共有 6 条评论

  1. 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/

  2. 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/

  3. 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/

  4. FoorryNox

    That said, veterinarians prescribe many drugs for non FDA approved uses buy generic cialis online

  5. Preliaf

    Optical microscopy adderall and viagra

  6. 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

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:508655