Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Escola Superior de Tecnologia e Gesto de Agueda a Universidade de Aveiro

MIUP 2009 Maratona Inter-Universitario de Programacao

Agueda, 10 of October 2009 10:00 15:00 http://mooshak.estga.ua.pt/

Contents
Team Program Preliminary remarks Event . . . . . . . . . . Documentation . . . . . Compilation . . . . . . . IDEs and Environment . Runtime Constraints . . Input Specication . . . Output Specication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 5 6 6 6 6 7 7 7 8 9 9 9 10 14 16 18 20 22 24 26 28

Documentation Online Help . . . . . . . . . . . . Contest Material . . . . . . . . . Problem A: Breakdown in space Problem B: It puzzles me! . . . . Problem C: Compute the volume Problem D: Toys for Christmas . Problem E: Greedy or Not? . . . Problem F: Calderan Hills . . . . Problem G: Domino Trouble . . Problem H: Corridor . . . . . . . Problem I: Finding Operations .

Team
Scientic Committee
Filipe Arajo, Universidade de Coimbra u Paul Crocker, Universidade da Beira Interior Pedro Guerreiro, Universidade do Algarve Pedro Rangel Henriques, Universidade do Minho Jos Paulo Leal, Universidade do Porto e Lu Seabra Lopes, Universidade de Aveiro s Margarida Mamede, Universidade Nova de Lisboa Vasco Manquinho, Instituto Superior Tcnico e Fbio Marques, Universidade de Aveiro a Rui Mendes, Universidade do Minho Isabel Nunes, Universidade de Lisboa Lu Paquete, Universidade de Coimbra s Andr Restivo, Universidade do Porto e Pedro Manuel Pinto Ribeiro, Universidade do Porto Fernando Silva, Universidade do Porto Paulo Sousa, Universidade de Lisboa Simo Melo de Sousa, Universidade da Beira Interior a Delm Torres, Universidade de Aveiro Telmo Silva, Universidade de Aveiro

Organizing Committee
Gonalo Paiva Dias c Fbio Marques a Mrio Rodrigues a Rita Santos Telmo Silva Anabela Simes o 3

ESTGA - UA

MIUP09

Sta
Catarina Almeida Ana Pereira Eliana Oliveira Paula Cipriano Daniela Resende Ana Tavares Ana Brites Liliana Mortgua a

Program
Location: Escola Superior de Tecnologia e Gesto de Agueda, a Universidade de Aveiro

When What 09H00 Reception and registration 09H30 MIUP opening session 09H45 Go to contest rooms 10H00 Contest beginning 15H00 Contest end 15H30 Problem discussion

Where Room 05.01.15 Auditorium Rooms 05.01.28/29/30 Rooms 05.01.28/29/30 Rooms 05.01.28/29/30 Auditorium

16H30 Award cerimony and closing session Auditorium

Preliminary remarks
Event
MIUP is a programming contest for teams, of atmost 3 elements, composed by higher education students of Portuguese Universities or Polytechnic Institutions. MIUP follows the International Collegiate Programming Contest (ICPC) rules. The set of problems used in MIUP is of the authorship of the Scientic Committee members. During the 5 hours of the contest (10 till 15), the teams must submit their solutions to Mooshak, an automatic judge developed at University of Porto. Mooshak allows an imediate evaluation of the submissions, delivering dierent feedback results according to the solutions. These feedbacks are: Accepted, Presentation Error, Runtime Error, Compile Time Error, Time Limit Exceeded, Memory Limit Exceeded, Wrong Answer and Invalid Function. During the contest are accepted the following programming languages: C, C++, Pascal and Java. During the competition, the judges (members of the Scientic Committee) provide online support through Mooshak. The team that manages to solve most of the problems is the winner. In case of a draw, Mooshak sums all the times needed for solving the problems (with 20 minutes penalty for every non-Accepted submission) and places rst the teams that needed less time.

Documentation
During the contest, documentation will be available at http://miup2009.estga.ua.pt/docs/. Aside from the Mooshak platform, this is the only web link accessible for contestants.

