Calling students to the stage
Statement
N students are standing in a single line in an auditorium. The students are numbered 1 through N, and we want to call all N of them up in order.
When we call student X, student X comes up onto the stage. The students who were behind student X then each move one spot forward. If student X is in position K, this takes K-1 seconds, which is the number of students they have to cross to reach the very front.
For example, suppose N=8 students stand in line as follows. Each student has a number from 1 to N.
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Student number | 7 | 3 | 5 | 1 | 8 | 6 | 4 | 2 |
If we now call student 1, student 1 stands up on the stage, and this takes 3 seconds. After that the students stand in line in the auditorium as follows.
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Student number | 7 | 3 | 5 | 8 | 6 | 4 | 2 |
So, given the information about the N students to call, write a program that finds the total time it takes for every student to come up onto the stage.
Suppose the initial state is the same as the example above, and students 1, 2, 3, 4, 5, 6, 7 and 8 are called in that order. Working out the student order and the time taken at each step gives the following.
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Time taken |
|---|---|---|---|---|---|---|---|---|---|
| Initial state | 7 | 3 | 5 | 1 | 8 | 6 | 4 | 2 | 0s |
| Call 1 | 7 | 3 | 5 | 8 | 6 | 4 | 2 | 3s | |
| Call 2 | 7 | 3 | 5 | 8 | 6 | 4 | 9s | ||
| Call 3 | 7 | 5 | 8 | 6 | 4 | 10s | |||
| Call 4 | 7 | 5 | 8 | 6 | 14s | ||||
| Call 5 | 7 | 8 | 6 | 15s | |||||
| Call 6 | 7 | 8 | 17s | ||||||
| Call 7 | 8 | 17s | |||||||
| Call 8 | 17s |
So in this case the answer is 17 seconds.
Input and Output Examples
Example 1
Input
8, [7, 3, 5, 1, 8, 6, 4, 2], [1, 2, 3, 4, 5, 6, 7, 8]
Output
17
Example 2
Input
10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output
0
Example 3
Input
10, [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output
45
Example 4
Input
10, [7, 3, 2, 5, 1, 6, 8, 9, 10, 4], [9, 2, 1, 5, 4, 6, 8, 10, 3, 7]
Output
26
Input
First, the number of students N is given as a natural number. (N is a natural number less than or equal to 1,000.)
Next comes an array students, holding the order in which the students numbered 1 through N initially stand in line.
Next comes an array calls, holding the numbers of the N students to call. Each student is called only once.
Output
Return the total time it takes for every student to come up onto the stage, as a natural number.
Requirements
- You must write a solution(N, students, calls) function and return the answer
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 4 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
4 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
8, [7, 3, 5, 1, 8, 6, 4, 2], [1, 2, 3, 4, 5, 6, 7, 8]
17
10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
0
10, [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
45
10, [7, 3, 2, 5, 1, 6, 8, 9, 10, 4], [9, 2, 1, 5, 4, 6, 8, 10, 3, 7]
26
Notes
Background that helps with solving. The rules you have to follow are in the input and output specs and the requirements above.
This is problem 1 of practice test 1 in the original.
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 4 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Input read time 1ms (excluded from the ranking)
Log in and you can see the code once you solve this problem.
Input read time 4ms (excluded from the ranking)
Log in and you can see the code once you solve this problem.
Input read time 7ms (excluded from the ranking)
This person kept their code private.
Input read time 7ms (excluded from the ranking)
Log in and you can see the code once you solve this problem.