CF253C.Text Editor

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Vasya is pressing the keys on the keyboard reluctantly, squeezing out his ideas on the classical epos depicted in Homer's Odysseus... How can he explain to his literature teacher that he isn't going to become a writer? In fact, he is going to become a programmer. So, he would take great pleasure in writing a program, but none — in writing a composition.

As Vasya was fishing for a sentence in the dark pond of his imagination, he suddenly wondered: what is the least number of times he should push a key to shift the cursor from one position to another one?

Let's describe his question more formally: to type a text, Vasya is using the text editor. He has already written n lines, the i-th line contains a__i characters (including spaces). If some line contains k characters, then this line overall contains (k + 1) positions where the cursor can stand: before some character or after all characters (at the end of the line). Thus, the cursor's position is determined by a pair of integers (r, c), where r is the number of the line and c is the cursor's position in the line (the positions are indexed starting from one from the beginning of the line).

Vasya doesn't use the mouse to move the cursor. He uses keys "Up", "Down", "Right" and "Left". When he pushes each of these keys, the cursor shifts in the needed direction. Let's assume that before the corresponding key is pressed, the cursor was located in the position (r, c), then Vasya pushed key:

  • "Up": if the cursor was located in the first line (r = 1), then it does not move. Otherwise, it moves to the previous line (with number r - 1), to the same position. At that, if the previous line was short, that is, the cursor couldn't occupy position c there, the cursor moves to the last position of the line with number r - 1;
  • "Down": if the cursor was located in the last line (r = n), then it does not move. Otherwise, it moves to the next line (with number r + 1), to the same position. At that, if the next line was short, that is, the cursor couldn't occupy position c there, the cursor moves to the last position of the line with number r + 1;
  • "Right": if the cursor can move to the right in this line (c < a__r + 1), then it moves to the right (to position c + 1). Otherwise, it is located at the end of the line and doesn't move anywhere when Vasya presses the "Right" key;
  • "Left": if the cursor can move to the left in this line (c > 1), then it moves to the left (to position c - 1). Otherwise, it is located at the beginning of the line and doesn't move anywhere when Vasya presses the "Left" key.

You've got the number of lines in the text file and the number of characters, written in each line of this file. Find the least number of times Vasya should push the keys, described above, to shift the cursor from position (_r_1, _c_1) to position (_r_2, _c_2).

瓦西娅不情愿地按着键盘上的按键,艰难地从自己想象的深潭中打捞出关于荷马史诗《奥德赛》中所描绘的经典叙事诗的句子……他该如何向自己的文学老师解释:自己将来并不打算成为一名作家呢?事实上,他立志成为一名程序员。因此,他非常乐意编写一个程序,却丝毫不想写一篇作文。

就在瓦西娅于自己想象力的幽暗深潭中苦苦寻觅一句恰当的句子时,他突然想到一个问题:将光标从一个位置移动到另一个位置,最少需要按多少次键?

我们更形式化地描述他的问题如下:瓦西娅正在使用一款文本编辑器输入文本。他已经写好了 nn 行文字,其中第 ii 行包含 aia_i 个字符(包括空格)。若某一行包含 kk 个字符,则该行总共存在 (k+1)(k+1) 个光标可停留的位置:即位于某个字符之前,或位于所有字符之后(行尾)。因此,光标的位置由一对整数 (r, c)(r,\,c) 唯一确定,其中 rr 表示行号,cc 表示光标在该行中的位置(位置编号从行首开始,自 1 起计)。

瓦西娅不用鼠标移动光标,而是仅使用 “上”、“下”、“右”、“左” 四个方向键。每次按下其中任一键,光标便朝对应方向移动一次。假设按相应按键前光标位于位置 (r, c)(r,\,c),则瓦西娅按下按键后:

  • “上”键:若光标当前位于第一行(即 r=1r = 1),则光标不移动;否则,光标移至前一行(行号为 r−1r-1)的相同列位置 cc。此时,若前一行较短(即该行长度不足以容纳位置 cc),则光标将移至第 r−1r-1 行的最后一个位置(即该行末尾);
  • “下”键:若光标当前位于最后一行(即 r=nr = n),则光标不移动;否则,光标移至下一行(行号为 r+1r+1)的相同列位置 cc。此时,若下一行较短(即该行长度不足以容纳位置 cc),则光标将移至第 r+1r+1 行的最后一个位置(即该行末尾);
  • “右”键:若光标在当前行中尚可向右移动(即 c<ar+1c < a_r + 1),则光标向右移动一位(至位置 c+1c+1);否则,光标已位于该行末尾,按下“右”键后不发生任何移动;
  • “左”键:若光标在当前行中尚可向左移动(即 c>1c > 1),则光标向左移动一位(至位置 c−1c-1);否则,光标已位于该行开头,按下“左”键后不发生任何移动。

现已知文本文件的行数,以及每行所含字符数。请计算:瓦西娅最少需按上述哪些键多少次,才能将光标从初始位置 (r1, c1)(r_1,\,c_1) 移动至目标位置 (r2, c2)(r_2,\,c_2)。

输入格式

The first line of the input contains an integer n (1 ≤ n ≤ 100) — the number of lines in the file. The second line contains n integers _a_1, _a_2, ..., a__n (0 ≤ a__i ≤ 105), separated by single spaces. The third line contains four integers _r_1, _c_1, _r_2, _c_2 (1 ≤ _r_1, _r_2 ≤ n, 1 ≤ _c_1 ≤ _a__r_1 + 1, 1 ≤ _c_2 ≤ _a__r_2 + 1).

输入的第一行包含一个整数 nn(1≤n≤1001 \leq n \leq 100)—— 文件中的行数。
第二行包含 nn 个整数 a1, a2, …, ana_1,\,a_2,\,\dots,\,a_n(0≤ai≤1050 \leq a_i \leq 10^5),以单个空格分隔。
第三行包含四个整数 r1, c1, r2, c2r_1,\,c_1,\,r_2,\,c_2(1≤r1, r2≤n1 \leq r_1,\,r_2 \leq n,1≤c1≤ar1+11 \leq c_1 \leq a_{r_1}+1,1≤c2≤ar2+11 \leq c_2 \leq a_{r_2}+1)。

输出格式

Print a single integer — the minimum number of times Vasya should push a key to move the cursor from position (_r_1, _c_1) to position (_r_2, _c_2).

输出一个整数——Vasya 从位置 (r1, c1)(r_1,\,c_1) 移动光标到位置 (r2, c2)(r_2,\,c_2) 所需按键的最少次数。

输入输出样例

  • 输入#1

    4
    2 1 6 4
    3 4 4 2

    输出#1

    3
  • 输入#2

    4
    10 5 6 4
    1 11 4 2

    输出#2

    6
  • 输入#3

    3
    10 1 10
    1 10 1 1

    输出#3

    3

说明/提示

In the first sample the editor contains four lines. Let's represent the cursor's possible positions in the line as numbers. Letter s represents the cursor's initial position, letter t represents the last one. Then all possible positions of the cursor in the text editor are described by the following table.

123

12

123s567

1t345

One of the possible answers in the given sample is: "Left", "Down", "Left".

在第一个样例中,编辑器包含四行。我们用数字表示光标在行中的可能位置。字母 s 表示光标的初始位置,字母 t 表示最终位置。那么文本编辑器中光标所有可能的位置可由下表描述:

123

12

123s567

1t345

给定样例中的一种可能答案是:“左”、“下”、“左”。

输入解题思路,AI测评打分。不知道怎么写?

首页