You are on page 1of 6

www.iarcs.org.

in

Zonal Computing Olympiad 2012, 26 Nov 2011


10:00 am-1:00 pm IST
Problem 1 : Matched Brackets
A sequence of opening and closing brackets is well-bracketed if we can pair up each opening bracket
with a matching closing bracket in the usual sense. For instance, the sequences (), (()) and ()(()) are wellbracketed, while (, ()), (()(), and )( are not well-bracketed.
The nesting depth of a well-bracketed sequence tells us the maximum number of levels of inner matched
brackets enclosed within outer matched brackets. For instance, the nesting depth of () and ()()() is 1, the
nesting depth of (()) and ()(()) is 2, the nesting depth of ((())) is 3, and so on.
Given a well-bracketed sequence, we are interested in computing the following:
The nesting depth, and the first position where it occursthis will be the position of the first opening
bracket at this nesting depth, where the positions are numbered starting with 1.
The maximum number of symbols between any pair of matched brackets, including both the outer
brackets, and the first position where this occursthat is, the position of the first opening bracket of this
segment.
For instance, the nesting depth of ()(())()(()())(()()) is 2 and the first position where this occurs is 4. The
opening bracket at position 10 is also at nesting depth 2 but we have to report the first position where
this occurs, which is 4.
In this sequence, the maximum number of symbols between a pair of matched bracket is 6, starting at
position 9. There is another such sequence of length 6 starting at position 15, but this is not the first
such position.
Input format
The input consists of two lines. The first line is a single integer N, the length of the bracket sequence.
Positions in the sequence are numbered 1,2,,N. The second line is a sequence of N space-separated
integers that encode the bracket expression as follows: 1 denotes an opening bracket ( and 2 denotes a
closing bracket ). Nothing other than 1 or 2 appears in the second line of input and the corresponding
expression is guaranteed to be well-bracketed.
Output format
Your program should print 4 space-separated integers in a line, denoting the four quantities asked for in
the following order: nesting depth, first position that achieves the nesting depth, length of the maximum
sequence between matching brackets and the first position where such a maximum length sequence
occurs.

Testdata
You may assume that 2 = N = 105. In 30% of the test cases, 2 = N = 103.
Sample Input
20
12112212112122112122
Sample Output
2469
Time and memory limits
The time limit for this task is 1 second. The memory limit is 32MB.

Zonal Computing Olympiad 2012, 26 Nov 2011


10:00 am-1:00 pm IST
Problem 2 : Wormholes
The year is 2102 and today is the day of ZCO. This year there are N contests and the starting and ending
times of each contest is known to you. You have to participate in exactly one of these contests. Different
contests may overlap. The duration of different contests might be different.
There is only one examination centre. There is a wormhole V that transports you from your house to the
examination centre and another wormhole W that transports you from the examination centre back to
your house. Obviously, transportation through a wormhole does not take any time; it is instantaneous.
But the wormholes can be used at only certain fixed times, and these are known to you.
So, you use a V wormhole to reach the exam centre, possibly wait for some time before the next contest
begins, take part in the contest, possibly wait for some more time and then use a W wormhole to return
back home. If you leave through a V wormhole at time t1 and come back through a W wormhole at time
t2, then the total time you have spent is (t2 - t1 + 1). Your aim is to spend as little time as possible overall
while ensuring that you take part in one of the contests.
You can reach the centre exactly at the starting time of the contest, if possible. And you can leave the
examination centre the very second the contest ends, if possible. You can assume that you will always be
able to attend at least one contestthat is, there will always be a contest such that there is a V wormhole
before it and a W wormhole after it.

For instance, suppose there are 3 contests with (start,end) times (15,21), (5,10), and (7,25), respectively.
Suppose the V wormhole is available at times 4, 14, 25, 2 and the W wormhole is available at times 13
and 21. In this case, you can leave by the V wormhole at time 14, take part in the contest from time 15 to
21, and then use the W wormhole at time 21 to get back home. Therefore the time you have spent is (21
- 14 + 1) = 8. You can check that you cannot do better than this.
Input format
The first line contains 3 space separated integers N, X, and Y, where N is the number of contests, X is the
number of time instances when wormhole V can be used and Y is the number of time instances when
wormhole W can be used. The next N lines describe each contest. Each of these N lines contains two
space separated integers S and E, where S is the starting time of the particular contest and E is the
ending time of that contest, with S < E. The next line contains X space separated integers which are the
time instances when the wormhole V can be used. The next line contains Y space separated integers
which are the time instances when the wormhole W can be used.
Output format
Print a single line that contains a single integer, the minimum time needed to be spent to take part in a
contest.
Testdata
All the starting and ending times of contests are distinct and no contest starts at the same time as
another contest ends. The time instances when wormholes are available are all distinct, but may
coincide with starting and ending times of contests. All the time instances (the contest timings and the
wormhole timings) will be integers between 1 and 1000000 (inclusive).
You may assume that 1 = N = 105, 1 = X = 105, and 1 = Y = 105.
In 30% of the cases, 1 = N = 103, 1 = X = 103, and 1 = Y = 103.
Sample Input
342
15 21
5 10
7 25
4 14 25 2
13 21
Sample Output
8
Time and memory limits
The time limit for this task is 1 second. The memory limit is 32MB.

Zonal Computing Olympiad 2012, 26 Nov 2011


2:00 pm-5:00 pm IST
Problem 1 : Matched Brackets
We consider sequences of opening and closing brackets with two types of brackets, () and []. A bracket
sequence is well-bracketed if we can pair up each opening bracket with a matching closing bracket in the
usual sense. For instance, the sequences (), [] ([]) and []([]) are well-bracketed, while (, ()], (], )( and [(])
are not well-bracketed. In the last case, each opening bracket has a matching closing bracket and vice
versa, but the intervals spanned by the different types of brackets intersect each other instead of being
contained one within the other.
The alternating depth of a well-bracketed sequence tells us the maximum number of times we switch
between the two types of brackets when we have inner matched brackets enclosed within outer
matched brackets. For instance, the alternating depth of (), [[[]]] and ()[][] is 1, the alternating depth of
[()] and ()([]) is 2, the alternating depth of ([()]) and [()][(([]))] is 3, and so on.
Given a well-bracketed sequence, we are interested in computing three quantities.
The alternating depth of the sequence.
The maximum number of symbols between any pair of matched brackets of the type ( and ), including
both the outer brackets.
The maximum number of symbols between any pair of matched brackets of the type [ and ], including
both the outer brackets.
For instance, the alternating depth of (([]))[[[()]]] is 2, the maximum number of symbols between a
matched pair () is 6 and the maximum number of symbols between a matched pair [] is 8.
Input format
The input consists of two lines. The first line is a single integer N, the length of the bracket sequence.
Positions in the sequence are numbered 1,2,,N. The second line is a sequence of N space-separated
integers that encode the bracket expression as follows: 1 denotes an opening bracket (, 2 denotes a
closing bracket ), 3 denotes an opening bracket [ and 4 denotes a closing bracket ]. Nothing other than 1,
2, 3 or 4 appears in the second line of input and the corresponding expression is guaranteed to be wellbracketed.
Output format

Your program should print 3 space-separated integers in a line, denoting the three quantities asked for in
the following order: alternating depth, length of the maximum sequence between matching () brackets
and length of the maximum sequence between matching [] brackets.
Testdata
You may assume that 2 = N = 105. In 30% of the test cases, 2 = N = 103.
Sample Input
14
11342233312444
Sample Output
268
Time and memory limits
The time limit for this task is 1 second. The memory limit is 32MB.

Zonal Computing Olympiad 2012, 26 Nov 2011


2:00 pm-5:00 pm IST
Problem 2 : Round Table
It's dinner time in Castle Camelot, and the fearsome Knights of the Round Table are clamouring for
dessert. You, the chef, are in a soup. There are N knights, including King Arthur, each with a different
preference for dessert, but you cannot afford to make desserts for all of them.
You are given the cost of manufacturing each Knight's preferred dessertsince it is a round table, the list
starts with the cost of King Arthur's dessert, and goes counter-clockwise.
You decide to pick the cheapest desserts to make, such that for every pair of adjacent Knights, at least
one gets his dessert. This will ensure that the Knights do not protest.
What is the minimum cost of tonight's dinner, given this condition?
For instance, suppose there are 5 Knights and their desserts cost 1, 2, 1, 2 and 2. In this case, the
minimum cost is 4, which you can achieve by feeding the first, third and fourth (or fifth) Knights.
Input format

There are 2 lines of input. The first line contains a single integer N, the number of seats at the table. The
next line contains N space separated integers, each being the cost of the dessert of a Knight, listed in
counterclockwise order around the table, starting with King Arthur.
Output format
The output should be a single line containing a single integer, the minimum possible cost for you, the
chef.
Testdata
Each Knight's dessert costs strictly more than 0 and strictly less than 1000. You may assume that 1 = N =
106. In 30% of the test cases, 1 = N = 103.
Sample Input
5
12122
Sample Output
4
Time and memory limits
The time limit for this task is 1 second. The memory limit is 32MB.

You might also like