You are on page 1of 23

Alexandria University

Competitive programming
junior training
Problem solving strategies

Homework problems

TheTower

TheTower

Minimum number of moves to group K pieces in


one cell is the same as saying find a point where
sum of the manhattan distances to the K pieces is
minimum.

One good thing to do is to Imagine that you already


know the K pieces that will give minimum distance,
Now can you find the point ?

Similar to meet point, we already know that the row


is one of the rows where the pieces is located, and
the column is one of the columns where the pieces
are located.

Thus the solution is, select a row where the pieces


are located, select a column where the pieces are
located, then for each K find the distance to the
nearest K, use priority queue to speed up things!

Imagine how the solution looks like, then find properties


that will help you find it!

Hyperclock
0

0
4
3

1
2
0

2
0

A Configuration can be described by N numbers

There are k0*k1*k2**kn configurations

0
4
3

1
2
1

2
4

The problem

If we imagine the states of the clocks as nodes in a


graph, theres an edge from one node to another if we
can move from state of first node to state of second node

Then the solution for the problem is a path that starts at


node for state (0, 0, , 0) and visits each vertex in the
graph exactly once then returns back to (0, 0, , 0)

Simple: One clock


0
4

1
3

01 2 3 4

Simple: Two clocks


0
0

0
3

0
1 1

1
2

Strategy might change depending on


whether each clock has odd or even number of ticks

We can then compress any 2 clocks into a single clock


Thus keep compressing until you end up with one clock!
(0, 0)"
(0, 1)"
(0, 2)"
(1, 2)
(1, 1)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(1, 3)
(0, 3)
(0, 0)

(0, 0)
(0, 3)
(0, 1)

0
1
2

(2, 0)

Chip construction

For each row, we can find how many 1xL shapes that
start in this row (using the number of 1x1 shapes and
the information from previous rows).

Starting at the first row, if we know that K 1xL shapes


start at this row, at which columns should we put
them ?

Greedily put them on the columns which need MOST


1xL first!

Watch out for overlaps!

Circles

Given n non intersecting circles, find a straight line


that intersects/touches the largest number of
circles.

Solution

line equation y = mx + c, m, c can have any value

infinite number of solutions

do we need to check all the lines ? perhaps a


smaller subset of them contain the solution

lets take a look at some solution, and play with it

Special lines

Special lines are lines which are tangent to two or more


circles

Any line intersecting 2 or more circles can be rotated to


tangent two circles without changing the number of
circles intersected.

Theres only O(n^2) special lines (open belt, crossed


belt).

This problem is hard to implement now, theres a much


easier example in the homework!

If the search space is large or


infinite look for a smaller subset of
it where a solution is guaranteed to
exist

LittleElephantAndString

Given two strings A and B, a move involves taking


a character from A and moving it to the front, find
the minimum number of moves to transform A to B.

Example

ABC

CBA

Minimum number of moves = 2

Think about the solution

Imagine we already know which characters are


going to be moved to front.

The rest of characters from A will form a suffix of B

A: ABMKOOFRN!
B: OMABKOFRN!

ABMKOOFRN!
M O!
AB K OFRN!
OMABKOFRN!

M O!
AB K OFRN!
OMABKOFRN!

Solution: Find the largest suffix of B that is also a


subsequence of A, the number of moves then is the
number of characters left.

Here we solved the problem by looking at the


properties of the solution

Problems

Session

Circles

LittleElephantAndStrings(http://community.topcoder.com/stat?
c=problem_statement&pm=12854)

Homework

TaroFriends (http://community.topcoder.com/stat?
c=problem_statement&pm=13005)

MysticAndCandies (http://community.topcoder.com/stat?
c=problem_statement&pm=12997)

Charging Cargos From codejam 2014 (https://code.google.com/codejam/


contest/2984486/dashboard#s=p0)

You might also like