Compilation
Constraints
Maximum compilation time: 60 seconds Maximum source code size: 100kb Every source code must be submitted in a single le In case of Java submissions, the .java le has to have the same name as the class that contains the main method. There is no limit for the number of classes to be contained in that le.

Command line
gcc -ansi -lm g++ -Wall fpc -v0w -oprog 6

ESTGA - UA javac -nowarn (execution:java -Xss8M -Xmx64M)

MIUP09

Compiler versions
gcc 4.2.4 Free Pascal 2.2.0 jdk 1.6.0 06

IDEs and Environment


All machines will run Ubuntu Linux 8.04, with the following IDE/editors: Eclipse Xemacs Gnu emacs Vim Gedit Kate Kdevelop DDD Netbeans Kwrite

Runtime Constraints
Max CPU time: 1 second (except when stated otherwise) Available memory for dynamic and global variables: 64 MB Available memory for local variables and execution stack: 8 MB

Input Specication
For each problem, exists a section named Input that describes exactly the details of the input test les that are used by Mooshak. Extreme attention must be paid to this section. Although there may be exceptions, there were used the following guidelines: Generally, the rst line of the input data le contains an integer indicating the number of test sets, or the size of the test at stake. However, this may have some exceptions. The last line of the le always terminate with a \n (new line) Except when stated, white space is used as a separator. No line starts or ends with a whitespace. 7

ESTGA - UA

MIUP09

To test your program you may built your own input les that follow the exact description of the Input section of the problem. You should compile your source code using the Command Line section above. To test it just as Mooshak, suppose a.out is the compiled source code and input1 is your test le corresponding to the sample input of the problem, use the following call:

$ a.out<input1

The result should be the expected sample output.

Output Specication
As the input, the output as a specic section where you may nd rigorous description of the format of the output. Any small detail may be a reason to a Presentation Error or a Wrong Answer. Pay attention to the number of whitespaces or \n or capitalization. To simulate the Mooshak evaluation, you may use the sample output in conjuntion with the sample input, as follows:

(command to produce the result) $ a.out<input1>output.mine (usage of di to compare results) $ di output.mine output.correct

Documentation
Online Help
Contestants have three sources of online help: Mooshak automatic help. Press the help button when youre logged in. This is a page that has information on available (memory and time) limits, compilers and a description of a number of Mooshak items (e.g. explanation of what a Presentation Error means). Mooshak online judge help (Questions). Press the Ask button and you can ask a question directly to the judge. Dont forget to choose the correct problem (otherwise the judge wont know what your question refers to). And please check before in the Question section if there is information that already satises your doubts. STL, Javadocs and Free Pascal docs. Just access http://mooshak.estga.ua.pt/docs/.

Contest Material
MIUP follows the ICPC SWERCs Rules, as such it is not allowed to bring reference materials (book, program listings or notes). It is not allowed the usage of any electronic device. You may bring pens and pencils. The paper is provided by the organization. Your team may prepare a notebook of no more than 25 private A4 pages, font greater or equal to 8. Each team member may prepare use an exact copy of the notebook. You have to submit your team notebook notebook at the registration desk on arrival. It will be made available to you at the beginning of the contest.(note excerp from SWERC rule page)

ESTGA - UA

MIUP09

Problem A Breakdown in space

Introduction
Captain Zapp Brannigan is having some serious problems with the navigation system of his Starship Nimbus. The ship has only one last impulse of energy left and it can only be steered by using the gravity of nearby asteroids. Luckily, he is exactly in the middle of the only asteroid eld in the galaxy with a ship repair station. Zapps ship can only navigate in 4 dierent directions (up, down, left and right) and can only use the gravitational eld of an asteroid if he passes right by its side. As all starships, the Nimbus is equipped with a Gravity Nullier that allows the ship to ignore the asteroid gravitational grip of an asteroid at any time. In this way, there are 4 possible maneuvers that can be executed by The Nimbus when approaching an asteroid: continue straight ahead, turn left, turn right and reverse direction (see Figure 1). Asteroids can be extremely close to each other creating larger asteroids. Figure 1 shows how in some asteroid congurations it is impossible to do certain maneuvers.

