IOI 2014 day 2
D. Gondola
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mao-Kong Gondola is a famous attraction in Taipei. The gondola system consists of a circular rail, a single station, and $$$n$$$ gondolas numbered consecutively from $$$1$$$ to $$$n$$$ running around the rail in a fixed direction. After gondola $$$i$$$ passes the station, the next gondola to pass the station will be gondola $$$i + 1$$$ if $$$i \lt n$$$, or gondola 1 if $$$i = n$$$.

Gondolas may break down. Luckily we have an infinite supply of spare gondolas, which are numbered $$$n + 1, n + 2$$$, and so on. When a gondola breaks down we replace it (in the same position on the track) with the first available spare gondola, that is, the one with the lowest number. For example, if there are five gondolas and gondola 1 breaks down, then we will replace it with gondola 6.

You like to stand at the station and watch the gondolas as they pass by. A gondola sequence is a sequence of $$$n$$$ numbers of gondolas that pass the station. It is possible that one or more gondolas broke down (and were replaced) before you arrived, but none of the gondolas break down while you are watching.

Note that the same configuration of gondolas on the rail can give multiple gondola sequences, depending on which gondola passes first when you arrive at the station. For example, if none of the gondolas have broken down then both $$$(2, 3, 4, 5, 1)$$$ and $$$(4, 5, 1, 2, 3)$$$ are possible gondola sequences, but $$$(4, 3, 2, 5, 1)$$$ is not (because the gondolas appear in the wrong order).

If gondola $$$1$$$ breaks down, then we might now observe the gondola sequence $$$(4, 5, 6, 2, 3)$$$. If gondola $$$4$$$ breaks down next, we replace it with gondola $$$7$$$ and we might observe the gondola sequence $$$(6, 2, 3, 7, 5)$$$. If gondola $$$7$$$ breaks down after this, we replace it with gondola $$$8$$$ and we may now observe the gondola sequence $$$(3, 8, 5, 6, 2)$$$.

Broken gondolaNew gondolaPossible gondola sequence
16$$$(4, 5, 6, 2, 3)$$$
47$$$(6, 2, 3, 7, 5)$$$
78$$$(3, 8, 5, 6, 2)$$$

A replacement sequence is a sequence consisting of the numbers of the gondolas that have broken down, in the order in which they break down. In the previous example the replacement sequence is $$$(1, 4, 7)$$$. A replacement sequence $$$r$$$ produces a gondola sequence $$$g$$$ if, after gondolas break down according to the replacement sequence $$$r$$$, the gondola sequence $$$g$$$ may be observed.

Gondola Sequence Checking

In the first three subtasks you must check whether an input sequence is a gondola sequence. See the table below for examples of sequences that are and are not gondola sequences. You need to implement a function valid.

  • int valid(int n, int inputSeq[])
    • $$$n$$$: the length of the input sequence.
    • $$$inputSeq$$$: array of length $$$n$$$; $$$inputSeq[i]$$$ is element $$$i$$$ of the input sequence, for $$$0 \le i \le n - 1$$$.
    • The function should return $$$1$$$ if the input sequence is a gondola sequence, or $$$0$$$ otherwise.

Replacement Sequence

In the next three subtasks you must construct a possible replacement sequence that produces a given gondola sequence. Any such replacement sequence will be accepted. You need to implement a function replacement.

  • int replacement(int n, int gondolaSeq[], int replacementSeq[])
    • $$$n$$$: the length of the input sequence.
    • $$$gondolaSeq$$$: array of length $$$n$$$; $$$gondolaSeq$$$ is guaranteed to be a gondola sequence, and $$$gondolaSeq[i]$$$ is element $$$i$$$ of the sequence, for $$$0 \le i \le n - 1$$$.
    • The function should return $$$l$$$, the length of the replacement sequence.
    • $$$replacementSeq$$$: array that is sufficiently large to store the replacement sequence; you should return your sequence by placing element $$$i$$$ of your replacement sequence into $$$replacementSeq[i]$$$, for $$$0 \le i \le l - 1$$$.

Count Replacement Sequences

In the next four subtasks you must count the number of possible replacement sequences that produce a given sequence (which may or may not be a gondola sequence), modulo $$$1\,000\,000\,009$$$. You need to implement a function countReplacement.

  • int countReplacement(int n, int inputSeq[])
    • $$$n$$$: the length of the input sequence.
    • $$$inputSeq$$$: array of length $$$n$$$; $$$inputSeq[i]$$$ is element $$$i$$$ of the input sequence, for $$$0 \le i \le n - 1$$$.
    • If the input sequence is a gondola sequence, then count the number of replacement sequences that produce this gondola sequence (which could be extremely large), and return this number modulo $$$1\,000\,000\,009$$$. If the input sequence is not a gondola sequence, the function should return $$$0$$$. If the input sequence is a gondola sequence but no gondolas broke down, the function should return $$$1$$$.
