Text editor

시간 제한4초메모리 제한1024 MB

요약
텍스트 파일에서 커서를 한 줄과 열 위치에서 다른 위치로 옮기는 데 필요한 화살표 키 입력의 최솟값을 구한다.
난이도

보통10점 중 6점

유형
그리디, 그래프, 수학
정답자
아직 제출이 없습니다

문제

Robert is competing in CEOI 2024. He has almost finished his solution to the hardest problem of the day, and not just that, he's pretty sure it will get 100 points! There is just one small issue: He made a typo! What's worse, his favorite computer mouse, which he had been using since 2008, seems to have finally broken and isn't responding at all. Therefore, he will have to navigate to the typo using the arrow keys on his keyboard.

Robert's program has NN lines with lengths l_1,l_2,…,l_Nl\_1, l\_2, \ldots, l\_N. Robert always ends his programs with an empty line, therefore l_N=0l\_N = 0. The cursor can be placed between two characters, as well as at the start or end of a line. As such, line ii has l_i+1l\_i + 1 possible cursor positions (called columns) numbered from 11 to l_i+1l\_i + 1. For example, this is what a cursor placed on line 2 in column 6 would look like:

Robert wants to move his cursor from line s_ls\_l column s_cs\_c to line e_le\_l column e_ce\_c. He would like to know the minimum number of key presses needed to do so.

The horizontal arrow keys are quite simple. Pressing left will move the cursor to the previous column, unless the cursor was at the start of a line, in which case it will move to the end of the previous line. Similarly, pressing right will move the cursor to the next column, or to the start of the next line if the cursor was at the end of a line.

For example, left presses can look like this:

And right presses can look like this:

Pressing left at the very start of the file or pressing right at the very end of the file will have no effect.

The vertical arrow keys are slightly more complicated. Pressing upwill move the cursor to the previous line and pressing down will move it to the next line, without changing the column number. However, if this would put the cursor past the end of the new line, the cursor will jump to the end of that line instead.

For example, up presses can look like this:

And down presses can look like this:

If pressing up or down would put the cursor on a line that doesn't exist, the cursor won't move at all.

입력

The first line of input contains the integer NN — the number of lines of Robert's solution. The second line contains two integers s_ls\_l and s_cs\_c separated by spaces — the initial cursor position. Similarly, the third line contains two integers e_le\_l and e_ce\_c — the target cursor position. The fourth line contains NN space-separated integers l_1,l_2,…,l_Nl\_1, l\_2, \ldots, l\_N — the length of each line.

출력

Your program should output a single line containing a single integer — the minimum number of key presses to move the cursor from (s_l,s_c)(s\_l, s\_c) to (e_l,e_c)(e\_l, e\_c).

제한

  • 1≤N≤1061 \leq N \leq 10^6
  • 0≤l_i≤1090 \leq l\_i \leq 10^9 (for each ii such that 1≤i≤N1 \leq i \leq N)
  • l_N=0l\_N = 0
  • 1≤s_l,e_l≤N1 \leq s\_l, e\_l \leq N
  • 1≤s_c≤l_s_l+11 \leq s\_c \leq l\_{s\_l} + 1
  • 1≤e_c≤l_e_l+11 \leq e\_c \leq l\_{e\_l} + 1.

예제2

  1. 예제 1

    입력
    5
    3 1
    2 8
    7 10 9 9 0
    
    예상 출력
    3
    
  2. 예제 2

    입력
    5
    1 20
    3 25
    25 10 40 35 0
    
    예상 출력
    16