HDU 1008 Elevator

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=1008


Elevator Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 52650 Accepted Submission (s): 29078


Problem DescriptionThe Highest building in We City have only one elevator. A request list is made to with N positive numbers. The numbers denote at which floors the elevator would stop, in specified order. It costs 6 seconds to move the elevator-one floor, and 4 seconds to move-down-one floor. The elevator would stay for 5 seconds on each stop.

For a given request list, you is 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 having to return to the ground floor when the requests was Fu Lfilled.

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

Outputprint the total time, on a, on a, a, and each test case.

Sample Input
1 23 2) 3 10

Sample Output
1741

Test instructions: The elevator on a layer to spend 6s, the next floor to spend four seconds, did not listen to spend five seconds, ask the elevator in the overall process of the total time.

Problem-Solving ideas: 1 each floor to stop 5s, on a floor to 6s, the next floor to 4s.
2) Here is a note for the last point of arrival, but also to stop 5s, so the final should be added n * 5, instead of (n-1) * 5.
3) Start at the beginning of the 0 floor, do not have to go back to the 0 floor.

Complexity Analysis: Time complexity: O (n)
Space complexity: O (n)

#include <iostream>  using namespace std;  int main ()  {      int n,i,sum,tap;      int a[101];      while (CIN >> n && N)      {          sum=0;          Tap=0;          for (i=0;i<n;i++) CIN >> A[i];          for (i=0;i<n;i++)          {              if (tap<a[i]) sum = sum+6* (A[i]-tap) +5;              else if (tap>a[i]) sum = sum+4* (tap-a[i]) +5;              else sum +=5;              Tap=a[i];          }          cout << sum << endl;      }      return 0;  }



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1008 Elevator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.