[Meetcoder] Crossing Bridge

Source: Internet
Author: User

Crossing BridgeDescription

N people wish to cross a bridge at night. It's so dark around there that they has to cross the bridge under the help of a little lamp. Only one little lamp was available (Oh, men ...) and they has to be the little lamp walking with them. The bridge is of a width such, a maximum of 2 people may cross at a time.

Walks at his/her fixed speed. If The people cross the bridge together, they must walk at the pace of the slower one. How fast can I get all N people over the bridge?

Input

The input should be a list of n positive integers, where n was less than or equal to 1,000. And each integer of the list should is less than or equal to 100.

Output

The output should be the minimum time, all of the people can cross the bridge.

Sample Input
1 2
Sample Output
2
HINT

Greedy

Bridge problem! Main reference: http://blog.csdn.net/morewindows/article/details/7481851

1 classSolution {2 Private:3     int_bridge (vector<int> &v,intN) {4         if(n = =0)return 0;5         if(n = =1)returnv[0];6         if(n = =2)returnv[1];7         if(n = =3)returnv[0] + v[1] + v[2];8         intres =0;9         intA = v[0], B = v[1], x = v[n-2], y = v[n-1];Ten         if(2* b > A + x) Res + =2* A + x +y; One         ElseRes + = A +2* B +y; A         returnRes + _bridge (V, N-2); -     } -  Public: the     intBridge (vector<int> &v) { - sort (V.begin (), V.end ()); -         return_bridge (V, V.size ()); -     } + }; - /************************************************************** + problem:45 A User:easonliu at language:c++ - result:accepted - time:1 Ms - memory:2708 KB - ****************************************************************/

[Meetcoder] Crossing Bridge

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.