Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Local Selection Contest Solution

The Coin Game


Dynamic Programming. Assume the number of coins is given by the array num,
dp[l][r][k] denotes the maximum number of coins player k (k = 0, 1) can get
given num[l...r], then:
dp[l][r][k] = max(sum[l + 1...r] dp[l + 1][r][1 k] + num[l], sum[l...r 1]
dp[l, r 1, 1 k] + num[r]), k = 0, 1

The Coin Thief


Greedy algorithm. We know that the sum of k 1 , k 2 , ...k n1 is always less than
k n , so choosing pile n is always better than choosing all the previous piles. Thus,
we always attempt to choose the biggest pile available, until no pile is no left.

My Dictionary 8 My Dog
Just sort the given words in alphabetical order.

Secret Life of Numbers


To make the problem easier, let us count the sum of integers up to n which are
relatively prime to n instead. One important observation is: if gcd(n, i) = 1 then
gcd(n, n i) = 1. Assume (n) is the count of positive integers up to n that are
relatively prime to n also known as Eulers totient function. As each such pair
(i, n i) sum up to n, and we
Pnhave (n)/2 such pairs, thus the sum of them is
n (n)/2. The answer is ( i=1 i) n (n)/2.

Unique Substring(s)
Count the number of occurence of all k-substrings using a map, and output the
one which occurs only once with the smallest starting index.

Robot
The critical part is how do we efficiently execute the repeat command. Create
an array rec which records the x coordinate (x), y coordinate (y) and orientation
(south/north/east/west) (o) of the robot after each command. Given a repeat
command R, i, j, we first find out the robots movement between the i-th and
j-th command. This could be described with (rec[j].x rec[i 1].x, rec[j].y
rec[i 1].y, rec[j].o rec[i 1].o) and rec[i 1].o. Then we can compute the
effect of the repeat command under the robots current orientation by doing
matrix multiplication or simply enumerating all the cases.

Taijitu
Binary search. If we get the i-th symbol after repeating the given process for k
times, then we can find a corresponding flipped symbol generated after k 1
times of flip. Repeat this until you find the corresponding symbol in the original
sequence or in the middle of a sequence (which is always 1).

You might also like