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