Figure 1: Possible maneuvers for Zapps ship.

Figure 2: Sample Problem and 2 Possible Solutions with dierent lengths.

Figure 3: The solution on the right should be choosen.

10

ESTGA - UA

MIUP09

Problem
Zapp Brannigan asked you, Kip Kroker, to develop a program that will discover the quickest way to go from the ships current position to the repair station without smashing into an asteroid or exiting the asteroid eld. The program should be able to receive a map containing the position of the ship, the asteroids and the repair station and plot the proposed course. Figure 2 shows a possible conguration and two possible solutions. The solution on the right should be chosen as it is shorter. Furthermore, Zapp is extremely superstitious and he has had enough bad luck as it is. Given two dierent paths with the same length, he will always prefer the path that goes up at the point where they dier. If none of the paths go up at that point, he will prefer the one that goes right. If none of the two paths go up or right he will choose the one that goes down. Figure 3 shows a possible conguration and two possible solutions with the same length. The solution on the right should be chosen as goes right when the other one goes down at the point they dier.

Input
On line containing the width (w) and the height (h) of the asteroid eld. Followed by h lines, each one containing w characters. The following are the possible characters: . empty space; * the initial position of The Nimbus; # the position of each asteroid; + the repair station.

Constraints
2 w 20 2 h 20

Output
The rst line of the output should contain a rst line indicating the length of the best solution (not including the starting point). This line should be followed by h lines, each one containing w characters. The following are the possible characters: . empty space; * part of The Nimbus route to the space station; # the position of each asteroid; + the repair station. If there is no solution, the program should output a single line containing no solutions.

Sample Input 1
8 8 ........ ........ .#..#... .#.##... 11

ESTGA - UA ........ *.#.#.#. .+...... ........

MIUP09

Sample Output 1
solution length: 12 ........ ***..... *#*.#... *#*##... ***..... **#.#.#. .+...... ........

Sample Input 2
10 10 .......... *......... .....#.... .......... .......... ..#....#.. .......... .......... .+........ ..........

Sample Output 2
solution length: 26 .......... *******... .....#*... ......*... .********. .*#...*#*. .*....***. .*........ .+........ ..........

Sample Input 3
10 8 .......... .*........ ....##.... ...####... .......... ....###... 12

ESTGA - UA .......+.. ..........

MIUP09

Sample Output 3
no solutions

13

ESTGA - UA

MIUP09

Problem B It puzzles me!

Introduction
I work at the quality control department of the Puzzle-Addicted Corporation. My job is to randomly select jigsaw puzzles ready for the market, solve them and detect possible deciencies. It is a great job for a person that loves jigsaw puzzles as much as I do, but solving them by hand is becoming impracticable as sales increase and production is outsourced to the far east. Therefore, I have been challenged to automate the process as much as I can. Using a robotic arm and a television camera I was able to sort out and recognize puzzle pieces. Now I need your help to write a program to process puzzle shapes and solve jigsaw puzzles. We need to solve it without looking to the image printed on the front part of the pieces, since the camera only recognizes shapes of pieces but not the details of the picture. You can assume that the jigsaw puzzle has a single solution (pieces only t together in one way, unless they are all rotated by the same angle) and, at this stage, we will only process valid (solvable) puzzles. Your program has to determine where each piece goes in the puzzle so that we can use the robotic arm to assemble it.

Problem
Our jigsaw puzzle pieces have 4 sides. Apart from puzzle edges, each piece side has either a tab or a blank, and tabs and blanks of adjacent pieces always t each other perfectly. There are 9 dierent types of tabs and 9 corresponding types of blanks, plus the straight side for puzzle edges. Combining these side types we can produce pieces with many dierent shapes. Nevertheless, all pieces are approximately a square and a jigsaw puzzle can be seen as a matrix with a given number of lines and columns. Using the robotic arm and the camera we can place all pieces on the same side. Actually, they are all with the back side up (that is, with the image facing down) since this facilitates shape recognition and we do not need the picture anyway. Then all pieces are placed in a similar orientation, that is, each with a sides facing a standard direction (north, east, south and west). However, pieces may have to be rotated in order do t tabs with corresponding blanks. Fortunately, given the large number of shapes of pieces, there is always a single way to t all pieces together. Your task is to write a program that reads in a sequence of pieces of a puzzle with given dimensions and writes the correct sequence of pieces in a standard order, specifying a rotation for each piece to place it in a correct orientation.

