luogu#P5552. Chino的试卷

Chino的试卷

Background

orz trz

Chino took the “cute girls” final exam, and it is time to hand out the papers again. However, teacher yggygg and teacher ggyggy 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 yggygg's strategy for handing out papers. Please help him compute the total distance walked to finish handing out all papers.

Problem 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 1n1\sim n in order. Define the distance between positions ii and jj as ij|i - j|. Each paper has a number on it, indicating which girl (at that numbered position) it should be handed to.

Teacher yggygg is handing out the papers. We define that this teacher has two hands. At the beginning, all papers are in teacher yggygg's left hand, and the teacher is at position ss. When handing out papers, teacher yggygg 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 pp of the papers and the teacher’s initial position ss, 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+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,sn, s, representing the number of people and the teacher’s initial position, respectively.
  • The second line contains nn non-negative integers. The ii-th number denotes pip_i, i.e., the ii-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.

5 1
2 3 1 4 5
8

Hint

Test Point n=n= Test Point n=n =
1 3×1013\times10^1 11 3×1053\times10^5
2 3×1023\times10^2 12
3 13
4 3×1033\times10^3 14
5 15
6 16 3×1063\times10^6
7 3×1043\times10^4 17
8 18
9 19
10 20

For the first 1515 test points, the time limit is 1s1s.

For the last 55 test points, the time limit is 4s4s.

Translated by ChatGPT 5