Advertisement
Guest User

Untitled

a guest
May 24th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. 1
  2. 0 5 8 10
  3. 0 1 7
  4.  
  5. -> 1
  6.  
  7. 1 2
  8. 4 0 5 8 10
  9. 3 0 1 7
  10.  
  11. -> 1
  12.  
  13. 1
  14. 0 999 1001 2000
  15.  
  16. -> 3
  17.  
  18. 3
  19. 0 42
  20.  
  21. -> 0
  22.  
  23. """
  24. Slow and huge, but useful for checking smaller inputs.
  25.  
  26. input: m, followed by n lines of start and stop times, all servers are turned off in the beginning, all numbers fit inside 32bit signed integers, counts are optional
  27. output: number of nines of uptime
  28.  
  29. example:
  30. 1
  31. 0 5 8 9
  32. 0 1 7
  33. parsed:
  34. 1st node: on 0:5, 7:9
  35. 2nd node: on 0:1, 6:9
  36. at least one server is running during:
  37. 0:5, 7:9
  38. known time interval: 0:9, only timestep 6 is down, so 90% uptime during the part we can see.
  39. output:
  40. 1
  41.  
  42. """
  43.  
  44. import sys
  45.  
  46.  
  47. def rangeify(l):
  48. res = []
  49. for i in range(0, len(l), 2):
  50. res += range(l[i], l[i + 1] + 1)
  51. return res
  52.  
  53.  
  54. def parse():
  55. m = input()
  56. lines = [map(int, x.split()) for x in sys.stdin.read().split("n")[:-1]]
  57. if not lines:
  58. return [], m
  59. top = max(map(max, lines))
  60. nlines = [x + [top] if len(x) % 2 else x for x in lines]
  61.  
  62. n2lines = map(rangeify, nlines)
  63. return n2lines, m
  64.  
  65.  
  66. def flatten(ls):
  67. res = []
  68. for l in ls:
  69. res += l
  70. return sorted(res)
  71.  
  72.  
  73. def join(ups):
  74. res = {}
  75. for up in ups:
  76. if up not in res:
  77. res[up] = 0
  78. res[up] += 1
  79. return res
  80.  
  81.  
  82. def bin(ups, m):
  83. res = []
  84. for up in range(max(ups)+1):
  85. res += [up in ups and ups[up] >= m]
  86. return res
  87.  
  88.  
  89. def nines(a):
  90. s = ("%.15f" % a)[2:]
  91. s2 = s.lstrip("9")
  92. return len(s) - len(s2)
  93.  
  94.  
  95. def solve(l, m):
  96. if len(l) < m:
  97. return nines(0)
  98. b = bin(join(flatten(l)), m)
  99. return nines(sum(b) / float(len(b)))
  100.  
  101.  
  102. print solve(*parse())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement