Problemset Naq 2016
Problemset Naq 2016
Problem A
A New Alphabet
For instance, translating the string “Hello World!” would result in:
[-]3110 \/\/0|Z1|)!
Note that uppercase and lowercase letters are both converted, and any other characters remain the same
(the exclamation point and space in this example).
Input
Input contains one line of text, terminated by a newline. The text may contain any characters in the
ASCII range 32–126 (space through tilde), as well as 9 (tab). Only characters listed in the above table
(A–Z, a–z) should be translated; any non-alphabet characters should be printed (and not modified). Input
has at most 10 000 characters.
Output
Output the input text with each letter (lowercase and uppercase) translated into its New Alphabet coun-
terpart.
Sample Input 1
All your base are belong to us.
Sample Output 1
@11 ‘/0|_||Z 8@$3 @|Z3 8310[]\[]6 ’][’0 |_|$.
Sample Input 2
What’s the Frequency, Kenneth?
Sample Output 2
\/\/[-]@’][’’$ ’][’[-]3 #|Z3(,)|_|3[]\[](‘/, |<3[]\[][]\[]3’][’[-]?
Problem B
Arcade!
Have you recently visited an arcade? Arcade
games seem to have become more boring over the
years, requiring less and less skill. In fact, most
arcade games these days seem to depend entirely
on luck. Consider the arcade game shown in the
picture, which consists of different holes arranged
in a triangular shape. A ball is dropped near the
hole at the top. The ball either falls into the hole,
in which case the game ends, or it bounces to one
of its (up to) 4 neighbors, denoted by the red ar-
rows. Different holes have different payouts —
some may even be negative! If the ball reaches
another hole, the process repeats: the ball either falls into the hole, ending the game — or it bounces to
one of its neighbors, possibly ad infinitum!
Write a program that computes the expected payout when dropping a ball into the machine!
Input
The input consists of a single test case. The first line contains an integer N (1 ≤ N ≤ 32) describing
the number of rows of the arcade machine. The second line contains H = N (N + 1)/2 integers vi
(−100 ≤ vi ≤ 100) describing the payout (positive or negative) if the ball drops into hole i. Holes are
numbered such that hole 1 is in the first row, holes 2 and 3 are in the second row, etc. The k th row starts
with hole number k(k − 1)/2 + 1 and contains exactly k holes.
These two lines are followed by H lines, each of which contains 5 real numbers p0 p1 p2 p3 p4 , denoting
the probability that the ball bounces to its top-left (p0 ), top-right (p1 ), bottom-left (p2 ), or bottom-right
(p3 ) neighbors or that the ball enters the hole (p4 ). Each probability
P is given with at most 3 decimal digits
after the period. It is guaranteed that 0.0 ≤ pi ≤ 1.0 and pi = 1.0. If a hole does not have certain
neighbors because it is located near the boundary of the arcade machine, the probability of bouncing to
these non-existent neighbors is always zero. For instance, for hole number 1, the probabilities to jump
to the top-left and top-right neighbors are both given as 0.0.
You can assume that after the ball has bounced b times, the probability that it has not fallen into a hole
is at most (1 − 10−3 )bb/Hc .
Output
Output a single number, the expected value from playing one game. Your answer is considered correct
if its absolute or relative error is less than 10−4 .
Hint: Using Monte Carlo-style simulation (throwing many balls in the machine and simulating which
hole they fall into using randomly generated choices) does not yield the required accuracy!
Problem C
Big Truck
Your boss has hired you to drive a big truck, transporting items be-
tween two locations in a city. You’re given a description of the city,
with locations of interest and the lengths of roads between them. Your
boss requires that you take a shortest path between the starting and
ending location, and she’ll check your odometer when you’re done to
make sure you didn’t take any unnecessary side trips. However, your
friends know you have plenty of unused space in the truck, and they
have asked you to stop by several locations in town, to pick up items Photo by Phil Whitehouse
for them. You’re happy to do this for them. You may not be able to
visit every location to pick up everything your friends want, but you’d like to pick up as many items as
possible on your trip, as long as it doesn’t make the path any longer than necessary.
The two graphs above show examples of what the city may look like, with nodes representing locations,
edges representing roads and dots inside the nodes representing items your friends have asked you to
pick up. Driving through a location allows you to pick up all the items there; it’s a big truck, with no
limit on the items it can carry. In the graph on the left, for example, you have to drive the big truck from
location 1 to location 6. If you follow the path 1 → 2 → 3 → 6, the length is 9, and you’ll get to pick
up 4 items. Of course, it would be better to drive 1 → 4 → 5 → 6; that’s still a length of 9, but going
this way instead lets you pick up an additional item. Driving 1 → 4 → 3 → 6 would let you pick up
even more items, but it would make your trip longer, so you can’t go this way.
Input
The first line of input contains an integer, n (2 ≤ n ≤ 100), giving the number of locations in the
city. Locations are numbered from 1 to n, with location 1 being the starting location and n being the
destination. The next input line gives a sequence of n integers, t1 . . . tn , where each ti indicates the
number of items your friends have asked you to pick up from location i. All the ti values are between 0
and 100, inclusive. The next input line contains a non-negative integer, m, giving the number of roads
in the city. Each of the following m lines is a description of a road, given as three integers, a b d. This
indicates that there is a road of length d between location a and location b. The values of a and b are in
the range 1 . . . n, and the value of d is between 1 and 100, inclusive. All roads can be traversed in either
direction, there is at most one road between any two locations, and no road starts and ends at the same
location.
Output
If it’s not possible to travel from location 1 to location n, just output out the word “impossible”. Oth-
erwise, output the length of a shortest path from location 1 to location n, followed by the maximum
number of items you can pick up along the way.
Problem D
Brackets
A bracket sequence consisting of ‘(’ and ‘)’ is defined to be valid as
follows:
For example, “(())”, “()()”, and “(()())()” are all valid bracket sequences, while “(” and “())” are invalid
bracket sequences.
You get a bracket sequence from the professor of length n. However, it might not be valid at the moment.
The professor asks you to check if it is possible to make the sequence valid by performing at most one
segment inversion operation. That is, you may choose two 1-based indices l and r (1 ≤ l ≤ r ≤ n)
and invert each bracket with an index in the closed interval [l, r]. After the inversion, a left bracket ‘(’
becomes a right bracket ‘)’, and a right bracket ‘)’ becomes a left bracket ‘(’.
You can make “())(” valid by inverting the segment [3, 4]. You can make “()))” valid by inverting the
segment [3, 3], or alternatively by inverting the segment [2, 2]. However, there does not exist a segment
you can invert to make “)))(” valid.
Input
The input consists of one line containing between 1 and 5 000 brackets.
Output
Output “possible” if you can make the bracket sequence valid by performing at most one segment inver-
sion, or “impossible” otherwise.
Problem E
Dots and Boxes
Alice and Bob are playing Dots and Boxes. The game is
played on an N × N square lattice of dots, and they alter-
nate drawing a line segment between horizontally or verti-
cally adjacent dots that haven’t been connected before. Ev-
ery time a unit square is formed by four line segments, the
player who put down the last segment scores one point for
that square. The game ends when the square lattice has been
completely filled with line segments, and whoever scored A game of Dots and Boxes.
Input
Input starts with a line containing an integer N (2 ≤ N ≤ 80), the size of the square lattice. Then
follows an ASCII representation of the current state of the game, 2N − 1 rows high and 2N − 1 columns
wide, listed in row-major order. There are cells of four types (1 ≤ i, j ≤ N ):
• Cell (2i, 2j − 1) is ‘|’ if dots (i, j) and (i + 1, j) have been connected by a line segment, and ‘.’
otherwise.
• Cell (2i − 1, 2j) is ‘-’ if dots (i, j) and (i, j + 1) have been connected by a line segment, and ‘.’
otherwise.
It is guaranteed that no player has scored a point, meaning that no unit squares have been formed.
Output
Output the number of moves that can be made, in the worst case, before either Alice or Bob is guaranteed
to have scored a point.
Problem F
Free Desserts
Quido has lunch in Hugo’s restaurant every day. He likes the restaurant
because all of its prices are expressed as integers, and for each possible
price (i.e. $1, $2, $3, etc.) there is at least one beverage and at least
one main dish on the menu. Every day there are three entries printed
on Quido’s lunch bill: the beverage price, the main dish price, and the
total price. Hugo knows of Quido’s interest in computational problems
and he offered Quido a free dessert each time his lunch bill meets the
following three constraints:
• the price of the beverage is less than the price of the main dish,
and
Photo by Lotus Head
• the prices listed on the bill cannot mutually use the same digit. In
essence, any digit which occurs in any of the entries (beverage,
main dish, total) must be different from any of the digits of the
other two entries.
Quido is on a budget and he pays the same price for his lunch every day. How many times can he have
a free dessert?
Input
The input consists of a single line with one integer representing the price P which Quido pays for each
lunch. The value of P is positive and less than 1018 .
Output
Output the maximum number of times Quido can have a free dessert at Hugo’s restaurant, provided
that the price of his lunch is always P . Next, the possible bills which result in a free dessert are listed
in ascending order with respect to the beverage price. Each bill consists of the price of the beverage
followed by the price of the main dish. For simplicity, the value P , which is always the same, is not
included in the bill.
If there are more than 5 000 possible bills, then output only the first 5 000 bills (but still report the total
number of possible bills before the list of bills).
Problem G
Inverse Factorial
Input
The input contains the factorial n! of a positive integer n. The number of digits of n! is at most 106 .
Output
Problem H
Nine Packs
This is a quote from the 1986 movie, “True Stories”, and it’s true;
well, almost true. You could buy four packs of 10 hotdogs and
five packs of 8 buns. That would give you exactly 40 of each.
However, you can make things even with fewer packs if you buy
two packs of 10 hotdogs, along with a pack of 8 buns and another
pack of 12 buns. That would give you 20 of each, using only 4 total packs.
For this problem, you’ll determine the fewest packs you need to buy to make hotdogs and buns come
out even, given a selection of different bun and hotdog packs available for purchase.
Input
The first input line starts with an integer, H, the number of hotdog packs available. This is followed by
H integers, h1 . . . hH , the number of hotdogs in each pack. The second input line starts with an integer,
B, giving the number of bun packs available. This is followed by B integers, b1 . . . bB , indicating the
number of buns in each pack. The values H and B are between 0 and 100, inclusive, and the sizes of
the packs are between 1 and 1 000, inclusive. Every available pack is listed individually. For example,
if there were five eight-bun packs available for purchase, the list of bun packs would contain five copies
of the number eight.
Output
If it’s not possible to purchase an equal number of one or more hotdogs and buns, just output “impossible”.
Otherwise, output the smallest number of total packs you can buy (counting both hotdog and bun packs)
to get exactly the same number of hotdogs and buns.
Problem I
Primonimo
Input
Output
Problem J
Quick Estimates
Let’s face it... you are not that handy. When you need to make a major
home repair, you often need to hire someone to help. When they come
for the first visit, they make an estimate of the cost. Here they must be
careful: if they overestimate the cost, it might scare you off, but if they
underestimate, the work might not be worth their time.
Because the worker is so careful, it can take a long time for them to
Photo by Simon A. Eugster
produce the estimate. But that’s frustrating — when you ask for an
estimate, you really are asking for the magnitude of the cost. Will this
be $10 or $100 or $1 000? That’s all you really want to know on a first visit.
Please help the worker make the type of estimate you desire. Write a program that, given the worker’s
estimate, reports just the magnitude of the cost — the number of digits needed to represent the estimate.
Input
Input begins with a line containing an integer N (1 ≤ N ≤ 100). The next N lines each contain one
estimated cost, which is an integer between 0 and 10100 . (Some of the workmen overcharge quite a bit.)
Output
For each estimated cost, output the number of digits required to represent it.
Problem K
Robotopia
Input
The first line contains an integer n (1 ≤ n ≤ 100). The following n lines each contain one test case.
Each has six integers: l1 a1 l2 a2 lt at , where l1 and a1 are the number of legs and arms (respectively)
for the first type of robot, l2 and a2 are those for the second type, and lt and at are the total number of
observed legs and arms. All values are in the range [1, 10 000].
Output
For each test case output two positive integers denoting the number of each of the two types of robots.
Give first the count for the first type listed. If the test case has no solution or multiple solutions, output
“?” (a question mark).
Problem L
Unusual Darts
In the game of Unusual Darts, Alice throws seven darts onto a 2-foot
by 2-foot board, and then Bob may or may not throw three darts.
Alice’s seven darts define a polygon by the order in which they are
thrown, with the perimeter of the polygon connecting Dart 1 to Dart 2
to Dart 3 to Dart 4 to Dart 5 to Dart 6 to Dart 7, and back to Dart 1.
If the polygon so defined is not simple (meaning it intersects itself)
then Alice loses. If the polygon is simple, then Bob throws three darts.
He is not a very good player, so although his darts always land on the
board, they land randomly on the dart board following a uniform distribution. If all three of these darts
land within the interior of the polygon, Bob wins, otherwise Alice wins.
For this problem you are given the locations of Alice’s darts (which form a simple polygon) and the
probability that Bob wins. Your job is to determine the order in which Alice threw her darts.
Input
The first line of input contains an integer N (1 ≤ N ≤ 1 000), indicating the number of Darts games
that follow. Each game description has 8 lines. Lines 1 through 7 each have a pair of real numbers with
3 digits after the decimal point. These indicate the x and y coordinates of Alice’s seven darts (x1 y1 to
x7 y7 ), which are all at distinct locations. All coordinates are given in feet, in the range (0 ≤ xi , yi ≤ 2).
The 8th line contains a real number p with 5 digits after the decimal point, giving the probability that Bob
wins. In all test cases, Alice’s darts do form a simple polygon, but not necessarily in the order given.
Output
For each Darts game, output the order in which the darts could have been thrown, relative to the order
they were given in the input, so that Bob wins with probability p. If several answers are possible, give
the one that is lexicographically least. Any ordering that would give Bob a probability of winning within
10−5 of the given value of p is considered a valid ordering.