Вы находитесь на странице: 1из 15

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 elds 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

Bucharest Camp: Try Out Contest by University ITMO May 18, 2013 The rst piece is the black one. It is always a horizontal piece. For any level, there will always be at least one solution.

Output
In the rst line output the minimal number of steps M . The following M lines should contain the descriptions of these steps. If there are several solutions, output any. For an ordinary step, that is, a step that does not nish the game, output two numbers fi and ci the number of piece to move (one-based) and the number of positions to move. If the piece is vertical, a negative ci means ci cells up and a positive ci means ci cells down. For horizontal pieces, a negative ci means ci cells left and a positive ci means ci cells right. The description of a step that nishes the game must consist of a single zero. For this step, a black piece must be free to reach the exit and move outside the eld. Refer to example tests and an explanation below for clarity.

Examples
afridge.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.out

-1 -4 -3 -3 2 2 2

Note
This is what the answer for the second example test looks like.

Page 2 of 15

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

Problem B. Bibliophile
Input le: Output le: Time limit: Memory limit: bibliophile.in bibliophile.out 2 seconds 256 mebibytes

Friedrich is a crazy bibliophile. Recently he has acquired yet another load of books and now he wants to place them on a shelf. Each book he got has its year of publication yi . Remember, Friedrich is crazy, 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 . He has only one free shelf now, and he wants it to be lled as tight as possible. That is, he wants to select a subset of books whose total sum of years of publication does not exceed Y and is maximum possible. Help Friedrich to nd such a subset.

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. The second line contains N integer numbers yi (1 yi 2009) the years of publication of the books.

Output
In the rst line output the number of books M to be selected. In the second line output M integers the indices of books to be selected. The books are numbered starting with 1 in the order of appearance in the input le.

Example
bibliophile.in 5 6011 2001 2002 2003 2004 2005 bibliophile.out 3 2 4 5

Page 3 of 15

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

Problem C. Cue Sports


Input le: Output le: Time limit: Memory limit: cue.in cue.out 2 seconds 256 mebibytes

Billiards, or cue sports, are a wide variety of games of skill generally played with a cue stick which is used to strike billiard balls, moving them around a cloth-covered billiards table bounded by rubber cushions. In this problem, we consider an one-dimensional version of cue sports. There are only two balls. The size of these balls is innitely small. One of them has a mass of m and another one has a mass of M . They move along X -axis. There is a wall at X = 0. Initially the ball with mass m is located at X = 1 and another one is at X = 2. Any collision between balls or between a ball and a wall is absolutely elastic. That means that both kinetic energy and momentum of the system are preserved. For collision of balls, let u1 and v1 be the speeds of balls with masses of m and M , respectively, before collision, and let u2 and v2 be the speeds of balls with masses of m and M , respectively, after collision. 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, the speed of the ball changes its sign, preserving the absolute value. In the beginning of the game a strike in the direction of X = 0 is applied to the ball with mass of M . It is known that M = m 100N , where N is a non-negative integer number. Your task is to nd the total number of collisions in this system.

Input
The input le contains one integer N (0 N 15).

Output
Output the number of collisions that will happen in the system.

Example
cue.in 0 3 cue.out

Page 4 of 15

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

Problem D. Divide Faster


Input le: Output le: Time limit: Memory limit: divide.in divide.out 2 seconds 256 mebibytes

It is widely known that, among basic operations with integer numbers, division is the slowest one. Modern compilers often try to avoid division and substitute it with an equivalent chain of instructions, if possible. In this task, you are to perform optimization of division by a constant. More formally, given an integer D, you must, if possible, write a program which takes P as a parameter and returns Q = P/D. To do this, you are provided with a simple language. Of course, this language will not allow you to perform division. The language has only one variable type: an unsigned 32-bit integer type. There are only 26 variables to use, they are named from a to z. At the beginning of the programs execution all variables except x are initialized with zeros. The variable x is initialized with P , the programs only parameter. The program in this language consists of a sequence of instructions. 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, and <operator> stands for one of the following: + - * & | ^ << >> addition with possible overow; subtraction with possible underow; multiplication with possible overow; bitwise AND; bitwise OR; bitwise XOR; bitwise shift left; bitwise shift right (a logical variant, as opposed to arithmetic, or sign-preserving one).

The execution of the program is performed instruction by instruction. Return instruction must be the last instruction of the program. The returned value is considered to be the result of execution. For the performance reasons, your program must consist of no more than 10 instructions. Your program (that is, the output for a particular test), if any, is checked the following way. First, the program is checked for correctness, and if errors are found, then Presentation Error is reported. Then the program is executed for P from 0 to 65535, both inclusively. If any of the results reported by your program is wrong, then Wrong Answer is reported. If everything is OK, your program is accepted.

Input
The input le contains one integer D (1 D 65535).

Page 5 of 15

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. Otherwise, write IMPOSSIBLE (quotes for clarity).

Examples
divide.in 1 2 return x y = x >> 1 return y divide.out

Page 6 of 15

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

Problem E. Edit Distance


Input le: Output le: Time limit: Memory limit: edit.in 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, where an operation is an insertion, deletion or substitution of a single character. A cyclic shift of a string S = s1 s2 . . .sn by the number of symbols k (0 k < n) is dened in this problem as a string S = sk+1 sk+2 . . .sn s1 . . . sk . Given two strings S and T , nd the edit distances between S and all possible cyclic shifts of T .

Input
Input le consists of two lines. The rst line contains the string S , the second line contains T . Both strings consist of symbols with ASCII codes between 33 and 126, inclusively. (1 |S |, |T | 2000).

Output
Output L numbers, where L is the length of the string T . 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. Numbers should be separated by spaces and/or line breaks.

Example
edit.in yozhiki_xrushechki shechkiyo_zhikixru edit.out 16 14 12 10 8 6 4 2 4 5 6 8 10 12 14 15 15 16

Page 7 of 15

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

Problem F. Factory Fun


Input le: Output le: Time limit: Memory limit: factory.in factory.out 2 seconds 256 mebibytes

There is a little factory just near the end of the world, and there are N workers. A factory produces N kinds of goods, and once in K working days the reseller arrives at the factory and buys all the goods produced by this period of time. All the workers on the factory are well-qualied in dierent areas, and some of them even have doctoral degrees. In particular, for i-th worker it is known whether he can produce j -th kind of goods. The management of the factory loves their workers very much. They denetely do not want any worker to get bored doing the same things again and again, but they understand that, for each kind of goods, the production of that kind should keep going every working day. So they decided to create a new schedule for K working days that meets the following criteria: Every day, every kind of goods should be produced by exactly one worker; Any worker should produce only these kinds of goods that he can to. Any worker should produce dierent kinds of goods on dierent days. Help the management to create a schedule that meets these criteria.

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. The following N lines contain descriptions of what kinds of goods a certain worker can produce. An i-th description is a string of length N . The symbol with number j is equal to + if i-th worker can produce j -th kind of goods, and is equal to - otherwise.

Output
If it is impossible to create such a schedule, output 1. Otherwise, output K lines containing descriptions of who produces which kind of goods on which day. Each line contains N numbers, the i-th number in the j -th line is the kind of goods produced on j -th day by i-th worker. All the numbers are one-based.

Examples
factory.in 2 1 +++ 2 2 +++ 1 2 factory.out

-1

Page 8 of 15

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

Problem G. Go out of here


Input le: Output le: Time limit: Memory limit: goout.in goout.out 3 seconds 256 mebibytes

There is a eld with width W and height H . Every cell of that eld is either free or busy, in latter case we say that there is a wall in that cell. For simplicity, we also assume that there are walls in outer cells.

N W S E H W

Somewhere in that eld there are two robots. These robots understand the following commands: N E S W move one cell north; move one cell east; move one cell south; move one cell west.

The execution of a command is performed the following way. If there is a free cell (that is, not occupied by a wall) in the selected direction, the robot moves to that cell and reports Y. Otherwise, robot remains in the same place and reports N. Robots are so small that they can stand in the same cell simultaneously. Initially, neither a map of the eld nor the locations of the robots are known. The only knowledge available is width W and height H of the eld, the fact that the eld is surrounded with the walls and the fact that the robots stand in the free cells. One issues a sequence of N commands, each for either of robots, and writes down the robots reports. The information that one can get from these commands and reports may be enough to determine the coordinates of the robots. Your task is to nd the minimal number of commands after which these coordinates are determined precisely. You may assume that after N commands the coordinates are known for sure. Note that sometimes the coordinates may be dened without execution of any command. You may also assume that all the reports are consistent.

