Olympiad in Mathematics - Category Programming

Problem Set of the Second Round

2000/01



P-II-1

John found a box with wooden rods bellow the roof of his grandma's house. He started playing with them by forming triangles. John's older brother noticed the game and became interested to know how many different triangles can be formed. Your task is to help him find the answer.

Task:

Your program will receive a poisitve integer N - number of rods, followed by N mutually different positive integers l1 to lN (lengths of sticks). Your program should determine the number of tripples i, j, k (1 <= i < j < k <= N), for which the numbers di, dj, and dk, satisfy the triangular inequality (di < dj + dk, dj < di + dk, and dk < di + dj).

Example:
Input:

N = 5
d1 = 5.5
d2 = 1.5
d3 = 2.0
d4 = 2.5
d5 = 7.5
Output:
2

P-II-2

The society for equality of robots and humans is trying to make a robot, which could move autonomously inside of a room with obstacles. Unfortunatelly, this society is lacking a software expert and thus you were called to help.

The robot will move in a rectangular room. There is a square grid on the floor. Some of the squares contain obstacles, and the robot may not enter those squares. Robot can move only parallel to one of the walls of the room.

The society is lacking a technician as well, and thus the robot has very limitted set of movements. The robot can recognize only 3 commands: Step, Left, and Right. After accepting a command Step, the robot will move to the neigboring square in the direction of its current heading. After accepting a command Left, it will turn 90o left and on a command Right, it will turn 90o right.

Task:

Write a program, which will read the dimensions of the square grid (M and N), the coordinates of the current location of the robot, and the coordinates of the square where the robot should move. The upper-left corner has coordinates [1,1] (see example below). Each of the following M lines contains N numbers 0 or 1. If the j-th number on i-th line is 1, then the sqaure at coordinates [i, j] contains an obstacle. If this number is 0, the square is free. The task is to print a sequence of commands using which the robot will move from its current location to the destination. However, there is one rub in the task: The processing of commands Left and Right is very time-consuming. The produced sequence of commands should contain as few as possible of them. The number of Step commands can be arbitrary. The starting heading of the robot can be decided by the program. If the robot cannot pass from its current location to the destination, the program should inform about it.

Example:

Imagine the following room with a sqaure grid 4 x 8:

               
  # # # # #    
  #       # #  
C     #       T

Input:

4 8
4 1
4 8
0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 0
0 1 0 0 0 1 1 0
0 0 0 1 0 0 0 0

The optimal program for the robot is as follows (the starting heading is upwards):

Step Step Step Right Step Step Step Step
Step Step Step Right Step Step Step
The robot will make 13 steps and turn twice. Note that there is a shorter path with 9 steps and 4 turnings. This path is shorter, but not optimal, because it contains more turnings.


P-II-3

One day, John (the one from the task 1) happened to play with scissors. And since John inherrited the talent after his father (who is a modern art painter), he decided to improve one of the paintings of shape of a convex polygon. John picked two vertices and cut the picture along the line connecting these two vertices. Then he took one of the new parts, picked two new vertices and made a new cut. After a while, John's father discovered the disaster, removed the scissors from John's hands, and lamented over the result. He found out that it is impossible to put the picture together again, and thus decided to find the part that has the largest number of vertices and display it on his next exhibit as miniature. Your task is to help him to find this part.

Task:

Find the most efficient algorithm, which will read the number of vertices of the original picture n, the number of John's cuts k and the cut descriptions, and then determine the number of vertices of the resulting part that contains the largest number of them. Each cut is described by a pair of numbers (ai, bi) - the numbers of vertices in the original polygon where the John cut. The polygon vertices are numbered from 1 to n on the circumference. Try to find an algorithm with both time and space complexities not depending on the number of polygon vertices.

Example:
Input:

n = 10
k = 3
cuts:
 1 8
 7 5
 4 2
Output:
The largest part has 6 vertices.

P-II-4

