# Bucharest Camp: Try Out Contest by University ITMO May 18, 2013

Problem A. aFridge
Input ﬁle: Output ﬁle: Time limit: Memory limit: afridge.in afridge.out 2 seconds 256 mebibytes

aFridge is a trademark of a company Avocado, Inc. that produces and sells refrigerators. These refrigerators are so well known and cost so much money that if a housewife buys one, she is considered to be very rich and gains much authority among other housewives. Each of aFridge refrigerators has a sensor panel on its door. It is designed to observe and control temperature inside the fridge, to show time, to set up alarm signals and so on. In addition to this, it contains a game that is aimed to sweeten long hours of food cooking and dish washing. There is a ﬁeld of size 6 × 6. On the ﬁeld, there are rectangle pieces. Each of the pieces has size either wj × 1 (a “horizontal” piece) or 1 × hi (a “vertical” piece). One of these pieces is black and the other pieces are gray. No two pieces overlap, and all pieces are completely inside the ﬁeld. The black piece is always horizontal.

In one step, one can move a vertical piece by any number of cells up or down, provided that during and after this move no two pieces overlap and no piece moves outside the ﬁeld. Horizontal pieces are able to be moved by any number of cells left or right under the same conditions. The ﬁeld has an exit, it is located on the ﬁeld’s boundary to the right of a black piece (see the picture). The goal of the game is to move a black piece out of the ﬁeld through the exit. No other piece except the black one may pass that exit. After a happy housewife completes yet another level of this game, a congratulation message appears to her. In particular, it says whether the number of moves was optimal or not. The company Avocado, Inc. has many good engineers, but lacks good programmers. So it asks you for help! Given a level of the game, ﬁnd the minimal number of steps that is necessary to complete this level.

Input
The ﬁrst line of the input ﬁle contains one integer number N (N ≥ 1) following N lines contain descriptions of the pieces, one per line. the number of pieces. The

A description of a piece consist of three integer numbers xi , yi , li and a character di , where (xi , yi ) are coordinates of the top left cell of a piece, li is the length of a piece and di is “ V” if the piece is vertical and “ H” if it is horizontal. The coordinate system is set up in such a way that (0; 0) is a topmost and leftmost cell, (5; 0) is a topmost and rightmost cell and (5; 5) is a bottommost and rightmost cell. All coordinates are in the range of [0; 5], all lengths are in the range of [2; 6].

Page 1 of 15

For an ordinary step. If there are several solutions. output any.in 1 0 7 1 3 4 5 3 3 2 2 4 H 2 1 1 1 4 5 4 2 3 3 3 3 3 2 H V V V H H V 1 0 8 1 7 5 6 2 3 4 0 afridge. a step that does not ﬁnish the game. If the piece is vertical. there will always be at least one solution. output two numbers fi and ci the number of piece to move (one-based) and the number of positions to move. a negative ci means “ −ci cells left” and a positive ci means “ ci cells right”. 2013 The ﬁrst piece is the black one. Page 2 of 15 .out -1 -4 -3 -3 2 2 2 Note This is what the answer for the second example test looks like. The description of a step that ﬁnishes the game must consist of a single zero. For any level. Refer to example tests and an explanation below for clarity. Output In the ﬁrst line output the minimal number of steps M . a negative ci means “ −ci cells up” and a positive ci means “ ci cells down”. For this step. It is always a horizontal piece.Bucharest Camp: Try Out Contest by University ITMO May 18. a black piece must be free to reach the exit and move outside the ﬁeld. that is. For horizontal pieces. Examples afridge. The following M lines should contain the descriptions of these steps.