Input
The rst line of the input le contains three integer numbers W , H (1 W, H 200) and N (1 N 200 000). Each of the following N lines contains a command and a corresponding report. A command consists of an integer Si the number of robot which this command is directed to and a character Di dening the direction to move. A report consists of one character Ri . 1 Si 2, Di is either N, E, S or W, Page 9 of 15

Bucharest Camp: Try Out Contest by University ITMO May 18, 2013 Ri is either Y or N. The meaning of the characters is explained before. Refer to the sample tests for the exact format of a line.

Output
Output one number the mininal number of commands that determine the coordinates of the robots.

Examples
goout.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.out

Note
Consider the rst sample test. Before any commands, there are four possible ways the robots can stand two ways for each robot. 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. The second command does not bring in any new information. The third commands result tells us that the second robot succeeded in moving west, that is, it is standing in the western cell. So the coordinates of robots are determined. Now turn to the second sample test. The rst command is the same as in the rst sample test. The second commands result tells us that the second robot cannot move west. As it is known there are no additional walls in the eld, one must conclude that the second robot stands in the western cell. This time, it took two commands to fully determine the coordinates.

Page 10 of 15

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

Problem H. Hrundys Sequence


Input le: Output le: Time limit: Memory limit: hrundy.in hrundy.out 2 seconds 256 mebibytes

Professor Hrundy has a much in common with P. M. Grundy. Once he even invented a sequence which, he hopes, will eventually make him as famous among contestants as Grundy is. The sequence is dened the following way: H (1) = 1; 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. For example, H (2) = 2, because pairwise sums 1 + 1 = 2, 1 + 2 = 3, 2 + 2 = 4 are all distinct. H (3) can not be equal to 3, because 1 + 3 = 2 + 2, but it can be equal to 4 since 1 + 4 = 5, 2 + 4 = 6 and 4 + 4 = 8 are distinct from the previous values. Your task is, given n, to nd H (n).

Input
The input le contains one integer n (1 n 1000).

Output
Output the value of H (n).

Examples
hrundy.in 1 2 3 1 2 4 hrundy.out

Page 11 of 15

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

Problem I. Invariant
Input le: Output le: Time limit: Memory limit: invariant.in invariant.out 4 seconds 256 mebibytes

In this task you will get acquainted with some interesting properties of Kaprekars transform. A Kaprekars transform of an N -digit number is dened as follows: let A be an N -digit number, possibly with leading zeros; let B be an N -digit number consisting of digits of the number A sorted in non-ascending order; let C be an N -digit number consisting of digits of the number A sorted in non-descending order; then the result of the transform is K(A) = B C . For example, consider A = 2009, then B = 9200, C = 0029 and K(2009) = 9200 29 = 9171. For some N , there exist some invariants of this transform. An invariant of the Kaprekars transform is the number I where K(I ) = I . For any N , zero is the invariant of the transform, but sometimes there are the other, nontrivial invariants. For example, for N = 4 there is only one nontrivial invariant: K(6174) = 7641 1467 = 6174. Your task is to nd all nontrivial invariants of Kaprekars transform for a given N .

Input
In the rst line of the input le there is one integer number N (1 N 60).

Output
In the rst line output the number of nontrivial invariants K . In the following K lines output the invariants themselves, one by line, in arbitrary order with exactly N digits (print leading zeros if necessary). All the numbers must be dierent.

Example
invariant.in 4 1 6174 invariant.out

Page 12 of 15

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

Problem J. Java Code Validation


Input le: Output le: Time limit: Memory limit: javalidation.in javalidation.out 2 seconds 256 mebibytes

When a programmer writes code, he or she often tries to prove correctness of pieces of code. Generally, it is impossible to prove a programs correctness using another computer program, but some simple verication tasks can, and thus should, be performed by computer. There are specialized editors and IDEs for almost any programming language. They often oer lots of attractive features besides simple error highlighting, they may notice some possible subtle errors, such as cycles that are executed only once, and show some ways to x these. This time, your task will be to write a similar tool for the Java programming language. No, not for the whole language just for some usages of one simple class. Class StringTokenizer performs splitting a string into tokens using a set of delimiters. Let us consider an example: we need to break a string .this.is simple! considering whitespaces, dots and exclamation marks as delimiters. To do this, we write: StringTokenizer t = new StringTokenizer(".this.is simple!", " .!");

