2017 Mini Competition 0 M1701-5: Tony Wong M1706: Alex Tung (Story by Tony Wong) Yuju Sinb /News/End?Id=7941357
Total Page:16
File Type:pdf, Size:1020Kb
2017 Mini Competition 0 M1701-5: Tony Wong M1706: Alex Tung (story by Tony Wong) http://m.star.naver.com/GFRIEND Yuju SinB /news/end?id=7941357 Eunha Yerin Sowon “ Umji M1701 - Hearts https://youtu.be/bwTVerz9X3c 2017 Mini Competition 0 3 M1701 - Hearts ▸ Each small heart and large heart requires exactly 1 Buddy ▸ Each extra large heart requires 2 extra Buddies ▸ These 2퐷 Buddies can make 2퐷 mini hearts ▹ 퐴 ← max(0, 퐴 − 2퐷) ▸ Finally, 퐴/2 additional Buddies are required to make the remaining mini hearts ▸ long long is required 2017 Mini Competition 0 4 M1702 – Archery http://ent.mbc.co.kr/content/4918 2017 Mini Competition 0 5 M1702 - Archery ▸ Compute the Euclidean distance of the center of the arrow to the origin: 푑 = 푥2 + 푦2 ▸ The score of the arrow is the max 푝 ∈ 1, 2, 3, … , 10 such that 푑 − 푟 ≤ 40(11 − 푝) ▸ To avoid precision error, we square both sides of the inequality 푥2 + 푦2 ≤ [40 11 − 푝 + 푟]2 ▸ To further avoid precision error, we store 푥, 푦 and 푟 using integers by multiplying the numbers by 10 (i.e. 푟 = 12for 1.2mm) 푥2 + 푦2 ≤ [400 11 − 푝 + 푟]2 2017 Mini Competition 0 6 M1703 – Photo Collage Jeremy's favourite photo http://childyenni.tistory.com/11 2017 Mini Competition 0 7 M1703 – Photo Collage ▸ How to represent the photos? ▹ Sowon, Yerin, Eunha, Yuju, SinB, Umji ▹ Method 1: Index the members using 0, 1, 2, 3, 4, 5. Create a 2D boolean array A of size N x 6. Set A[i][j] to true if member j is present in photo i ▹ Method 2 (better): Index the members using 1, 2, 4, 8, 16, 32. Sum up the numbers of the members present in the photo For example, we use 1 + 8 + 32 = 41 to represent a photo containing Sowon (1), Yuju (8) and Umji (32) 2017 Mini Competition 0 8 M1703 – Photo Collage ▸ Subtask 1: Each photo contains exactly 3 members ▹ Sowon, Yuju and Umji (1 + 8 + 32 = 41) can be paired with Yerin, Eunha and SinB (2 + 4 + 16 = 22 = 63 - 41) ▹ The 10 possible pairings are: 7 – 56 11 – 52 13 – 50 14 – 49 19 – 44 21 – 42 22 – 41 25 – 38 26 – 37 28 – 35 ▹ If we have a frequency array f that contains the count of each kind of photo, the answer would be: f[7] * f[56] + f[11] * f[52] + ... + f[28] * f[35] 2017 Mini Competition 0 9 M1703 – Photo Collage ▸ Subtask 2: Each photo contains exactly 2 members ▸ 6C2 = 15 kinds of photos with 2 members ▸ For i = 3, 5, 6, 9, 10, 12, 17, 18, 20, 24, 33, 34, 36, 40, 48 ▹ For j = 3, 5, 6, 9, 10, 12, 17, 18, 20, 24, 33, 34, 36, 40, 48 ▹ Make sure that no members are present in both i and j ▹ This can be done using binary operators: (i & j) == 0 ▹ answer += f[i] * f[j] * f[63 - i - j] ▸ Output answer / 6 2017 Mini Competition 0 10 M1703 – Photo Collage ▸ The task can be solved using Dynamic Programming ▸ dp[x][y] = The number of ways to make collage containing the members y (binary representation) using only the first x photos ▹ x = 0 .. N and y = 0 .. 63 ▸ Initially, dp[0][0] = 1 and dp[0][1..63] = 0 (There is one way to make a collage with no one in it using no photos) ▸ Consider the photos one by one, if i-th photo has members z (binary) ▹ For j = 0 .. 63, we try to combine the photo with the collages having j ▹ dp[i][j] = dp[i - 1][j] + dp[i - 1][j - z] if (j & z) == 0 ▹ dp[i][j] = dp[i - 1][j] if (j & z) != 0 ▸ Finally, answer = dp[N][63] have same members, ▸ Optionally save memory using dp[j] += dp[j - z] not allowed to combine 2017 Mini Competition 0 11 M1704 – Outfit Numbers https://youtu.be/MZ6o8jT2PXo 2017 Mini Competition 0 12 M1704 – Outfit Numbers ▸ Subtask 1: N <= 10 ▹ Can be solved by O(2N N) exhaustion ▹ Each person has two options: month (0) or day of month (1) ▹ For example when N = 3, try these 23 = 8 combinations: ▹ 000, 001, 010, 011, 100, 101, 110, 111 ▹ For a combination, check whether two chosen numbers are the same in O(N) time 2017 Mini Competition 0 13 M1704 – Outfit Numbers ▸ One important observation is when N >= 32, there is no solution ▸ Or generally, if the number of distinct integers in the input > N, there is no solution ▸ One way to cut down the search tree: ▹ Backtrack immediately if two persons is assigned the same number ▸ Or, exhaust the other way: ▹ For each number 1, 2, ..., 31 either skip it or assign it to one person ▹ Backtrack when number of numbers left < number of unassigned persons ▸ The constraints are small enough for these to pass 2017 Mini Competition 0 14 M1704 – Outfit Numbers ▸ In fact, this task's constraints are so special that it can be solved using many ways: ▹ If any valid assignment is allowed, the task can be modelled as 2-SAT ▹ The ith person has boolean variable 푏푖 – whether the month is taken ▹ If the ith person's month is same as the jth person's month, person j must take the day of month. Therefore, 푏푖 → ¬푏푗 (푏푖 implies not 푏푗) ▹ 2-SAT solved by finding Strongly Connected Components in the Implication Graph (e.g. Tarjan's Algorithm) ▹ Alternatively, use network flow algorithms to determine validity 2017 Mini Competition 0 15 M1704 – Outfit Numbers ▹ Take an input (M, D) as an (undirected) edge connecting nodes M and D ▹ Assignment is done by directing the edges ▹ Valid iff every node has in-degree at most 1 ▹ If solution exists, any connected component with K nodes must have at most K edges ▹ K - 1 edges: tree • Direct the edge that appears first in input • The remaining part is solved similarly ▹ K edges: 頂環樹 (cycle + many branches) • Only two ways to direct the edges 2017 Mini Competition 0 16 M1705 – Slippery Stage https://youtu.be/41Sh-RYHiNU 2017 Mini Competition 0 17 M1705 – Slippery Stage ▸ Subtask 1: Stage has only 1 row ▸ There is only one optimal path from one cell to another ▸ For example, to move from c1 to c2 (assume c2 > c1), add the following to the answer ▹ S1, c1+1 + S1, c1+2 + S1, c1+3 + ... + S1, c2 2017 Mini Competition 0 18 M1705 – Slippery Stage ▸ We can model the stage as a directed weighted graph ▹ NxM nodes ~4NM edges ▸ Weighted graph shortest path can be found using Dijkstra's Algorithm – O(NM lg NM) ▸ Performing Dijkstra's once per query is not efficient enough ▸ Instead, precompute all-pairs distances by running Dijkstra's NM 2 2 times – O(N M lg NM) and answer each query in O(1) time – 80% ▸ Notice that the max distance is small ▹ Use an array of queues instead of priority queue: O(N2M2 ) – 100% 2017 Mini Competition 0 19 M1706 – Anniversary Gifts https://www.facebook.com/HKGBUDDIES/photos /a.2298324910308451.1073741836.229777703 2017 Mini Competition 0 20 0363239/2354372181370390/?type=3&theater Mini-Comp 0 Q6 Solution Alex Tung February 14, 2017 Simplifying Assumptions We make several assumptions before explaining the solution. • There are N positions and each time we move clockwise by K steps. Let g = gcd(N; K). The result is unchanged if we replace (N; K) by (N=g; K=g). From now on, assume gcd(N; K) = 1. • The case N = 1 is trivial. From now on, assume N > 1. • Now, we 0-index the presents and choose A 2 [0;N) such that AK ≡ 1 (mod N). We can remodel the task as one which asks whether the permutation (0, A, 2A mod N, 3A mod N, ..., (N − 1)A mod N) is odd or even. (An odd permutation is one that has an odd number of inversions. An even permutation is one that has an even number of permutations.) For example, the first sample test has N = 10, K = 4. • g = gcd(10; 4) = 2. Now, take N = 5 and K = 2. • A = 3. We want to know: is the permutation (0; 3; 1; 4; 2) odd or even? In fact, for the second simplifying step, we need not calculate A; the answer will be the same if we consider (0, K, 2K mod N, 3K mod N, ..., (N − 1)K mod N). Hence, for the example above, it suffices to consider the permutation (0; 2; 4; 1; 3). This fact may seem puzzling at the moment; that it is true follows (though nontrivially) from the solution. Now, we have a transformed task at hand: Given 1 ≤ K < N with gcd(N; K) = 1, is the permutation (0, K, 2K mod N, 3K mod N, ..., (N − 1)K mod N) odd or even? Propositions will be stated without proof. Most of them are simple facts and it will be cumbersome to put down all the proofs here. 1 Special Case: N is an odd prime We focus on the special case where N is an odd prime. First, we use the following proposition: Proposition 1. An odd permutation becomes even after swapping two (not necessarily adjacent) elements, and vice versa. Thus, we only need to consider the number of (not necessarily adjacent) swappings needed to sort the elements of the permutation. An Example Consider the following example (N = 11, K = 8). The permutation is: 0 8 5 2 10 7 4 1 9 6 3 Discarding 0 (this will not affect the answer), we see that the elements are nicely split into two halves: 8 5 2 10 741963 Do you see the nice symmetry? 8+3 = 11 5+6 = 11 2+9 = 11 10+1 = 11 7+4 = 11 Therefore, if we swap the blue elements that are \too large" (i.e.