Recently he has acquired yet another load of books and now he wants to place them on a shelf. Output In the ﬁrst line output the number of books M to be selected. so he once set up a rule that a sum of the years of publication for all books standing at one shelf must not exceed Y . Friedrich is crazy. Input The ﬁrst line of the input ﬁle contains two integer numbers N (1 ≤ N ≤ 5000) and Y (0 ≤ Y ≤ 109 ) the number of books and the capacity of the shelf. Help Friedrich to ﬁnd such a subset.in bibliophile. and he wants it to be ﬁlled as tight as possible.out 2 seconds 256 mebibytes Friedrich is a crazy bibliophile. Example bibliophile. The second line contains N integer numbers yi (1 ≤ yi ≤ 2009) the years of publication of the books. That is. Remember.in 5 6011 2001 2002 2003 2004 2005 bibliophile. Each book he got has its year of publication yi . He has only one free shelf now. Bibliophile Input ﬁle: Output ﬁle: Time limit: Memory limit: bibliophile.Bucharest Camp: Try Out Contest by University ITMO May 18. he wants to select a subset of books whose total sum of years of publication does not exceed Y and is maximum possible.out 3 2 4 5 Page 3 of 15 . In the second line output M integers the indices of books to be selected. 2013 Problem B. The books are numbered starting with 1 in the order of appearance in the input ﬁle.

Cue Sports Input ﬁle: Output ﬁle: Time limit: Memory limit: cue. In this problem. after collision. 2013 Problem C. Initially the ball with mass m is located at X = 1 and another one is at X = 2. we consider an one-dimensional version of cue sports. Your task is to ﬁnd the total number of collisions in this system. respectively.in 0 3 cue. There are only two balls. preserving the absolute value. Example cue. are a wide variety of games of skill generally played with a cue stick which is used to strike billiard balls. Any collision between balls or between a ball and a wall is absolutely elastic. In the beginning of the game a strike in the direction of X = 0 is applied to the ball with mass of M . That means that both kinetic energy and momentum of the system are preserved. One of them has a mass of m and another one has a mass of M . It is known that M = m · 100N . There is a wall at X = 0. Output Output the number of collisions that will happen in the system. For collision of balls. the speed of the ball changes its sign. and let u2 and v2 be the speeds of balls with masses of m and M . before collision. Input The input ﬁle contains one integer N (0 ≤ N ≤ 15). respectively.Bucharest Camp: Try Out Contest by University ITMO May 18. The size of these balls is inﬁnitely small. Then the following equations are true: u1 · m + v 1 · M = u2 · m + v 2 · M 2 2 2 u2 1 · m + v 1 · M = u2 · m + v 2 · M When a ball collides with a wall. or cue sports. moving them around a cloth-covered billiards table bounded by rubber cushions.in cue. let u1 and v1 be the speeds of balls with masses of m and M . They move along X -axis.out 2 seconds 256 mebibytes Billiards.out Page 4 of 15 . where N is a non-negative integer number.