The tokens are extracted one by one using a method of StringTokenizer called nextToken, which returns a string: String first = t.nextToken(); //first is "this" String second = t.nextToken(); //second is "is" One may test whether there are more tokens by calling method called hasMoreTokens, which returns a boolean value: boolean hasThird = t.hasMoreTokens(); //hasThird is true String third = t.nextToken(); //third is "simple" boolean hasFourth = t.hasMoreTokens(); //hasFourth is false If one calls method nextToken when no tokens remained, an exception NoSuchElementException is thrown. If no one catches this exception, it will propagate to outer methods and probably will lead to termination of the program. Generally, allowing it to be thrown is a programming error. String fourth = t.nextToken(); //an exception will be thrown here... doSomethingWise(); //... and this code will not be executed For the purpose of this task, we assume that StringTokenizer does not have other methods or constructors. The language to be used will also be simplied. There are only two variable types: String and StringTokenizer. In this problem, class String does not have any methods it serves just as a sequence of Latin letters, lowercase or uppercase. The simplied language is dened as follows:

Page 13 of 15

Bucharest Camp: Try Out Contest by University ITMO May 18, 2013 program statements statement new-tokenizer next-token strname tokname constant ::= statements ::= statement | statement statements ::= new-tokenizer | next-token ::= StringTokenizer tokname = new StringTokenizer( strname , constant ); ::= String strname = tokname .nextToken(); ::= a sequence of Latin letters, lowercase or uppercase ::= a sequence of Latin letters, lowercase or uppercase ::= " a sequence of Latin letters, lowercase or uppercase "

In addition to this denition, it is not allowed to dene variable names that are already dened. There is one predened variable a variable s of type String. Your task is to generate warnings for the code in the described language. More specically, there are two types of warnings: a call to nextToken will, independently of the value of s, throw NoSuchElementException; in the constructor of StringTokenizer, some characters in the delimiter string may be safely omitted. Note that you should not generate warnings for unreachable code, that is, for the code that will never be executed under any circumstances. You may nd useful that a variable s may contain an arbitrary number of Latin letters, lowercase or uppercase. No StringTokenizer will ever be created using an empty delimiter string.

Input
The input le contains a program in the language dened above. The program will always be valid. The tokens from the languages denition may be separated with whitespaces and/or line breaks. The size of the input le will not exceed one megabyte.

Output
For each call to nextToken which will always produce an exception, output the message Statement <statement-number>: An exception will be thrown.. For each call to the constructor of StringTokenizer which contains unnecessary characters in the delimiter string, output the message Statement <statement-number>: There are unnecessary delimiter characters: "<characters>". where <characters> should be substituted by actual list of unnecessary characters. All characters must be dierent and should be listed in alphabetical order, all uppercase letters come before any lowercase letter. In all the messages, <statement-number> should be substituted by the one-based number of statement which contains the warning. These messages should be listed in the order the corresponding calls are located in the code. If there is no warnings, output No warnings found..

Page 14 of 15

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

Examples
javalidation.in StringTokenizer st = new StringTokenizer(s, "qwerty"); String t = st.nextToken(); StringTokenizer uv = new StringTokenizer(t, "abc"); String a = uv.nextToken(); String bacaba = uv.nextToken(); javalidation.out No warnings found. javalidation.in StringTokenizer st = new StringTokenizer(s, "accepted"); String x = st.nextToken(); StringTokenizer xx = new StringTokenizer(x, "face"); String y = xx.nextToken(); javalidation.out Statement 1: There are unnecessary delimiter characters: "ce". Statement 3: There are unnecessary delimiter characters: "ace".

Note
Consider the second example test. The rst statement creates a new StringTokenizer with the delimiter string equal to accepted. In this string, characters c and e are specied more than once. Extra instances of these characters are, for sure, unnecessary. The third statement creates a new StringTokenizer from the string returned by the rst tokenizer. In such a string, characters a, c and e cannot appear under any circumstances because they all are in the delimiter string of the rst tokenizer. So, they are unnecessary, too.

Page 15 of 15

Вам также может понравиться