P5552 Chino's Exam Papers.
Background
orz trz
Chino took the “cute girls” final exam, and it is time to hand out the papers again. However, teacher $ygg$ and teacher $ggy$ had a dispute about how to hand out the papers with the least effort. Now, to resolve this conflict, you decide to help Chino speak with data.
Below is teacher $ygg$'s strategy for handing out papers. Please help him compute the total distance walked to finish handing out all papers.
Description
To simplify the problem, we assume that all of Chino’s classmates (girls) took the exam and are seated in a single row, with positions numbered $1\sim n$ in order. Define the distance between positions $i$ and $j$ as $|i - j|$. Each paper has a number on it, indicating which girl (at that numbered position) it should be handed to.
Teacher $ygg$ is handing out the papers. We define that this teacher has two hands. At the beginning, all papers are in teacher $ygg$'s left hand, and the teacher is at position $s$. When handing out papers, teacher $ygg$ will use his right hand to pick up one paper from the top of the left-hand stack. If this is the last paper that still needs to be handed out, then obviously he has no choice but to walk to that paper owner’s position and hand it out. If there are still papers left in his left hand, then he will make a comparison: he compares whether the walking distance to hand out the paper on the top of the left hand is shorter, or the walking distance to hand out the paper currently in the right hand is shorter. If the left-hand paper requires a shorter distance, he will put the right-hand paper to the very bottom of the left-hand stack. Otherwise, he will hand out the right-hand paper directly, and stay at the position where he just handed it out. In any case, he will take another paper from the left hand to make the next decision, until all papers are handed out.
Now, given the initial order sequence $p$ of the papers and the teacher’s initial position $s$, ask what the total distance he walks to finish handing out all papers is.
Chino wants to know the answer quickly, so you must finish this problem within $+4s$ time, qwq.
Orz yky,dyh,wjk,jjy,cxr,gsy,cpy,zcy,tyz,yy,hz,zhr,ygg.
Input Format
- The first line contains two positive integers $n, s$, representing the number of people and the teacher’s initial position, respectively.
- The second line contains $n$ non-negative integers. The $i$-th number denotes $p_i$, i.e., the $i$-th paper from top to bottom should be handed to that position.
Output Format
Output one integer, representing the total distance that needs to be walked.
Explanation/Hint
| Test Point | $n= $ | Test Point | $n =$ |
| :----: | :-----------: | :----: | :-----------: |
| 1 | $3\times10^1$ | 11 | $3\times10^5$ |
| 2 | $3\times10^2$ | 12 | $3\times10^5$ |
| 3 | $3\times10^2$ | 13 | $3\times10^5$ |
| 4 | $3\times10^3$ | 14 | $3\times10^5$ |
| 5 | $3\times10^3$ | 15 | $3\times10^5$ |
| 6 | $3\times10^3$ | 16 | $3\times10^6$ |
| 7 | $3\times10^4$ | 17 | $3\times10^6$ |
| 8 | $3\times10^4$ | 18 | $3\times10^6$ |
| 9 | $3\times10^4$ | 19 | $3\times10^6$ |
| 10 | $3\times10^4$ | 20 | $3\times10^6$ |
For the first $15$ test points, the time limit is $1s$.
For the last $5$ test points, the time limit is $4s$.
Translated by ChatGPT 5