An Arc 2008

download An Arc 2008

of 15

Transcript of An Arc 2008

  • 8/9/2019 An Arc 2008

    1/15

  • 8/9/2019 An Arc 2008

    2/15

    [A] Tobo or not Tobo Program: tobo.(c|cpp|java)Input: tobo.inBalloon Color: Orange

    Description

    The game of Tobo is played on a plastic board designed into a 3x3 gridwith cells numbered from 1 to 9 as shown in gure (a). The grid has fourdials (labeled A to D in the gure.) Each dial can be rotated in 90degrees increment in either direction. Rotating a dial causes the four cells

    currently adjacent to it to rotate along. For example, gure (b) showsthe Tobo after rotating dial A once in a clockwise direction. Figure (c)shows the Tobo in gure (b) after rotating dial D once in a counter-clockwise direction.Kids love to challenge each other playing the Tobo. Starting with thearrangement shown in gure (a), (which well call the standard arrange-ment, ) one kid would randomly rotate the dials, X number of times, inorder to shuffle the board. Another kid then tries to bring the boardback to its standard arrangement, taking no more than X rotations todo so. The less rotations are needed to restore it, the better. This iswhere you see a business opportunity. You would like to sell these kids

    a program to advise them on the minimum number of steps needed tobring a Tobo back to its standard arrangement.

    Input FormatYour program will be tested on one or more test cases. Each test case isspecied on a line by itself. Each line is made of 10 decimal digits. Letscall the rst digit Y. The remaining 9 digits are non-zeros and describe thecurrent arrangement of the Tobo in a row-major top-down, left-to-rightordering. The rst sample case corresponds to gure (c).The last line of the input le is a sequence of 10 zeros.

    Output Format

    For each test case, print the result using the following format:k. R

    where k is the test case number (starting at 1,) is a single space, and Ris the minimum number of rotations needed to bring the Tobo back to itsstandard arrangement. If this cant be done in Ydials or less, then R=-1.

    Sample Input/Output

    tobo.in341356972811654327890000000000

    OUTPUT1. 22. -1

    Page 1 of 14

  • 8/9/2019 An Arc 2008

    3/15

    [B] Adding Sevens Program: seven.(c|cpp|java)Input: seven.inBalloon Color: Red

    DescriptionA seven segment display, similar to the one shown on the right, is composedof seven light-emitting elements. Individually on or off, they can be combinedto produce 127 different combinations, including the ten Arabic numerals. Thegure on the next page illustrates how the ten numerals are displayed. 7-segdisplays (as theyre often abbreviated) are widely used in digital clocks, electronicmeters, and calculators. A 7-seg has seven connectors, one for each element,(plus few more connectors for other electrical purposes.) Each element can beturned on by sending an electric current through its pin. Each of the seven pinsis viewed by programmers as a single bit in a 7-bit number, as they are morecomfortable dealing with bits rather than electrical signals. The gure on theright shows the bit assignment for a typical 7-seg, bit 0 being the right-mostbit. For example, in order to display the digit 1, the programmer knows thatonly bits 1 and 3 need to be on, i.e. the 7-bit binary number to display digit 1is 0001010, or 10 in decimal. Lets call the decimal number for displaying adigit, its display code, or just code for short. Since a 7-seg displays 127 differentcongurations, display codes are normally written using 3 decimal places withleading zeros if necessary, i.e. the display code for digit 1 is written as 010.In a 9-digit calculator, 9 7-seg displays are stacked next to each other, and are allcontrolled by a single controller. The controller is sent a sequence of 3 n digits,representing n display codes, where 0 < n < 10. If n < 9, the number is right justied and leading zeros are automatically displayed. For example, the displaycode for 13 is 010079 while for 144 it is 010106106Write a program that reads the display codes of two numbers, and prints thedisplay code of their sum.

    Input FormatYour program will be tested on one or more test cases. Each test case is speciedon a single line in the form of A+B=where both A and B are display codes for

    decimal numbers a and b respectively where 0 < a, b < a + b < 1, 000, 000, 000.The last line of the input le is the word "BYE"(without the double quotes.)

    Output FormatFor each test case, print A+B=Cwhere C is the display code for a + b.

    Sample Input/Outputseven.in

    010079010+010079=106010+010=BYE

    OUTPUT010079010+010079=010106106106010+010=106093

    Page 2 of 14

  • 8/9/2019 An Arc 2008

    4/15

    B. Adding Sevens Page 3 of 14

  • 8/9/2019 An Arc 2008

    5/15

    [C] Match Maker Program: match.(c|cpp|java)Input: match.inBalloon Color: Gold

    DescriptionIn Computer Science, pattern matching is the act of checking if a certain sequence conforms(matches) a given pattern. Patterns are usually specied using a language based on regular expression. In this problem, well use a simple regular expression to express patterns on sequencesof decimal digits. A pattern is a sequence of one or more decimal digits 0 . . . 9 , asterisks * ,and hash signs # . A * denotes a sequence of an even number of digits, whereas a # denotesa sequence of an odd number of digits. For example, the pattern "129" only matches the sequence129. The pattern "1*3" matches all sequences beginning with 1, ending with 3, and having aneven number of decimal digits between the rst and last digits. As another example, the pattern"#55" matches the sequences 155, 12355, 1234555, but none of the sequences 55, 1255, 123455.Your task is to write a program to nd if a given sequence matches a given pattern.

    Input FormatYour program will be tested on one or more data sets. Each data set contains a single patternand one or more sequences to match. The rst line of each data set species the pattern, andthe remaining lines specify the sequences to match against that pattern. The end of a data set(except the last) is identied by the word "END"(without the double quotes.) The end of the lastdata set is identied by the word "QUIT". All lines are 100,000 characters long or shorter.

    Output Format

    k.s. result

    Where k is the test case number (starting at one,) and s is the sequence number (starting atone within each test case,) and result is either the word "match" if the given string matches thepattern, or the word "not" if it doesnt.

    Sample Input/Output

    match.in12912991291129END1*31231223END#5515512355551255QUIT

    OUTPUT1.1. not1.2. match1.3. not2.1. not2.2. match3.1. match3.2. match3.3. not3.4. not

    Page 4 of 14

  • 8/9/2019 An Arc 2008

    6/15

    [D] Adding up Triangles Program: tri.(c|cpp|java)Input: tri.inBalloon Color: Green

    Description

    Take a look at the triangle on the right. It is made of 9 (unit) trianglesarranged in three rows ( N=3). Needless to say, a unit triangle is a trianglewith N=1. If you study the gure for few seconds, youll realize that youcan nd 13 different triangles (which well call sub-triangles.) Of these 13

    sub-triangles we have: Nine unit triangle; three with N=2, and one with N=3.The following table lists the number of sub-triangles in arrangements withN < 5.

    # of Rows: N = 1 N = 2 N = 3 N = 4# of Sub-triangles: 1 5 13 27

    Lets dene the value of a unit triangle to be the integer value written inthat triangle. In general, the value of a triangle is the sum of values in allits unit triangles. The gure on the right is the same as the one above butwith the sub-triangle having the largest value being highlighted. Write aprogram to determine the sub-triangle with the largest value.

    Input Format

    Your program will be tested on one or more test cases. Each test case is specied in a single linemade of integers (separated by spaces.) The rst integer is the number of rows in the test case,and the remaining integers are the values of the unit triangles specied in a top-down, left-to-rightorder. (the rst test case in the example below is the same as the one in the gure.) The lastline of the input le contains the number 0 (which is not part of the test cases.)

    The maximum number of rows is 400. The absolute value of a unit triangle is less than 1000.

    Output FormatFor each test case, print the result using the following format:

    k. V

    where k is the test case number (starting at 1,) is a single space, and Vis the maximum valueof a sub-triangle in that test case.

    Sample Input/Output

    tri.in3 6 -24 0 12 -10 12 40 -4 6

    4 1 1 -1 1 1 -1 1 -1 1 1 -1 1 -1 1 -1 10

    OUTPUT1. 54

    2. 4

    Page 5 of 14

  • 8/9/2019 An Arc 2008

    7/15

    [E] Relax! Its just a gameProgram: game.(c|cpp|java)Input: game.inBalloon Color: Blue

    You: Whats the score? Did I miss much?

    Me: Its 2-1 for elAhli and the second half juststarted. The rst half was quite boring.

    You: Who scored rst? elAhli or ezZamalek?

    Me: What difference does it make?

    You: Big difference! I can predict the outcomeof the match if I knew the order of which goalswere scored in the rst half.

    Me: What do you mean?

    You: Its 2-1 for elAhli, right? One of threethings could have happened: elAhli scored twogoals then ezZamalek scored; Or, elAhli scoredits rst goal, then ezZamalek, then elAhliagain; Or, ezZamalek scored rst, then elAhliscored its two goals.

    Me: So?!! I still dont understand what differ-

    ence does that make? Its still 2-1 for elAhli!Why dont you just relax and let us continuewatching the game in peace.

    You: You dont understand!! I believe the

    probability of wholl win depends on the or-der of how goals were scored. Now I have topredict the outcome for 3 possibilities.

    Me: And what if the score was 3-2? Whatwould you have done then?

    You: I would have to work for 5 different pos-sibilities. No?

    Me: Of course not! The number of possibili-ties isnt always equal to the sum.

    You: Can you tell me when will it be equal tothe sum?

    Me: Youre a programmer, why dont youwrite a program that counts the number of possibilities and compare it to the sum?

    You: I dont have the time, I want to watchthe match. Besides, I have nine other prob-lems to worry about.

    Me: Ill give you a hint. The possibilities willbe equal to the sum only if one of the teamsscored a certain number of goals.

    Input FormatYour program will be tested on one or more test cases. Each test case species two naturalnumbers ( Aand B) (separated by one or more spaces) representing the score of the rst half. Noteam will be able to score more than 10 goals. The last line of the input le contains two -1s(which is not part of the test cases.)

    Output FormatFor each test case where the number of possibilities is equal to the sum, print:

    A+B=C

    Where Aand B are as above and C is their sum. If the number of possibilities is not equal to thesum, replace the = sign with != (without the quotes.)

    Sample Input/Output

    game.in2 11 0-1 -1

    OUTPUT2+1=31+0=1

    Page 6 of 14

  • 8/9/2019 An Arc 2008

    8/15

    [F] Einbahnstrasse Program: one.(c|cpp|java)Input: one.inBalloon Color: Purple

    Description

    Einbahnstrae (German for a one-way street) is a street on which vehicles should only move in onedirection. One reason for having one-way streets is to facilitate a smoother ow of traffic throughcrowded areas. This is useful in city centers, especially old cities like Cairo and Damascus. Carefulplanning guarantees that you can get to any location starting from any point. Nevertheless, driversmust carefully plan their route in order to avoid prolonging their trip due to one-way streets.

    Experienced drivers know that there are multiple paths to travel between any two locations. Notonly that, there might be multiple roads between the same two locations. Knowing the shortestway between any two locations is a must! This is even more important when driving vehicles thatare hard to maneuver (garbage trucks, towing trucks, etc.)

    You just started a new job at a car-towing company. The company has a number of towing trucksparked at the companys garage. A tow-truck lifts the front or back wheels of a broken car inorder to pull it straight back to the companys garage. You receive calls from various parts of thecity about broken cars that need to be towed. The cars have to be towed in the same order asyou receive the calls. Your job is to advise the tow-truck drivers regarding the shortest way inorder to collect all broken cars back in to the companys garage. At the end of the day, you haveto report to the management the total distance traveled by the trucks.

    Input Format

    Your program will be tested on one or more test cases. The rst line of each test case speciesthree numbers ( N , C , and R ) separated by one or more spaces. The city has N locations withdistinct names, including the companys garage. C is the number of broken cars. R is the numberof roads in the city. Note that 0 < N < 100, 0 C < 1000, and R < 10000. The second lineis made of C + 1 words, the rst being the location of the companys garage, and the rest beingthe locations of the broken cars. A location is a word made of 10 letters or less. Letter case issignicant. After the second line, there will be exactly R lines, each describing a road. A road isdescribed using one of these three formats:

    A --v-> BA

  • 8/9/2019 An Arc 2008

    9/15

    Output Format

    For each test case, print the total distance traveled using the following format:

    k. V

    Where k is test case number (starting at 1,) is a space, and Vis the result.Sample Input/Output

    one.in4 2 5NewTroy Midvale MetrodaleNewTroy MidvaleMidvale --50-> BakerlineNewTroy Bakerline

    0 0 0OUTPUT

    1. 80

    F. Einbahnstrasse Page 8 of 14

  • 8/9/2019 An Arc 2008

    10/15

    [G] Think Ill Buy Me a Football Team Program: money.(c|cpp|java)Input: money.inBalloon Color: Pink

    Description

    Falling Stocks. Bankrupted companies. Banks with no Cash.Seems like the best time to invest: Think Ill buy me a football team!No seriously, I think I have the solution to at least the problem

    of cash in banks. Banks nowadays are all owing each other greatamounts of money and no bank has enough cash to pay otherbanks debts even though, on paper at least, they should haveenough money to do so. Take for example the inter-bank loansshown in gure (a). The graph shows the amounts owed betweenfour banks ( A. . . D). For example, A owes B 50M while, at thesame time, B owes A150M. (It is quite common for two banks toowe each other at the same time.) A total amount of 380M incash is needed to settle all debts between the banks.In an attempt to decrease the need for cash, and after studyingthe example carefully, I concluded that theres a lot of cash being

    transferred unnecessarily. Take a look:

    1. C owes Dthe same amount as Dowes A, so we can say thatC owes Aan amount of 30M and get Dout of the picture.

    2. But since Aalready owes C 100M, we can say that Aowes Can amount of 70M.

    3. Similarly, Bowes A100M only, (since Aalready owes B50M.)This reduces the above graph to the one shown in gure (b)which reduces the needed cash amount to 190M (A reduc-tion of 200M, or 53%.)

    4. I can still do better. Rather than B paying A 100M and Apaying 70M to C, B can pay 70M (out of As 100M) directlyto C. This reduces the graph to the one shown in gure (c).Banks can settle all their debts with only 120M in cash. Atotal reduction of 260M or 68%. Amazing!

    I have data about inter-bank debts but I cant seem to be ableto process it to obtain the minimum amount of cash needed tosettle all the debts. Could you please write a program to do that?

    Page 9 of 14

  • 8/9/2019 An Arc 2008

    11/15

    Input Format

    Your program will be tested on one or more test cases. Each test case is specied on N + 1 lineswhere N < 1, 000 is the number of banks and is specied on the rst line. The remaining N linesspecies the inter-bank debts using an N N adjacency matrix (with zero diagonal) specied inrow-major order. The ith row species the amounts owed by the ith bank. Amounts are separatedby one or more spaces. All amounts are less than 1000.The last line of the input le has a single 0.

    Output Format

    For each test case, print the result using the following format:

    k. B A

    where k is the test case number (starting at 1,) is a space character, B is the amount of cashneeded before reduction and Ais the amount of cash after reduction.

    Sample Input/Outputmoney.in

    40 50 100 0

    150 0 20 00 0 0 30

    30 0 0 00

    OUTPUT1. 380 120

    G. Think Ill Buy Me a Football Team Page 10 of 14

  • 8/9/2019 An Arc 2008

    12/15

    [H] Musical Chairs Program: chairs.(c|cpp|java)Input: chairs.inBalloon Color: Maroon (Brown)

    Description

    In the traditional game of Musical Chairs, N + 1 children run around N chairs(placed in a circle) as long as music is playing. The moment the music stops,children run and try to sit on an available chair. The child still standing leavesthe game, a chair is removed, and the game continues with N children. The last

    child to sit is the winner.In an attempt to create a similar game on these days game consoles, you modifythe game in the following manner: N Children are seated on N chairs arrangedaround a circle. The chairs are numbered from 1 to N . Your program pre-selectsa positive number D . The program starts going in circles counting the childrenstarting with the rst chair. Once the count reaches D , that child leaves thegame, removing his/her chair. The program starts counting again, beginningwith the next chair in the circle. The last child remaining in the circle is thewinner.For example, consider the game illustrated in the the adjacent gure for N = 5and D = 3. In the gure, the dot indicates where counting starts and indicates

    the child leaving. Starting off, child #3 leaves the game, and counting restartswith child #4. Child #1 is the second child to leave and counting restart withchild #2 resulting in child #5 leaving. Child #2 is the last to leave, and child #4is the winner. Write a program to determine the winning child given both N and D .

    Input FormatYour program will be tested on one or more test cases. Each test case speciestwo positive integers N and D on a single line, separated by one or more spaces,where N,D < 1, 000, 000.The last line of the input le contains two 0s and is not part of the test cases.

    Output FormatFor each test case, write the winner using the following format:

    N D W

    Where N and D are as above, is a space character, and W is the winner of that game.

    Sample Input/Output

    chairs.in5 37 4

    0 0

    OUTPUT5 3 47 4 2

    Page 11 of 14

  • 8/9/2019 An Arc 2008

    13/15

    [I] I Speak Whales Program: matrix.(c|cpp|java)Input: matrix.inBalloon Color: Yellow

    Description

    According to Wikipedia, a Walsh matrix is a specic square matrix, with dimensions equal to apower of 2, the entries of which are +1 or 1, and the property that the dot product of any twodistinct rows (or columns) is zero. Below are the rst three Walsh Matrices. (The gray lines areimaginary lines for illustration purpose only.)

    W 1 = 1 W 2 = 1 1

    1 1W 4 =

    1 1 1 11 1 1 11 1 1 11 1 1 1

    A Walsh Matrix of size 2 N +1 can be constructed as the union of 4 Walsh Matrices of size 2 N

    arranged such that the lower right matrix is inverted whereas the other 3 matrices are not, i.e.:

    W 2 N +1 = W 2 N W 2 N

    W 2 N W 2 N

    Lets number the rows of a given Walsh Matrix from the top starting with row 0. Similarly, letsnumber the columns of the matrix from the left starting with column 0. Given the four integersN , R , S , and E , write a program that will construct a Walsh Matrix of size 2 N and will print thesum of all the numbers in row # R between columns # S and # E (inclusive.)

    Input Format

    Your program will be tested on one or more test cases. Each test case is specied using a singleline listing four integers in the following order: N , R , S , and E , where 0 N 60, 0 R < 2N ,0 S E < 2N , and E S 10, 000.

    The last line of the input le has four -1s and is not part of the test cases.

    Output FormatFor each test case, print the output on a single line.

    Sample Input/Output

    matrix.in2 1 0 148 0 0 47-1 -1 -1 -1

    OUTPUT048

    Page 12 of 14

  • 8/9/2019 An Arc 2008

    14/15

    [J] A Day at the Races Program: grand.(c|cpp|java)Input: grand.inBalloon Color: Black

    DescriptionFormula One is the highest class of car racing sports. A typical Formula One season consists of a series of races called Grands Prix which constructors like Ferrari, Renault, etc. and othersparticipate with one or more cars driven by the best drivers in the world. During the season, teamscompete in two parallel championships: the drivers championship and the teams championship.

    In the drivers championship, drivers compete to achieve the maximum total number of points

    by the end of the season, the rules of the competition states that the top eight drivers at eachGrand Prix receive 10,8,6,5,4,3,2,1 points respectively. In case of points tie, the driver with thehighest number of rst places leads. If still tied, then the highest second places, and so on till thehighest 8th places. If still tied, then drivers are sorted lexicographically by their last and then bytheir rst names.After each race, the points received by each driver are added to his teams pocket, and at the endof the season the team with the highest number of points wins the teams championship. To addexcitement to the season, team sponsors are allowed to buy drivers from other teams even withinthe same season. In case of points tie between teams, teams are sorted lexicographically by theirnames. In this problem, you are given data of a formula one season and youre asked to processthese data according to the rules above to determine both the drivers and teams standings.

    Input FormatYour program will be tested on one or more data-sets, each representing a Formula One season.All input lines are 255 characters or less. Studying the sample I/O youll discover that the rstline of each season has an integer N , where 0 < N < 32 and representing the number of GrandsPrix in that season. For each Grand Prix, the name of the Grand Prix appears on a line by itself (maximum length is 64 characters) followed by a table of the rst name, last name and teamname of the top eight drivers, from 1 to 8, in that Grand Prix. Each of the rst and last names isa sequence of printable ASCII characters, no longer than 12 characters, and contains no spaces.Each team name is a sequence of printable ASCII characters, no longer than 18 characters, andmay contain spaces (but no leading or trailing spaces.) Each team name is followed by a singleperiod . which is not part of the name. Trailing white space may follow. A line of three - sfollows the listing of each Grand Prix. The last line of the input le contains a single zero.

    Output FormatFor each data set in the input you must print Season k: where k is the data-set number(starting from 1.) The next line must state Drivers Standing: . On subsequent lines list thedrivers standing for that season. For each driver, print their rst and last names separated byexactly one space and left justied in a eld of width 25, followed by a single space, followed bythe total number of points achieved by the driver during the season. The drivers standing shouldbe followed by a blank line.The next line must state Teams Standing: On subsequent lines list the teams standing for thethat season. For each team, print the team name left justied in a eld of width 25, followed by asingle space, followed by the total number of points the team has scored during the season. Theteams standing should be followed by a blank line.

    Page 13 of 14

  • 8/9/2019 An Arc 2008

    15/15

    Sample Input/Outputgrand.in

    2FORMULA 1 Gran Premio Telefonica de Espana 2006Pos Driver Team

    1 Fernando Alonso Renault.2 Michael Schumacher Ferrari.3 Giancarlo Fisichella Renault.4 Felipe Massa Ferrari.5 Kimi Raikkonen McLaren-Mercedes.6 Jenson Button Honda.7 Rubens Barrichello Honda.8 Nick Heidfeld Sauber-BMW.---FORMULA 1 Grand Prix de Monaco 2006Pos Driver Team

    1 Fernando Alonso Renault.2 Jaun-Pablo Montoya McLaren-Mercedes.3 David Coulthard RBR-Ferrari.4 Rubens Barrichello Honda.5 Michael Schumacher Ferrari.6 Giancarlo Fisichella Renault.7 Nick Heidfeld Sauber-BMW.8 Ralf Schumacher Toyota.---0

    OUTPUTSeason 1:Drivers Standing:Fernando Alonso 20Michael Schumacher 12Giancarlo Fisichella 9Jaun-Pablo Montoya 8Rubens Barrichello 7David Coulthard 6Felipe Massa 5Kimi Raikkonen 4

    Jenson Button 3Nick Heidfeld 3Ralf Schumacher 1

    Teams Standing:Renault 29Ferrari 17McLaren-Mercedes 12Honda 10RBR-Ferrari 6Sauber-BMW 3

    Toyota 1