Advertisement
wrequiems

Untitled

Mar 7th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.21 KB | None | 0 0
  1. """ Homework
  2. 1) Fix previous homework problems.
  3.    a) Binary search on a max tree: for every number, find the next number that is greater
  4.    b) Implement max problem from scratch - sum of max of all intervals
  5.    c) Implement gcd problem from scratch
  6.    d) Implement Dijkstra, https://www.spoj.com/problems/EZDIJKST/
  7. 2) Read CPH book up to page 34.
  8. ----------
  9. 3) Solve https://www.spoj.com/problems/DIVSUM/ faster than O(N).
  10. 4) For every edge in a tree, find the number of paths that contain this edge, O(N).
  11. 4*) For every vertex in a tree, find the number of paths that contain this vertex, O(N).
  12. """
  13.  
  14. ######
  15. # 1a #
  16. ######
  17.  
  18. n = 10
  19. a = [1, 4, 5, 3, 9, 0, 6, 7, 8, 2, 'not available']
  20. base = 1
  21. while base < n:
  22.     base *= 2
  23.  
  24. minus_inf = float('-inf')
  25. tree = [minus_inf] * (base * 2) + ['no bigger node']
  26.  
  27. for i in range(n):
  28.     tree[base + i] = a[i]
  29.  
  30. for i in range(base - 1, 0, -1):
  31.     tree[i] = max(tree[i * 2], tree[i * 2 + 1])
  32.  
  33. def next_bigger(i):
  34.     to_compare = a[i]
  35.     node = base + i
  36.     while node != 1:
  37.         if node % 2 == 0 and to_compare < tree[node + 1]:
  38.             node += 1
  39.             break
  40.         node //= 2
  41.  
  42.     if node == 1:
  43.         return n
  44.  
  45.     while node < base:
  46.         if to_compare < tree[node * 2]:
  47.             node = node * 2
  48.         else:
  49.             node = node * 2 + 1
  50.     return node - base
  51.  
  52.  
  53. for i in range(n):
  54.     position = next_bigger(i)
  55.     print("Next bigger number than {} at position {} is {} at position {}".format(a[i], i, a[position], position))
  56.  
  57. ######
  58. # 1b #
  59. ######
  60.  
  61. n = 10
  62. a = [1, 4, 5, 3, 9, 0, 6, 7, 8, 2]
  63.  
  64. total = 0
  65. for i in range(n):
  66.     for j in range(i, n):
  67.         total += max(a[i:j + 1])
  68. print(total)
  69.  
  70. base = 1
  71. while base < n:
  72.     base *= 2
  73.  
  74. tree = [0] * (base * 2)
  75. for i in range(n):
  76.     tree[base + i] = a[i]
  77.  
  78. for i in range(base - 1, 0, -1):
  79.     tree[i] = max(tree[i * 2], tree[i * 2 + 1])
  80.  
  81. def next_bigger(i):
  82.     node = base + i
  83.     while node > 1:
  84.         if node % 2 == 0:
  85.             if tree[node + 1] > a[i]:
  86.                 node += 1
  87.                 break
  88.         node //= 2
  89.  
  90.     if node == 1:
  91.         return n
  92.     while node < base:
  93.         node *= 2
  94.         if tree[node] < a[i]:
  95.             node += 1
  96.     return node - base
  97.  
  98. def prev_bigger(i):
  99.     node = base + i
  100.     while node > 1:
  101.         if node % 2 == 1:
  102.             if tree[node - 1] > a[i]:
  103.                 node -= 1
  104.                 break
  105.         node //= 2
  106.     if node == 1:
  107.         return -1
  108.     while node < base:
  109.         node *= 2
  110.         if tree[node + 1] > a[i]:
  111.             node += 1
  112.     return node - base
  113. total = 0
  114. for i in range(n):
  115.     total += (next_bigger(i) - i) * (i - prev_bigger(i))
  116. print(total)
  117.  
  118. ######
  119. # 1c #
  120. ######
  121.  
  122. from math import gcd
  123.  
  124. n = 8
  125. a = [60, 180, 120, 140, 20, 40, 15, 3]
  126.  
  127. total = 0
  128. for i in range(n):
  129.     curr_gcd = a[i]
  130.     total += curr_gcd
  131.     for j in range(i + 1, n):
  132.         curr_gcd = gcd(curr_gcd, a[j])
  133.         total += curr_gcd
  134. print(total)
  135.  
  136. base = 1
  137.  
  138. while base < n:
  139.     base *= 2
  140.  
  141. minus_inf = float('-inf')
  142. t = [minus_inf] * (base * 2)
  143.  
  144. for i in range(n):
  145.     t[base + i] = a[i]
  146.  
  147. for i in range(base - 1, 0, -1):
  148.     t[i] = gcd(t[i * 2], t[i * 2 + 1])
  149.  
  150. def search(node, current_gcd):
  151.     while node > 1:
  152.         if node % 2 == 0:
  153.             if t[node + 1] % current_gcd != 0:
  154.                 node += 1
  155.                 break
  156.         node //= 2
  157.     if node == 1:
  158.         return base+n, None
  159.     while node < base:
  160.         if t[node * 2] % current_gcd != 0:
  161.             node = node * 2
  162.         else:
  163.             node = node * 2 + 1
  164.     return node, gcd(current_gcd, t[node])
  165.  
  166. total = 0
  167. for i in range(n - 1):
  168.     x = base + i
  169.     current_gcd = t[base + i]
  170.     while x < base + n:
  171.         prev = x
  172.         prev_gcd = current_gcd
  173.         x, current_gcd = search(x, current_gcd)
  174.         total += (x - prev) * prev_gcd
  175. print(total + a[-1])
  176.  
  177.  
  178. ######
  179. # 1d #
  180. ######
  181.  
  182. import heapq
  183.  
  184. cases = int(input())
  185. inf = float('inf')
  186. for _ in range(cases):
  187.     n, m = [int(i) for i in input().split(' ')]
  188.     h = []
  189.     dist = [inf] * (n + 1)
  190.     visited = [False] * (n + 1)
  191.     graph = {}
  192.  
  193.     for _ in range(m):
  194.         x = [int(i) for i in input().split(' ')]
  195.         graph.setdefault(x[0], [])
  196.         graph[x[0]].append((x[1], x[2]))
  197.     start, end = [int(i) for i in input().split(' ')]
  198.     dist[start] = 0
  199.     heapq.heappush(h, (dist[start], start))
  200.     while h:
  201.         item = heapq.heappop(h)
  202.         a = item[1]
  203.         if visited[a]:
  204.             continue
  205.         visited[a] = True
  206.         if a in graph:
  207.             for p in graph[a]:
  208.                 b = p[0]
  209.                 distance = p[1]
  210.                 if dist[b] > dist[a] + distance:
  211.                     dist[b] = dist[a] + distance
  212.                     heapq.heappush(h, (dist[b], b))
  213.     if dist[end] != inf:
  214.         print(dist[end])
  215.     else:
  216.         print('NO')
  217.  
  218. #####
  219. # 2 #   DONE
  220. #####
  221.  
  222. #####
  223. # 3 #
  224. #####
  225.  
  226. from math import ceil, sqrt
  227.  
  228. n = int(input())
  229.  
  230. for i in range(n):
  231.     number = int(input())
  232.     total = 1
  233.     for j in range(2, ceil(sqrt(number))):
  234.         d = number // j
  235.         m = number % j
  236.         if m == 0:
  237.             total += j + d
  238.     print(total)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement