Advertisement
Guest User

dd

a guest
Dec 26th, 2015
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. // div1 easy (cgy4ever's solution)
  2. public class DoubleOrOneEasy {
  3. public int minimalSteps(int a, int b, int newA, int newB)
  4. {
  5. int INF = 1100000000;
  6. int ret = INF;
  7. for(int i = 0; i < 30; i++)
  8. {
  9. long d = newA - 1L * a * (1<<i);
  10. long d2 = newB - 1L * b * (1<<i);
  11. if(d != d2) continue;
  12. if(d < 0) continue;
  13. int cnt = i;
  14. for(int j = i; j >= 0; j--)
  15. {
  16. int amount = (int)d / (1<<j);
  17. cnt += amount;
  18. d -= (1<<j) * amount;
  19. }
  20. ret = Math.min(ret, cnt);
  21. }
  22. if(ret == INF) return -1;
  23. return ret;
  24. }
  25. }
  26.  
  27. // div1 med (Arterm's solution)
  28. #include <bits/stdc++.h>
  29.  
  30. using namespace std;
  31.  
  32. const int M = 104;
  33. typedef long long ll;
  34.  
  35. vector<int> g[M];
  36. int n;
  37. long mx;
  38. ll d[M][M];
  39. ll temp[M];
  40. bool used[M];
  41.  
  42. void merge(ll *d, ll *h) {
  43. fill(temp, temp + M, 0);
  44.  
  45. for (int i = 0; i <= mx; ++i) {
  46. for (int j = 1; i + j <= mx; ++j)
  47. temp[max(i, j)] += d[i] * h[j - 1];
  48. for (int j = 2; i + j <= mx; ++j)
  49. temp[max(i, j)] += d[i] * h[j - 2];
  50. }
  51.  
  52. copy(temp, temp + M, d);
  53. }
  54.  
  55. void dfs(int v) {
  56. used[v] = true;
  57. fill(d[v], d[v] + M, 0);
  58. d[v][0] = 1;
  59.  
  60. for (int to : g[v])
  61. if (!used[to]) {
  62. dfs(to);
  63. merge(d[v], d[to]);
  64. }
  65. }
  66.  
  67. class DiameterOfRandomTree {
  68. public:
  69. double getExpectation(vector<int> a, vector<int> b) {
  70. n = a.size() + 1;
  71. for (int i = 0; i < n - 1; ++i) {
  72. g[a[i]].push_back(b[i]);
  73. g[b[i]].push_back(a[i]);
  74. }
  75.  
  76. ll ans = 0;
  77. ll prev = 0;
  78. for (mx = 0; mx < M; ++mx) {
  79. fill(used, used + M, 0);
  80. dfs(0);
  81. ll bon = 0;
  82. for (int i = 0; i <= mx; ++i)
  83. bon += d[0][i];
  84. ans += (bon - prev) * mx;
  85. prev = bon;
  86. }
  87.  
  88. return 1.0 * ans / (1ll << (n - 1));
  89. }
  90. };
  91.  
  92. # div1 hard (lg5293's solution)
  93. class GameOnGraph():
  94. def findSolution(self, graph, p):
  95. n = len(p)
  96. conn = [[graph[i][j] == 'Y' for j in range(n)] for i in range(n)]
  97.  
  98. # first find a hamiltonian cycle,
  99. seen = [False] * n
  100. cycle = []
  101. for j in range(n):
  102. for k in range(n):
  103. if conn[0][j] and conn[j][k] and conn[k][0]:
  104. cycle = [0,j,k]
  105. seen[0] = seen[j] = seen[k] = True
  106. break
  107. if len(cycle) > 0: break
  108.  
  109. def growCycle(prev):
  110. din = []
  111. dout = []
  112. for next in range(n):
  113. if seen[next]: continue
  114. for idx in range(len(prev)):
  115. nid = (idx+1)%len(prev)
  116. if conn[prev[idx]][next] and conn[next][prev[nid]]:
  117. seen[next] = True
  118. return prev[nid:] + prev[:nid] + [next]
  119.  
  120. if conn[next][prev[0]]:
  121. din.append(next)
  122. else:
  123. dout.append(next)
  124.  
  125. for x in din:
  126. for y in dout:
  127. if conn[y][x]:
  128. seen[x] = seen[y] = True
  129. return prev + [y,x]
  130.  
  131. while len(cycle) != n:
  132. cycle = growCycle(cycle)
  133.  
  134. ret = []
  135. curp = list(p)
  136. def move(a,b):
  137. ret.append(a)
  138. curp[a],curp[b] = curp[b],curp[a]
  139.  
  140. # then bubble sort.
  141. comp = [cycle.index(i) for i in range(n)]
  142. goal = range(n)
  143. pos = cycle.index(0)
  144. while curp != goal:
  145. a,b,c = cycle[pos%n], cycle[(pos+1)%n], cycle[(pos+2)%n]
  146. if comp[curp[b]] == n-1 or comp[curp[b]] < comp[curp[c]]:
  147. move(a,b)
  148. pos += 1
  149. else:
  150. if conn[a][c]:
  151. move(a,c)
  152. pos += 2
  153. else:
  154. move(a,b)
  155. move(b,c)
  156. move(c,a)
  157. pos %= n
  158. return ret[::-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement