The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox...

20
NWERC 2005 The 2005 ACM Northwestern European Programming Contest KTH - Royal Institute ofTechnology, Stockholm, Sweden The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox D Bowlstack E Pesky Heroes F Reduced ID Numbers G Tantrix H Guardian of Decency I Up the Stairs

Transcript of The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox...

Page 1: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

NWERC 2005The 2005 ACM Northwestern European Programming Contest

KTH - Royal Institute of Technology, Stockholm, Sweden

The Problem Set

A Unequalled ConsumptionB Declaration of ContentC LaserboxD BowlstackE Pesky HeroesF Reduced ID NumbersG TantrixH Guardian of DecencyI Up the Stairs

Page 2: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

Almost blank page

Page 3: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

Problem A: Unequalled Consumption 1

Problem A: Unequalled Consumption

The Association of Candy Makers is preparingto launch a new product. Its idea is old witha novel twist: it simply sells boxes of candies.But since people are what they consume and ev-eryone wants to be unique these days, the ACMwants every candy box to be unique, in the sensethat no two boxes should contain the same com-position of candy types.

The ACM is only able to make a small num-ber n of different types of candy, but while lim-ited in imagination, it is virtually limitless in re-sources, so it is able to produce as many as itwants of each type of candy. Furthermore, thecandy types have different weights (though some may weigh the same), and in order tosimplify pricing matters, the ACM wants all candy boxes to have the same total weight.

With these restrictions, the ACM will only be able to make a limited number of boxes.For instance, if there are three types of candy, weighing 5, 5 and 10 grams respectively, 4different boxes can be made with total weight 10 grams (using either two of type 1, or twoof type 2, or one of type 3, or one each of types 1 and 2). The ACM would like to be ableto make at least one box for everyone in the cosmos. So, given queries in the form of thenumber of people P in the cosmos, your job is to find the smallest possible total weight wsuch that P different boxes containing exactly w grams of candies can be made.

Input

The input consists of several data sets (at most 20). Each data set consists of four lines. Thefirst line contains an integer 1 ≤ n ≤ 5, the number of candy types. The next line contains nintegers w1, . . . , wn, where 1 ≤ wi ≤ 10 is the weight (in grams) of the i:th candy type. Thethird line contains an integer 1 ≤ q ≤ 10, the number of queries. The last line of a data setcontains q integers P1, . . . , Pq, where 1 ≤ Pj ≤ 1015 is the j:th query. Input is terminated byan incomplete data set where n = 0, which should not be processed.

Output

For the i:th data set, write a line “Set i”, followed by q lines giving, for each query Pj, theminimal possible positive weight Wj (in grams) of a candy box. If there is no weight Wj suchthat at least Pj candy boxes can be made, print “no candy for you” for that query. Youmay assume that Wj, if it exists, will be at most 100 · Pj.

Page 4: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

2 Problem A: Unequalled Consumption

Sample input Sample output35 5 101443 1 4 22142 70011011000

Set 110Set 22342Set 3no candy for you

Page 5: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

Problem B: Declaration of Content 3

Problem B: Declaration of Content

Most food you can buy at your local gro-cery store has a declaration of content. Thedeclaration of content lists the ingredients ofthe product. It does not necessarily tell youthe exact amount of every ingredient, onlythe ordering of the ingredients, from mostcommon to least common. For some ingre-dients, an exact percentage might be given,either required by law or because the pro-ducer wants you to know how much of thefine expensive ingredients they have used.

Given a set of different products and their respective declarations of content you shoulddetermine which contain the most or the least of some given ingredients. For simplicity, weassume in this problem that the percentage of each ingredient always is an integer.

Input

The input consists of several test cases. Each test case consists of two parts.The first part of a test case begins with an integer P, 1 ≤ P ≤ 10, the number of different

products in this test case, on a line of its own. Then follows the description of the P products.Each product description consists of a line giving the name of the product, followed by a linecontaining an integer n, 1 ≤ n ≤ 100, giving the number of ingredients in this product. Thenfollow n lines, the i:th of which contains the name of the i:th most common ingredient of theproduct. In case of ties, the ingredients will be listed in arbitrary order. Optionally, everyingredient name can be followed by space, an integer p, 0 ≤ p ≤ 100 and a percentage sign.If this is present, it specifies the exact amount of this ingredient in the product. Otherwise,because all percentages in this problem are integers, the ingredient makes up at least onepercent of the total product.

The second part of a test case begins with an integer Q, 1 ≤ Q ≤ 100, the number ofqueries. Then follow Q lines, each containing a query. A query is of the form “least X”, or“most X”, where X is the name of an ingredient. In the “most X” case, the ingredient X isguaranteed to be present in at least one of the products.

A name of a product or an ingredient is a string of alphabetic characters (A-Z and a-z),digits (0-9) and underscore. Case is significant. No name will be longer than 30 characters.You may assume that each declaration of content is valid.

The last test case to be processed is followed by a line consisting of the integer 0.

Output

The output consists of one line for every query in the input data. For each query, outputthe name of the product containing the most or the least of ingredient X, as indicated by thequery. If there are several possible such products, output all of them, in the same order as theproducts were presented in the test case input data. The product names should be separatedby a single space.

Page 6: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

4 Problem B: Declaration of Content

Sample input Sample output3Product 13ABCProduct 23CBAProduct 32BC 35%4most Amost Bmost Cleast D0

Product 1Product 3Product 2 Product 3Product 1 Product 2 Product 3

Page 7: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

Problem C: Laserbox 5

Problem C: Laserbox

A laserbox is a game involving some opti-cal equipment. The game board is a squaren × n grid. On each grid point, a gad-get called a right-turner can be placed andseveral such gadgets are included. Finally,there is a ruby laser, and if the laser ismounted at the bottom end of a column, thebeam will be directed northwards throughthat column. Analogously, the laser beammay be directed southwards from the top ofa column, eastwards from the start of a rowor westwards from the end of the row.

The game starts with some right-turnersbeing spread out on some grid points andthe laser (switched off) being mountedsomewhere along the border of the rectangle. The player then tries to deduce where thebeam will emerge when the laser is switched on. The effect of a right-turner is to deflect thebeam ninety degrees to the right, regardless of from which of the four directions it enters.

Your program must do exactly what the player is supposed to do.

Input

On the first line of the input is a single positive integer, telling the number of test cases tofollow. The first line of each test case consists of two integers n r, where 1 ≤ n ≤ 50 is the sizeof the board and 1 ≤ r ≤ 50 the number of right-turners. The following r lines contain thecoordinates x y of the right-turners. No two right-turners will have the same coordinates.

Finally, a line with two integers indicating the laser position follows. The bottom ofcolumn six is denoted by 6 0 and the start of row seven by 0 7. If the zeroes are replaced byn + 1, the laser is placed at the top of column six and the end of row seven, respectively.

Output

For each test case, output one line containing the coordinates X Y of the beam as it leavesthe board. The same rules as for the laser apply, so X may equal 0 or n + 1 or else Y equal 0or n + 1. If the beam gets caught and does not leave the board, the output should be 0 0.

Page 8: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

6 Problem C: Laserbox

Sample input Sample output22 31 11 22 23 13 61 11 32 22 33 13 22 0

2 00 2

Page 9: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

Problem D: Bowlstack 7

Problem D: Bowlstack

Baking bread is my favourite spare-time pursuit. I have a number of stainless steel mixingbowls with straight sides, a circular bottom and a wider circular top opening. Geometrically,my bowls are truncated circular cones and for this problem, the thickness of the metal maybe disregarded.

I store these bowls stacked in the natural way, that is with a common vertical axis, and Istack them in an order that minimises the total height of the stack. Finding this minimum isthe purpose of your program.

Input

On the first line of the input is a positive integer, telling the number of test cases to follow.Each case starts with one line containing an integer n, the number of bowls (2 ≤ n ≤ 9).The following n lines each contain three positive integers h, r, R, specifying the height, thebottom radius and the top radius of the bowl, and r < R holds true. You may also assumethat h, r, R < 1000.

Output

For each test case, output one line containing the minimal stack height, truncated to aninteger (note: truncated, not rounded).

❆❆

❆❆❆

✁✁✁✁✁

❈❈❈❈❈❈

✄✄✄✄✄✄

✚✚

✚✚

❍❍❍❍❍❍❍

✟✟✟✟✟✟✟

❅❅

❅❅❅

Sample input Sample output2260 20 3040 10 50350 30 8035 25 7040 10 90

7055

Page 10: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

Almost blank page

Page 11: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

Problem E: Pesky Heroes 9

Problem E: Pesky Heroes

“Pesky Heroes!” — the evil mage stormedinto his throneroom — “They don’t haveany respect for us evil masterminds any-more. They’ve gone and invaded my caveagain, and while the traps were down formaintenance! And they’ve set up theircamp in the cave! The nerve!”

“But, milord, then surely they will bedoomed when the traps have been re-activated,” were the last words of a servant,uttered milliseconds before he. . . well, youget the picture. “Not necessarily, I may haveto teleport my trained orcs to seal their fate.You there, figure out where I should send them. Now!”

Inspired by the fate of the last servant to speak, you hurry off with a map of the cave.The map is a complete map of the cave, with the positions of all traps marked, and you seethat the cave has a single entrance. On the map, there are n strategic key points which havebeen marked with the numbers 1 to n. All passages in the cave connect two key points. Alltraps have been placed at key points, and all dead ends are key points. Your mathematicallyinclined mind quickly discovers that for every key point there are at most three passagesconnecting to it.

Your master can open a magic gateway from the throneroom to any passage (but not tokey points), and the trained orcs will be sent in. Despite their long training, the orcs are stillnot too bright. When they’ve gone through the gateway, they will begin walking away fromthe light coming from the entrance (there will always be a unique path from each point in thecave to the entrance). In each intersection, they will normally always choose the left path,and when they come back to the gateway, they will come back to the throneroom, ready tobe teleported to a new location, if necessary.

These orcs have been trained, so they’re smarter than the average bear, erm, orc. Youcan give them a number t when they walk in, and the t:th time they are at an intersection,they will turn right instead of left. Despite their eight years of training, they can still only betrusted to remember one number until they return.

“Oh, and one last thing. I saw the heroes in my crystal ball, they’ve set up camp in adead-end.”, the mage shouts after you.

To maximize your chances of survival, you want your master to have to work as little aspossible (i.e. to open as few gateways as possible), but you must have the orcs search everydead-end in the cave which can reach the entrance without passing any traps. The orcs arevery dear to your master, so you better make sure they don’t walk into any traps!

Input

The input will consist of several data sets. Each set will start with a line consisting of twonumbers, n, m where 2 ≤ n ≤ 50000 is the number of key points on the map and 0 ≤ m ≤ 500is the number of traps.

Page 12: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

10 Problem E: Pesky Heroes

The next n lines will consist of 2 to 4 space-separated integers. Line i contains an integerni, 1 ≤ ni ≤ 3, the number of passages connecting to key point i, followed by a list of theni key points that the passages lead to, in clockwise order. The next m lines consist of singleintegers, the key points at which there are traps. Key points are labelled 1, . . . , n and keypoint 1 is the (implicit) entrance to the outside world. Key point 1 is guaranteed to alwayshave exactly one passage.

The last case will be followed by a line with m = n = 0, which should not be processed.

Output

For each case, output the minimum number of gateways required, on a line by itself.

Sample input Sample output5 11 23 1 4 31 22 2 51 434 11 22 1 32 2 41 340 0

10

Page 13: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

Problem F: Reduced ID Numbers 11

Problem F: Reduced ID Numbers

T. Chur teaches various groups of students at university U. Every U-student has a uniqueStudent Identification Number (SIN). A SIN s is an integer in the range 0 ≤ s ≤ MaxSIN withMaxSIN = 106 − 1. T. Chur finds this range of SINs too large for identification within hergroups. For each group, she wants to find the smallest positive integer m, such that withinthe group all SINs reduced modulo m are unique.

Input

On the first line of the input is a single positive integer N, telling the number of test cases(groups) to follow. Each case starts with one line containing the integer G (1 ≤ G ≤ 300):the number of students in the group. The following G lines each contain one SIN. The SINswithin a group are distinct, though not necessarily sorted.

Output

For each test case, output one line containing the smallest modulus m, such that all SINsreduced modulo m are distinct.

Sample input Sample output211248663124866111111987651

18

Page 14: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

Almost blank page

Page 15: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

Problem G: Tantrix 13

Problem G: Tantrix

Tantrix is a two player game played with 56 hexagonal tiles. Eachtile contains three links in different colours. Both players have fivetiles in hand and take turns in placing them on the playing field. Thefigure to the right shows how the game could have progressed afternine played tiles.

There are four different link colours: red, green, yellow and blue.No two tiles are identical, and no tile is rotation symmetric. A tilewill be described in the input as a six letter string, specifying the linkcolours in clockwise direction. The uppercase letters ’R’, ’G’, ’Y’ and’B’ will be used for red, green, yellow and blue, respectively.

In this problem, a move is defined as placing one of the tiles in hand somewhere on theplaying field, subject to these rules:

1. A tile must always be placed next to tiles already played.

2. The links in all touching tiles must match colour.

3. An empty space which is surrounded by three tiles is called a forced space. If the playercan place one of his tiles in a forced space, he must do so. If there are several forcedspaces, and several ways to place a tile in a forced space, he may select any of those.

4. It’s not allowed to place a tile so that a forced space is created containing three links ofthe same colour (since no tile could ever be placed there).

5. The two sides along a forced space are called controlled sides. It’s not allowed to place atile along a controlled side.

If there are one or more forced spacesand the player can’t place any of his tilesin hand in those spaces, he will have toplay any other legal move. Note that aplayer may not be allowed to place a tilein a forced space due to rule 4.

The figure on the right illustratesthese rules. There are three forcedspaces. The interposed tile may not beplaced in the lower left forced space, asthat would create a new forced spacewith three red links. The dark grayspaces lie on controlled sides created bythe forced spaces; no tiles may be placedthere. If the player to move can’t placea tile in any of the three forced spaces,he must place a tile in any of the whitespaces.

Your task is to count the numberof legal moves the player to move has,

Page 16: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

14 Problem G: Tantrix

given the position and orientation of already played tiles and the tiles in hand for the playerto move. If a tile can be placed at several locations, or in several orientations, each suchcombination is counted as a distinct move.

Input

The first line in the input will contain the number of cases (atmost 50).

Each case begins with a single line containing an integern (1 ≤ n ≤ 20), the number of tiles that have already beenplayed. Then follow n lines containing the coordinates and de-scription of these tiles. The first character in the tile descriptionbelongs to the link facing up; the remaining colours follow asper usual in clockwise direction. Then follows a line with thedescription of the five tiles in hand, the tile descriptions beingseparated with a single space.

The mapping between the spaces and the coordinates isshown in the figure below (note that the playing field is in-finite and not restricted to these coordinates). All tiles in theinput will be valid and distinct. The layout will represent a position that could have arisenfrom a legal game. One of the played tiles will have coordinates 0, 0.

Output

For each test case, output a single line containing an integer: the number of legal moves.

Sample input Sample output260 0 BRYRBY1 0 GRGBRB-1 1 GGYBYB0 1 YYBBGG-2 2 YYBGBG-3 3 BYGYGBBBRRGG GBYBYG RBRBGG GYBGBY GRBBRG40 0 BYYGBG-1 1 GRGBBR1 0 YRBRYB2 0 YGGRRYRBBRYY GBGYBY YBBRYR YBYBRR RBBRGG

