Crossing River
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 10311 |
|
Accepted: 3889 |
Description
A group of N people wishes to go across a and only one boat, which can at most carry both persons. Therefore some sort of shuttle arrangement must be arranged on order to row the boat back and forth so, all people may Cross. Each person had a different rowing speed; The speed of a couple are determined by the speed of the slower one. Your job is to determine a strategy this minimizes the time for these people to get across.
Input
The first line of the input contains a single integer t (1 <= t <=), the number of test cases. Then T cases follow. The first line of all case contains N, and the second line contains n integers giving the time for each people to cross t He river. Each case was preceded by a blank line. There won ' t is more than, people and nobody takes more than, seconds to cross.
Output
For each test case, print a line containing the total number of seconds required for all the N people to cross the river.
Sample Input
141 2 5 10
Sample Output
17
thought: (If 1-n personal time time[n]. Increment arrangement)
- When there is only one person: sum=time[1];
- Two persons: Sum=time[1]+time[2]
- Three people's time: sum=time[1]+time[2]+time[3]
- The point ! When more than four people. For the pursuit of shortest time. There are only two modes of delivery: (1) fastest. Fastest time to---> slowest. Times slow---times fast TIME[2]+TIME[1]+TIME[N]+TIME[N-1]+TIME[2]
- (2) Fastest, slowest--fastest and fastest. Times-Quick go back to time[n]+time[1]+time[n-1]+time[1]
#include <cstdio> #include <algorithm> #define MAXN 100001using namespace Std;int time[maxn];int main () {int T ; scanf ("%d", &t); while (t--) {int n,sum=0; scanf ("%d", &n); for (int i=1;i<=n;i++) scanf ("%d", time+i); Sort (time+1,time+n+1); while (n) {if (n==1) {sum+=time[1]; n=0; } else if (n==2) {sum+=time[2]; n=0; } else if (n==3) {sum+=time[1]+time[2]+time[3]; n=0; } else {if (time[2]*2>=time[1]+time[n-1]) sum+=2*time[1]+time[n ]+TIME[N-1]; else Sum+=2*time[2]+time[1]+time[n]; n-=2; }} printf ("%d\n", sum); } return 0;}
POJ 1700 Cross River (mathematical simulation)