Sunteți pe pagina 1din 14

Competencia Iberoamericana de

Informtica por Correspondencia

Problemset
(5 hours 4 problems)

July, 2012
Information

Problems A B C D

Symmetries in the The New Rafael's


Title Boxes Complicated Traffic
Mirror House

Memory
128 MB 128 MB 128 MB 128 MB
Limit

Time Limit

(Pentium 4 1 second 1 second 1 second 1 second

@ 2,4GHz)

Maximum
100 100 100 100
Points

Your program should always read from the standard input (stdin), and write to the standard output

(stdout). All lines from the input and output should be terminated with a single newline character (see

example source code);

The given limits are per test case. There will be a minimum of 15 single test cases on each problem;

The solutions will be tested on a Linux environment;

The available programming languages are:

Programming
C C++ Pascal
Language

Extension to

use
.c .cpp .pas
Compiler GCC GCC FreePascal

Compiler
4.3.2 4.3.2 2.2.4
Version

Command
"gcc -O2 -lm prog.c" "g++ -O2 prog.cpp" "fpc -O2
Line prog.pas"

#include <stdio.h> #include <iostream> program sum;


Example code

int main() { int main() { var


(read two int a, b; int a, b; a, b: integer;
numbers

separated by a
scanf("%d %d", &a, &b); std::cin >> a >> b; begin
space and
printf("%d\n", a+b); std::cout << a+b << std::endl; read(a, b);
writeln(a+b);
print their
return 0; return 0; end.
sum) } }

Important note: Given the fact that the contest will be held on different dates in different countries, you are

strictly forbidden to publish in any form any kind of information regarding the problems. When all countries

have already held the contest, you will be informed and only then can you publish any information.

CIIC 2012 2/14


Problem A - Symmetries in the Mirror

Many devices, such as a calculator or a digital clock, show the numbers using the SSD system (seven-segment

display). This kind of decimal number display is very simple and consists of seven small segments that are

turned on (or off) according to the number to represent, as illustrated in the following figure:

Rafael was in front of the mirror with his calculator, when he noticed that the number on the display, which was

2005, stayed exactly the same on its reflected image in the mirror:

Curious as he is, Rafael tried to discover more numbers that presented this property. For that, he started by

writing in its calculator all possible digits, taking notice of the reflex in the mirror, as indicated in the following

figure:

The digits'0', '1' and '8' remain "the same" when seen in the mirror, that is, they represent the same digit.
'2' transforms into '5' (and vice-versa). '3', '4', '6', '7' and '9', when reflected, do not represent
any other valid digit. Note that besides these transformations on individual digits, the number becomes

inverted, that is, in the reflex the leftmost digit becomes the rightmost digit, and so on.

When the reflex of a number in the mirror represents the exact same original non reflected number, we say that

the numbers presents "reflection symmetry". The first 10 positive integer numbers that present this property

are the following: 1, 8, 11, 25, 52, 88, 101, 111, 181 e 205. Other bigger numbers, such as 8528 or 2110115 also

have reflection symmetry. On the other hand, numbers such as 22, 10, 347 or 6776 do not present this

characteristic.

Given any number, Rafael thought that it would be interesting to find the closest number which would present

reflection symmetry. For instance, if the number is 200, the answer would be 205, because there is no other

number closer to 200 with the desired property. 181 also presents the kind of symmetry we want, but it is more

distant, because |200-181| is bigger than |205-200|. Can you help him with this task?

The Problem

Given a list of N positive integer numbers, for which one of them you have to discover which is the closest

number with reflection symmetry. If the given number has this kind of symmetry, then the answer to give is

the original number itself. Otherwise, you should compute which is the integer number with reflection

symmetry that presents the smaller difference to the original number (in absolute value). If there exists more

than one number with the same difference, print the smaller one.

CIIC 2012 3/14


Input

On the first line comes an integer number N, the quantity of numbers to consider.

Then follow exactly N lines, each one containing a single positive integer number X , indicating the i-th
i

number. These numbers can come in any order and there may exist repetitions.

Output

The output should be made of N lines, each one in the format"X : Y " (without the quotation marks), where Y
i i i

is the smaller number with reflection symmetry that is closest to X , as was described before. Note that the
i

numbers should in the same order of the input.

Restrictions

The fo llowing limits are guaranteed for all the test cases that will be used for evaluating your program:

1  N  10 4
Quantity of numbers

1  X  10
100
Numbers to consider
i

Note about the Evaluation

For a set of test cases worth 30% of the points, it always happens that N 10 and X 10
i
5
.

For a set of test cases worth 60% of the points, it always happens that X 10
i
9
.

Example Input

5
123
20
52
5432
888

Example Output

123: 111
20: 25
52: 52
5432: 5522
888: 888

CIIC 2012 4/14


Problem B - The New Rafael's House

After having lived for 3 years in the same place, Rafael decided that it is time to search for a new house in the

most prestigious neighborhood of the city. Having found a house with a very good price, Rafael wants to start

by replacing all the wooden floor of his new house (except divisions like the bathroom or the kitchen) by a

carpet floor, so that he will not be cold.

Rafael went then to the International Center for Properties and Carpets (Centro Internacional de Inmuebles y

Carpetas - CIIC). In there we was surprised: they only sold square segments of carpet with integer sizes (in

meters), each one of them sold at the same price, regardless of its size. CIIC will put the carpet in place in the

new house as long as the following conditions are respected:

The carpet segments cannot be cut;

All the positions of the new house, except for the divisions that are specifically marked for not having

carpet, should get covered by exactly one carpet segment;

No carpet segment can be at outside of the house.

CIIC only puts the carpet in its place, and it is the client that has to plan which segments to buy and where to

put them. The new house can can be thought of as a rectangle of integer width and height (in meters). The

divisions that should remain without carpet, can also be thought of as rectangles, of smaller sizes, with integer

width and height.

The following figure illustrates a house of 7x6 meters, and two divisions that should not have carpet: one in the

top of 3x1 meters, and the other one in the bottom of 3x2 meters. If the cost of each segment is 25 euros, a

possible way of minimizing the price its to put a 4x4 segment, a 3x3 segment, and two 2x2 segments, as

illustrated. On total, he would have to buy 4 segments, with a final cost of 100 euros.

Rafael needs your help for discovering the most economic solution, that is, the one that minimizes the amount

of needed money for renovating the floor of his new house.

The Problem

Given the integer dimensions of the new house, N and M, representing its width and height, a set of D

rectangular divisions where no carpet should be put, and the price P of each square segment of carpet

(regardless of its size), your task is to discover the least amount of money to spend in order to renovate floor of

the house with carpet, as was described before. You can only use square segments, not being able to cut them,

and the entire house (with the exception of the indicated divisions) should be covered with carpet.

CIIC 2012 5/14


Input

The first line contains two integers N and M, separated by a single space, indicating the house has NxM

meters.

The fo llowing line contains a single integer D, the number of divisions that should not be covered in carpet.

Then follow exactly D lines describing these divisions, each one with four integers X1 Y1 X2 Y2 , indicating
i i i i

that there is a division with corners on cells (X1 ,Y1 ) and (X2 ,Y2 ). The bottom leftmost cell is (1,1). These
i i i i

divisions are always fully contained inside the house and do not intersect each other. It is also guaranteed that

there is always a part of the house that must be covered in carpet.

Finally, the last line contains a single integer P, the price of each carpet segment.

Output

A single line containing one integer number representing the minimum price to pay for renovating the floor

with carpet, as was explained before.

Restrictions

The fo llowing limits are guaranteed for all the test cases that will be used for evaluating your program:

1  N,M  20 House dimensions

0  D < N*M Number of divisions that should not be covered with carpet

1  X1  X2  N Dimensions of the X axis for the divisions with no carpet


i i