and <operator> stands for one of the following: • “ +” • “ -” • “ *” • “ &” • “ |” • “ ^” • “ <<” • “ >>” addition with possible overﬂow. Of course. The returned value is considered to be the result of execution. you must. if any. Modern compilers often try to avoid division and substitute it with an equivalent chain of instructions. Return instruction must be the last instruction of the program. this language will not allow you to perform division. If everything is OK. among basic operations with integer numbers. the output for a particular test). bitwise shift left. There are only 26 variables to use. The execution of the program is performed instruction by instruction. is checked the following way. Divide Faster Input ﬁle: Output ﬁle: Time limit: Memory limit: divide.out 2 seconds 256 mebibytes It is widely known that. The language has only one variable type: an unsigned 32-bit integer type. Page 5 of 15 . The variable “x” is initialized with P . In this task. Input The input ﬁle contains one integer D (1 ≤ D ≤ 65535). the program is checked for correctness. your program must consist of no more than 10 instructions. given an integer D. write a program which takes P as a parameter and returns Q = ⌊P/D⌋. 2013 Problem D. The program in this language consists of a sequence of instructions. the program’s only parameter. you are to perform optimization of division by a constant. At the beginning of the program’s execution all variables except “x” are initialized with zeros. both inclusively. or “sign-preserving” one). then “Presentation Error” is reported. bitwise XOR. More formally. bitwise OR. and if errors are found. subtraction with possible underﬂow. First. division is the slowest one. multiplication with possible overﬂow. your program is accepted. then “Wrong Answer” is reported. To do this. bitwise shift right (a “logical” variant. bitwise AND. For the performance reasons. you are provided with a simple language. Your program (that is. If any of the results reported by your program is wrong. Then the program is executed for P from 0 to 65535.in divide.Bucharest Camp: Try Out Contest by University ITMO May 18. if possible. Each instruction has one of the following forms: • <variable> = <variable or constant> • <variable> = <variable or constant> <operator> <variable or constant> • return <variable or constant> where <variable or constant> stands for “a variable or a 32-bit unsigned integral constant”. if possible. as opposed to “arithmetic”. they are named from “a” to “z”.

Bucharest Camp: Try Out Contest by University ITMO May 18. 2013 Output If it is possible to write a program which performs division by D. output the program itself. one instruction by line. Examples divide.out Page 6 of 15 . write “ IMPOSSIBLE” (quotes for clarity).in 1 2 return x y = x >> 1 return y divide. Otherwise.

. where an operation is an insertion. Both strings consist of symbols with ASCII codes between 33 and 126. Edit Distance Input ﬁle: Output ﬁle: Time limit: Memory limit: edit. (1 ≤ |S |. . . ﬁnd the edit distances between S and all possible cyclic shifts of T . inclusively.sn by the number of symbols k (0 ≤ k < n) is deﬁned in this problem as a string S ′ = sk+1 sk+2 . The ﬁrst line contains the string S . the second line contains T .out 16 14 12 10 8 6 4 2 4 5 6 8 10 12 14 15 15 16 Page 7 of 15 . Example edit. .out 6 seconds 256 mebibytes The edit distance between two strings (also known as the Levenstein distance) is the minimum number of operations necessary to transform the ﬁrst string into the second one. The i-th number (1 ≤ i ≤ L) should be equal to the edit distance between S and the cyclic shift of T by i − 1. where L is the length of the string T . .in edit. |T | ≤ 2000).Bucharest Camp: Try Out Contest by University ITMO May 18. A cyclic shift of a string S = s1 s2 . Given two strings S and T . . Numbers should be separated by spaces and/or line breaks. deletion or substitution of a single character.in yozhiki_xrushechki shechkiyo_zhikixru edit. sk . 2013 Problem E. Input Input ﬁle consists of two lines.sn s1 . Output Output L numbers.

So they decided to create a new schedule for K working days that meets the following criteria: • Every day. Examples factory. The symbol with number j is equal to “ +” if i-th worker can produce j -th kind of goods. but they understand that.in 2 1 +++ 2 2 +++ 1 2 factory. Output If it is impossible to create such a schedule. Help the management to create a schedule that meets these criteria. and some of them even have doctoral degrees. Input In the ﬁrst line of the input ﬁle there are two integer numbers N (1 ≤ N ≤ 100) and K (1 ≤ K ≤ N ) the number of workers (and kinds of goods) and the number of days. for i-th worker it is known whether he can produce j -th kind of goods. All the workers on the factory are well-qualiﬁed in diﬀerent areas. the production of that kind should keep going every working day. output −1. A factory produces N kinds of goods. every kind of goods should be produced by exactly one worker. They deﬁnetely do not want any worker to get bored doing the same things again and again. output K lines containing descriptions of who produces which kind of goods on which day. for each kind of goods.out 2 seconds 256 mebibytes There is a little factory just near the end of the world. Otherwise. All the numbers are one-based. In particular. and once in K working days the reseller arrives at the factory and buys all the goods produced by this period of time. An i-th description is a string of length N . 2013 Problem F.out -1 Page 8 of 15 . and is equal to “ -” otherwise. • Any worker should produce only these kinds of goods that he can to. The management of the factory loves their workers very much. and there are N workers.Bucharest Camp: Try Out Contest by University ITMO May 18. Factory Fun Input ﬁle: Output ﬁle: Time limit: Memory limit: factory. The following N lines contain descriptions of what kinds of goods a certain worker can produce.in factory. Each line contains N numbers. • Any worker should produce diﬀerent kinds of goods on diﬀerent days. the i-th number in the j -th line is the kind of goods produced on j -th day by i-th worker.

we also assume that there are “walls” in “outer” cells. the fact that the ﬁeld is surrounded with the walls and the fact that the robots stand in the free cells. Your task is to ﬁnd the minimal number of commands after which these coordinates are determined precisely. move one cell south. 1 ≤ Si ≤ 2. not occupied by a wall) in the selected direction. One issues a sequence of N commands. neither a map of the ﬁeld nor the locations of the robots are known. Note that sometimes the coordinates may be deﬁned without execution of any command. 2013 Problem G. For simplicity. Robots are so small that they can stand in the same cell simultaneously. These robots understand the following commands: • “ N” • “ E” • “ S” • “ W” move one cell north. H ≤ 200) and N (1 ≤ N ≤ 200 000). move one cell west.in goout. Page 9 of 15 . and writes down the robots’ reports. the robot moves to that cell and reports “ Y”. move one cell east. each for either of robots. The only knowledge available is width W and height H of the ﬁeld. in latter case we say that there is a “wall” in that cell.Bucharest Camp: Try Out Contest by University ITMO May 18. N W S E H W Somewhere in that ﬁeld there are two robots. “ E”. You may assume that after N commands the coordinates are known for sure. The execution of a command is performed the following way. H (1 ≤ W. A report consists of one character Ri . Initially. A command consists of an integer Si the number of robot which this command is directed to and a character Di deﬁning the direction to move. Go out of here Input ﬁle: Output ﬁle: Time limit: Memory limit: goout. Each of the following N lines contains a command and a corresponding report. Otherwise.out 3 seconds 256 mebibytes There is a ﬁeld with width W and height H . If there is a free cell (that is. The information that one can get from these commands and reports may be enough to determine the coordinates of the robots. Input The ﬁrst line of the input ﬁle contains three integer numbers W . Di is either “ N”. Every cell of that ﬁeld is either free or busy. “ S” or “ W”. robot remains in the same place and reports “ N”. You may also assume that all the reports are consistent.

The meaning of the characters is explained before. Output Output one number the mininal number of commands that determine the coordinates of the robots. it is standing in the western cell. So the coordinates of robots are determined. Now turn to the second sample test. After the ﬁrst command. we can ﬁnd that the entire ﬁeld (2 cells) is free from walls and the ﬁrst robot is standing in the eastern cell. Before any commands. there are four possible ways the robots can stand two ways for each robot. it took two commands to fully determine the coordinates. The third command’s result tells us that the second robot succeeded in moving west. The second command does not bring in any new information. Examples goout.Bucharest Camp: Try Out Contest by University ITMO May 18. Refer to the sample tests for the exact format of a line. Page 10 of 15 . The ﬁrst command is the same as in the ﬁrst sample test. As it is known there are no additional walls in the ﬁeld.in 2 1 1 2 2 1 2 2 1 E W W 1 E W E 3 Y Y Y 3 Y N Y 3 goout. This time.out 2 Note Consider the ﬁrst sample test. 2013 Ri is either “ Y” or “ N”. that is. The second command’s result tells us that the second robot cannot move west. one must conclude that the second robot stands in the western cell.

out 2 seconds 256 mebibytes Professor Hrundy has a much in common with P. For example. will eventually make him as famous among contestants as Grundy is. Your task is. • H (k ) is the smallest number greater than H (k − 1) such that all sums H (i) + H (j ) where 1 ≤ i ≤ j ≤ k are distinct. 2013 Problem H. 2 + 4 = 6 and 4 + 4 = 8 are distinct from the previous values. H (3) can not be equal to 3.Bucharest Camp: Try Out Contest by University ITMO May 18. Examples hrundy. Hrundy’s Sequence Input ﬁle: Output ﬁle: Time limit: Memory limit: hrundy. but it can be equal to 4 since 1 + 4 = 5. because pairwise sums 1 + 1 = 2. 2 + 2 = 4 are all distinct. he hopes. given n. Input The input ﬁle contains one integer n (1 ≤ n ≤ 1000). to ﬁnd H (n). Once he even invented a sequence which.in 1 2 3 1 2 4 hrundy. M. Grundy. Output Output the value of H (n). 1 + 2 = 3. H (2) = 2. because 1 + 3 = 2 + 2.out Page 11 of 15 .in hrundy. The sequence is deﬁned the following way: • H (1) = 1.

• let C be an N -digit number consisting of digits of the number A sorted in non-descending order. All the numbers must be diﬀerent. An invariant of the Kaprekar’s transform is the number I where K(I ) = I . Invariant Input ﬁle: Output ﬁle: Time limit: Memory limit: invariant. C = 0029 and K(2009) = 9200 − 29 = 9171. For some N . A Kaprekar’s transform of an N -digit number is deﬁned as follows: • let A be an N -digit number. For example. one by line. 2013 Problem I. possibly with leading zeros. • let B be an N -digit number consisting of digits of the number A sorted in non-ascending order. • then the result of the transform is K(A) = B − C . Input In the ﬁrst line of the input ﬁle there is one integer number N (1 ≤ N ≤ 60). there exist some invariants of this transform. for N = 4 there is only one nontrivial invariant: K(6174) = 7641 − 1467 = 6174. Example invariant.in 4 1 6174 invariant.out 4 seconds 256 mebibytes In this task you will get acquainted with some interesting properties of Kaprekar’s transform. Your task is to ﬁnd all nontrivial invariants of Kaprekar’s transform for a given N .out Page 12 of 15 . but sometimes there are the other. consider A = 2009.in invariant.Bucharest Camp: Try Out Contest by University ITMO May 18. For example. zero is the invariant of the transform. nontrivial invariants. In the following K lines output the invariants themselves. then B = 9200. in arbitrary order with exactly N digits (print leading zeros if necessary). For any N . Output In the ﬁrst line output the number of nontrivial invariants K .

hasMoreTokens(). There are specialized editors and IDEs for almost any programming language. Class StringTokenizer performs splitting a string into tokens using a set of delimiters.nextToken(). //hasFourth is false If one calls method nextToken when no tokens remained. we assume that StringTokenizer does not have other methods or constructors. Java Code Validation Input ﬁle: Output ﬁle: Time limit: Memory limit: javalidation. he or she often tries to prove correctness of pieces of code. but some simple veriﬁcation tasks can. which returns a string: String first = t. There are only two variable types: String and StringTokenizer. class String does not have any methods it serves just as a sequence of Latin letters. your task will be to write a similar tool for the Java programming language. an exception NoSuchElementException is thrown.this. They often oﬀer lots of attractive features besides simple error highlighting. we write: StringTokenizer t = new StringTokenizer(". The language to be used will also be simpliﬁed. allowing it to be thrown is a programming error. doSomethingWise(). The simpliﬁed language is deﬁned as follows: Page 13 of 15 . it will propagate to outer methods and probably will lead to termination of the program. Generally. No.nextToken().. //third is "simple" boolean hasFourth = t. To do this. they may notice some possible subtle errors..Bucharest Camp: Try Out Contest by University ITMO May 18.is simple!". In this problem. and thus should. If no one catches this exception. " . Generally.this. This time. Let us consider an example: we need to break a string “ . //first is "this" String second = t.out 2 seconds 256 mebibytes When a programmer writes code. and show some ways to ﬁx these. //second is "is" One may test whether there are more tokens by calling method called hasMoreTokens. and this code will not be executed For the purpose of this task.in javalidation. not for the whole language just for some usages of one simple class.is simple!” considering whitespaces.hasMoreTokens()..nextToken(). dots and exclamation marks as delimiters.. //an exception will be thrown here. it is impossible to prove a program’s correctness using another computer program. String fourth = t.nextToken(). such as cycles that are executed only once. 2013 Problem J.!"). //. be performed by computer. The tokens are extracted one by one using a method of StringTokenizer called nextToken. lowercase or uppercase. //hasThird is true String third = t. which returns a boolean value: boolean hasThird = t.

nextToken(). some characters in the delimiter string may be safely omitted. independently of the value of “ s”. for the code that will never be executed under any circumstances. Page 14 of 15 . output “ No warnings found. lowercase or uppercase " In addition to this deﬁnition. “ <statement-number>” should be substituted by the one-based number of statement which contains the warning.” where “ <characters>” should be substituted by actual list of unnecessary characters. There is one predeﬁned variable a variable “ s” of type String. For each call to the constructor of StringTokenizer which contains unnecessary characters in the delimiter string.Bucharest Camp: Try Out Contest by University ITMO May 18.”. The program will always be valid. • in the constructor of StringTokenizer. The tokens from the language’s deﬁnition may be separated with whitespaces and/or line breaks. there are two types of warnings: • a call to nextToken will. lowercase or uppercase ::= " a sequence of Latin letters. You may ﬁnd useful that a variable “ s” may contain an arbitrary number of Latin letters. Note that you should not generate warnings for unreachable code. The size of the input ﬁle will not exceed one megabyte. lowercase or uppercase ::= a sequence of Latin letters. Your task is to generate warnings for the code in the described language. Output For each call to nextToken which will always produce an exception. All characters must be diﬀerent and should be listed in alphabetical order. output the message “ Statement <statement-number>: An exception will be thrown. ::= String strname = tokname . output the message “ Statement <statement-number>: There are unnecessary delimiter characters: "<characters>".”. Input The input ﬁle contains a program in the language deﬁned above. 2013 program statements statement new-tokenizer next-token strname tokname constant ::= statements ::= statement | statement statements ::= new-tokenizer | next-token ::= StringTokenizer tokname = new StringTokenizer( strname . throw NoSuchElementException. that is. These messages should be listed in the order the corresponding calls are located in the code. If there is no warnings. More speciﬁcally. all uppercase letters come before any lowercase letter. No StringTokenizer will ever be created using an empty delimiter string. ::= a sequence of Latin letters. lowercase or uppercase. it is not allowed to deﬁne variable names that are already deﬁned. In all the messages. constant ).

2013 Examples javalidation. So. In this string. characters “a”. characters “c” and “e” are speciﬁed more than once.in StringTokenizer st = new StringTokenizer(s. Statement 3: There are unnecessary delimiter characters: "ace". String t = st. for sure. StringTokenizer uv = new StringTokenizer(t.out Statement 1: There are unnecessary delimiter characters: "ce". The ﬁrst statement creates a new StringTokenizer with the delimiter string equal to “ accepted”. Note Consider the second example test.out No warnings found. String bacaba = uv.nextToken().Bucharest Camp: Try Out Contest by University ITMO May 18.nextToken(). "abc"). javalidation. String x = st. too. Page 15 of 15 .in StringTokenizer st = new StringTokenizer(s. "qwerty"). In such a string. unnecessary.nextToken(). StringTokenizer xx = new StringTokenizer(x. String a = uv. String y = xx. javalidation. "accepted"). “c” and “e” cannot appear under any circumstances because they all are in the delimiter string of the ﬁrst tokenizer.nextToken(). they are unnecessary. Extra instances of these characters are. "face"). javalidation.nextToken(). The third statement creates a new StringTokenizer from the string returned by the ﬁrst tokenizer.