2022ICPC Problems Regional
2022ICPC Problems Regional
Problem Set
Please check that you have 12 problems that are spanned across 25 pages in total (including this
page).
Problem A
Card Game
Time Limit: 1.0 Seconds
Alice and Bob play a game of taking turns removing cards from the grid board. At the beginning of the game,
there is one card in each cell of the 𝑁𝑁 × 𝑀𝑀 sized grid board, and each card is painted in one of three colors: red,
black, or green. In the grid, the position of the upper-left cell is indicated by (1,1), and the position of the lower-
right cell is indicated by (𝑁𝑁, 𝑀𝑀).
Alice and Bob choose one of the cards placed on the grid, and then remove the cards according to the rules
below.
If the color of the chosen card is red, all 'connected cards' placed on a diagonal with a slope of 1 based
on it are removed.
If the color of the chosen card is blue, all 'connected cards' placed on a diagonal with a slope of -1
based on it are removed.
If the color of the chose card is green, all 'connected cards' placed on the diagonal in both directions
based on it are removed.
'Connected cards' to the chosen card are consecutively adjacent cards along a diagonal with a slope of 1 or -1
including the chosen card.
For example, when the current board situation during the game is as in Figure A.1, let the chosen card be a red
card placed at (4,3). As shown in Figure A.1, 'connected cards' placed on the diagonal line with a slope of 1
refer to the cards placed in the oval circle, which should be removed. That is, cards placed in the cells on the
movement path while moving diagonally in both directions from the position (4,3) are 'connected cards'.
However, while moving in both directions along the diagonal at the chosen cell (4,3), if it encounters a grid
boundary or a blank cell, the movement stops.
Figure A.1. An example to illustrate connected cards to the red card at (𝟒𝟒, 𝟑𝟑)
Similarly, when the current board situation during the game is as shown in Figure A.2, let the chosen card be
the blue card placed at (3,5). As shown in Figure A.2, 'connected cards' placed on the diagonal line with a slope
of -1 refer to the cards placed in the oval circle, which should be removed.
Figure A.3 shows the cards to be removed when the chosen card is green card placed at (4,5).
Figure A.3. An example to illustrate connected cards to the green card at (𝟒𝟒, 𝟓𝟓)
Alice and Bob alternately choose a card from the grid, and according to the color of the chosen card, remove
the 'connected cards' according to the rules described above. Whoever removes the last card wins the game.
That is, the player who cannot remove any card because there are no cards to choose from on the grid loses the
game. Both Alice and Bob have a good understanding of the strategy of how to win the game and do their best
to win.
Given the size of the grid board and the information on color of the cards placed on the board, write a program
to determine whether Alice can win when she starts the game.
Input
Your program is to read from standard input. The input starts with a line containing two integers, 𝑁𝑁 and 𝑀𝑀 (1 ≤
𝑁𝑁, 𝑀𝑀 ≤ 25), where 𝑁𝑁 is the number of rows and 𝑀𝑀 is the number of columns of the grid. In the following 𝑁𝑁
lines, the 𝑖𝑖-th line contains a string of length 𝑀𝑀, which represents the colors of the 𝑀𝑀 cards in the 𝑖𝑖-th row in the
grid. Every character in the string is either ‘R’, ‘B’, or ‘G’, which stands for red, blue, or green, respectively.
Output
Your program is to write to standard output. Print exactly one line. The line should contain an upper-case letter:
either ‘W’ if Alice wins or ‘L’ if Alice loses.
The following shows sample input and output for three test cases.
Problem B
Castle Design
Time Limit: 1.0 Seconds
The ICPC kingdom has decided to build a new castle. The boundary of the castle is designed as a rectilinear
polygon with edges parallel to the 𝑥𝑥-axis or to the 𝑦𝑦-axis. To minimize the damage exposed by the enemy
attack, the kingdom wants to minimize the perimeter of the rectilinear polygon. Let us go into more detail.
A rectilinear polygon 𝑃𝑃 of 𝑛𝑛 vertices with integer coordinates realizes a turn sequence 𝑆𝑆 of length 𝑛𝑛 of two
letters L and R if there is a counterclockwise traverse along the boundary of 𝑃𝑃 such that the turns at vertices of
𝑃𝑃, encountered during the traverse, form the turn sequence 𝑆𝑆; the left turn at a convex vertex corresponds to L
and the right turn at a reflex vertex corresponds to R. For example, the rectilinear polygon in Figure B.1(a)
realizes the turn sequence 𝑆𝑆 = RLLRLLLRRLLRLRLL of length 16. Another turn sequence 𝑆𝑆 =
LLRLLRLLRLRLLR of length 14 can be realized by rectilinear polygons in Figure B.1(b) and B.1(c). Note
that a turn sequence can have infinitely many realizations of rectilinear polygons in the integral plane.
A polygon is simple if there are no two edges that intersect except at the end vertices of adjacent edges. A
polygon is monotone to an axis if its intersection with a line orthogonal to the axis is at most one segment. The
monotone polygon is called 2-monotone if it is monotone to both 𝑥𝑥-axis and the 𝑦𝑦-axis, and 1-monotone if it
is monotone to the one axis but not to the other axis. For example, the polygon in Figure B.1(a) is 1-monotone
because it is monotone to only one axis, the 𝑥𝑥-axis, while the polygons in Figure B.1(b) and B.1(c) are 2-
monotone. A turn sequence is also said to be 𝑡𝑡-monotone if any simple rectilinear polygon realizing the turn
sequence is 𝑡𝑡-monotone where 𝑡𝑡 = 1, 2.
Figure B.1 (a) A simple 1-monotone rectilinear polygon realizing an 1-monotone turn sequence
RLLRLLLRRLLRLRLL (starting from the marked vertex). (b) A simple 2-monotone rectilinear
polygon realizing a 2-monotone turn sequence LLRLLRLLRLRLLR (starting from the marked
vertex). (c) The 2-monotone rectilinear polygon with the minimum perimeter for the turn
sequence in (b).
The perimeter of a rectilinear polygon is the sum of the length of its edges. The perimeter of the polygon in
Figure B.1(b) is 18, but this is not minimum for LLRLLRLLRLRLLR. Its minimum perimeter should be 16 as
in Figure B.1(c).
Given a 𝑡𝑡-monotone turn sequence of 𝑛𝑛 turns for 𝑡𝑡 = 1, 2, write a program to compute the minimum perimeter
of simple 𝑡𝑡-monotone rectilinear polygons that realize the input 𝑡𝑡-monotone turn sequence.
Output
Your program is to write to standard output. Print exactly one line. The line should contain the positive integer
that is the minimum perimeter of simple 𝑡𝑡-monotone rectilinear polygons that realize the input 𝑡𝑡-monotone
turn sequence for 𝑡𝑡 = 1, 2.
The following shows sample input and output for four test cases.
Problem C
Empty Quadrilaterals
Time Limit: 2.0 Seconds
A quadrilateral is a polygon with exactly four distinct corners and four distinct sides, without any crossing
between its sides. In this problem, you are given a set 𝑃𝑃 of 𝑛𝑛 points in the plane, no three of which are
collinear, and asked to count the number of all quadrilaterals whose corners are members of the set 𝑃𝑃 and
whose interior contains no other points in 𝑃𝑃.
For example, assume that 𝑃𝑃 consists of five points as shown in the left of the figure above. There are nine
distinct quadrilaterals in total whose corners are members of 𝑃𝑃, while only one of them contains a point of 𝑃𝑃
in its interior, as in the right of the figure above. Therefore, there are exactly eight quadrilaterals satisfying the
condition and your program must print out 8 as the correct answer.
Input
Your program is to read from standard input. The input starts with a line containing an integer 𝑛𝑛 (1 ≤ 𝑛𝑛 ≤
300), where 𝑛𝑛 denotes the number of points in the set 𝑃𝑃. In the following 𝑛𝑛 lines, each line consists of two
integers, ranging from −109 to 109 , representing the coordinates of a point in 𝑃𝑃. There are no three points in
𝑃𝑃 that are collinear.
Output
Your program is to write to standard output. Print exactly one line consisting of a single integer that represents
the number of quadrilaterals whose corners are members of the set 𝑃𝑃 and whose interior contains no other
points in 𝑃𝑃.
The following shows sample input and output for three test cases.
Problem D
Folding Stick
Time Limit: 0.4 Seconds
There is a folding stick made up of 𝑛𝑛 segments of positive length. The segments are connected by hinges
(stretchable strings), allowing the segments to be folded 180 degrees at each hinge. The wrapping length is
said to be the length of the folded stick after the stick is folded at hinges one or more. Depending on the
folding strategy, the wrapping length may be different.
You are to find the minimum wrapping length under the condition that the stick is folded in the following way:
First, place the segments of the stick along a horizontal line. Then, fold the stick clockwise from left to right.
During folding, the segment attached to the left side of each hinge rotates 180 degrees clockwise or not at all.
For example, the figure below shows a four-segment stick with a sum of segment lengths of 10. In the figure,
the lengths of the segments are 3, 2, 2, and 3 from left to right, and the hinges are marked with , , .
In this example, the stick cannot be folded at both hinges and . This is because if the stick is folded at
hinge and then at hinge , the segment with length 3 passing over the hinge will be broken. If it is
folded only at hinge , the wrapping length is 5. If it is folded at hinges and in order, the wrapping
length is 4 as shown in the figure below.
Given a sequence of segments lengths of a folding stick, write a program to output the minimum wrapping
length of the stick.
Input
Your program is to read from standard input. The input starts with a line containing an integer, 𝑛𝑛 (2 ≤ 𝑛𝑛 ≤
100,000), where 𝑛𝑛 is the number of segments of a folding stick. The next line contains 𝑛𝑛 positive integers
which represent a sequence of lengths of segments from the leftmost one to rightmost one of the stick. Each
segment length is no more than 20,000.
Output
Your program is to write to standard output. Print exactly one line. The line should contain the positive integer
representing the minimum wrapping length.
A GPS navigation company ICPC(International Control Perfection Company) designs a car navigation system
for a transportation network. The system abstracts the transportation network as a directed graph 𝐺𝐺(𝑉𝑉, 𝐸𝐸) with
edge cost 𝑐𝑐. For a directed edge (𝑣𝑣, 𝑤𝑤) ∈ 𝐸𝐸, 𝑐𝑐(𝑣𝑣, 𝑤𝑤) denotes the distance from a place 𝑣𝑣 ∈ 𝑉𝑉 to another place
𝑤𝑤 ∈ 𝑉𝑉. The company wants to implement the shortest path module in the system. To reflect the normal
situation that we cannot turn to some directions in a junction of transportation network, we want to find the
shortest path that does not contain forbidden turns as a subpath.
A path from 𝑣𝑣 to 𝑤𝑤 is a sequence of vertices (𝑣𝑣1 , 𝑣𝑣2 , ⋯ , 𝑣𝑣𝑘𝑘 ) where 𝑣𝑣1 = 𝑣𝑣, 𝑣𝑣𝑘𝑘 = 𝑤𝑤, (𝑣𝑣𝑖𝑖 , 𝑣𝑣𝑖𝑖+1 ) ∈ 𝐸𝐸 for 1 ≤
𝑖𝑖 ≤ 𝑘𝑘 − 1. Unlike the common definition of the path, you are here allowed to repeat the same vertices in a
path one or more. A subpath of a path is a contiguous subsequence of the sequence that corresponds to the
path. A forbidden turn is a path (i.e., triplet) (𝑥𝑥, 𝑦𝑦, 𝑧𝑧) such that 𝑥𝑥, 𝑦𝑦, 𝑧𝑧 ∈ 𝑉𝑉 and (𝑥𝑥, 𝑦𝑦) ∈ 𝐸𝐸 and (𝑦𝑦, 𝑧𝑧) ∈ 𝐸𝐸. The
distance of a path (𝑣𝑣1 , 𝑣𝑣2 , ⋯ , 𝑣𝑣𝑘𝑘 ) is defined as ∑𝑘𝑘−1
𝑖𝑖=1 𝑐𝑐(𝑣𝑣𝑖𝑖 , 𝑣𝑣𝑖𝑖+1 ). The shortest path from 𝑣𝑣 ∈ 𝑉𝑉 to 𝑤𝑤 ∈ 𝑉𝑉 is a
path from 𝑣𝑣 to 𝑤𝑤 with the minimum distance. The company wants to find the distance of the shortest path that
avoids the forbidden turns between two designated vertices. Note that the shortest path from 𝑣𝑣 ∈ 𝑉𝑉 to 𝑣𝑣 ∈ 𝑉𝑉
has distance 0 and it avoids all the forbidden turns.
Let's see the following example in the figure below. Each edge cost lies beside each edge and the list of three
forbidden turns are in the right box. The shortest path without forbidden turns from the vertex 3 to the vertex
2 is (3, 0, 1, 5, 4, 1, 2) which is denoted as blue arrows in the following figure. The distance of the shortest
path is 3 + 12 + 4 + 7 + 8 + 2 = 36. Note that we cannot take the shorter paths (3, 0, 1, 2) and (3, 0, 1, 5, 2)
since they contain forbidden turns (0, 1, 2) and (1, 5, 2), respectively.
Given a directed graph 𝐺𝐺(𝑉𝑉, 𝐸𝐸) with the edge cost 𝑐𝑐, a set of forbidden turns 𝐹𝐹, and two vertices 𝑣𝑣 and 𝑤𝑤,
write a program to output the distance of the shortest path from 𝑣𝑣 to 𝑤𝑤 that avoids all the forbidden turns. We
assume that out-degree of each vertex 𝑣𝑣, i.e., the number of edges that starts from 𝑣𝑣 is at most 10.
Output
Your program is to write to standard output. Print exactly one line. The line should contain an integer that
represents the distance of the shortest path from 𝑣𝑣 to 𝑤𝑤 which avoids all the forbidden turns. If such a path
does not exist, the line should contain −1.
The following shows sample input and output for three test cases.
Problem F
Frog Jump
Time Limit: 1.0 Seconds
A frog is living in a beautiful lake. On the lake, there are a lot of lotus leaves floating in a row, which are
represented by closed intervals on the line. The frog likes to be on lotus leaves and moves between them.
The 𝑛𝑛 closed intervals, representing lotus leaves, on the line, that is, on the 𝑥𝑥-axis are given and the frog is
initially on some interval 𝐼𝐼0 . The frog can move from an interval 𝐼𝐼 to an interval 𝐽𝐽 if they overlap. Two
intervals overlap if they share a common point. So the frog can move through overlapping intervals. When the
frog is moving to the right (left) through the overlapping intervals, it may reach an interval 𝐻𝐻, where it can no
longer move to the right (left) from the right (left) endpoint of 𝐻𝐻. In this case, the frog can jump to the interval
𝐾𝐾 with the smallest (largest) left (right) endpoint among intervals whose left (right) endpoint is greater
(smaller) than the right (left) endpoint of 𝐻𝐻 if they exist. Then, the jump length is defined to be the length
between the right (left) endpoint of 𝐻𝐻 and the left (right) endpoint of 𝐾𝐾. See Figure F.1.
A sequence of 𝑘𝑘 intervals 𝐼𝐼1 , 𝐼𝐼2 , … , 𝐼𝐼𝑘𝑘 is given and the frog should visit the intervals in order from the initial
interval 𝐼𝐼0 . In this travel, the frog has to jump if necessary.
For example, in Figure F.2, eight intervals [1, 8], [2, 4], [5, 11], [13, 15], [15, 17], [16, 18], [19, 22] and
[20, 22] are given and numbered from 1 and 8. The frog is initially on interval 1. Intervals 3, 7, 4, 6, 3 which
the frog should visit in a sequence are given. Then the frog moves from interval 1 to 3 with no jump, and it
moves from 3 to 7 with two jumps, say, 3 → 4 and 6 → 7 whose jump length is 3 totally. In this movement,
the frog passes through the interval 4. Nevertheless, it should visit the interval 4 after the interval 7. Then,
there are two jumps during the movements from 7 to 4 and from 6 to 3 whose jump length is 3 totally. Thus
after the frog visits all the given intervals, the total jump length is 6.
Given 𝑛𝑛 intervals on the line and a sequence of 𝑘𝑘 intervals, write a program to output the total jump length
during the travel that the frog visits the 𝑘𝑘 intervals in order from its initial interval 1.
Output
Your program is to write to standard output. Print exactly one line. The line should contain the total jump
length of frog when it visits the given 𝑘𝑘 intervals in order.
The following shows sample input and output for three test cases.
Problem G
Linear Regression
Time Limit: 5.0 Seconds
Chansu is a graduate student at University of ICPC, working in a laboratory for his master’s degree. His
research theme is to reveal a relation between the obesity and the yearly income of individuals in a certain
group G.
Chansu collected data of the form (𝑥𝑥𝑖𝑖 , 𝑦𝑦𝑖𝑖 ) from 𝑛𝑛 persons in G, where 𝑥𝑥𝑖𝑖 and 𝑦𝑦𝑖𝑖 denote the obesity index and
the yearly income of the 𝑖𝑖-th person, and made an apparent hypothesis:
There is a linear dependency between the obesity and the yearly income of individuals in group G.
To prove his hypothesis, Chansu tried to find an optimal linear function 𝑓𝑓 ∗ (𝑥𝑥) with real coefficients such that
the error with respect to the collected data is minimized. More specifically, the error of 𝑓𝑓 with respect to the
data is defined to be the maximum of |𝑦𝑦𝑖𝑖 − 𝑓𝑓(𝑥𝑥𝑖𝑖 )| over all 𝑖𝑖 = 1, … , 𝑛𝑛.
However, the result was disappointing because the error of the optimal function 𝑓𝑓 ∗ (𝑥𝑥) was unexpectedly big.
This means that his hypothesis cannot be proven in this way.
Chansu tried to figure out the reason of the big errors. One day, he plotted the data (𝑥𝑥𝑖𝑖 , 𝑦𝑦𝑖𝑖 ) as points on the
coordinated plane and realized that there are a small number 𝑘𝑘 of points that are unusually far from the others,
so the error of the optimal function can be drastically reduced after removing them.
You, as a friend of Chansu, would love to help Chansu. Write a program that finds an optimal linear function
minimizing the error after removing some 𝑘𝑘 values from the given data {(𝑥𝑥1 , 𝑦𝑦1 ), … , (𝑥𝑥𝑛𝑛 , 𝑦𝑦𝑛𝑛 )} and prints out
the error value, when the number 𝑘𝑘 is given as part of input.
Input
Your program is to read from standard input. The input starts with a line containing two integers, 𝑛𝑛 and 𝑘𝑘
𝑛𝑛
(1 ≤ 𝑛𝑛 ≤ 50,000, 0 ≤ 𝑘𝑘 ≤ min{ , 300}), where 𝑛𝑛 is the number of collected data values. In each of the
2
following 𝑛𝑛 lines, each data value (𝑥𝑥𝑖𝑖 , 𝑦𝑦𝑖𝑖 ) is given by two integers 𝑥𝑥𝑖𝑖 and 𝑦𝑦𝑖𝑖 (−109 ≤ 𝑥𝑥𝑖𝑖 , 𝑦𝑦𝑖𝑖 ≤ 109 ) for 𝑖𝑖 =
1, … , 𝑛𝑛. You can assume that no three of them are collinear when plotting them in the coordinated plane.
Output
Your program is to write to standard output. Print exactly one line. The line should contain a real number 𝑧𝑧
representing the minimum possible error of a linear function with respect to the data after removing some 𝑘𝑘
values. Your output 𝑧𝑧 should be in the format that consists of its integer part, a decimal point, and its
fractional part, and will be decided to be “correct” if it holds that 𝑎𝑎 − 10−6 < 𝑧𝑧 < 𝑎𝑎 + 10−6 , where 𝑎𝑎 denotes
the exact answer.
For a string 𝑆𝑆 of length 𝑛𝑛 ≥ 1 and a positive integer 𝑘𝑘 (1 ≤ 𝑘𝑘 ≤ 𝑛𝑛), a non-empty substring of 𝑆𝑆 is called a 𝑘𝑘-
substring if the substring appears exactly 𝑘𝑘 times. Such 𝑘𝑘 occurrences are not necessarily disjoint, i.e., are
possibly overlapping. For example, if 𝑆𝑆 = "ababa", the 𝑘𝑘-substrings of 𝑆𝑆 for every 𝑘𝑘 = 1, … , 5 are as follows:
• There are four 1-substrings in 𝑆𝑆, "abab", "ababa", "bab", and "baba" because these substrings appear
exactly once in 𝑆𝑆. Note that "aba" is not a 1-substring because it appears twice.
• There are four 2-substrings in 𝑆𝑆, "ab", "aba", "b", and "ba". The substring "ab" appears exactly twice
without overlapping. Two occurrences of the substring "aba" are overlapped at a common character "a",
but it does not appear three times or more.
• There is only one 3-substring in 𝑆𝑆, "a".
• Neither 4-substrings nor 5-substrings exist in 𝑆𝑆.
For a 𝑘𝑘-substring 𝑇𝑇 of 𝑆𝑆, let 𝑑𝑑(𝑇𝑇) be the maximum number of the disjoint occurrences of 𝑇𝑇 in 𝑆𝑆. For example,
a 2-substring 𝑇𝑇 = "ab" can be selected twice without overlapping, that is, the maximum number of the disjoint
occurrences is two, so 𝑑𝑑(𝑇𝑇) = 2 . For a 2 -substring 𝑇𝑇 = "aba", it cannot be selected twice without
overlapping, so 𝑑𝑑(𝑇𝑇) = 1. For a 3-substring 𝑇𝑇 = "a", it can be selected three times without overlapping,
which is the maximum, so 𝑑𝑑(𝑇𝑇) = 3.
Let 𝑓𝑓(𝑘𝑘) be the length of the longest one among all 𝑘𝑘-substring 𝑇𝑇 with the largest 𝑑𝑑(𝑇𝑇) for 1 ≤ 𝑘𝑘 ≤ 𝑛𝑛. For
example, 𝑓𝑓(𝑘𝑘) for 𝑆𝑆 = "ababa" and 𝑘𝑘 = 1, … , 5 is as follows:
• For 𝑘𝑘 = 1, all 1-substrings 𝑇𝑇 can be selected only once without overlapping, so 𝑑𝑑(𝑇𝑇) = 1. Thus, the
longest one among all 1-substrings with 𝑑𝑑(𝑇𝑇) = 1 is "ababa", so 𝑓𝑓(1) = 5.
• For 𝑘𝑘 = 2, 𝑑𝑑(𝑇𝑇) = 1 for 𝑇𝑇 = "aba", but 𝑑𝑑(𝑇𝑇) = 2 for the other 2-substrings 𝑇𝑇 = "ab", "b", "ba".
Among 2-substrings with 𝑑𝑑(𝑇𝑇) = 2, "ab" and "ba" are the longest ones, so 𝑓𝑓(2) = 2.
• For 𝑘𝑘 = 3, 𝑓𝑓(3) = 1 because there is only one 3-substring "a".
• For 𝑘𝑘 = 4, 5, there are no 𝑘𝑘-substrings, so 𝑓𝑓(4) = 0 and 𝑓𝑓(5) = 0.
Given a string 𝑆𝑆 of length 𝑛𝑛, write a program to output 𝑛𝑛 values of 𝑓𝑓(𝑘𝑘) from 𝑘𝑘 = 1 to 𝑘𝑘 = 𝑛𝑛. For the above
example, the output should be 5 2 1 0 0.
Input
Your program is to read from standard input. The input starts with a line containing the string 𝑆𝑆 consisting of 𝑛𝑛
(1 ≤ 𝑛𝑛 ≤ 50,000) lowercase alphabets.
Output
Your program is to write to standard output. Print exactly one line. The line should contain exactly 𝑛𝑛 non-
negative integers, separated by a space, that represent 𝑓𝑓(𝑘𝑘) from 𝑘𝑘 = 1 to 𝑘𝑘 = 𝑛𝑛 in order, that is,
𝑓𝑓(1) 𝑓𝑓(2) … 𝑓𝑓(𝑛𝑛). Note that 𝑓𝑓(𝑘𝑘) should be zero if there is no 𝑘𝑘-substring for some 𝑘𝑘.
Problem I
Palindrome Type
Time Limit: 1.0 Seconds
A palindrome string is a word which reads the same backward as forward, such as madam or racecar. In this
problem we only consider strings with lowercase alphabets.
We newly define the types of palindromes. If a string is not a palindrome, we try to make it a palindrome by
removing the minimum number of characters in the string. For a string 𝑤𝑤, if 𝑘𝑘 is the minimum number of
characters removed to make the string a palindrome, we call the string 𝑤𝑤 type-𝑘𝑘 palindrome. Thus, if 𝑤𝑤 is a
palindrome, then 𝑤𝑤 is a type-0 palindrome.
Input
Your program is to read from standard input. The input is a single line containing a string 𝑤𝑤 with length 𝑛𝑛 (5 ≤
𝑛𝑛 ≤ 105 ) of lowercase alphabets.
Output
Your program is to write to standard output. Print exactly one line. The line should contain a number 𝑘𝑘 among
{0, 1, 2, 3, −1} if the input string is a type-𝑘𝑘 palindrome where 𝑘𝑘 = 0, 1, 2, 3 and otherwise −1. The negative
number −1 means the input string is not a type-𝑘𝑘 palindrome where 𝑘𝑘 = 0, 1, 2, 3.
The following shows sample input and output for three test cases.
Problem J
Parentheses Tree
Time Limit: 1.0 Seconds
A rooted ordered tree 𝑇𝑇 can be expressed as a string of matched parentheses 𝑝𝑝(𝑇𝑇). The string representation
𝑝𝑝(𝑇𝑇) can be defined recursively. As a base case, a tree consisting of a single node is expressed by a pair of
parentheses (). When a rooted ordered tree 𝑇𝑇 consists of a root node and 𝑘𝑘 ordered subtrees 𝑇𝑇1 , 𝑇𝑇2 , … , 𝑇𝑇𝑘𝑘
having their roots as child nodes of the root node, the string representation 𝑝𝑝(𝑇𝑇) is defined as follows:
In the above expression, the operator + means the concatenation of two strings. The figure below shows two
examples of rooted ordered trees. The string representations 𝑝𝑝(𝑇𝑇𝐿𝐿 ) and 𝑝𝑝(𝑇𝑇𝑅𝑅 ) are ((()()())()) and
(()((()(()))())), respectively.
The distance from the root node to a leaf node is defined as the number of edges to be traversed to reach the
leaf from the root. In the figure above, the root nodes are colored in blue, and the distances from the root node
to all leaf nodes are shown. For trees 𝑇𝑇𝐿𝐿 and 𝑇𝑇𝑅𝑅 , the sum of the distances from the root to all leaf nodes are 7
and 10, respectively.
Given a string of matched parentheses representing only one rooted ordered tree, write a program to output the
sum of the distances from the root of the tree to all leaf nodes.
Input
Your program is to read from standard input. The input consists of one line containing a string of matched
parentheses which represents only one rooted ordered tree. The input does not contain any characters other
than parentheses, and the length of string is at least 2 and no more than 107 .
Output
Your program is to write to standard output. Print exactly one line. The line should contain the sum of the
distances from the root of the rooted ordered tree to all leaf nodes.
Problem K
Shuffle Game
Time Limit: 1.5 Seconds
Shuffle Game is a simple card game between the dealer and the player. Initially, the same deck of 𝑛𝑛 cards is
given to both the dealer and the player. Each card in the deck suits with one of the four symbols (𝐶𝐶, 𝐷𝐷, 𝐻𝐻, or
𝑆𝑆), followed by the one of 13 kinds (2, 3, 4, 5, 6, 7, 8, 9, 10, 𝐴𝐴, 𝐽𝐽, 𝐾𝐾 or 𝑄𝑄). Therefore, there are 52
different types of cards and the same cards can exist in the deck. After the cards are given to the dealer and the
player, the dealer first creates their own deck 𝑋𝑋 from the deck given to the dealer using any shuffling method
and shows 𝑋𝑋 to the player. After that, the player creates the deck 𝑌𝑌 by the following steps: 𝑌𝑌 is initially empty.
Step 1. Create two decks 𝑃𝑃1 and 𝑃𝑃2 from the deck given to the player. The number of cards in 𝑃𝑃1 and 𝑃𝑃2
can be different.
Step 2. Interleave 𝑃𝑃1 and 𝑃𝑃2. That is, move a card at the bottom of 𝑃𝑃1 or 𝑃𝑃2 to the current top of 𝑌𝑌, until
there is no card on both 𝑃𝑃1 and 𝑃𝑃2. Note that the player does not need to move the cards in 𝑃𝑃1 and
𝑃𝑃2 alternately to 𝑌𝑌. Also, since both the dealer and the player create their own deck from the same
deck of 𝑛𝑛 cards, 𝑌𝑌 always consists of the same cards as 𝑋𝑋.
We define a sequence of a deck as the sequence of the cards in the deck from bottom to top. Then the player’s
score is defined as the length of the longest common subsequence between the sequences 𝑋𝑋 and 𝑌𝑌. For
example, suppose the deck of 𝑛𝑛 = 5 cards, (𝐶𝐶2, 𝐶𝐶𝐶𝐶, 𝐷𝐷5, 𝐻𝐻𝐻𝐻, 𝑆𝑆7) is given to both the dealer and the player
(here, we represent the deck as its sequence). Then the dealer creates the deck 𝑋𝑋 = (𝐶𝐶𝐶𝐶, 𝐷𝐷5, 𝐻𝐻𝐻𝐻, 𝐶𝐶2, 𝑆𝑆7)
and shows 𝑋𝑋 to the player. After that, the player creates their deck by (i) creating two decks 𝑃𝑃1 = (𝐷𝐷5, 𝐻𝐻𝐻𝐻)
and 𝑃𝑃2 = (𝐶𝐶𝐶𝐶, 𝑆𝑆7, 𝐶𝐶2) from the given deck and (ii) create 𝑌𝑌 = (𝐷𝐷5, 𝐶𝐶𝐶𝐶, 𝑆𝑆7, 𝐻𝐻𝐻𝐻, 𝐶𝐶2) by interleaving 𝑃𝑃1
and 𝑃𝑃2. In this example, the player’s score is 3 since (𝐶𝐶𝐶𝐶, 𝐻𝐻𝐻𝐻, 𝐶𝐶2) is the longest common subsequences
between the sequences of 𝑋𝑋 and 𝑌𝑌. Now, after finishing Step 1, the player wants to know the maximum
possible score to that the player can achieve after applying Step 2. For example, the maximum possible score
from 𝑋𝑋 and 𝑌𝑌 in the previous example is 4 since it is possible to create 𝑌𝑌 from 𝑃𝑃1 and 𝑃𝑃2 as (𝐶𝐶𝐶𝐶, 𝐷𝐷5,
𝐻𝐻𝐻𝐻 𝑆𝑆7, 𝐶𝐶2).
Given 𝑛𝑛, 𝑋𝑋, 𝑃𝑃1 and 𝑃𝑃2, write a program to compute the maximum possible score that the player can achieve.
Input
Your program is to read from standard input. The input starts with a line containing three positive integers 𝑛𝑛,
𝑝𝑝 and 𝑞𝑞 (3 ≤ 𝑛𝑛 ≤ 500, 𝑝𝑝 + 𝑞𝑞 = 𝑛𝑛), where 𝑛𝑛 is the number of cards in the initial deck, and 𝑝𝑝 and 𝑞𝑞 are the
number of cards in 𝑃𝑃1 and 𝑃𝑃2, respectively. In the following three lines, the dealer’s deck 𝑋𝑋 consisting of 𝑛𝑛
cards, and the player’s two decks 𝑃𝑃1 and 𝑃𝑃2 consisting of 𝑝𝑝 and 𝑞𝑞 cards, respectively, are given. Each card in
𝑋𝑋, 𝑃𝑃1, and 𝑃𝑃2 is represented as its suit (uppercase alphabet 𝐶𝐶, 𝐷𝐷, 𝐻𝐻, or 𝑆𝑆) followed by its kind (2, 3, 4, 5, 6,
7, 8, 9, 10, uppercase alphabet 𝐴𝐴, 𝐽𝐽, 𝐾𝐾 or 𝑄𝑄). The cards in the same line are ordered from bottom to top of
the corresponding deck.
Output
Your program is to write to standard output. Print exactly one line. The line should contain the maximum
possible score that the player can achieve from 𝑋𝑋, 𝑃𝑃1 and 𝑃𝑃2 after applying Step 2.
Somin and Eunjoo are famous dancers and very talented choreographers, but they haven't won a contest recently.
To win the contest this year, they are trying to help each other to make new choreographies. Actually, nobody
has tried smoothly appending static motions, and they are going to give it a try for the first time!
Somin and Eunjoo want to make two choreographies consisting of 𝑛𝑛 static motions for each of them. They have
a good understanding of how to smoothly append static motions, and they concluded that exactly 2𝑛𝑛 − 3
unordered pairs of static motions are enough for them to perform freely. The order of static motions in a pair
{𝐴𝐴, 𝐵𝐵} does not matter, i.e., if motion 𝐵𝐵 can be appended after motion 𝐴𝐴, then 𝐴𝐴 can also be appended after 𝐵𝐵.
The choreographies which Somin and Eunjoo want to perform are as follows. The two choreographies last for
the same amount of time, which means that each one should consist of the same number of static motions. Each
choreography should end at its first static motion. More precisely, two choreographies 𝐶𝐶1 and 𝐶𝐶2 are sequences
of distinct 𝑙𝑙 static motions, 𝐶𝐶1 = (𝑎𝑎0 , 𝑎𝑎1 , … , 𝑎𝑎𝑙𝑙 ) and 𝐶𝐶2 = (𝑏𝑏0 , 𝑏𝑏1 , … , 𝑏𝑏𝑙𝑙 ) where 𝑎𝑎0 = 𝑎𝑎𝑙𝑙 and 𝑏𝑏0 = 𝑏𝑏𝑙𝑙 . For the
entertainment of the audience, 𝐶𝐶1 and 𝐶𝐶2 should be different, that is, there should be some 0 ≤ 𝑖𝑖 ≤ 𝑙𝑙 − 1 which
{𝑎𝑎𝑖𝑖 , 𝑎𝑎𝑖𝑖+1 } in 𝐶𝐶1 is not equal to any of {𝑏𝑏𝑗𝑗 , 𝑏𝑏𝑗𝑗+1 } in 𝐶𝐶2 for 0 ≤ 𝑗𝑗 ≤ 𝑙𝑙 − 1 . (For example, (1,2,3,4,5,1) and
(3,4,5,2,1,3) are different but (1,2,3,4,5,1) and (3,4,5,1,2,3) are not.) Also, the audience easily gets bored, so
the choreography should not be too short, and contain at least 3 distinct static motions, that is, 𝑙𝑙 ≥ 3.
For this, you are given 2𝑛𝑛 − 3 unordered pairs 𝑃𝑃 of static motions from 𝑛𝑛 distinct static motions 𝑚𝑚1 , … , 𝑚𝑚𝑛𝑛 that
two dancers can perform. For a pair {𝑚𝑚𝑖𝑖 , 𝑚𝑚𝑗𝑗 } where 𝑖𝑖 ≠ 𝑗𝑗, one of 𝑚𝑚𝑖𝑖 and 𝑚𝑚𝑗𝑗 can appear after the other in the
sequence; there is no specific order between them. You should write a program to find two different
choreographies 𝐶𝐶1 = (𝑎𝑎0 , 𝑎𝑎1 , … , 𝑎𝑎𝑙𝑙 ) and 𝐶𝐶2 = (𝑏𝑏0 , 𝑏𝑏1 , … , 𝑏𝑏𝑙𝑙 ) of the same length 𝑙𝑙 ≥ 3 such that {𝑎𝑎𝑖𝑖 , 𝑎𝑎𝑖𝑖+1 } ∈ 𝑃𝑃,
{𝑏𝑏𝑖𝑖 , 𝑏𝑏𝑖𝑖+1 } ∈ 𝑃𝑃 for any 0 ≤ 𝑖𝑖 ≤ 𝑙𝑙 − 1, and 𝑎𝑎0 = 𝑎𝑎𝑙𝑙 and 𝑏𝑏0 = 𝑏𝑏𝑙𝑙 .
Input
Your program is to read from standard input. The input starts with a line containing a single integer, 𝑛𝑛 (4 ≤ 𝑛𝑛 ≤
100,000), where 𝑛𝑛 is the number of static motions two dancers can represent. Each static motion is numbered
as an integer from 1 to 𝑛𝑛. The following 2𝑛𝑛 − 3 lines represent 2𝑛𝑛 − 3 unordered pairs of stack motions, 𝑃𝑃.
Each line contains two distinct integers representing two static motions of a pair of 𝑃𝑃. Note that no two pairs in
𝑃𝑃 are identical.
Output
Your program is to write to standard output. If you cannot find two choreographies of static motions, then print
-1. If not, you should print exactly three lines. The first line contains an integer 𝑙𝑙 ≥ 3 which is the number of
distinct static motions in each choreography. The second line contains exactly 𝑙𝑙 integers, separated by a space,
each representing a choreography of the 𝑙𝑙 static motions in order. The last repeated motion should be omitted.
The third line contains exactly 𝑙𝑙 integers representing the other choreography.