Input

The sample grader reads the input in the following format:

  • line 1: $$$T$$$, the subtask number your program intends to solve $$$(1 \le T \le 10)$$$.
  • line 2: $$$n$$$, the length of the input sequence.
  • line 3: If $$$T$$$ is $$$4$$$, $$$5$$$, or $$$6$$$, this line contains $$$gondolaSeq[0], \ldots, gondolaSeq[n - 1]$$$. Otherwise this line contains $$$inputSeq[0], \ldots, inputSeq[n - 1]$$$.
Output

The sample grader prints the following output:

If subtask is 1, 2 or 3

  • line 1: return value of valid

If subtask is 4, 5 or 6

  • line 1: return value of replacement $$$l$$$.
  • lines $$$2 + l (0 \le i \lt l)$$$: $$$replacementSequence[i]$$$.

If subtask is 7, 8, 9 or 10

  • line 1: return value of countReplacement
Scoring
SubtaskPointsninputSeq
15$$$n \le 100$$$has each number from $$$1$$$ to $$$n$$$ exactly once
25$$$n \le 100\,000$$$$$$1 \le inputSeq[i] \le n$$$
310$$$n \le 100\,000$$$$$$1 \le inputSeq[i] \le 250\,000$$$
SubtaskPointsngondolaSeq
45$$$n \le 100$$$$$$1 \le gondolaSeq[i] \le n + 1$$$
510$$$n \le 1\,000$$$$$$1 \le gondolaSeq[i] \le 5\,000$$$
620$$$n \le 100\,000$$$$$$1 \le gondolaSeq[i] \le 250\,000$$$
SubtaskPointsninputSeq
75$$$4 \le n \le 50$$$$$$1 \le inputSeq[i] \le n + 3$$$
815$$$4 \le n \le 50$$$ $$$1 \le inputSeq[i] \le 100$$$, and at least $$$n - 3$$$ of the initial gondolas did not break down.
915$$$n \le 100\,000$$$$$$1 \le inputSeq[i] \le 250\,000$$$
1010$$$n \le 100\,000$$$$$$1 \le inputSeq[i] \le 1\,000\,000\,000$$$
Examples
Input
1
30
16 26 18 19 20 13 22 21 24 25 17 27 28 29 30 1 2 3 11 5 6 8 7 9 10 12 4 23 14 15
Output
0
Input
2
6
3 4 5 6 1 2
Output
1
Input
3
7
1 2 3 4 5 6 5
Output
0
Input
4
2
3 2
Output
1 1
Input
5
14
12 13 14 1 2 3 4 5 6 7 8 9 10 11
Output
0 
Input
6
50
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 51 47 48 49 50 1 2 3
Output
1 46
Input
7
11
4 5 6 7 8 9 10 11 1 2 3
Output
1
Input
8
14
6 94 8 9 10 93 12 13 95 1 2 3 4 5
Output
224280120
Input
9
4
1 2 7 6
Output
2
Input
10
4
4 7 4 7
Output
0
Note

You have to submit exactly one file, called gondola.cpp. This file should implement all three subprograms described above (even if you only plan to solve some of the subtasks), using the signatures. You also need to include a header file gondola.h for C/C++ implementation.

SubtaskinputSeq Return value Note
1$$$(1, 2, 3, 4, 5, 6, 7)$$$1—
1$$$(3, 4, 5, 6, 1, 2)$$$1—
1$$$(1, 5, 3, 4, 2, 7, 6)$$$01 cannot appear just before 5
1$$$(4, 3, 2, 1)$$$04 cannot appear just before 3
2$$$(1, 2, 3, 4, 5, 6, 5)$$$0two gondolas numbered 5
3$$$(2, 3, 4, 9, 6, 7, 1)$$$1replacement sequence (5, 8)
3$$$(10, 4, 3, 11, 12)$$$04 4 cannot appear just before 3
SubtaskgondolaSeq Return value ReplacementSeq
4$$$(3, 1, 4)$$$1$$$(2)$$$
4$$$(5, 1, 2, 3, 4)$$$0$$$()$$$
5$$$(2, 3, 4, 9, 6, 7, 1)$$$2$$$(5, 8)$$$
SubtaskinputSeq Return value Replacement sequence
7$$$(1, 2, 7, 6)$$$2$$$(3, 4, 5)$$$ or $$$(4, 5, 3)$$$
8$$$(2, 3, 4, 12, 6, 7, 1)$$$1$$$(5, 8, 9, 10, 11)$$$
9$$$(4, 7, 4, 7)$$$0$$$inputSeq$$$ is not a gondola sequence
10$$$(3, 4)$$$2$$$(1, 2)$$$ or $$$(2, 1)$$$

