Connection Status:
Competition Arena > GameOfInterval
SRM 716 · 2017-05-20 · by cgy4ever · Recursion
Class Name: GameOfInterval
Return Type: String
Method Name: construct
Arg Types: (int)
Problem Statement

Problem Statement

Fox Ciel is playing a single-player game called the 'Game of Interval'.

In this problem an interval is a non-empty segment of consecutive integers. We will use the notation [lo,hi] to denote an interval of all numbers x such that lo <= x <= hi. For example, [1,1] is an interval containing the number 1, and [3,5] is an interval containing the numbers 3, 4, and 5. In other words, [3,5] is the set {3,4,5}.

The union of intervals is simply the set union. Note that the union of two intervals is not necessarily an interval. For example, the union of [1,1] and [3,5] is the set {1,3,4,5} which is not an interval, but the union of [1,1], [3,5], and [2,2] is an interval: the interval [1,5].

Initially, Fox Ciel has n cards: for each i from 0 to n-1, inclusive, there is a card that contains the interval [i,i].

Ciel starts playing the game by choosing a non-negative integer k.

Then, she can do the following operation k times:
  1. Choose two different cards that contain intervals I1 and I2 such that their union is again an interval.
  2. Create a new card with the interval that is the union of I1 and I2. (You also keep the two original cards.)
For example, if n = 4, Ciel can proceed as follows:
  1. She chooses k = 2.
  2. She chooses the cards with intervals [1,1] and [2,2] and produces a card with the interval [1,2].
  3. She then chooses the cards with intervals [1,2] and [3,3] and produces a card with the interval [1,3].
  4. Thus, she now has six cards: the original four with [0,0], [1,1], [2,2], and [3,3], and the two new ones with [1,2] and [1,3].
Once Ciel finishes merging the intervals, we will calculate the penalty p as follows:

Suppose we ask Ciel to show us some cards such that the union of their intervals is exactly the interval [L,R]. Let c(L,R) be the smallest number of cards Ciel needs to show. The penalty p is the largest of all values c(L,R). The maximum is taken over all pairs (L,R) such that 0 <= L <= R <= n-1.

Intuitively, if Ciel chooses a very small k, the penalty p will be large. For example, if she chooses k = 0, the penalty will be p = n because she'll need to show n cards for the pair (L,R) = (0,n-1). On the other hand, if she prepares a lot of cards, the penalty will be small. In particular, if she prepares all possible cards, the penalty will be p = 1.

Ciel's final score in this game is defined as s = k + p * n. Help Ciel play the game in a way that will result in a reasonably small score. Note that you don't have to find the best possible score: any solution with a score that does not exceed 6415 will be accepted.

Use a base-36 encoding when outputting your solution, as described below. We will use digits and uppercase letters. The individual symbols have the following values: '0' = 0, '9' = 9, 'A' = 10, 'Z' = 35. Any number between 0 and n-1 can now be expressed as a number in base-36 with exactly two digits. For example, 4 is "04" and 84 is "2C" (as 84 = 2*36 + 12). An interval [L,R] can be represented as the concatenation of L and R. For example, [0,83] will be "002B".

Output a String of length 4*k: the concatenation of the k intervals you want to produce, in the order in which you'll produce them. For example, if k = 3 and these intervals are [1,2], [2,3], [1,3], then you should output "010202030103". The value of k must not exceed 4500. (I.e., the length of your output must not exceed 4500*4 = 18000). We guarantee the solution always exist under these constraints.

Constraints

  • n will be between 1 and 1,000, inclusive.
Examples
0)
4
Returns: "0102"

Here, Ciel chose k = 1 and she used the cards [1,1] and [2,2] to produce a new card [1,2]. Now let's calculate the penalty. The list below contains rows of the form "interval Ciel needs to show : one smallest set of cards she may show". [0,0] : [0,0] [0,1] : [0,0] + [1,1] [0,2] : [0,0] + [1,2] [0,3] : [0,0] + [1,2] + [3,3] [1,1] : [1,1] [1,2] : [1,2] [1,3] : [1,2] + [3,3] [2,2] : [2,2] [2,3] : [2,2] + [3,3] [3,3] : [3,3] As we can see from the list, we have p = 3, because Ciel needs at least 3 cards for the interval [0,3]. Hence, her total score is 1 + 3 * 4 = 13. As 13 <= 6415, this is a valid solution.

1)
6
Returns: "00010102020303040405000301040205"

The example input describes the following k = 8 cards being made, in order: [0,1], [1,2], [2,3], [3,4], [4,5], [0,3], [1,4], [2,5]. For this set of cards we have p = 2. For example, note that Ciel can show the interval [0,4] by showing the cards [0,3] and [1,4]. Note that the intervals shown by Ciel may overlap. All we care about is that the union of the intervals on the cards shown by Ciel has to be the desired interval. Thus, Ciel's score is 8 + 2 * 6 = 20.

2)
5
Returns: "00010001"

Here, Ciel produced two new cards, each with the interval [0,1]. This is obviously redundant, but it is still a valid solution.

3)
80
Returns: ""

The score is 80*80 = 6400.

4)
81
Returns: "00010203"

Note that this time "" is not a valid solution since 81*81=6561 > 6415.

5)
1
Returns: ""

The only solution is "". Note that you can't output "0000" - there is no way to union two different cards to get [0,0].

Submissions are judged against all 96 archived test cases, of which 6 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class GameOfInterval with a public method string construct(int n) · 96 test cases · 2 s / 256 MB per case

Submitting as anonymous