Input
The input is a jigsaw puzzle description. The rst line is a pair of integers dening the jigsaw puzzle dimension, respectively height and width. The following height x width lines to describe each piece and the order of the pieces is irrelevant. Each piece is described by ve (5) integers. The rst is the ID of the piece. The following represent the type of each side in a standard order north, east, south and west in the current orientation. To keep things simple, tabs are numbered 14

ESTGA - UA

MIUP09

using positive integers and blanks are numbered using negative integers. Matching tabs and blanks have the same absolute value (e.g. tab 3 matches with blank -3). The edges are numbered with 0.

Constraints
All input values are integers 3 < width <= 15 2 < height <= 15 0 < id <= 225 -10 < side < 10

Output
The output is a sequence of height x width lines with two integers identifying the correct order and orientation of pieces, from left-to-right and top-to-bottom. The rst integer in a line is the ID of piece, given as input, and the second is the piece rotation. Rotations are non-negative multiples of 90 degrees, less than 360 degrees. Since the program is not seeing the picture, it may solve the jigsaw puzzle upside down. To make it unanbiguous you must output the sequence of pieces, respecting the dimension of the puzzle (height and width) and with ID of the upper left corner higher than the ID of lower right corner.

Sample Input
2 3 1 0 -2 -3 -1 2 0 0 3 1 3 -1 2 0 0 4 -2 4 0 0 5 3 -3 0 -4 6 0 0 1 2

Sample Output
6 1 3 4 5 2 270 0 180 0 0 90

15

ESTGA - UA

MIUP09

Problem C Compute the volume

Introduction
You have a new job as a developer of educational software for kids in a toys company. They asked you to develop a package to help the kids to solve very basic geometric problems. However, you got stuck with a very simple problem: how to compute the volume of a certain type of three-dimensional orthogonal polyhedron, as shown in the Figure below?

Figure 4: A Polyhedron Fortunately, the polyhedron has some interesting property that can be explored from a computational point of view. Are you able to see it?

Problem
Consider the component-wise order among d-dimensional vectors, that is, given two vectors q, p Rd , q p q = p and qi pi , for i = 1, . . . , d. If p q, we say that p dominates q (or q is dominated by p). If p q and q p, then we say that both are nondominated. In our particular case, we have that d = 3. Given a nite set P of nondominated points in the positive orthant R3 , let the hole-free orthogonal polyhedron be dened as follows 0
3 = {q R0 : q p, for some p P }.

Therefore, polyhedron corresponds to the space that is dominated by all points in the set P . The goal is to compute the volume of .

Input
For each test case you have the set of points in P . The rst line contains the number of points. Then, each following line contains the coordinates of each point, separated by a single space. The coordinates are positive integers. Note that there may be points with repeated coordinates in the same axis, but no two points are equal.

Constraints
Each test case contains at most 50 000 points. The largest coordinate value is at most 200 000. The computed volume is at most 4 1014 . 16

ESTGA - UA

MIUP09

Output
For each test case, output the volume in a single line.

Sample Input

Figure 5: Polyhedron for the sample input The following test case corresponds to the polyhedron of Figure . 3 1 2 3 2 3 1 3 1 2

Sample Output
13

17

ESTGA - UA

MIUP09

Problem D Toys for Christmas

