Elevator

http://acm.zju.edu.cn/show_problem.php?pid=2108

Time limit: 1 Seconds

Memory limit: 32768K

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.

1. Input

There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.

2. Output

Print the total time on a single line for each test case.

3. Sample Input

{{{1 2 3 2 3 1 0}}}

4. Sample Output

17
41


   1 /*Written by czk*/
   2 #include <iostream>
   3 #include <sstream>
   4 using namespace std;
   5 
   6 int main() {
   7         while(1) {
   8                 int count;
   9                 int current = 0;
  10                 int next;
  11                 int total_time = 0;
  12                 cin >> count;
  13                 if(count==0) break;
  14                 for(int i = 0; i < count; i++) {
  15                         cin >> next;            
  16                         if(next > current)
  17                                 total_time += 6 * (next - current) + 5;
  18                         else if(next < current)
  19                                 total_time +=4*(current - next)+5;
  20                         else
  21                                 total_time += 5;
  22                         current = next;
  23                 }
  24                 cout << total_time  << endl;
  25         }
  26 
  27 }

zju2108 (2008-02-23 15:35:51由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.