Title Description: n Individuals across the river, there is only one boat, up to two people rowing, each rowing speed is different, the speed of the boat speed of the slowest person. Input T is the number of case, each case input n is the number of people, the next line of input is the time each person crosses the river, are not the same. The time required to output n people all across the river
Algorithm thought: Adopt the Greedy method. There are two ways of rowing, one is the fastest + slowest, the fastest back, the fastest + times slow, the fastest back, cycle down, the second is the fastest + times faster, faster back, the slowest + times slow, the fastest back, cycle down. So when the rest of the population n>3, compare the first and second time, using a short-time method to cross the river. n=1,2,3 the time to discuss it alone.
#include <iostream>#include<stdio.h>#include<algorithm>#include<string.h>using namespacestd;intpt[1010];//Store time of peopleintMain () {intT; scanf ("%d",&T); while(t--){ intN; scanf ("%d",&N); Memset (PT,0,sizeof(PT)); for(intI=0; i<n;i++) {scanf ("%d", pt+i); } sort (Pt,pt+N); intt=0; while(n>3){ if(2*pt[1]+pt[0]>2*pt[0]+pt[n-2]) {T+=2*pt[0]+pt[n-1]+pt[n-2]; } Else{T+=2*pt[1]+pt[n-1]+pt[0]; } N-=2; } if(n==3) {T+=pt[2]+pt[0]+pt[1]; } Else if(n==2) {T+=pt[1]; } Else{T+=pt[0]; } printf ("%d\n", T); } return 0;}
Crossing River poj1700 Greedy