Introduction
A network of children care institutions is preparing Christmas. They have received many toys from some manufacturers and they already know which of them are suitable for each child. Now, they need to nd out how those toys should be distributed to the children so as to maximise the number of children with a suitable toy. Notice that, in most cases, there are several units of the same toy. Therefore, the same toy might be given to several children. Besides, each childs supervisor selected the suitable toys for the child from the list of existing toys, taking into account age, preferences, and personality, but disregarding the number of units of each toy. There may be children for which no toy is suitable. To ensure that all children will be happy, it has been decided that every child will receive an appropriate toy, even if this requires some more toys to be acquired. So, at this moment, it is crucial to nd a distribution of the existing toys that maximises the number of children with a suitable toy (that is to say, a distribution of the existing toys that maximises the number of children with a toy selected by the supervisor). The number of children with a suitable toy in such a distribution will be called the maximum number of happy children.

Problem
Given a set of toys, the number of units of each toy, a list of children, and a set of suitable toys for each child, the goal is to compute the maximum number of happy children.

Input
The rst line of the input contains two integers, T and C, separated by a single space. T is the number of toys and C is the number of children. Toys and children are identied by integers, ranging, respectively, from 1 to T and from 1 to C. Each of the following T lines contains an integer U , which denotes the number of units of a toy (from toy 1 to toy T ). Each of the following C lines species the set of suitable toys for a child (from child 1 to child C). A set of toys contains the number N of toys, followed by N distinct integers representing the toys. All integers are separated by a single space.

Constraints
1 T 100 1 C 600 1 U 20 0 N 30

Output
The output has a single line with the maximum number of happy children. 18

ESTGA - UA

MIUP09

Sample Input
3 6 1 2 3 2 0 1 1 2 1 7

1 2 3 2 1 2 3 2 3 2

Sample Output
5

19

ESTGA - UA

MIUP09

Problem E Greedy or Not?

Introduction
The very classical change making problem is the problem of representing a given value with the fewest coins possible in a given coin system. In fact the problem of giving change for a certain amount is solved everyday by every cashier in the world. It is a very simple problem as it follows a very simple procedure: when giving change, always use the largest coin possible. The euro system uses the following sequence: (200, 100, 50, 20, 10, 5, 1). Thus to give change for 5 euros and 47 cents (i.e. 547 cents), you use two coins of 2 euros, one coin of 1 euro, two coins of 20 cents, one coin of 5 cents and two coins of 1 cent. Thus we have 547 = 2 200 + 1 100 + 2 20 + 1 5 + 2 1, thus using 8 coins. This is the minimum number of coins possible. Depending on the coin system, one can use a simple greedy algorithm for solving the problem (by repeatedly taking the largest coin whose value is no larger than the remaining amount), but in general this problem is NP-Hard. A coin system is said canonical if it allows to solve the change making problem by a greedy algorithm. As a matter of fact, consider the coin system (5, 4, 1), the value 13 using the greedy algorithm yields 13 = 2 5 + 3 1 and uses 5 coins when another way of giving change would only use 4 coins, 13 = 3 4 + 1 1. Thus, the coin system (5, 4, 1) is not canonical. Note that this situation also occurs with the value 8. The greedy solution is one coin of 5 and three coins of 1, despite the fact that two coins of 4 are enough. In fact, 8 is the smallest counter-example in this coin system that exhibit such a behavior. The smallest counter-example x of a coin system has nice properties. For instance, it cannot use the same type of coins of a greedy solution. If this were not true (both the optimal solution and the greedy solution of x use the same coin ck ), we can subtract ck from both the solutions and obtain a greedy and a optimal solution for x ck . Thus x is not the smallest counter-example.

Problem
In this problem, we only consider coin systems that can express any amount. Given a coin system, your task is to determine if it is canonical or not. I.e. if one can use the greedy algorithm to make the minimum number of coins possible for any amount, or not.

Input
The input is organized as follow: The rst line contains the number n of coins in the considered coin system C = {c1 , . . . , cn }. The remaining n lines contains the value of the coins of C, one value per line. Therefor, the ith of these lines contains ci . Admit that for all i in {1, n 1}, ci > ci+1 and cn = 1.

Constraints
The size of the coin system is limited to 300 and the value of the biggest coin is limited to 5000

