Practice solutions: NEERC 2007

A. Snooker

B. Join the Strings

Sort the strings using the sorting criteria sisi+1<si+1si.

Proof:

If there is sisi+1>si+1si, you can swap si and si+1 to get a better solution.

Question

Can the case that s1>s2>s3 and s1<s3 happen?

C. Twisting the number

Summary

Find the minimum m such that the binary shifting generator {1,2,...,m} can produce all numbers from 1 to n.

Solution

Suppose n=10010000100 in binary.
smallest group representation (SGR) = smallest number that start from 1 and can be used to make n by binary shifting.
In this case, SGR = 1000100100
We can see that mSGR
Consider 1000111111 m1000111111
Consider 1001000011, its SGR = 1000011100 m1000011100.

Number bigger than maxm will be covered by 1xmaxm

E. XOR-omania

H. Billing

I. Just Matrix

Summary

Given n×n matrix A, top, and left.
Construct the matrix such that

Solution

Consider some example
0 0 0 a3>a2>a1
0 0 2 a3<a1<a2
0 1 2 a3...
So from the top and left matrices, we can determine the relationship between elements in the matrix A
Create a directed graph and do topological sort.

The problem is how do we actually build the relationship.
Cannot do naive brute-force because n600

Maintain a BIT
Consider the number from bottom to top (or from right to left)

0 1 0 2

Our BIT Tree: 0 0 0 0
a4 must be placed in a position that there is exactly two holes before it the third place.
Our BIT Tree: 0 0 1 0
a3 must be placed in a position that there is exactly zero holes the first element.
Our BIT Tree: 1 0 1 0
a2 must be placed in a position that there is exactly one hole before it the fourth position.

Can easily find the position by maintaining a BIT of holes and do binary search.

Total complexity: O(n2log2n)

J. Numbers Painting

K. Extrasensory Perception

Summary

Count the number of pair of n permutations in which two permutations (guess permution and solution permutation) in a pair has exactly k matches (k fixed points).

Bryan’s Solution

Observation 1

Since the probability of each solution is the same we can fixed the solution to 1 2 3 … n.

Can do dynamic programming to find the number of permutation that has exactly k fixed points.

Robert’s Solution

Using inclusion-exclusion principle.

3!
(31)×2! // (# of ways to choose fixed point × # of ways to choose other points (new fixed points can occur, but we are over counting))
+(32)×1!
(33)×0!

Need BigInteger in Java or coding in Python.

L. Remote Control