Roadside Trees (Simplified Edition) CodeForces - 265B - 模拟

Roadside Trees (Simplified Edition) CodeForces - 265B

松鼠Liss喜欢坚果。一条街上有 n 棵树(从西到东编号为 1 到 n),每棵树的顶部都有一颗美味的坚果。
树的高度我很高。莉丝想吃所有的坚果。
现在,Liss位于编号为 1 的树的根上。在一秒钟内,Liss可以执行以下操作之一:

  • 在树上爬上或爬下一个单元。
  • 吃当前树顶部的坚果。
  • 跳到下一棵树。在这个动作中,Liss的高度不变。
    更正式地说,当Liss位于树 i 的高度 h[i] 时,她跳到树 i 的高度 h[i+1],
    如果 h[i] > h[i+1],则无法执行此操作。

计算吃掉所有坚果所需的最短时间(以秒为单位)。

Input

第一行包含整数 n(1≤n≤1e5)- 树木数量。
接下来的 n 行包含树的高度:第 i 行包含一个整数 hi(1≤hi≤1e4)- 编号为 i 的树的高度。

Output

打印一个整数-在几秒钟内吃掉所有坚果所需的最短时间。

Sample Input

2
1

Sample Output

5

Sample Input2

2
5
2
1
2
1
1

Sample Output2

14

分析

模拟题,需要注意跳的树是等高的,单向的。
可以简单画一个草图,看一下变化过程。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+10, INF=0x3f3f3f3f;
int a[N],n;

int main(){
    // freopen("data.in", "r", stdin);
    while(~scanf("%d", &n)){
        for(int i=1; i<=n; i++) scanf("%d", &a[i]);
        int ans=-1;
        for(int i=1; i<=n; i++){
            ans += abs(a[i]-a[i-1])+2;
        }
        printf("%d\n", ans);
    }
    return 0;
}
posted @ 2022-09-23 15:30  HelloHeBin  阅读(44)  评论(0)    收藏  举报