Output
If the coin system is canonical then the output is the single line with the word YES. Else the output is the single line with the word NO. 20

ESTGA - UA

MIUP09

Sample Input
3 4 3 1

Sample Output
NO

21

ESTGA - UA

MIUP09

Problem F Calderan Hills

Introduction
From the window of my room, I can see the Calderan hills. Its not the Himalayas, I know. Still, theyre nice hills. They are not very tall, but they are quite numerous. Indeed, as I look at them form afar, I see many hilltops against the blue sky and it is as if they are a string of hills from west to east, all in the same plane, perpendicular to my line of view: theres a hill, then a valley, then another hill, another valley, and so forth. And yes, my house is south of the hills, and I am facing north. One of the nice things about Calderan hills, which enormously pleases the mathematician within us, is that the slopes are perfectly straight at exactly 45 degrees. So, the Calderan hills resemble a carpenters saw, with triangularly protruding teeth. Well, the analogy is not perfect, because some teeth are more protruding than others, on the hills, whereas in the saw they should be equally sized. In fact, I can see clearly the tallest hill, and I have been told that the view from its top is astounding. One of these days, I will go hiking on the hills, starting from the west, and I will walk to the top of that tallest hill. From my window, I fantasize that I will go up each hill, to the top, and then down the valleys, to the bottom where the next hill commences, etc., always along the crest, so that, if I was at my window, I would see me against the blue sky in the background, all the time. During the stroll, I will walk uphill and I will walk downhill, until I reach the top of the highest peak. The question is: what distance will I walk uphill and what distance will I walk downhill?

Problem
Given the x, y coordinates of the peaks of each of the hills, where x is measured from west to east, and y is measured vertically from the sea level, compute the total length that one must walk, uphill and downhill separately, from the western base of the westernmost hill to the top of the tallest hill. In case two or more hills have the same greatest altitude, consider the rst such hill, from the west. The western base of the westernmost hill is the x-coordinate of the point where the western slope of the westernmost hill intersects the x-axis. 22

ESTGA - UA

MIUP09

Input
The rst line of the input le has one number, N, representing the number of hills. N lines follow, each with two integer numbers, X and Y, representing the x, y coordinates of the each peak, in strictly increasing order of the x coordinate.

Constraints
1 N 100000 0 < X < 10000000 0 Y < 10000 Please note that although the altitude of the peaks is always greater or equal to zero, the altitude of the bottom of the valleys may be negative, but in all cases it is greater than -10000. The numbers on the output shall be written using xed point decimal notation roundedto six decimal places.

Output
The output le has only one line with two oating point numbers, separated by a space. The rst number represents the length of the uphill component of the walk from the base of the westernmost hill to the tip of the rst tallest hill, and the second the length of the downhill component. I am a precision freak: the coordinates have been taken exactly, and are expressed in an integer number of metres. I absolutely want my two lengths rounded to the micrometre.

Sample Input
4 5 3 9 5 14 2 17 3

Sample Output
8.485281 1.414214

23

ESTGA - UA

MIUP09

Problem G Domino Trouble

Introduction
You have probably already played dominoes. Like cards and dice, they provide a simple and basic gaming device suitable for all ages and nationalities, with guaranteed fun for everyone. As you know, dominoes are tiled pieces divided in two sections, each one having from zero to six small dots. Here is an example of 6 dierent domino pieces laid out on a table:

Counting the number of dots in the upper row you obtain a sum of 14 (1+5+3+2+1+2), while if you sum the lower row you obtain 20 (3+4+6+0+4+3). Looking at this you wonder if by taking out some pieces out of the table you can obtain an equal sum in both rows. Using exactly the same pieces as in the gure above, if you take out the following two pieces, you obtain an equal sum of 10 (1+5+2+2=3+4+0+3):

In this case, this is the minimum number of pieces you must remove in order to equalize the sum of both rows. And in the general case? How many pieces must you remove?

