Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. input_lines = """
  4. 4 6
  5. 1 3 2
  6. 7 13 10
  7. 18 20 13
  8. 3 4 2
  9. 0
  10. 1
  11. 2
  12. 3
  13. 5
  14. 8
  15. """.split("\n")[1:-1]
  16.  
  17. construction_num, person_num = map(int, input_lines[0].split())
  18. input_lines = input_lines[1:]
  19.  
  20. class Construction:
  21. def __init__(self, start, end, pos):
  22. self.start = start
  23. self.end = end
  24. self.pos = pos
  25. self.start_pos = start - pos
  26. self.end_pos = end - pos
  27.  
  28. class Constructions:
  29. def __init__(self):
  30. self.list = []
  31.  
  32. def append(self, construction):
  33. self.list.append(construction)
  34.  
  35. def sort(self):
  36. self.list = sorted(self.list, key=lambda x:x.start_pos)
  37.  
  38. def encounter(self, p_pos):
  39. li = [c.pos for c in self.list if c.start_pos <= p_pos and p_pos < c.end_pos]
  40. if len(li) > 0:
  41. return min(li)
  42. else:
  43. return -1
  44.  
  45. def __str__(self):
  46. return str([(c.start_pos, c.end_pos) for c in self.list])
  47.  
  48. class Person:
  49. def __init__(self, departure):
  50. self.departure = departure
  51.  
  52. class Persons:
  53. def __init__(self):
  54. self.list = []
  55.  
  56. def append(self, person):
  57. self.list.append(person)
  58.  
  59. constructions = Constructions()
  60. for index, line in enumerate(input_lines):
  61. if index < construction_num:
  62. start, end, pos = map(int, line.split())
  63. c = Construction(start, end, pos)
  64. constructions.append(c)
  65.  
  66. input_lines = input_lines[construction_num:]
  67.  
  68. persons = Persons()
  69. for line in input_lines:
  70. departure = int(line)
  71. p = Person(departure)
  72. persons.append(p)
  73.  
  74. constructions.sort()
  75.  
  76. for p in persons.list:
  77. print(constructions.encounter(p.departure))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement