Professional Documents
Culture Documents
Solucionario - Ciic2012 - Ingles
Solucionario - Ciic2012 - Ingles
Pedro Ribeiro
A - Symmetries in the Mirror
Palindromes are a very well known special class of strings. In their general form, palin-
dromes are words than can be read the same in either direction: backwards or forwards. In
this problem you are asked to work with a similar definition, but in a more constrained form.
You are only concerned with numbers and they must present reflection symmetry (RS), a
property based on the author’s own memories of playing with his calculator in front of a
mirror. Given a number, we must compute which is the closest number (to the one given)
that presents RS.
Before starting the presentation of possible solutions to this task, let’s define two concepts:
For any given number n, the closest number with RS will either be n itself, previous(n)
or next(n). Any other smaller or larger number with RS will be further away from n. Our
task can now be reduced to finding out these two numbers, given n.
1
number n we can easily use binary search to find out previous(n) and next(n). Care must
be taken when the n = 109 , because next(109 ) is (obviously) larger than 109 . In fact it is...
109 + 1.
How could we generate the numbers? There were several ways of doing this efficiently.
Basically, we could limit the used digits to the ones that matter and generate (in order) the
first half of each RS number, with the second half being a consequence of the first half.
This approach was enough to score 60% of the points.
2
B - The New Rafael’s House
Task originally proposed by: Mario Cruz and Ivan Arias (Colombia)
Task modifications: Pedro Ribeiro (Portugal)
Task type: Search, Backtracking, Branch and Bound, BFS, A*
The original formulation of this problem did not have any areas specifically marked for
not having carpet. We decided to include this new constraint so that the problem input
was not merely the width and height of the house, which would allow different types of
approaches. In fact, it could become possible to pre-compute the answers for all possible
20*20 cases, even with a solution that would take more than 1 second, and simply submit
code with all the answers. More than that, the inclusion of these special “no-carpet” areas,
allow us to create all kinds of shapes.
Incorrect approaches
We could wrongly suppose that a greedy approach would suffice for this problem. For
instance, some contestants tried to always use the largest possible square on any yet unused
cell. This would generate a possible “good” solution but that nevertheless was far from being
guaranteed to be the optimal, which is what the task asks for. Can the reader think of some
simple counter-examples?
Another incorrect approach was to think that the problem admitted a “typical” dynamic
programming (DP) solution. Suppose that for a given empty cell we would try all possible
square sizes S and partition the remaining area in two different rectangles on these two ways,
using DP to “memoize” the solution for smaller rectangles (see the 2 figures on the left):
This kind of solution would not consider tilings that need a square to be between the two
created partitions. In other words, we would be “cutting” through squares of the optimal
solution, ending up using more carpets than needed. The figure on the right illustrates a
counter-example, where by putting the bottom-left square we would not be able to divide
the remaining area in two distinct rectangles.
3
Both the greedy, DP and similar approaches would be able to get a small amount of
points on a reduced number of test cases, that were designed to at least reward the attempt
to make an admissible solution.
4
With this kind of state we save memory and we can be more efficient on generating new
states.
Breadth-first search
Instead of using DFS, we could use BFS and traverse the states in the order of needed
carpets. By keeping the memory usage as low as possible, we could reach many different
combinations. We could also experiment in trading memory for time, by keeping a memory
of the visited states, avoiding the redundant work of visiting them again, because the state
can be reached using different carpet placements.
All of this in place would suffice to get around 80% of the points.
Further improvements
For the used test cases, the described approach was enough. However, we could do better.
For instance, implemented solutions used features such as bidirectional search (also known
as meet-in-the-middle technique), where we started looking at the same time for placement
of carpets on opposite sides of the house. We also experimented with more constrained
versions of the state, squeezing the information for every possible bit and we used more
stronger heuristics than simply counting the unfilled corners.
5
C - Boxes
In this problem we were asked to simulate a specific procedure for filling up boxes with
objects, computing the minimum amount of boxes needed.
Logarithmic simulation
For the given constraints we need to better than the linear search on the simulation. If
fact, we can have logarithmic time for finding the leftmost (or right most) box that still has
enough capacity, when performing a single simulation.
There are several hypotheses for this, but we will describe an approach based on segment-
trees. Imagine that we create a tree where all the boxes are in leafs, storing the capacity
6
still available, and that parent nodes always store the maximum value of its descendants.
The following figure illustrates a tree for a case in which we have boxes with capacities
[3, 5, 7, 7, 7, 4, 1, 1]:
It is easy to see that the depth of the tree will be in the order of log N. Now, when we are
looking for where to put a certain object we can traverse the tree just once accordingly. For
instance, imagine that we are Rafael (we want to use the leftmost possible box) and need to
place a box of size 6. We start by the root an look at the left child, which has max value
7 and therefore we know that we can go that way. We now look at the following left child,
which is 5 and smaller than what need. We therefore go to the right side. We continue this
behavior until we reach a leaf. For Ana, the algorithm is the same, but we prefer to go right
when both children have enough capacity for the object.
After putting an object in a box, we need to update the tree. We can do that by making
a single bottom-up traversal from the leaf to the node, updating the visited nodes with new
child maximums.
This whole procedure (finding the box plus updating the tree) run in O(logN ). With this
we can improve our algorithm for this task to O(N log 2 N ), since for each binary searched
amount of boxes we must pass through all objects and logarithmically compute their assigned
box.
This kind of approach enough to receive 100% of the points.
7
D - Complicated Traffic
A contest without a graph problem would be like going to Italy and not eating pasta :)
This was the problem set’s graph problem and essentially asked for paths which maximized
the minimum weight of the path, which is also known as maximin distance.
It does not matter exactly where we did the road renovations (as long as we don’t repeat
nodes in path), because the only thing that can influence the outcome of the path is the min
distance up to that point and how many renovations we can still use.
This could be used coupled with the brute force approach to solve cases for larger K’s,
which would slightly improve the score.
8
an edge, we relax its adjacent edges and verify if its induces better paths for any of the
not visited nodes. Here we can do the same, with a slight modification: we need to search
for the maximum (we want to obtain the largest possible speed) and we need to adapt the
relaxation process to reflect maximin distance. We should also consider K nodes for each
graph node, representing arriving there with 0 renovations made, with 1 renovation, etc.
A simple Dikjstra algorithm takes time O(N 2 ) (for each step we must linearly choose the
maximum among the unvisited nodes) and this was enough for about 75% of the points.