PAT甲级 1008 Elevator (20 分) 模拟

1008 Elevator (20 分)
Time limit: 400 ms
Memory limit: 64 MB
Source limit: 16 KB

The highest building in our city has only one elevator. A request list is made up with N N N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input

Each input file contains one test case. Each case contains a positive integer N N N, followed by N N N positive numbers. All the numbers in the input are less than 100.

Output

For each test case, print the total time on a single line.

Example

input
3 2 3 1
output
41

题意:

开始时电梯位于第0层,电梯每上升一层需要6秒时间,每下降一层需要4秒时间。电梯到达时会停留5秒。求电梯运行时间。注意电梯最终不需要返回到第0层。

思路:

电梯停留时间显然为 5 N 5N 5N秒,然后模拟电梯上升/下降求解电梯上升/下降所需的时间。最终加起来便为解。

参考代码:

#include 
int main() {
    int N;
    scanf("%d", &N);
    int cur = 0, x, ans = N * 5;
    while (N--) {
        scanf("%d", &x);
        int d = x - cur;
        if (d > 0) ans += d * 6;
        else ans -= d * 4;
        cur = x;
    }
    printf("%d\n", ans);
    return 0;
}

你可能感兴趣的:(PAT甲级)