Problem
Given N domino pieces and the orientation of the pieces on the table, your task is to nd the minimum number D of domino pieces you have to remove in order to make the sum of the dots in the upper row equal to the sum of the dots in the lower row. D can be zero (meaning that the sums are already equal) and it may happen that you must remove all pieces in order to equalize the sum (meaning that the upper and lower sum would be equal to zero). Note that you cannot rotate the domino pieces in order to swap the lower and upper values.

Input
In the rst line comes a single integer N indicating the number of domino pieces in the table. Then follow exactly N lines, each one with two integers Ui and Li indicating respectively the upper and lower number of dots in that piece.

Constraints
1 N 5000 0 U i , Li 6 24

ESTGA - UA

MIUP09

Output
You should output a line with a single integer D indicating the minimum number of pieces to remove in order to equalize the number of dots in the upper and lower rows as described before. If no combination reaches an equal number of dots, you may remove all the pieces to have 0 dots in both rows.

Sample Input 1
6 1 5 3 2 1 2 3 4 6 0 4 3

Sample Output 1
2

Sample Input 2
4 1 4 2 0 5 3 1 6

Sample Output 2
4

Sample Input 3
3 4 4 3 6 5 2

Sample Output 3
0

25

ESTGA - UA

MIUP09

Problem H Corridor

Introduction
Wally is a robot whose body is cylindrical with a given diameter. Wally has had enough from his master and wants to escape to explore the outside world. All that stands between Wally and the outside world is the Corridor. The Corridor is full of deadly vertical (oor-to-ceiling) laser beams and Wally has to zigzag between the beams to escape. Can Wally escape through the Corridor?

Problem
Given a rectangular Corridor, the diameter of Wally, and the position of the vertical laser beams, your mission is to nd if it is possible for Wally to escape his master through the Corridor (without touching the deadly beams). Assume that the laser beams have diameter 0 and therefore Wally can pass between two beams [resp. a beam and a wall] if and only if the beam-to-beam [resp. beam-to-wall] distance is greater than or equal to the diameter of Wally.

Input
All values in the input are positive integers and the positions of the laser beams are coordinates in a two-dimensional Cartesian coordination system. The rst line of the input species the length L (x-axis) and width W (y-axis) of the rectangular Corridor. The lower left corner of the Corridor is assumed to be (0,0). The following line of the input indicates the diameter D of Wally. Then, the next line species the number B of laser beams, and each of the following B lines species the coordinates of each laser beam (Bx , By ). Assume that Wally starts completely outside the Corridor.

Constraints
2 L, W 10000 1 D < L, W 1 B 5000 1 Bx < L 1 By < W 26

ESTGA - UA

MIUP09

Output
The program should print a single phrase: Wally is free or Poor Wally. Wally is free should be printed if it is possible for Wally to escape his master through the Corridor. Otherwise, the program should print Poor Wally.

Sample Input
50 6 9 48 48 48 48 48 48 48 48 48 50

5 10 15 20 25 30 35 40 45

Sample Output
Poor Wally

27

ESTGA - UA

MIUP09

Problem I Finding Operations

Introduction
The basic mathematical operations that supports all theorems, formulas, calculus, etc, are +, -, *, /, Powers, Exponentiation and identities. This problem involves the operations (+, -, *, /).

Problem
Find the number of combinations of operations that makes a mathematical expression equal to zero using a set of numbers. For example, using the numbers 1,2,4,1 (in this order) the operations that lead to an expression equal to zero are +, -, +: 1 + 2 -4 +1 = 0 Notes: the order of numbers and operations is important. the signal / represents an integer division. the operations are executed as they appear in the expression. The precedence mathematical rules should not be considered.

Input
The inputs are the mathematical expression operands in the correct order, separated by space character. In the above example: 1 2 4 1.

Constraints
The operands are non-zero positive integers. There are, at maximum, 4 operations.

Output
The output is the number of combinations that lead to an expression equal to zero.

Sample Input 1
1 3 5 8

Sample Output 1
17

Sample Input 2
1 2 5 3

Sample Output 2
12 28

ESTGA - UA

MIUP09

Sample Input 3
123 10 1 10

Sample Output 3
0

29

You might also like