题目描述:
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
141 2 5 10
Sample Output
17 题目大意:过河,速度取慢的 题解:有点像汉诺塔的感觉,贪心算法,直接看代码吧,大水题
#include#include #include #include #include #include #include using namespace std;#define PI 3.14159265358979323846264338327950int 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 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\n",t); }}