题目描述:
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> #include<cstring> #include<sstream> #include<cmath> #include<cstdlib> #include<queue> using namespace std; #define PI 3.14159265358979323846264338327950 int T,n,a[1003],t; int main() { scanf("%d",&T); while(T--) { t=0; scanf("%d",&n); memset(a,0,sizeof(a)); for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); if(n==1) t=a[0]; else if(n==2) t=a[1]; else if(n==3) t=a[0]+a[1]+a[2]; else { for(int i=n-1;i>0;i=i-2) { if(i>=3) { if(a[0]+a[i-1]>=2*a[1]) t=t+a[0]+2*a[1]+a[i]; else t=t+2*a[0]+a[i]+a[i-1]; } if(i==2) t=t+a[0]+a[1]+a[2]; if(i==1) t=t+a[1]; } } printf("%d ",t); } }