Ejercicios ACM Master

download Ejercicios ACM Master

of 21

Transcript of Ejercicios ACM Master

  • Ptzcuaro Michoacn ITSPA Abril 2014 !!

    Set Test ACM Senior ITSPA

    ACADEMIA DE TICS !!

    !1SET TEST

  • A. Da Vinci Code !The Da Vinci Code is one of the most widely read but controversial books of all time.

    In this book the writer Dan Brown used a very interesting encryption technique to keep a

    secret message. It required a great deal of intelligence to decipher the code since there was

    not enough information available. Here, a similar kind of problem is given with sufficient

    clues to solve.

    !In this problem, you will be given a series of numbers, taken from Fibonacci series and a

    cipher text. Your task is to decipher the text using the decryption technique described below.

    !Lets follow an example. Any cipher text will consist of two lines. First line is the key

    which contains some numbers drawn from Fibonacci series. The second line is the actual

    cipher text.

    !So, given the following cipher text

    13 2 89 377 8 3 233 34 144 21 1

    OH, LAME SAINT!

    the output will be:

    THE MONA LISA

    !For this problem, assume that the first number in Fibonacci series is 1, second one is 2

    and each subsequent number is found by adding previous two numbers of the series. So, the

    Fibonacci series is 1, 2, 3, 5, 8, 13...

    !So, how do we get the string THE MONA LISA from the string OH, LAME

    SAINT!? Here some numbers are drawn from Fibonacci series, given in the first line. The

    first one is 13 which is the sixth (6th) Fibonacci number in Fibonacci series. So the first

    uppercase letter in the cipher text O goes to the sixth (6th) position in the output string.

    Second number in the input string is 2 which is the second Fibonacci number and thus H

    goes to second position in the output string; then comes 89 which is the 10th Fibonacci

    number, so L which is the third uppercase letter in the cipher goes to the 10th position in the

    output string. And this process continues until the cipher text ends and hence we find the

    !2SET TEST

  • string THE MONA LISA. Note that only uppercase letters conveys the message; other

    characters are simply garbage.

    !If a Fibonacci number is missing in the input sequence then a blank space is put at its

    position in the output string. As in the above example fourth and ninth Fibonacci numbers 5

    and 55 are missing. So, two blank spaces are inserted in fourth and ninth positions of the

    output string. But there must not be any trailing spaces.

    !Input

    Input starts with a line consisting of a single number T. T test cases follow. Each test

    case consists of three lines. The first line contains a single positive integer N. In the second

    line there are N numbers drawn from the Fibonacci series. The numbers will be separated

    from each other using spaces. Finally, the third line contains the cipher text to be decrypted.

    !Output

    For each test case, output contains a single line containing the decrypted text.

    Remember that the decrypted text will contain only uppercase letters.

    !Constraints

    - Value of any input Fibonacci number is less than 231.

    - The length of the cipher text will be at most 100.

    sd

    !!

    !3SET TEST

    InputInput starts with a line consisting of a single number T. T test cases follow. Each test case

    consists of three lines. The first line contains a single positive integer N. In the second line

    there are N numbers drawn from the Fibonacci series. The numbers will be separated from

    each other using spaces. Finally, the third line contains the cipher text to be decrypted.

    OutputFor each test case, output contains a single line containing the decrypted text. Remember that

    the decrypted text will contain only uppercase letters.

    Constraints- Value of any input Fibonacci number is less than 231.

    - The length of the cipher text will be at most 100.

    Sample Input Output for Sample Input2

    11

    13 2 89 377 8 3 233 34 144 21 1

    OH, LAME SAINT!

    15

    34 21 13 144 1597 3 987 610 8 5 89 2 377 2584 1

    O, DRACONIAN DEVIL!

    THE MONA LISA

    LEONARDO DA VINCI

    Problem setter: Mohammed Shamsul AlamSpecial thanks: Mohammed Saifur Rahim, Tanveer Ahsan

  • B. Network !A packet-switching network handles information in small units, breaking long messages

    into multiple packets before routing. Although each packet may travel along a different path,

    and the packets comprising a message may arrive at different times or out of order, the

    receiving computer reassembles the original message correctly.

    !The receiving computer uses a buffer memory to hold packets that arrive out of order.

    You must write a program that calculates the minimum buffer size in bytes needed to

    reassemble the incoming messages when the number of messages (N), the number of packets

    (M), the part of the messages in each packet, the size of each message, and the order of the

    incoming packets are given.

    !When each packet arrives, it may be placed into the buffer or moved directly to the

    output area. All packets that are held in the buffer are available to be moved to the output

    area at any time. A packet is said to ``pass the buffer" when it is moved to the output area. A

    message is said to ``pass the buffer" when all of its packets have passed the buffer.

    !The packets of any message must be ordered so the data in the sequence of packets that

    pass the buffer is in order. For example, the packet containing bytes 3 through 5 of a message

    must pass the buffer before the packet containing bytes 6 through 10 of the same message.

    Messages can pass the buffer in any order, but all packets from a single message must pass the

    buffer consecutively and in order (but not necessarily at the same time). Note that unlike

    actual buffering systems, the process for this problem can look ahead at all incoming packets

    to make its decisions.

    !The packets consist of data and header. The header contains three numbers: the

    message number, the starting byte number of data in the packet, and the ending byte number

    of data in the packet respectively. The first byte number in any message is 1.

    !For example, the figure below shows three messages (with sizes of 10, 20, and 5 bytes)

    and five packets. The minimum buffer size for this example is 10 bytes. As they arrive, packet

    #1 and packet #2 are stored in the buffer. They occupy 10 bytes. Then packet #3 passes the

    !4SET TEST

  • buffer directly. Packet #4 passes the buffer directly and then packet #2 exits the buffer. Finally,

    packet #5 passes the buffer directly and packet #1 exits the buffer.

    !Input

    The input file contains several test cases. The first line of each test case contains two

    integersN(1 N 5)andM(1 M 1000). The second line containsNintegers that are

    the sizes of messages in bytes; the first number denotes the size of message #1, the second

    number denotes the size of message #2, and so on. Each of the followingMlines describes a

    packet with three integers: the message number and the starting and ending byte numbers of

    data in the packet. No packet contains more 64 bytes of data.

    !The last test case is followed by a line containing two zeroes.

    !Output

    For each test case, print a line containing the test case number (beginning with 1)

    followed by the minimum buffer size in bytes required to reassemble the original messages.

    Put a blank line after the output for each test case. Use the format of the sample output.

    !Sample Input

    3 3

    5 5 5

    1 1 5

    2 1 5

    3 1 5

    3 5

    10 20 5

    2 16 20

    !5SET TEST

  • 1 6 10

    3 1 5

    1 1 5

    2 1 15

    0 0

    !Sample Output

    Case 1: 0

    Case 2: 10

    !!

    !6SET TEST

  • C. Pascals Triangle of Death !In this problem, you are asked to generate Pascal's Triangle. Pascal's Triangle is useful in

    many areas from probability to polynomials to programming contests. It is a triangle of

    integers with ``1'' on top and down the sides. Any number in the interior equals the sum of

    the two numbers above it. For example, here are the first 5 rows of the triangle.

    1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

    !In ``Pascal's Triangle of Death,'' you are to generate a left justified Pascal's Triangle.

    When any number in the triangle is exceeds or equals 1060, your program should finish

    printing the current row and exit. The output should have each row of the triangle on a

    separate line with one space between each element.

    !The final element of each line should be directly followed by a newline. There is no

    space after the last number on each line.

    !Sample Input

    There is no input for this problem.

    !Sample Output

    1

    1 1

    1 2 1

    1 3 3 1

    1 4 6 4 1

    .

    .

    .

    etc.

    !7SET TEST

  • D. A Chess Knight !Presumably everybody knows how a knight can move on a chessboard. One may agree

    that its movements are quite monotonous, so to make them more entertaining lets define a so

    called dynamic knight. A dynamic knight can perform many different movements that may

    belong to three types:

    type K: two fields forward (in any direction) and one sidewise - like regular knight;

    type B: two fields diagonally - more like a bishop;

    type T: sort of teleportation to a field which is a mirror reflection with respect to any of

    two axes of symmetry of the chessboard (we take into consideration only axes of symmetry

    parallel to sides of the chessboard);

    !The picture below shows all possible movements of a knight divided into three types K,

    B and T. Obviously our knight, like the regular one cannot move outside the chessboard.

    For a dynamic knight it is not relevant whether the fields between the starting field and

    ending one are occupied or not (again like for the regular knight). It only matters whether

    the ending field is empty. Then the movement can be perfumed. There has to be a restriction

    among so many capabilities of a dynamic knight. It cannot perform the same sort of

    movements consecutively (just not to fall into routine).

    !Having redefined a chess knight, why not to redefine a chessboard? Our chessboard will

    be a square of size 2N x 2N. N can be any integer number from the range of 320. There

    can be several obstacles of any shape on a chessboard so a knight cannot stop on these

    defected fields.

    !8SET TEST

  • Your task is to write a program which can calculate the minimal number of movements

    to get knight from one given field to another one. It may be assumed that the first movement

    can be of type.

    !Input

    First one contains the number N, being the size of a chessboard. The second one

    contains field coordinates separated by scape character, which is a knight current standpoint.

    Upper left-hand size corner has coordinates (1,1). Third line contains destination field for the

    knight. Consecutive lines contain obstacle coordinates and the line with coordinates (0,0) ends

    the obstacle description. Input can contain several sets of data.

    !Inputs end is shown as a line defining chessboards size as 0.

    !Output

    It is supposed to be fairly simple - one line with a single number being the calculated

    minimal number of movements.

    !Sample Input

    3

    1 1

    1 1

    2 2

    0 0

    10

    1 1

    20 20

    20 1

    1 20

    3 1

    3 2

    3 3

    2 3

    !9SET TEST

  • 1 3

    0 0

    10

    2 1

    18 12

    2 2

    5 6

    7 2

    8 3

    9 4

    1 15

    7 12

    8 13

    9 11

    11 9

    12 4

    11 3

    9 5

    2 7

    3 8

    6 5

    0 0

    3

    1 1

    5 4

    2 2

    2 4

    2 5

    3 1

    3 2

    3 3

    !10SET TEST

  • 3 6

    4 2

    4 3

    4 5

    5 1

    5 3

    5 5

    6 1

    6 4

    6 5

    6 6

    0 0

    0

    !Sample Output

    Result : 0

    Solution doesnt exist

    Result : 6

    Result : 5

    !!!!!!!!

    !11SET TEST

  • E. Pascals Triangle of Death !The problem is to parse a series of commands that instruct a robot arm in how to

    manipulate blocks that lie on a flat table. Initially there are n blocks on the table (numbered

    from 0 to n-1) with block bi adjacent to block bi+1 for all ! as shown in the diagram

    below:

    Figure: Initial Blocks World

    !

    The valid commands for the robot arm that manipulates blocks are:

    move a onto b

    where a and b are block numbers, puts block a onto block b after returning any blocks

    that are stacked on top of blocks a and b to their initial positions. !

    move a over b

    where a and b are block numbers, puts block a onto the top of the stack containing

    block b, after returning any blocks that are stacked on top of block a to their initial positions. !

    pile a onto b

    where a and b are block numbers, moves the pile of blocks consisting of block a, and

    any blocks that are stacked above block a, onto block b. All blocks on top of block b are

    moved to their initial positions prior to the pile taking place. The blocks stacked above block a

    retain their order when moved. !

    pile a over b

    where a and b are block numbers, puts the pile of blocks consisting of block a, and any

    blocks that are stacked above block a, onto the top of the stack containing block b. The blocks

    stacked above block a retain their original order when moved. !

    quit

    terminates manipulations in the block world.

    !12SET TEST

  • Any command in which a = b or in which a and b are in the same stack of blocks is an

    illegal command. All illegal commands should be ignored and should have no affect on the

    configuration of blocks.

    !The Input

    The input begins with an integer n on a line by itself representing the number of blocks

    in the block world. You may assume that 0 < n < 25.

    !The number of blocks is followed by a sequence of block commands, one command per

    line. Your program should process all commands until the quit command is encountered.

    !You may assume that all commands will be of the form specified above. There will be

    no syntactically incorrect commands.

    !The Output

    The output should consist of the final state of the blocks world. Each original block

    position numbered i ( where n is the number of blocks) should appear followed immediately

    by a colon. If there is at least a block on it, the colon must be followed by one space, followed

    by a list of blocks that appear stacked in that position with each block number separated from

    other block numbers by a space. Don't put any trailing spaces on a line.

    !There should be one line of output for each block position (i.e., n lines of output where

    n is the integer on the first line of input).

    !Sample Input

    10

    move 9 onto 1

    move 8 over 1

    move 7 over 1

    move 6 over 1

    pile 8 over 6

    pile 8 over 5

    !13SET TEST

  • move 2 over 1

    move 4 over 9

    quit

    !Sample Output

    0: 0

    1: 1 9 2 4

    2:

    3: 3

    4:

    5: 5 8 7 6

    6:

    7:

    8:

    9:

    !!!

    !14SET TEST

  • F. Up and Down Sequences !The quality of pseudo random-number generators used in some computations,

    especially simulation, is a significant issue. Proposed generation algorithms are subjected to

    many tests to establish their quality, or, more usually, their lack of it. One of the common tests

    is therun test.

    !In this test, sequences are tested for ``runs up" and ``runs down.

    !We will examine series of data values for the ``Up" and ``Down" sequences each series

    contains.

    !Within a series, an ``Up" sequence continues as long as each data-value received is not

    less than the previous data-value. An ``Up" sequence terminates when a data-value received is

    less than the previous data-value received.

    !A ``Down" sequence continues as long as each data-value received is not greater than

    the previous data-value. A ``Down" sequence terminates when a data-value received is

    greater than the previous data-value received.

    !An ``Up" sequence can be initiated by the termination of a ``Down" sequence and vice

    versa. (Sequences initiated in this manner have length one at this initiation point.)

    !All the initial data-values are part of an ``Up" sequence, and contribute to its length, if

    the first deviation of the data-values is upwards.

    !All the initial data-values are part of a ``Down" sequence, and contribute to its length,

    if the first deviation of the data-values is downwards.

    !If the data-values received don't allow classification as either an ``Up" or a ``Down"

    sequence, the data should be considered to have neither sequence.

    !

    !15SET TEST

  • Find the average length of both the ``Up" and the ``Down" sequences encountered for

    each input line in the data file. Report these average lengths as each input line is processed.

    !Input

    !Each of the separate series to be examined is contained on a single line of input.

    !Each series to be analyzed consists of at least one and no more than 30 unsigned, non-

    zero integers. Each integer in a series has at least one digit and no more than four digits. The

    integers are separated from each other by a single blank character. Each of the series will be

    terminated by a single zero (0) digit. This terminator should not be considered as being part

    of the series being analyzed.

    !Thesetof series to be analyzed is terminated by a single zero (0) digit as the input on a

    line. This terminator should not be considered to be a series, and no output should be

    produced in response to its encounter.

    !Output

    !A line with two real values is to be emitted for each input data set encountered. It must

    begin with the message ``Nr values =N:", whereNis the number of input data in the line;

    and then to continue with the average values for runs.

    !First, the average ``Up" run length, then the average ``Down" run length. Separate

    these values with a space.

    !Answers must be rounded to six digits after the decimal point.

    !16SET TEST

  • !!

    Sample Input

    1 2 3 0

    3 2 1 0

    1 2 3 2 1 0

    2 2 2 2 3 0

    4 4 4 4 3 0

    4 4 4 3 3 3 3 0

    4 4 4 3 3 3 4 0

    5 5 5 5 0

    1 2 3 2 3 4 5 0

    0

    Sample Output (Your output, using your chosen language, may be default-formatted differently).

    !

    Nr values = 3: 2.000000 0.000000

    Nr values = 3: 0.000000 2.000000

    Nr values = 5: 2.000000 2.000000

    Nr values = 5: 4.000000 0.000000

    Nr values = 5: 0.000000 4.000000

    Nr values = 7: 0.000000 6.000000

    Nr values = 7: 1.000000 5.000000

    Nr values = 4: 0.000000 0.000000

    Nr values = 7: 2.500000 1.000000.

    !17SET TEST

  • G. Instruens Fabulam

    Instruens Fabulam means drawing a chart (or table) in Latin. That's what you will do for

    this problem.

    Input

    The input consists of one or more table descriptions, followed by a line whose first

    character is '*', which signals the end of the file. Each description begins with a header line

    containing one or more characters that define the number and alignment of columns in the

    table. Each character in the header line is either '', and indicates a left-justified,

    centered, or right-justified column. Following the header are at least two and at most 21 data

    lines that contain the entries for each row. Each data line consists of one or more nonempty

    entries separated by an ampersand ('&'), where the number of entries is equal to the number

    of columns defined in the header line. The first data line contains entries for the column titles,

    and the remaining data lines contain entries for the body of the table. Spaces may appear

    within an entry, but never at the beginning or end of an entry. The characters '', '&',

    and '*' will not appear in the input except where indicated above.

    !Output

    For each table description, output the table using the exact format shown in the

    examples. Note that

    The total width of the table will never exceed 79 characters (not counting end-of-line).

    Dashes ('-') are used to draw horizontal lines, not underscores ('_'). 'At' signs ('@')

    appear at each of the four outer corners. Plus signs ('+') appear at intersections within the line

    separating the title from the body.

    The largest entry in a column is always separated from the enclosing bars ('|') by

    exactly one space.

    If a centered entry cannot be exactly centered within a column, the extra space goes

    on the right of the entry.

    Input and correct output files satisfy all the requirements listed in Notes to Teams,

    except that the output may contain two or more consecutive spaces. There are no spaces at

    the beginning or end of lines, and only spaces are used (never tabs).

    Sample Input

    =>

    !18SET TEST

  • TITLE&VERSION&OPERATING SYSTEM&PRICE

    Slug Farm&2.0&FreeBSD&49.99

    Figs of Doom&1.7&Linux&9.98

    Smiley Goes to Happy Town&11.0&Windows&129.25

    Wheelbarrow Motocross&1.0&BeOS&34.97

    >

    What is the answer?

    42

    Tweedledum&Tweedledee

    "Knock, knock."&"Who's there?"

    "Boo."&"Boo who?"

    "Don't cry, it's only me."&(groan)

    *

    Sample Output @-----------------------------------------------------------------@ | TITLE | VERSION | OPERATING SYSTEM | PRICE | |---------------------------+---------+------------------+--------| | Slug Farm | 2.0 | FreeBSD | 49.99 | | Figs of Doom | 1.7 | Linux | 9.98 | | Smiley Goes to Happy Town | 11.0 | Windows | 129.25 | | Wheelbarrow Motocross | 1.0 | BeOS | 34.97 | @-----------------------------------------------------------------@ @---------------------@ | What is the answer? | |---------------------| | 42 | @---------------------@ @---------------------------------------------@ | Tweedledum | Tweedledee | |----------------------------+----------------| | "Knock, knock." | "Who's there?" | | "Boo." | "Boo who?" | | "Don't cry, it's only me." | (groan) | @---------------------------------------------@ !

    !19SET TEST

  • H. One Move from Towers of Hanoi

    For this problem, we are concerned with the classic problem of Towers of Hanoi. In this

    problem there are three posts and a collection of circular disks. Lets call the number of disks

    n. The disks are of different sizes, with no two having the same radius, and the one main rule

    is to never put a bigger disk on top of a smaller one. We will number the disks from 1

    (smallest) to n (biggest) and name the posts A, B, and C. If all the disks start on post A, and

    the goal is to move the disks to post C by moving one at a time, again, never putting a bigger

    one on top of a smaller one, there is a well-known solution that recursively calls for moving n1

    disks from A to B, then directly moves the bottom disk from A to C, then recursively calls for

    moving the n1 disks from B to C.

    !Pseudocode for a recursive solution to classic Towers of Hanoi problem:

    move(num_disks, from_post, spare_post, to_post)

    if (num_disks == 0)

    return

    move(num_disks 1, from_post, to_post, spare_post)

    print ("Move disk ", num_disks, " from ",

    from_post, " to ", to_post)

    move(num_disks 1, spare_post, from_post, to_post)

    !The problem at hand is determining the k-th move made by the above algorithm for a

    given k and n.

    !Input

    Input will be two integers per line, k and n. End of file will be signified by a line with

    two zeros. All input will be valid, k and n will be positive integers with k less than 2n so that

    there is a k-th move, and n will be at most 60 so that the answer will fit in a 64-bit integer

    type.

    !Output

    Output the requested k-th move made by the above algorithm. Follow this format

    exactly: Case, one space, the case number, a colon and one space, and the answer for that

    !20SET TEST

  • case given as the number of the disk, the name of the from post, and the name of the to post

    with one space separating the parts of the answer. Do not print any trailing spaces.

    !Sample Input

    13 53 84 00

    !Sample Output

    Case 1: 1 A C

    Case 2: 1 B A

    Case 3: 4 A C

    !21SET TEST