Problem A - IQ Test
We can store two values,
countodd and
counteven, as the number of odd or even elements in the series. We can also store
lastodd and
lasteven as the index of the last odd/even item encountered. If only one odd number appears --- output
lastodd; otherwise only one even number appears, so output
lasteven.
Problem B - Telephone Numbers
There are many ways of separating the string into clusters of 2 or 3 characters. One easy way is to output 2 characters at a time, until you have only 2 or 3 characters remaining. Here is a possible C++ solution:
<code>
for( i=0; i<n; i++ )
{
putchar(buf[i]);
if( i%2 && i<n-(n%2)-2 ) putchar('-');
}
</code>
Problem C - Roads in Berland
If you are familiar with the
Floyd-Warshall algorithm, then this solution may be easier to see.
Initially, we are given a matrix
D, where
D[i][j] is the distance of shortest path between city
i and city
j. Suppose we build a new road between
a and
b with length shorter than
D[a][b]. How do we update the rest of the graph accordingly?
Define a new matrix
D', whose entries
D'[i][j] are the minimum path distance between
i and
j while taking into account the new road
ab. There are three possibilities for each
i, j:
- D'[i][j] remains unchanged by the new road. In this case D'[i][j] = D[i][j]
- D'[i][j] is shorter if we use the new road ab. This means that the new path i, v1, v2, ..., vn, j must include the road a, b. If we connect the vertices i, a, b, j together in a path, then our new distance will be D[i][a] + length(ab) + D[b][j].
- Lastly, we may have to use the road ba. (Note that this may not be the same as road ab.) In this case, we have D'[i][j] = D[i][b] + length(ab) + D[a][j].
Thus, for each new road that we build, we must update each path
i, j within the graph. Then we must sum shortest distances between cities. Updating the matrix and summing the total distance are both
O(N2), so about
3002 operations. Lastly, there are at most
300 roads, so in total there are about
3003 operations.
One thing to note is that the sum of all shortest distances between cities may be larger than an int; thus, we need to use a long when calculating the sum.
Problem D - Roads not only in Berland
Before we start this problem, it is helpful to know about the
union find data structure. The main idea is this: given some elements
x1, x2, x3, ..., xn that are partitioned in some way, we want to be able to do the following:
- merge any two sets together quickly
- find the parent set of any xi
This is a general data structure that sometimes appears in programming competitions. There are a lot of ways to implement it; one good example is written by Bruce Merry (aka BMerry)
here.
Back to the problem: Every day we are allowed to build exactly 1 road, and close exactly 1 road. Thus, we can break the problem into two parts:
- How do we connect the parts of the graph that are disconnected?
- How do we remove roads in a way that does not disconnect parts of the graph?
Let
build be the list all roads that need to be built, and let
close be the list of nodes that need to be closed. We can show that in fact, these lists are of the same size. This is because the connected graph with
n nodes is a tree if and only if it has
n - 1 edges. Thus, if we remove more roads than than we build, then the graph is disconnected. Also, if we build more roads than we remove, then we have some unnecessary roads (the graph is no longer a tree).
Now consider the format of the input data:
a1, b1a2, b2...
an - 1, bn - 1We can show that edge
(ai, bi) is unnecessary if and only if the nodes
ai, bi have already been connected by edges
(a1, b1), (a2, b2), ..., (ai - 1, bi - 1). In other words, if the vertices
ai, bi are in the same connected component
before we, add
(ai, bi) then we do not need to add
(ai, bi). We can use union-find to help us solve this problem:
<code>
for( i from 1 to n-1 )
{
if( find(
ai)=find(
bi) ) close.add
(ai, bi);
else merge(
ai, bi);
}
</code>
In other words, we treat each connected component as a set. Union find allows us to find the connected component for each node. If the two connected components are the same, then our new edge is unnecessary. If they are different, then we can merge them together (with union find). This allows us to find the edges that we can remove.
In order to find the edges that we need to add to the graph, we can also use union-find: whenever we find a component that is disconnected from component 1, then we just add an edge between them.
<code>
for( i from 2 to n )
if( find(
vi)!=find(
v1) )
{
then merge
(v1, vi);
build.add
(v1, vi);
}
</code>
We just need to store the lists of roads that are unnecessary, and the roads that need to be built.
Problem E - Test
The way I solved this problem is with a
hash function. Hash functions can fail on certain cases, so in fact, my solution is not 'correct'. However, it passed all the test cases =P
Let the input strings be
s0, s1, s2. We can build the shortest solution by permuting the strings and then trying to 'attach' them to each other. I.e., we need to find the longest overlapping segments at the end of string
a and the beginning of string
b. The obvious brute force solution won't run in time. However, we can use a hash function to help us calculate the result in
O(n) time, where
n is
min(len(a), len(b)). The hash function that I used was the polynomial
hash(x0, x1, ..., xn) = x0 + ax1 + a2x2 + ... + anxn. This polynomial is a good hash function in this problem because it has the following useful property:
Given
hash(xi, ..., xj), we can calculate the following values in
O(1) time:
- hash(xi - 1, xi, ..., xj) = xi - 1 + a × hash(xi, ..., xj)
- hash(xi, ..., xj, xj + 1) = hash(xi, ..., xj) + aj + 1 - i × xj + 1
In other words, if we know the hash for some subsequence, we can calculate the hash for the subsequence and the previous element, or the subsequence and the next element. Given two strings
a, b, we can calculate the hash functions starting from the end of
a and starting from the beginning of
b. If they are equal for length
n, then that means that (maybe)
a and
b overlap by
n characters.
Thus, we can try every permutation of
s0, s1, s2, and try appending the strings to each other. There is one last case: if
si is a substring of
sj for some
i ≠ j, then we can just ignore
si. We can use hash functions to check that one string is contained within another one.