Westminster College 2004 High School Programming Contest October 21, 2004

advertisement
Westminster College
2004 High School Programming Contest
October 21, 2004
Rules:
1. There are six questions to be completed in three hours.
2. All questions require you to read the test data from standard input and write results to standard
output. You should not use les for input or output.
3. The allowed programming languages are C++ and Java.
4. All programs will be re-compiled prior to testing with the judges ' data.
5. Programming style is not considered in this contest. You are free to code in whatever style you
prefer. Documentation is not required.
6. Judges' decisions are to be considered nal. No cheating will be tolerated.
2004 Westminster High School Programming Contest
Problem A:
1
Ad-ing it up
Kelly is in charge of the program book for her school's annual fund raiser. She raises money by selling
ads in the book. The cost of the ads are as follows: a full page ad costs $10, a 1/2 page ad costs $6
dollars and a 1/4 page ad costs $4. As ads come in, Kelly starts putting the program book together;
she creates a new page in the book as soon as she has enough ads for one page. For example, if the rst
ad she gets is a 1/2 page ad, and the second is a full page ad, the full page will be the rst in the book,
and the 1/2 page will go in only after she gets another 1/2 page ad or two 1/4 page ads. This process
can get a little repetitive after a while (giving her a feeling she calls ad-nauseum), so she comes to you
to help her put the book together.
Input
There will be multiple input sets. The rst line of the le will contain an integer n which indicates the
number of input sets. Each set starts with a line containing an integer m indicating the total number
of ads received. The next m lines list the ads in the form name size, where name is the name of the
company buying the ad and size is either the string \1", \1/2" or \1/4". name will contain no spaces
and will be no longer than 15 letters.
Output
For each input set, rst print out each page of the program book, with the ads listed in the order in
which they were put in. If multiple ads appear on one page, they should be listed in the order in which
they were received by Kelly, except for one case: if Kelly receives a 1/4 page ad, followed by a 1/2
page ad, and then by another 1/4 page ad, you should print the two 1/4 page ads rst, followed by
the 1/2 page ad. This rule would still apply even if several full page ads had been received in between
these three. For each page, print out the word Page, the page number, and then the company names,
separating each by a single space. The last page is the only page which may not be entirely full of ads.
After you nish printing out the program output the total amount of money raised by the ads on a
single line, starting with a $ sign. Insert a blank line after the last page.
(over)
2004 Westminster High School Programming Contest
Sample Input
2
3
IBM 1/2
ATT 1
Apple 1/2
5
McDonald's 1/4
BurgerKing 1
Wendy's 1/2
TacoBell 1/4
Pizza4U 1/4
Sample Output
Page 1 ATT
Page 2 IBM Apple
$22
Page 1 BurgerKing
Page 2 McDonald's TacoBell Wendy's
Page 3 Pizza4U
$28
2
3
2004 Westminster High School Programming Contest
Problem B:
Bowling Scores
Alley McBeal is the scorekeeper for a local bowling league, and her job includes keeping track of scores
and reporting them to area newspapers. She works out of her home and has the scores sent to her
electronically. For you non-keglers out there, the scoring of bowling is the following: A game is made
up of a total of 10 frames, and in each frame the bowler rolls two balls in an attempt to knock down
10 pins. If after rolling two balls the total number of pins knocked down is less than 10, then that is
the score for that frame. If the player gets all 10 pins down after the second ball this is a spare , and
scores 10 plus the number of pins knocked down by the next ball rolled. If the player gets all 10 pins
down on the rst ball this is known as a strike; the frame is over (she gets no second ball in that frame)
and the score is 10 plus the sum of the next two balls rolled. For example, if the bowler gets a spare
in the rst frame, then knocks down 4 pins following by 3 pins in the next frame, she has a score of 14
in the rst frame (10 + the 4 pins from the next roll), and 7 point in the second frame for a total of
21 for the two frames. If the rst three balls thrown are strikes, then she would have 30 points in the
rst frame (10 + the 20 pins from the next two balls), and the scores in the next two frames would be
pending depending on the next balls rolled. If the bowler ends the 10th frame with a spare, one extra
ball is rolled; if she ends the 10th with a strike, two extra balls are rolled (no extra balls are rolled after
these, regardless if 10 pins are knocked down). The cumulative score of all 10 frames is the nal score
for the game.
There is a standard way to present bowling scores, as shown in the example below. Each of the larger
squares represents a frame (frame 1 to the left, frame 10 to the right). The smaller squares contain the
number of pins knocked down by each ball, with a slash indicating a spare and an X indicating a strike.
The last frame contains one extra box in case a strike or spare is thrown in that frame. The larger
numbers are the cumulative scores after each frame
4
14
4
3
21
Q
Q
Q
41
8
Q
Q
Q
61
88
Q
Q
Q
107
7
2
116
Q
Q
Q
134
2
6
142
@@ 5
162
Alley has a small problem, however. Due to a mishap in communications, all she has are the number
of pins knocked down from each ball. For example, in the game above all she received was 4, 6, 4, 3,
10, 8, 2, 10, 10, 7, 2, 10, 2, 6, 10, 5 and 5. Given this, she was able to reconstruct the nal score sheet,
but she would like to see this process automatized for the remaining sets of scores.
Input
There will be multiple input sets. Each set will consist of one line. The rst value on each line will be
the number of balls rolled for the game, and that will be followed by the pins knocked down by each
ball. A line starting with 0 will end input and should not be processed
Output
For each input set, output a score sheet in the exact format shown below in the example, followed by a
blank line. Note that all of the running score values are right justied and that a value of 10 is never
printed in one of the smaller boxes.
(over)
2004 Westminster High School Programming Contest
Sample Input
17 4 6 4 3 10 8 2 10 10 7 2 10 2 6 10 5 5
20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 8
0
Sample Output
+---++---++---++---++---++---++---++---++---++-----+
|4|/||4|3||X| ||8|/||X| ||X| ||7|2||X| ||2|6||X|5|/|
+---++---++---++---++---++---++---++---++---++-----+
| 14|| 21|| 41|| 61|| 88||107||116||134||142|| 162|
+---++---++---++---++---++---++---++---++---++-----+
+---++---++---++---++---++---++---++---++---++-----+
|1|1||1|1||1|1||1|1||1|1||1|1||1|1||1|1||1|1||1|8| |
+---++---++---++---++---++---++---++---++---++-----+
| 2|| 4|| 6|| 8|| 10|| 12|| 14|| 16|| 18|| 27|
+---++---++---++---++---++---++---++---++---++-----+
4
2004 Westminster High School Programming Contest
Problem C:
5
Cryptograms
A cryptogram is an encoded message where every letter has been replaced by a dierent letter. For
example, a cryptogram-ed version of the sentence \Sam's mom is sassy" could be obtained by substituting an `h' for each `s', an `r' for each `a', an `s' for each `m', a `j' for each `o', a `z' for each `i' and
a `b' for each `y' resulting in \Hrs'h sjs zh hrhhb". Now cryptograms this size are hard to decode, but
for larger cryptograms letter frequency is a key in deciphering them. In the English language, the 10
most commonly used letters are
e t a i n o s r l d,
so it is a safe bet that the 5 letters which show up the most often in the cryptogram are each encodings
for one of these most common letters. For this problem, you are to write a program to aid in this
deciphering process.
Input
There will be multiple input sets. THe rst line will contain a single integer n indicating the number of
input sets. Each input set will be a single cryptogram spread over one or more lines. Each cryptogram
will be followed by a blank line to separate it from the next cryptogram (the last cryptogram will also
be followed by a blank line).
Output
For each input set, output the top 5 (or so) characters which appear the most often, in descending
order or frequency count. Print the number of appearances rst, followed by the letter, with a single
space between them. If more than one letter has the same frequency, print them all on the same line
in alphabetical order, again separating them each by a single space. Continue to print letters until you
have printed at least 5 letters or run out of letters. All letters should be output in lowercase. Print a
blank line after the last letter is printed.
(over)
6
2004 Westminster High School Programming Contest
Sample Input
3
Hrs'h sjs zh hrhhb
This isn't really a cryptogram but
is just a large example showing
that the cryptogram can contain lots
and
go
over multiple lines.
aba
Sample Output
6
3
2
1
h
s
r
b j z
12 a t
9 e
8 l o s
2 a
1 b
of
space
7
2004 Westminster High School Programming Contest
Problem D:
Disputed Claims
Kyle Pickett works in the county oce and is in charge of land ownership in his county. When two
people make claims on the same area of land, it is up to Kyle to determine who is the rightful owner.
All land claims are conveniently in the shape of rectangles aligned along the north-south and east-west
axes. When two people bring in claims, the rst thing Kyle needs to do is to determine whether or
not there is any overlap between them. Assuming that A and B are the two claims of land, there are
four scenarios of interest to Kyle: A completely surrounding B, B completely surrounding A, A and
B overlapping (but neither surrounding the other) and A and B not overlapping. The pictures below
show two examples each of all four cases. Note that if A and B intersect at only a point or line they
are considered non-overlapping.
A
B
B
A
B
A
B A
B
A surrounds B
B surrounds A
A
A
A
B
B
A
B
A and B overlap A and B do not overlap
For this problem, you will read in descriptions of two rectangular claims, and determine which of the
four cases is true.
Input
There will be multiple input sets. The rst line of the input will be an integer n indicating the number
of input sets. Each input set will consist of a single line containing 8 non-negative integers:
1A y 1A
x
2A y 2A
x
1B y 1 B
x
2 B y 2B
x
where x1A ; y 1A are the coordinates of the lower left corner of A's claim, and x2A ; y 2A are the upper
right corner of A's claim. The remaining four values are the corresponding corners of B's claim. All
claims will have non-zero area (pretty ridiculous claim otherwise!).
Output
For each input set, output either the phrase A surounds B, B surrounds A, A and B overlap or A
and B do not overlap. The two claims will never be identical.
Sample Input
3
10 10 20 20 12 0 14 18
0 0 5 5 0 0 6 6
0 0 5 5 0 5 5 10
Sample Output
A and B overlap
B surrounds A
A and B do not overlap
2004 Westminster High School Programming Contest
Problem E:
8
Palindrome Problem El Borpem Ord Nilap
A palindrome is a word or phrase which is spelled the same backwards and forwards (ignoring any
punctuation and whitespace). Some famous palindromes include
Madam, I'm Adam.
A man, a plan, a canal: Panama!
Go hang a salami, I'm a lasagna hog.
For this problem, you will read in a string of text and determine whether or not it is a palindrome.
Input
There will be multiple input sets. Each set will consist of one line of text of no more than 80 characters.
The last line of the le will contain the sentence THE END. and should not be processed
Output
For each input set, output either the word Yes or the word No depending on whether the input string
is a palindrome or not.
Sample Input
Go hang a salami, I'm a lasagna hog.
Westminster High School Programming Contest
Dennis
sinned
THE END.
Sample Output
Yes
No
Yes
2004 Westminster High School Programming Contest
Problem F:
9
Simple Statistics
Given a set of numerical data, there are several ways in which to describe it. One way is the so-called
5-number summary. The ve numbers used to describe the data set are the following: the minimum
value, the rst quartile, the median, the third quartile and the maximum value. The denition of the
minimum and maximum values are obvious. The median of a set of numbers is the value of the number
which would lie exactly in the middle of the set if it were sorted. For example, the median of the data
set 7; 1; 9; 4; 1 would be 4. If there is an even number of values in the set, then the median is the
average of the two values closest to the middle; if our set contained the values 7; 1; 9; 4; 1; 0 then the
median would be (1 + 4)=2 = 2:5.
The denition of the quartiles follows naturally from the denition of the median. The rst quartile is
the median of all of the values less than the median of the full set, and the third quartile is the median
of all of the values greater than the median of the full set. In our example above with 7; 1; 9; 4; 1; 0,
the rst quartile value would be 0 (the median of the value -1, 1 and 0 which are less than 2.5) and the
third quartile would be 7. One special case is when there is only one element in the list, in which case
the quartiles are equal to the median.
One other way to characterize data is its skewness. A distribution is considered right-skewed whenever
the maximum value is farther from the median than the minimum value, or when the maximum and
minimum are equally distant from the median, but the third quartile is farther from the median than
the rst quartile. A left-skewed distribution is one with the opposite situation. For our purposes, a
distribution which is neither left-skewed nor right-skewed is considered symmetric. Your task for this
problem is to read in various sets of numbers and output the 5-number summary for each, along with
the skewness of the data.
Input
There will be multiple input sets. Each input set will consist of a single line of the form
n
where n is the number of data values, and v1 ; : : : ; vn are the values. All the values will be integers and
the maximum value for n will be 100. A line which begins with 0 indicates end of input and should not
be processed.
n v1 v2 v3 : : : v
Output
For each input set, output the 5-number summary and skewness in the order minimum, rst quartile,
median, third quartile, maximum and skew, with a single space between each. Skew will either be the
phrase right-skewed, left-skewed or symmetric.
Sample Input
6 7 -1 9 4 1 0
15 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
15 15 14 13 12 11 10 9 8 7 6 5 4 3 2 0
0
Sample Output
-1 0 2.5 7 9 right-skewed
1 4 8 12 15 symmetric
0 4 8 12 15 left-skewed
Download