1  Y1  Y2  M
i i
Dimensions of the Y axis for the divisions with no carpet

1  P  1 000
i
Price of each square segment

Note about the Evaluation

For a set of test cases worth 40% of the points, it always happens that N and M are smaller or equal than 6.

Explanation: the first example

corresponds to the figure shown


Example Input Example Input
before. The second example
1 2 corresponds to the following figure:

7 6 5 5
2 3
5 1 7 2 1 2 2 5
5 6 7 6 1 1 1 1
25 3 1 5 2
100

Example
Example
Output 1
Output 2
100
200

CIIC 2012 6/14


Problem C Boxes

In a wharehouse, Rafael and Ana have the task of putting a series of objects inside some boxes. Their way of

doing this is the following:

There exist a set of boxes aligned in a row, all with the same maximum storage capacity C;

Rafael is always on the left side of the row, and Ana on the right side;

Each one of the two friends start by putting objects on their end of the row, always choosing to store the

object on the nearest box that still has enough capacity (it may happen that Rafael has to use the

rightmost box, the first box of Ana, and vice-versa);

Each one of the two friends has a list with an arbitrary number of objects, each one with a certain size.

These lists may have different quantities of objects (potentially zero) and each person must store their

objects in the order that they are given;

The boxes are filled alternately (taking turns), starting with Rafael (in case he has any object to store).

Therefore, the order is: 1st Rafael's object, 1st Ana's object, 2nd Rafael's object, etc.

Imagine for example that: the capacity of the boxes is 5; that Rafael has two boxes, the first one of size 4 and

the second one of size 2; that Ana has two objects, both of size 2. The following figure illustrates what would

happen if there existed two boxes to store the objects:

We start with Rafael. The object of size 4 is stored on the leftmost box, #1. Then, Ana stores her first object on

the closest box with enough space, which is #2. Then it's Rafael's turn again, who must now store an object of

size 2. Since it does not fit on box #1 (only an object of size 1 would fit in there), it is stored on the following

box, #2, which has enough space. Afterwards, it's Ana's turn again, but she does not have space in any box for

her second object. In this way, two boxes are not enough to store all the objects.

If there were 3 boxes, would this procedure terminate successfully? The following figure illustrates the initial

and final position of all objects showing that yes, in this case, 3 boxes would be enough to store all the objects

following the described procedure:

CIIC 2012 7/14


And for a general case? What would be the minimum number of boxes needed? You have to help Rafael and

Ana some money on box buying by computing this number.

The Problem

Given the capacity C of each box, the number R of Rafael's objects and respective R sizes, the number A of
i

Ana's objects and respective A sizes, your task is to compute the minimum quantity of boxes needed to store
i

all the objects following the given procedure.

Input

The first line contains a single integer C, the maximum storage capacity of each box.

Then comes a line containing an integer R, the number of Rafael's objects, followed by R lines, each one

containing a single integer R , the size of the respective i-th object, with the list coming in the same order the
i

objects should be stored.

Afterwards comes a line containing an integer A, the number of Ana's objects, followed by A lines, each one

containing a single integer A , the size of the respective i-th object. As in the case of Rafael, this list comes in
i

the same order the objects should be stored.

Output

A single line containing one integer number representing the smaller number of boxes needed in order to have a

successful end for the described procedure, with all the objects being stored in some box.

Restrictions

The fo llowing limits are guaranteed for all the test cases that will be used for evaluating your program:

1  C  1 000 000 000 Capacity of each box

0  R, A  50 000 Number of objects to be stored by Rafael and Ana

1  R , A  C Size of each object


i i

Note about the Evaluation

For a set of test cases worth 30% of the points, it always happens that R and A are smaller or equal than 500.

For a set of test cases worth 60% of the points, it always happens that R and A are smaller or equal than 5000.

CIIC 2012 8/14


Example Input 1 Example Input 2

5 5
2 4
4 3
2 2
2 1
2 5
2 3
3
4
1
Example Output 1

3
Example Output 2

CIIC 2012 9/14


