Professional Documents
Culture Documents
Bus (En)
Bus (En)
Implementation
You should submit a single file, with a .cpp extension.
☞ Among the attachments in this task you will find a template bus.cpp with a sample imple-
mentation.
Sample Grader
Among this task’s attachments you will find a simplified version of the grader used during evaluation,
which you can use to test your solutions locally. The sample grader reads data from stdin, calls the
function that you should implement and writes back on stdout using the following format.
1
The biggest spaceport currently active on Earth, located in Kazakhstan.
bus Page 1 of 3
The input file is made up of L + 1 lines, containing:
• Line 1: the two space-separated integers N and L.
• Line 1 + i (0 ≤ i ≤ L − 1): the integer Ki , followed by Ki integers Fi,0 , . . . , Fi,Ki −1 .
The output file contains a single line, containing the return value S of function pianifica.
Constraints
• 2 ≤ N ≤ 100 000.
• 1 ≤ L ≤ 100 000.
• Ki ≥ 2 for each 0 ≤ i < L.
• K0 + K1 + · · · + KL−1 ≤ 300 000.
• 0 ≤ Fi,j < N for each 0 ≤ i < L and 0 ≤ j < Ki .
• Fi,j ̸= Fi,j+1 for each 0 ≤ i < L and 0 ≤ j < Ki − 1.
Scoring
Your program will be tested on a number of testcases grouped in subtasks. In order to obtain the score
associated to a subtask, you need to correctly solve all the testcases it contains.
• Subtask 1 [ 0 points]: Sample cases.
• Subtask 2 [17 points]: Ki = 2 for each 0 ≤ i < L.
• Subtask 3 [20 points]: N ≤ 2000, and K0 + K1 + · · · + KL−1 ≤ 4000.
• Subtask 4 [19 points]: Fi,j < Fi,j+1 for each 0 ≤ i < L and 0 ≤ j < Ki − 1.
• Subtask 5 [12 points]: Ki is odd and Fi,j = Fi,Ki −j−1 for each 0 ≤ i < L and 0 ≤ j < Ki − 1
(each bus serves its first-half stops again in the opposite direction).
• Subtask 6 [14 points]: No station appears more than 50 times in the input.
• Subtask 7 [18 points]: No additional constraints.
Examples
stdin stdout
8 3 3
7 2 3 6 1 4 3 5
2 6 7
2 0 1
4 2 -1
4 2 3 1 0
2 0 1
stdin stdout
5 3 0
5 0 3 1 2 4
2 1 4
2 0 1
bus Page 2 of 3
Explanation
The optimal solution for the first sample case consists in:
• Taking line 2 at bus stop 0;
• Changing to line 0 at stop 1;
• Getting off the bus and back on line 0 at stop 3;
• Changing to line 1 at stop 6;
• Finally getting off the bus at stop 7.
This trip makes a total of 3 changes.
4 3 5
0 1 6 7
In the second sample case, it is impossible to reach stop 3 starting from stop 0.
In the third sample case, the optimal solution consists in taking line 0 from stop 0 to stop 4, without
any changes on the way. Note that it is also possible to reach stop 4 using the line 2 and changing at
stop 1 to line 1. This solution however requires more changes and is not optimal.
2 2
0 1
0 3
4
1 3
Figure 3: Second sample case Figure 4: Third sample case
bus Page 3 of 3