CodeForces - 253C:Text Editor(暴力枚举)

Discription
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 ai 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 < ar + 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 (r1, c1) to position (r2, c2).

Input
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 a1, a2, …, an (0 ≤ ai ≤ 105), separated by single spaces. The third line contains four integers r1, c1, r2, c2 (1 ≤ r1, r2 ≤ n, 1 ≤ c1 ≤ ar1 + 1, 1 ≤ c2 ≤ ar2 + 1).

Output
Print a single integer — the minimum number of times Vasya should push a key to move the cursor from position (r1, c1) to position (r2, c2).

Examples
Input

4
2 1 6 4
3 4 4 2

Output

3

Input

4
10 5 6 4
1 11 4 2

Output

6

Input

3
10 1 10
1 10 1 1

Output

3

Note
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”.

题意
给出n个数,每个数分别代表每一行有多少个数,给定四种操作:up,down,left,right。
up:在当前行上面还有行的情况下,光标向上移动到上一行相同列位置或者到达上一行的末尾;否则则不懂。
down,left,right的操作均类似。
给出两个位置(r1,c1),(r2,c2),求出从(r1,c1)到(r2,c2)的最小步数。

思路
假设光标一定会经过第i行,枚举第i行的位置。

AC代码

#include<bits/stdc++.h>
using namespace std;
/*int abs(int x)
{
    if(x>0)return x;
    else return  -x;
}*/
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n,a[101],r1,c1,r2,c2;
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
        a[i]++;
    }
    cin>>r1>>c1>>r2>>c2;
    int ans=100000001;
    for(int i=1; i<=n; i++)///暴力枚举一定会到达第i行
    {
        int l=min(i,min(r1,r2));
        int r=max(i,max(r1,r2));
        int c=c1;
        for(int j=l; j<=r; j++)
            c=min(c,a[j]);///取最小的c1列
        int ans1=abs(i-r1)+abs(i-r2)+abs(c-c2);///从第i行必须都能到达第r1和第r2行,从最小的列数到达第c2列
        ans=min(ans,ans1);
    }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值