Problem Set With Analysis Dhaka2008
Problem Set With Analysis Dhaka2008
Programming Contest
Dhaka Site
Sponsored by IBM
Hosted by North South University
Dhaka, Bangladesh
a) Solutions to problems submitted for judging are called runs. Each run is judged as accepted
or rejected by a judge, and the team is notified of the results.
b) Notification of accepted runs will NOT be suspended at the last one hour of the contest time
to keep the final results secret. Notification of rejected runs will also continue until the end of
the contest. But the teams will not be given any balloon and the public rank list will not be
updated in the last one hour.
c) A contestant may submit a clarification request to judges. If the judges agree that an
ambiguity or error exists, a clarification will be issued to all contestants.
d) Contestants are not to converse with anyone except members of their team and personnel
designated by the organizing committee. Systems support staff may advise contestants on
system-related problems such as explaining system error messages.
e) While the contest is scheduled for a particular time length (five hours), the Chief Judge has
the authority to alter the length of the contest in the event of unforeseen difficulties. Should
the contest duration be altered, every attempt will be made to notify contestants in a timely
and uniform manner.
f) A team may be disqualified by the Chief Judge for any activity that jeopardizes the contest
such as dislodging extension cords, unauthorized modification of contest materials, or
distracting behavior.
g) Ten problems will be posed. So far as possible, problems will avoid dependence on detailed
knowledge of a particular applications area or particular contest language. Of these ten
problems at least two will solvable by first year computer science student, two will be solvable
by a second year computer science student and rest will determine the winner.
h) Contestants will have foods available in their contest room during the contest. So they
cannot leave the contest room during the contest without permission from the room
invigilator. From a single room, members of only one team can get out at a time for maximum
15 minutes. The contestants are not allowed to communicate with members of any other
teams or coaches while they are outside the room. In other words rule (d) and (f) is valid
within or outside the contest room.
i) Team can bring upto 200 pages of printed materials with them but they can also bring three
additional books. But they are not allowed to bring calculators or any machine readable
devices like CD, DVD, Pen-drive, IPOD, MP3/MP4 players, floppy disks etc.
j) With the help of the volunteers the contestants can have printouts of their codes for
debugging purposes.
l) Teams should inform the volunteers if they don’t get reply from the judges within
15 minutes of submission. Volunteers will inform the floor supervisor. Teams should
also notify the volunteers if they cannot log in into the PC^2 system. This sort of
complains will not be entertained later.
2
Find the Format String
A Input: Standard Input
Output: Standard Output
Reading input is a very important and easy but boring part of programming. So functions
which can take difficult sort of inputs very easily are very popular among programmers. Such
a function is the ancient scanf() function of C, which exists even today with all its popularity
and glory.
The main strength of scanf() function is its format strings. For example “%d” is used to read
decimal integers, “%s” is used for reading strings. One less known but useful format string of
scanf() function is to specify the allowed characters within a third bracket pair. For example
the following scanf() function will only read decimal digits and will stop reading if it gets
anything else:
#include<stdio.h>
char s[101];
int main(void)
{
scanf("%[0123456789]",&s);
printf("%s\n",s);
}
For example, if the input string is “123abc345” then the value of s will be “123”, because
after reading “123” the scanf() function finds character ‘a’ which is not a decimal digit (Not
specified in the format string). So the output shown would be “123”.
Given the input string and the format string (In the example above the format string is
“0123456789” and input string is “123abc345”) it is very easy to determine the output
string “123”. But in this problem you will have to do the opposite. You will be given a set of
input strings and the corresponding output strings, and you will have to determine the format
string which will ensure all these inputs and corresponding outputs.
Input
The input file contains at most 3000 test cases. The description of each test case is given
below:
Each test case starts with an integer N (0<N<101) which denotes how many input/output
pairs will follow. The following N lines will contain two strings separated by a single space. The
strings are enclosed in double quotation. The first string denotes the input string and the
second string denotes the printed string (the characters that were actually read). You can
assume that the input and output strings will only contain alpha-numerals (‘0’-‘9’, ‘A’-
‘Z’ and ‘a’-‘z’) and their length will not exceed 100 (excluding the two double quotations)
and the input string (first string) will never be an empty string but the output string may be
empty. All the strings in the input file are enclosed in double quotation. So an empty string is
denoted as “”.
Output
For each set of input produce one line of output. This line contains the serial of output followed
by the desired format string that will ensure all the output string pairs. Make sure that this
format string contains only alpha-numerals. If there is more than one possible format string,
3
output the ASCII-graphically smallest one. (Lexicographically means word ordering as if they
were in lexicon or dictionary. So “and” is smaller than “bbt” because “and” would have
appeared before “bbt” if they both were kept in a dictionary. ASCII-cographically sorted
means sorted in the order in a lexicon where all the ascii characters are the alphabets. So here
“abc” is smaller than “cde”, “Mccain” is smaller than “barak” etc. The format string
must contain at least one alpha-numeral character and should contain no alpha-numeral more
than once. If no format string of positive length and consisting only alpha-numerals can
produce such output or the input output pair seems impossible then print the line
“I_AM_UNDONE” instead (without the quotes). Note that the format string is printed enclosed
in a third bracket. Look at the output for sample input for formatting details.
4
Switch Bulbs
B Input: Standard Input
Output: Standard Output
You are given n bulbs and m switches. Each of the switches toggles a list of bulbs. Initially all
the bulbs are turned off. Now for a set of desired states of the bulbs calculate the minimum
number of switch presses required to reach that state.
Input
Input contains multiple test cases. First line contains an integer T the number of test cases.
Each test case starts with a line containing 2 integers n (1≤n≤15) and m (1≤m≤40). Next m
line contains the description of m switches. Each of these lines starts with an integer k
denoting the number of bulbs that toggles their states after pressing this switch. The rest of
the line contains k distinct integers denoting the indices of the bulbs. The bulbs are numbered
from 0 to n-1. The next line contains an integer q(1≤q≤5000) that denotes the number of
queries. Each of the following q line contains a binary string of length n denoting the desired
states of the n bulbs: 1 means the bulb must be on and 0 means the bulb must be off. The
rightmost character is the state of bulb 0 and the leftmost character is the state of bulb n-1.
Output
For each test case output contains q+2 lines. First line contains “Case x:” where x is the
number of test cases. Each of the next q lines contains one integer denoting the minimum
number of switch presses required to reach the bulb states in the i’th query. If the state
cannot be reachable by a series of switch presses output -1. The last line will be a blank line.
5
Schedule of a Married Man
C Input: Standard Input
Output: Standard Output
Input
First line of the input file contains an integer N (0<N<2001) which denotes how many sets of
inputs are there. The input for each set is given in two lines. The description for each set is
given below:
First line of each set contains two strings separated by a single space. These two strings
denote the time span Danny is busy with his wife. The second line also contains two strings
which denotes the time when Danny has to attend a meeting. All the strings that denote time
are of the format hh:mm (two digit for hour and two digit for minute). For example “forty five
past eight” (Morning) is denoted as 08:45, “forty five past 9” (night) is denoted as 21:45. You
can assume that all times are valid 24-hour clock time, starting time strictly precedes ending
time and all times are within a single day.
Output
For each set of input produce one line of output. This line contains the serial of output followed
by a string which denotes Danny’s decision. If Danny can attend the meeting then print “Hits
Meeting” and if Danny misses (Mrs) the meeting as it conflicts with the time allotted for his
wife print “Mrs Meeting” instead.
6
Puzzles of Triangles
D Input: Standard Input
Output: Standard Output
Puzzles with geometric shapes are very interesting and is said to have great impact in
developing geometric insight of a child. ACM (Actual Challenge Makers) is planning to bring out
one such puzzle. The specialty of this puzzle is that all the pieces of this puzzle have the shape
of right-angled triangles. All these right angled pieces make the target rectangular shape and
it is shown in figure 2.
It may be a bit difficult to construct or even reconstruct a puzzle which is only composed of
pieces with the shape of a right-angled triangle. However if the actual rectangular puzzle is
divided into several square sub-regions, and then those sub-regions are divided into four
right-angled pieces then such puzzles are easy to make and solve.
Whether a rectangle can be divided into small pieces of squares can itself be an interesting
task to solve. But that is not the problem you have to solve here. Your job is to decide
whether a square shaped sheet can be divided into four right-angled triangles as shown in
figure 1. The machine that is used to cut the pieces can only deal with integers. So the length
of the square shaped sheets is always expressed in integer units and also cutter can only cut
in a straight line from one integer coordinate to another. The cutting must start on one of the
corners of the square shaped sheet, continue cutting in a triangular path (Like AEF showed in
Figure 1) and again finish at the corner it started. And if four corners of the square sheet are
lattice points (0,0), (N,0), (N,N) and (0,N), where N is the length of sides of the square
shaped sheet then point E and F should also be lattice points. So given a sheet, you have to
determine whether such cuts are possible, and if it is possible then in how many ways.
Input
The input file contains at most 800 lines of inputs.
Each line contains an integer N (0<N<1014), which means that we have to cut an (NxN) sheet.
Input is terminated by a line containing a single zero. This zero should not be processed.
Output
For each line of input produce one line of output. This line contains the serial of output
followed by a string or an integer. If it is not possible to cut the (NxN) sheet in the prescribed
7
way, then print a line “Impossible”, otherwise print an integer W. Here W denotes the
number of ways such a cut is possible. Look at the output for sample input for details
8
Chemical Plant
E Input: Standard Input
Output: Standard Output
Dealing with chemicals is a risky job, particularly if it is reactive. Reactive chemicals can be
dangerous if not stored in an appropriate environment. As a result, sophisticated instruments
that can support real-time processing, is required while transferring a reactive chemical from
one part of a chemical plant to another one. However, instruments that can ensure strictly
real-time service, is too much costly & hence chemical plants use networks that provide real-
time transfer with some tolerance.
Write a program to find a way to transfer the chemical from pit 1 to the safe storage with
maximum guaranteed weight left (in milligram) in the storage i.e. with minimum guaranteed
decay. Please note that, the chemical can not have negative weight at any point.
Input
The input file contains multiple test cases. First line of each test case contains three integers,
V (1≤V≤50,000 ), E (1≤E≤100,000 ) & W (1≤W≤2,000,000,000 ). The next line has two
integers, S (1≤S≤V) & T (0≤T≤2,000,000,000)
9
Each of the following E lines describes a tube. A Tube t is described with 6 integers, s
(1≤s≤V), d (1≤s≤V), sst, sct, dst, dct (0≤sst≤sct<dst≤dct≤2,000,000,000 ) where s, d, sst,
sct, dst & dct holds the meaning as in problem statement.
The end of input is denoted with a case where V = E = W = 0. This case should not be
processed.
Output
For each test case, print a single line of the form, “Plant C: L” where C is the test case
number and L is the weight of the chemical available in the safe storage at time T.
10
Clicking Checkboxes
F Input: Standard Input
Output: Standard Output
Creating a good and easy Graphical user interface is the heart of operating a computer in
modern days. One important part of this interface is enabling users to give inputs using mouse
and keyboard. Many different types of forms are there to take inputs from users. Such as (a)
Text Box (b) Radio Button (c) Check Box (d) Combo Box etc. In this problem we will
concentrate on check box.
Check boxes are used when a user needs to select more than one choice. Such a checkbox is
shown in the figure on the left below. The problem with checkbox is that when someone has
to choose n options or names he has to make n clicks, which can be very annoying when n is
high. So the new software giant toggle is using a new type of checkbox, which shows good
performance when number of boxes to be checked is high. In normal check box, a box is
checked or unchecked if and only if it is clicked but in toggle checkbox when a box is clicked
then all the boxes below it is also gets its status. That is if a box is originally not checked then
if someone clicks this box then this box and all the boxes below it will be checked. Similarly if
a box is checked and someone clicks on it then it will become unchecked and so will all the
boxes below it. So the selection made on the left can be made by only five clicks (The boxes
that has to be clicked are marked with red or dark square on the right figure) in toggle
checkbox (We are calling naming new type of checkbox as “toggle checkbox”).
Figure 1: 12 clicks are required in ordinary Figure 2: 5 clicks are required in toggle
checkbox checkbox.
The researchers of toggle have shown that toggle checkboxes show better performance when
at least 50% boxes are to be checked. For example for the selection shown in the above
picture we needed 12 clicks to check 12 boxes in normal checkbox but only five clicks were
required for toggle checkboxes. So in this situation toggle boxes show better performance.
11
In many other selection, toggle checkbox would also show strictly better performance. Your
job is to count in how many selection patterns (where at least m boxes out of the n boxes in
the checkbox are to be selected) toggle check box would show better performance and in how
many selection patterns ordinary checkbox would show better performance.
Input
The input file contains 1000 lines of inputs. The description of each line is given below:
Each line contains two integers n (0<n<64) and m (0≤m≤n). These two values indicate that
you have to consider a checkbox with n boxes to check and consider only the input patterns
where at least m of them are checked. Assume that initially all the boxes are unchecked.
Input is terminated by a line containing two zeroes. This line should be processed.
Output
For each line of input produce one line of output.
This line contains the serial of output followed by two integers TC and NC. Here TC is the total
number of selection patterns where toggle checkbox requires less clicks and NC is the total
number of selection patterns where ordinary or normal checkboxes require less clicks.
12
Illustration of first sample Input/Output:
When the total number of boxes that can be checked is five and at least three boxes has to be
5! 5! 5!
selected the total possible patterns for checking is + + = 10 + 5 + 1 = 16 . Of these 16
3!2! 4!1! 5!0!
patterns nine patterns are such that Toggle Checkbox shows better performance, three
patterns are such that Normal Checkbox shows better performance while other four others are
tied.
13
Magic Rings
G Input: Standard Input
Output: Standard Output
The first step of this level is to spot the locations of the monsters. After spotting the locations,
Tanzibal has to drop the superpowers one after another on the ground, at different locations,
in order to attack the monsters. A superpower, when dropped at certain point, creates a magic
ring of certain radius. Any monster that lies within the periphery of this ring will be injured.
The radii of the rings generated by the superpowers are all same, but its value is in Tanzibal’s
control. Tanzibal chooses a radius that is as low as possible. All the rings will have this
particular radius that Tanzibal opts for.
Given the location of N monsters and the number of superpowers that Tanzibal has at her
disposal, can you find out the minimum radius of the magic rings which would be enough to
injure all the monsters.
Input
The first line of input is an integer T(T≤80) that indicates the number of test cases. Each case
starts with two integers N(0< N< 19) and (0< K≤ N). N represents the number of monsters
and K represents the number of superpowers at hand. The next line contains the coordinates
of the N monsters in the format x1 y1 x2 y2 … xn yn. All the coordinates are integers in the
range [0, 10000].
Output
For each case, output the case number, followed by the minimum radius of the magic rings
rounded to 2 decimal places. Look at the output for sample input for details.
14
Line Chart
H Input: Standard Input
Output: Standard Output
SRT Rating
320 3
306 1
401 3
325 4
393 5
380 2
From this table, we see that his first SRT was SRT#306, and rating after that SRT was 1, so he
marked point (1, 1) as r1 in graph paper, his second SRT was SRT#320 and rating after that
SRT was 3, so he marked (2, 3) as r2, then he add r1 with r2 by a straight line and so on.
In general for his ith SRT he marked point (i, rating after ith SRT) by ri.
After marking all the points he will add point ri with ri-1 by straight lines, for all 1 < i <=N,
Where N is the total number of SRTs he played. For better idea look at figure 1:
15
After drawing line chart, he became very interested about the number of peaks. There are two
kinds of peaks in a line chart, 1) Upper Peak and 2) Lower Peak. Upper Peak is that point in a line
chart whose previous and next point has smaller y coordinates and lower peak is that points in a
line chart whose previous and next point has greater y coordinates. For example total number of
peak in figure 1 is 3. Two of them upper peak, which are (3, 4) and (5, 5), and one of them is
lower peak which is (4, 2).
ACRush observed that by ignoring SRT#380, his line chart will become like figure 2, in which
number of peak is only 1. By observing this he became more curious. Now he wants to know, by
ignoring 0 or more SRTs how many distinct line charts having K peaks is possible. ACRush calls
these line charts “K-peak Line charts”, in a K-peak line chart he doesn’t allow two consecutive
points to have same y coordinate.
Input
Input will start with an integer T ( T ≤ 12 ), which indicates the number of test cases. Each case
starts with a line having two integers N (1 ≤N ≤ 10000) and K ( 0 ≤ K ≤ 50 ). Each of the next N
lines will contain two integers SRT ( 1 ≤ SRT ≤ 1000000000) and Rating ( 1 ≤ Rating ≤
1000000000 ). All the SRT numbers will be distinct.
Output
For Each test case output a single Line "Case #: W", here # will be replaced by case number and
W will be replaced by the number of distinct K-peak line charts modulo 1000000.
Byte Communication Inc. (BYTECOM) is establishing its cellular network in Byteland. From the
first month of their establishment they are expanding their network very aggressively. Every
month they are constructing some transmission tower. They are considering the following policy
while establishing their networks.
1. BYTECOM have divided the Byteland into a logical hexagonal grid with N levels. Below is
the example of a hexagonal grid with 4 levels.
First level is the center and it contains only cell 1. The number of cells in subsequent
levels is 6,12,18,….. 2nd level contains cells from 2 to 7, 3rd level contains cells from 8-19,
4th level contains cells from 20-37,… In each of the first the lowest cell is numbered. Then
the rest of the cells are numbered in clockwise order.
2. Each transmission tower covers exactly one cell. So they will not build multiple towers in
the same cell.
3. There are 3 types of lines.
• The lines parallel to the connecting lines of the center of cell 1 and cell 2 is TYPE1
line. For example cell 14 and cell 29 are in the same TYPE1 line. Same for the cell
pairs (4, 28), (32, 35), (23, 25) etc.
• The lines parallel to the connecting lines of the center of cell 1 and cell 3 is TYPE2
line. For example cell 15 and cell 11 are in the same TYPE2 line. Same for the cell
pairs (4, 31), (10, 32), (16, 23) etc.
• The lines parallel to the connecting lines of the center of cell 1 and cell 4 is TYPE3
line. For example cell 13 and cell 17 are in the same TYPE3 line. Same for the cell
pairs (5, 27), (10, 37), (16, 28) etc.
• To clarify more there is not any other type of line. For example cell 3 and cell 5 is
not in any type of line. Same for the cell pairs (4, 6), (12, 29) etc.
Every month BYTECOM will construct a number of cellular towers. But they have
constraints for each the numbers of towers in each of the line type.
• Every month they can construct unlimited number of towers in each of the TYPE1
lines.
17
• Every month they can construct at most 2 towers in each of the TYPE2 lines.
• Every month they can construct at most 3 towers in each of the TYPE3 lines.
4. Initially the construction cost of a tower in cell i is Ci units of money. But because of
inflation it will increase by 1 unit of money every month. For example if C5 is 10 then
construction of a tower in cell 5 is 10 units of money in first month, 11 units of money in
second month, 12 units of money in third months and so on.
5. On month i BYTECOM will construct Mi number of towers with following the policy above.
Their plan is to minimize the cost of construction of these Mi towers. In each month they
just want to minimize the cost of that month, they don’t care for the following months.
Now given information of the cells and the monthly plans, help BYTECOM to minimize the cost of
tower construction for each of the months.
Input
Input contains multiple numbers of test cases. First line contains T(1≤T≤10) the number of test
cases. Each test case consists of 3 lines. First line contains n(2≤n≤20) and m(1≤m≤10). n is the
number of levels in cellular grid and m is the number of months for which BYTECOM is
constructing cellular towers. Second line contains 3n2-3n+1 integers (the total number of cells)
.The i’th integer is Ci (1≤ Ci ≤1000) denoting the cost of construction of a tower in cell i at first
month. Third line contains m integers. The i’th integer is Mi (1≤ Mi ≤50) denoting the number of
towers that need to be constructed on i’th month.
Output
For each test case output contains M+2 lines. First line is “Case i:” where i the number of test
case. Each of the next M lines contains the cost for the i’th month in the following format.
“Month i: c unit of money”
Where c is the minimum construction cost for Mi towers in month i.
Also the input guarantee that for every month the set of Mi cells where building Mi towers costs c
amount of money will be unique. No other set of unused cells will cost c or less amount of money
in that month. An unused cell is the one where no tower has been built yet.
Also it will be always possible to build Mi number of towers in i’th month.
18
Stopping Doom’s Day
J Input: Standard Input
Output: Standard Output
So! The time of the universe is up and it is the dooms day after five hours :-P, and you must stop
it. But to do so you have to know the value of the following expression T:
⎛ n n n n n ⎞
T = ⎜⎜ ∑∑∑∑∑ | (| i − j | * | j − k | * | k − l | * | l − m | * | m − i |) | ⎟⎟ % 10007
⎝ i =1 j =1 k =1 l =1 m=1 ⎠
Because the secret code that will save the universe from being doomed have something to do
with the value of the above expression for some value of n.
Input
The input file contains 1000 lines of inputs.
Output
For each line of input produce one line of output. This line contains the value of T.
19