E. Friend
time limit per test
1 second
memory limit per test
16 megabytes
input
standard input
output
standard output

We build a social network from $$$n$$$ people numbered $$$0, \ldots, n - 1$$$. Some pairs of people in the network will be friends. If person $$$x$$$ becomes a friend of person $$$y$$$, then person $$$y$$$ also becomes a friend of person $$$x$$$.

The people are added to the network in $$$n$$$ stages, which are also numbered from $$$0$$$ to $$$n - 1$$$. Person $$$i$$$ is added in stage $$$i$$$. In stage $$$0$$$, person $$$0$$$ is added as the only person of the network. In each of the next $$$n - 1$$$ stages, a person is added to the network by a host, who may be any person already in the network. At stage $$$i (0 \lt i \lt n)$$$, the host for that stage can add the incoming person $$$i$$$ into the network by one of the following three protocols:

  • IAmYourFriend makes person $$$i$$$ a friend of the host only.
  • MyFriendsAreYourFriends makes person $$$i$$$ a friend of each person, who is a friend of the host at this moment. Note that this protocol does not make person $$$i$$$ a friend of the host.
  • WeAreYourFriends makes person $$$i$$$ a friend of the host, and also a friend of each person, who is a friend of the host at this moment.

After we build the network we would like to pick a sample for a survey, that is, choose a group of people from the network. Since friends usually have similar interests, the sample should not include any pair of people who are friends with each other. Each person has a survey confidence, expressed as a positive integer, and we would like to find a sample with the maximum total confidence.

Task

Given the description of each stage and the confidence value of each person, find a sample with the maximum total confidence. You only need to implement the function findSample.

  • int findSample(int n, int confidence[], int host[], int protocol[])
    • $$$n$$$: the number of people.
    • $$$confidence$$$: array of length $$$n$$$; $$$confidence[i]$$$ gives the confidence value of person $$$i$$$.
    • $$$host$$$: array of length $$$n$$$; $$$host[i]$$$ gives the host of stage $$$i$$$.
    • $$$protocol$$$: array of length $$$n$$$; $$$protocol[i]$$$ gives the protocol code used in stage $$$i$$$ $$$(0 \lt i \lt n)$$$: $$$0$$$ for IAmYourFriend, $$$1$$$ for MyFriendsAreYourFriends, $$$2$$$ for WeAreYourFriends.
    • Since there is no host in stage $$$0$$$, $$$host[0]$$$ and $$$protocol[0]$$$ are undefined and should not be accessed by your program.
    • The function should return the maximum possible total confidence of a sample.
Input

The sample grader reads the input in the following format:

  • line 1: $$$n$$$.
  • line 2: $$$confidence[0], \ldots, confidence[n - 1]$$$.
  • line 3: $$$host[1], protocol[1], host[2], protocol[2], \ldots, host[n - 1], protocol[n - 1]$$$.
Output

The sample grader will print the return value of findSample.

Scoring
SubtaskPoints$$$n$$$ confidence Protocols used
111$$$2 \le n \le 10$$$$$$1 \le confidence \le 1\,000\,000$$$All three protocols
28$$$2 \le n \le 1\,000$$$$$$1 \le confidence \le 1\,000\,000$$$Only MyFriendsAreYourFriends
38$$$2 \le n \le 1\,000$$$$$$1 \le confidence \le 1\,000\,000$$$Only WeAreYourFriends
419$$$2 \le n \le 1\,000$$$$$$1 \le confidence \le 1\,000\,000$$$Only IAmYourFriend
523$$$2 \le n \le 1\,000$$$All confidence values are 1 Both MyFriendsAreYourFriends and IAmYourFriend
631$$$2 \le n \le 100\,000$$$$$$1 \le confidence \le 10\,000$$$All three protocols
Examples
Input
6
13 3 6 20 10 15
0 0 0 1 1 2 2 1 0 0
Output
35
Input
2
10 20
0 1
Output
30
Input
2
10 20
0 2
Output
20
Input
2
10 20
0 0
Output
20
Input
3
1 1 1
0 0 0 1
Output
2
Note

You have to submit exactly one file, called friend.cpp. This file should implement the subprogram described above, using the following signatures. You also need to include a header file friend.h for C/C++ implementation.

stagehostprotocolfriend relations added
10IAmYourFriend(1, 0)
20MyFriendsAreYourFriends(2, 1)
31WeAreYourFriends(3, 1), (3, 0), (3, 2)
42MyFriendsAreYourFriends(4, 1), (4, 3)
50IAmYourFriend(5, 0)