(blank page)

CIIC 2012 10/14


Problem D - Complicated Traffic

One of the biggest problem of Rafael's city is its complicated car traffic. Every day he goes from home to work

during the rush hour and a lot of time is spent completely stopped in the traffic, something which really irritates

him. Because of that, Rafael decides to carefully plan the best path to fo llow.

All the years he spent driving in the city have allowed him to make a detailed map with traffic information. For

simplifying, Rafael decided to numerate the several city localizations with numbers from 0 to N-1, with his

house always on position 0 and the work on position N-1. The map defines roads between a pair of positions,

indicating the respective average car velocity when following that road. Note that all roads allow circulation in

both ways. Given a path, its final value is equal to the smallest average velocity on of its constituent roads.

The following figure illustrates a path between 0 (house) and 8 (work). This path goes trough positions 0, 2, 4,
6 e 8, in this order, and has a value of 22 Km/h, the minimum of the average velocities of its roads, which are

40, 22, 28 and 50.

We can do better if we follow a different path, indicated in the following figure, which has a value of 32 Km/h,

larger than the path before:

Rafael knows that the mayor has some money available for renovating some of the roads. A renovated road

becomes an avenue with two lanes in each way, which effectively doubles its average velocity. For instance, if

the road between 2 and 3 is renovated, its average velocity would become 48 Km/h, and the following path of

value 35 Km/h (better than the ones shown before) would become possible:

CIIC 2012 11/14


In reality, the mayor will have money for renovating at most K roads. Since Rafael thinks that its own path is

very important, he decides to send the mayor a letter, explaining the roads that should be renovated to allow

him the best possible path. For instance, if K was 1, the scenario explained on the last figure would be the best

possible. If K was 2, than the best would be to renovate the roads between 2 and 4, and between 4 and 6,

allowing for a path of value 40Km/h, as illustrated on the following figure:

And for a general case? Which roads should be renovated to allow the best possible path for Rafael?

The Problem

Given a map with N positions, a set of E roads, with average car velocities between A and B defined by V
A,B

and a number K of roads that we can renovate, your task is to compute the value of the best possible path for

going from position 0 (the house) to position N-1 (the work), after at most K renovations are made.

Input

The first line contains a single integer N, the number of map positions. The second line contains a single integer

E, the number of existing roads.

Then come E lines, each one containing three integers A B V , separated by a single space, indicating that
A,B

there exists a road between A and B with average velocity of V . The roads can come in any order and there
A,B

are never two roads between the same pair of positions. There are no direct roads from a position to itself and

there is always at least one path between 0 and N-1.

Finally, the last line contains a single integer K, the number of roads that can be renovated.

Output

A single line containing one integer representing the value of the best possible achievable path by doing at most

K renovations.

The value of a path its the minimum average velocity of one of its roads. The best path is the one with the

largest possible value. Note that you can choose the renovations that will give origin to the best path and that a

single road can only be renovated once. If K is zero, then the result corresponds to the best possible path

without any road renovation.

Restrictions

The fo llowing limits are guaranteed for all the test cases that will be used for evaluating your program:

2  N  5 000 Number of positions in the map

1  E  50 000 Number of roads

1  V  200 Average velocity of a single road


A, B

0  K  20 Amount of possible road renovations

CIIC 2012 12/14


Note about the Evaluation

10, E40 and K1.


For a set of test cases worth 40% of the points, it always happens that N

Example Input 1 Example Input 2

9 9
11 11
0 2 40 0 2 40
2 4 22 2 4 22
4 6 28 4 6 28
6 8 50 6 8 50
0 1 32 0 1 32
1 3 32 1 3 32
3 5 43 3 5 43
5 7 35 5 7 35
7 8 47 7 8 47
2 3 24 2 3 24
4 5 21 4 5 21
1 2

Example Output 1 Example Output 2

35 40

CIIC 2012 13/14


(blank page)

CIIC 2012 14/14

S-ar putea să vă placă și