1008 Elevator (20 point(s))

1008 Elevator (20 point(s))

The highest building in our city has only one elevator. A request list is made up with 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.

Exampe:

#include<iostream>
#include<vector>

using namespace std;

int main()
{
    int K;
    cin >> K;
    int ans = 0;
    int floor = 0;
    for(int i = 0; i < K; i++) {
        int move, diff;
        cin >> move;
        diff = move - floor;
        floor = move;
        if(diff > 0) {
            ans += diff * 6 + 5;
        } else {
            ans += (-diff) * 4 + 5;
        }
    }
    cout << ans;
}

Ideas:

Dynamic programming, dp[i] = dp[i-1] + cost[i]

Guess you like

Origin blog.csdn.net/u012571715/article/details/113913787