Log inSign up
Problems / A001

Calling students to the stage

100 ptsCases 15Set by dongbinna
Added October 30.
4 solved5 attempted10 submissionssolvers took 1.5 tries on average

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.

Position12345678
Student number73518642

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.

Position1234567
Student number7358642

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.

Position12345678Time taken
Initial state735186420s
Call 173586423s
Call 27358649s
Call 37586410s
Call 4758614s
Call 578615s
Call 67817s
Call 7817s
Call 817s

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.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

4 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.

Input
Expected output
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.

0 chars1 lineMemory 256MB (recorded only)Reference 4.0ms

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.

1admin11ms367 charsJavaScriptMac OS X 10.15.7 / Chrome/154.0.0.0 / 12코어 / 32GB

Input read time 1ms (excluded from the ranking)

Log in and you can see the code once you solve this problem.

2today-news25ms192 charsPythonWindows NT 10.0 / Chrome/153.0.0.0 / 13코어 / 4GB

Input read time 4ms (excluded from the ranking)

Log in and you can see the code once you solve this problem.

3jaemin67ms178 charsPythonMac OS X 10.15.7 / Chrome/156.0.0.0 / 8코어 / 8GB

Input read time 7ms (excluded from the ranking)

This person kept their code private.

4bampal2113ms263 charsPythonWindows NT 10.0 / Chrome/156.0.0.0 / 8코어 / 16GB

Input read time 7ms (excluded from the ranking)

Log in and you can see the code once you solve this problem.