462

Tantrix is copyrighted by Tantrix Games Ltd

Page 17: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

Problem H: Guardian of Decency 15

Problem H: Guardian of Decency

Frank N. Stein is a very conservative high-school teacher. He wants to take some of hisstudents on an excursion, but he is afraid that some of them might become couples. Whileyou can never exclude this possibility, he has made some rules that he thinks indicates a lowprobability two persons will become a couple:

• Their height differs by more than 40 cm.

• They are of the same sex.

• Their preferred music style is different.

• Their favourite sport is the same (they are likely to be fans of different teams and thatwould result in fighting).

So, for any two persons that he brings on the excursion, they must satisfy at least one of therequirements above. Help him find the maximum number of persons he can take, giventheir vital information.

Input

The first line of the input consists of an integer T ≤ 100 giving the number of test cases. Thefirst line of each test case consists of an integer N ≤ 500 giving the number of pupils. Nextthere will be one line for each pupil consisting of four space-separated data items:

• an integer h giving the height in cm;

• a character ’F’ for female or ’M’ for male;

• a string describing the preferred music style;

• a string with the name of the favourite sport.

No string in the input will contain more than 100 characters, nor will any string contain anywhitespace.

Output

For each test case in the input there should be one line with an integer giving the maximumnumber of eligible pupils.

Page 18: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

16 Problem H: Guardian of Decency

Sample input Sample output2435 M classicism programming0 M baroque skiing43 M baroque chess30 F baroque soccer827 M romance programming194 F baroque programming67 M baroque ping-pong51 M classicism programming80 M classicism Paintball35 M baroque ping-pong39 F romance ping-pong110 M romance Paintball

37

Page 19: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

Problem I: Up the Stairs 17

Problem I: Up the Stairs

John is moving to the penthouse of a tall sky-scraper. Hepacked all his stuff in boxes and drove them to the en-trance of the building on the ground floor. Unfortunatelythe elevator is out of order, so the boxes have to be movedup the stairs.

Luckily John has a lot of friends that want to help car-rying his boxes up. They all walk the stairway at the samespeed of 1 floor per minute, regardless of whether theycarry a box or not. The stairway however is so narrow thattwo persons can’t pass each other on it. Therefore they de-ciced to do the following: someone with a box in his handsis always moving up and someone empty-handed is al-ways moving down. When two persons meet each othersomewhere on the stairway, the lower one (with a box)hands it over to the higher one (without a box). (And thenthe lower one walks down again and the higher one walksup.) The box exchange is instantaneous. When someoneis back on the ground floor, he picks up a box and startswalking up. When someone is at the penthouse, he dropsthe box and walks down again.

After a while the persons are scattered across the stairway, some of them with boxes intheir hands and some without. There are still a number of boxes on the ground floor andJohn is wondering how much more time it will take before all the boxes are up. Help him tofind out!

Input

One line with a positive number: the number of test cases. Then for each test case:

• One line with three numbers N, F, B with 1 ≤ N, F ≤ 1, 000 and 1 ≤ B ≤ 1, 000, 000:the number of persons, the number of floors (0=ground floor, F=penthouse) and thenumber of boxes that are still on the ground floor.

• N lines with two numbers fi and bi with 0 ≤ fi ≤ F and bi = 0 or bi = 1: the floorswhere the persons are initially and whether or not they have a box in their hands(1=box, 0=no box).

Output

One line with the amount of time (in minutes) it will take to get all the remaining boxes tothe penthouse.

Page 20: The Problem Set · The Problem Set A Unequalled Consumption B Declaration of Content C Laserbox DBowlstack ... Problem A: Unequalled Consumption The Association of Candy Makers is

18 Problem I: Up the Stairs

Sample input Sample output23 10 50 00 00 02 5 12 13 0

308