Initially the network contains only person 0. The host of stage 1 (person 0) invites the new person 1 through the IAmYourFriend protocol, hence they become friends. The host of stage 2 (person 0 again) invites person 2 by MyFriendsAreYourFriends, which makes person 1 (the only friend of the host) the only friend of person 2. The host of stage 3 (person 1) adds person 3 through WeAreYourFriends, which makes person 3 a friend of person 1 (the host) and people 0 and 2 (the friends of the host). Stages 4 and 5 are also shown in the table above. The final network is shown in the following figure, in which the numbers inside the circles show the labels of people, and the numbers next to the circles show the survey confidence. The sample consisting of people 3 and 5 has totalsurvey confidence equal to $$$20 + 15 = 35$$$, which is the maximum possible total confidence.

F. Holiday
time limit per test
3 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

Jian-Jia is planning his next holiday in Taiwan. During his holiday, Jian-Jia moves from city to city and visits attractions in the cities.

There are $$$n$$$ cities in Taiwan, all located along a single highway. The cities are numbered consecutively from $$$0$$$ to $$$n - 1$$$. For city $$$i$$$, where $$$0 \lt i \lt n - 1$$$, the adjacent cities are $$$i - 1$$$ and $$$i + 1$$$. The only city adjacent to city $$$0$$$ is city $$$1$$$, and the only city adjacent to city $$$n - 1$$$ is city $$$n - 2$$$.

Each city contains some number of attractions. Jian-Jia has $$$d$$$ days of holiday and plans to visit as many attractions as possible. Jian-Jia has already selected a city in which to start his holiday. In each day of his holiday Jian-Jia can either move to an adjacent city, or else visit all the attractions of the city he is staying, but not both. Jian-Jia will never visit the attractions in the same city twice even if he stays in the city multiple times. Please help Jian-Jia plan his holiday so that he visits as many different attractions as possible.

Task

Please implement a function findMaxAttraction that computes the maximum number of attractions Jian-Jia can visit.

  • long long int findMaxAttraction(int n, int start, int d, int attraction[])
    • $$$n$$$: the number of cities.
    • $$$start$$$: the index of the starting city.
    • $$$d$$$: the number of days.
    • $$$attraction$$$: array of length $$$n$$$; $$$attraction[i]$$$ is the number of attractions in city $$$i$$$, for $$$0 \le i \le n - 1$$$.
    • The function should return the maximum number of attractions Jian-Jia can visit.
Input

The sample grader reads the input in the following format:

  • line 1: $$$n$$$, $$$start$$$, $$$d$$$.
  • line 2: $$$attraction[0], \ldots, attraction[n - 1]$$$.
Output

The sample grader will print the return value of findMaxAttraction.

Scoring

In all subtasks $$$0 \le d \le 2n + \lfloor \frac{n}{2} \rfloor$$$, and the number of attractions in each city is nonnegative.

SubtaskPoints$$$n$$$ maximum number of attractions in a city starting city
17$$$2 \le n \le 20$$$$$$1\,000\,000\,000$$$no constraints
223$$$2 \le n \le 100\,000$$$100city 0
317$$$2 \le n \le 3\,000$$$$$$1\,000\,000\,000$$$no constraints
453$$$2 \le n \le 100\,000$$$$$$1\,000\,000\,000$$$no constraints
Examples
Input
5 2 7
10 2 20 30 1
Output
60
Input
100 0 150
4 82 9 38 25 3 48 61 2 39 42 73 64 23 58 42 39 32 34 90 45 12 75 98 90 36 62 97 86 89 69 56 70 44 94 95 47 7 22 16 46 64 89 77 53 46 18 92 45 18 48 56 30 89 20 86 24 48 83 76 36 17 31 72 62 91 32 75 98 54 91 10 85 80 87 37 92 71 96 2 89 9 59 86 98 79 71 21 26 19 63 28 37 94 100 65 50 31 39 13
Output
4436
Note

You have to submit exactly one file, called holiday.cpp. This file should implement the subprogram described above using the following signatures. You also need to include a header file holiday.h for C/C++ implementation.

Suppose Jian-Jia has 7 days of holiday, there are 5 cities (listed in the table below), and he starts from city 2. On the first day Jian-Jia visits the 20 attractions in city 2. On the second day Jian-Jia moves from city 2 to city 3, and on the third day visits the 30 attractions in city 3. Jian-Jia then spends the next three days moving from city 3 to city 0, and visits the 10 attractions in city 0 on the seventh day. The total number of attractions Jian-Jia visits is $$$20 + 30 + 10 = 60$$$, which is the maximum number of attractions Jian-Jia can visit in 7 days when he starts from city 2.

CityNumber of attractions
010
12
220
330
41
DayAction
1visit the attractions in city 2
2move from city 2 to city 3
3visit the attractions in city 3
4move from city 3 to city 2
5move from city 2 to city 1
6move from city 1 to city 0
7visit the attractions in city 0