一、问题描述
Crossing River
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 6123 |
|
Accepted: 2112 |
Description
A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has a different rowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.
Input
The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. The first line of each case contains N, and the second line contains N integers giving the time for each people to cross the river. Each case is preceded by a blank line. There won't be more than 1000 people and nobody takes more than 100 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
1
4
1 2 5 10
Sample Output
17
二、代码
#include<iostream> #include<algorithm> using namespace std; int a[1005]; int main() { int N; int i,j; int ans=0; int T; cin>>T; for(j=0;j<T;++j) { ans=0; cin>>N; for(i=0;i<N;++i) cin>>a[i]; sort(a,a+N); if(N<=3) { if(3==N) ans=a[0]+a[1]+a[2]; else { if(2==N) ans=a[1]; else ans=a[0]; } } else { int A=a[0],B=a[1]; int C,D; for(i=N-1;i>2;i-=2) { C=a[i-1]; D=a[i]; if((B+B)<(A+C)) ans+=(A+B+B+D); else ans+=(A+A+C+D); } if(1==N%2) ans+=(A+B+a[2]); else ans+=B; } cout<<ans<<endl; } return 0; }
|
阅读(1017) | 评论(0) | 转发(0) |