Advertisement
wrequiems

Untitled

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