Consider a matrix A with dimensions m times n containing only zeros and ones. By a framed rectangle we mean such submatrix of A that has at least two rows and two columns and that contains only ones in the first and the last rows as well as in the first and the last columns. The interior of the rectangle may contain arbitrary elements.
Example:
Input: m=11, n=24 000000000000000000000000 001111100000000001100000 001100111111111111111110 001011111111111111111110 001010100000100011100000 001111100100100011100000 000010001100100111111000 000010000000100100101000 010011111111100100001000 001000000000000111111000 000000000000000000000000 |
Output: The largest framed rectangle has dimensions 6 times 9 and its upper-left corner is in the 4th line and the 5th column. |
The Piping Research Institute has a new problem, this time with a system of oriented pipes. In order to clean this type of system, it is required that the cleaning robot will pass each pipe exactly once. The pipe system is oriented - a direction, in which the robot must pass the pipes has been already decided for each pipe. The programmers of the PRI wrote a program, which will find, for a given system, one possible path for the cleaning robot or it will determine that no such path exists. However, it is sometimes important to know whether there exist more than one such path (the robot lifetime is prolonged by alternating the cleaning paths).
A set of pipes consists of n nodes numbered 1, ..., n with m unidirectional pipes numbered 1, ..., m. Each pipe connects two (different) nodes, doesn't branch or cross with other pipes. Each pair of nodes is connected with at most one pipe. It is guaranteed that there exists a path for a robot starting and ending in the node 1 that is passing through each pipe exactly once and in the correct direction. The researchers of PRI found this path using their program, and it is at your disposition. Your task is to figure out whether there exists a different path starting and ending in the node 1, passing each pipe exactly once and in the correct direction. Your program doesn't have to write this path out, the answer should be YES or NO.
Example:
Input n = 5, m = 7 Pipes: 1 2 1 5 2 3 3 1 3 4 4 1 5 3 Path: 1 2 3 4 1 5 3 1 |
Output YES |
Note: 1 2 3 1 5 3 4 1 is an example of a different path. | Input n = 5, m = 6 Pipes: 1 2 2 3 3 1 3 4 4 5 5 3 Path: 1 2 3 4 5 3 1 |
Output NO |
Students are writing a written test in a classroom. A supervisor is watching them to make sure no illegal cooperation occurs. Most of the time, the supervisor is looking in the same direction, a default direction. However, when he hears any suspicious sounds, he quickly turns in that direction to see what is going on. The trick is to choose the default direction in such a way that the average angle that he has to turn is minimal.
Hint: Suppose a function angle(x,y), which you can use in your solution, returns the turning angle of the supervisor between the point [1;0] and point [x;y] in a counterclockwise direction (i.e. an angle between 0 and 360o).
Example:
Input Output Note: The average turning angle is 67,5o. There are more correct solutions, for example a point [-2,-2] (student number 3). |
![]() |
Comparator nets are used in the design of parallel algorithms. It is also possible to implement them in electronic circuits. A comparator is a simple part which receives two numbers on the input, compares them and returns the lower of them on its upper output and the greater of them on its bottom output. Comparators can be combined into more complex circuits, here called comparator nets.
A comparator net consists of n horizontal wires, which are connected by comparators at arbitrary locations. The comparators are arranged into layers, representing the steps of the computation. In the beginning of the computation (step 0), the net takes n input numbers - each wire takes one. After the computation step k-1 is finished, the outputs from the step k-1 are sent to the inputs of the comparators in the layer k. A comparator in the layer k connects two wires (not necessarily neighboring ones). If the bottom wire contains a value lower than the upper wire, the comparator exchanges the values. Otherwise the values are not modified and they are simply copied further. One layer may contain more than one comparator (and the computation is performed in parallel). However, each wire can enter only one comparator in each layer. When the computation is finished, the outputs contain the same values as inputs, but their order can be different.
The wires are represented graphically as horizontal lines, the comparators as vertical connections of their input wires. The comparators from the same layer are horizontally aligned, or appear in several neighboring columns. The layers are divided by a dashed line. The computation flows from left to right.
When designing nets, we are trying to construct them in such a way that the computational time would be the shortest possible, i.e. the net should have the least number of layers. Another important criterium is the number of comparators (this has a direct influence on the cost of the circuit production).
Example.
Consider the leftmost net on the figure. This net receives four inputs and returns them sorted in an ascending order. After the first two steps of computation, the smallest input will be on the first or the second wire, and the largest input on the third or the fourth. The following two steps place the smallest and the largest elements to their place and the last step will finish sorting. Note that the first and the second comparators (and the third and the fourth one) can be merged into a single layer. The resulting and faster net is in the middle of the figure. A computation flow for input 4, 1, 2, 3 is shown on the right.
Example. Construct a net, which takes n numbers on the input and outputs the smallest of them on the wire 1 (the order of the remaining numbers does not matter). Suppose n is a power of two.
Solution. The net will be constructed recursively. Label S(n) a net, which solves the task for n inputs. If n=1, S(n) does not contain any separator, since there's only one input. Let thus n>1 and let the inputs be divided into upper and lower halves. We apply the net S(n/2) to both - they can work in parallel. When finished, the wire 1 contains the smallest value of the upper half and the wire n/2 + 1 contains the smallest value of the lower half. Adding a single comparator between wires 1 and n/2 + 1 will result in the global minimum on wire 1. The net S(n) thus consists of two nets S(n/2) and one comparator. The following figure shows the construction of the net S(n) on the left, an example for n=8 is shown on the right.
Notice that the depth of the recursion is log2 n, because the size of the input decreases to half in each recursive step. Each recursive level contributes to the resulting net by one layer and thus the the net S(n) has log2 n layers. The number of used comparators is 1 in the last layer, and this number doubles in each layer in backward direction. Let the number of inputs is n=2k. Then the number of comparators used is 1+2+4+...+2k-1=2k-1=n-1 (the sum of a geometric sequence). Our net thus contains O(log n) layers and used O(n) comparators..
Example: n=3, inputs 1,4,5,2,3,6.
The first three outputs will contain 1,2,3 in an arbitrary order
and the remaining wires contain the numbers