Let's start with several definitions: Tiles are squares of an equal size with colored edges. A particular assignemt of colours to edges is called type of tile and is represented by an ordered quadruple (l,p,h,d) specifying the color of left, right, top, and bottom edges in this order. To simplify our work, we will use different symbols - letters, digits, etc. instead of regular color names. For example a tile of type (1,2,3,4) looks like this:

Image: tile

The space we want to tile (called wall) has a shape of a rectangle of size m times n (both m and n are natural numbers; the unit is the length of the edge of one tile). The sides of the rectangle are divided into segments of unit length. Each segment is assigned a color. Our goal is to tile the wall with the tiles in such a way that each of the m*n unit squares of the wall will contain exactly one tile, the neighboring tiles will be facing each other with edges of the same color and the tiles on the sides will be facing the sides of the rectangle always with the edge of the same color as is the color of the corresponding segment of the wall border. The tiles can not be rotated.

Example:

Figure: correct and incorrect tiling

Tiling can be easily used to solve tasks, in which the result is a simpe "yes" or "no" answer. To transform such a task into a tiling problem, we can construct a suitable set of tile types (these are independent from the input, and are fixed for a given problem). Then we color the segments of one side of a sufficiently large wall with colors according to the problem input. The remaining sides are painted with one color and the the transformed task is whether this wall can be tiled or not. The result should be the same as is the result of the corresponding task.

The width of the wall will be always the same as is the length of the input to the original problem. The height of the wall will be the lowest possible, for which there exists a tiling using the designed set of tiles.

This way of computation resembles traditional programming. The designed set of tiles corresponds to a computer program and the required height of wall corresponds to the running time of the program - thus we will try to minimize it in our solutions.

More formally, a tiling program is an ordered quadruple D=(T,l[0],p[0],d[0]), where T is a finite set of tile types {(l[1],p[1],h[1],d[1]), ...,(l[k],p[k],h[k],d[k])} and l[0], p[0] and d[0] are border colors. A decision problem P(x) is a task to determine whether the input x (a finite sequence of symbols (i.e. colors) from a predetermined finite set of symbols) has the required property P. A tiling program solves a decision problem P(x), if P(x)="yes" if and only if there exists such v>0 that it is possible to tile a wall with dimensions |x| times v using tile types from the set T, where the top side of the wall is painted correspondingly to the input x, and the left, right, and bottom sides are painted by colors l[0], p[0] and d[0] in this order. It is possible to use as many tiles of each type as needed. The complexity of a tiling program D for a given input x is the lowest v, for which the tiling is possible; if such v doesn't exist, and thus P(x)="nie", the complexity is defined to be zero. The complexity of the program is a function of the input length n, and its value is the maximum of all complexities of tiling programs for different inputs of the length n.

Example: Let's construct a tiling program, which will determine whether a given sequence of natural numbers x1, ..., xn (0 <= xi <= 9) is balanced. A sequence is balanced, if it contains the same number of even and odd numbers.

The idea of the solution is very simple: we will construct a set of tiles, which will allow only such tilings, where one even and one odd number is rewritten to "dot" in each line. The bottom side of the wall will be painted by the color "dot" as well. Clearly, the coloring of the bottom line is balanced and each new line preserves the balance. Therefor, if there exists a tiling for an input sequence, it must be balanced. Opposite, given a balanced sequence, the tiling exists: while there are numbers left, an arbitrary pair of one odd and one even number can be chosen to be replaced by "dot" - such pair must exist, if the sequence is balanced. This process is repeated n/2-times, until all numbers are rewritten. If we manage to construct the described tiling program, it will be solving the task with a complexity O(n). The program might look like for example like this:

Figure: a solution to the balanced sequence problem

the left border is colored using color A, the right border using color B, and the bottom border using color "dot". Only the lines of the following form

Figure: possible tiling of a line

can be constructed from these tiles, where xi is even and zk is odd, or

Figure: possible tiling of a line

for xi odd and xk even. These are exactly the lines that were needed.

Task

Construct a tiling program, which will decide whether a sequence of natural numbers x1, x2, ..., xn, (0 <= xi <= 9) is symmetric, i.e. whether x1=xn, x2=xn-1, ..., xi=xn-i+1, ..., xn=x1.