Time limit: +Ms | Memory Limit:65535KB Difficulty:5
-
-
Describe
-
In the dark of the night, N-travelers came to a narrow and no-fence side of the bridge. If you don't use a flashlight, everyone is afraid to cross the bridge anyway. Unfortunately, a total of n individuals with only one flashlight, and the bridge is only narrow enough to allow two people at the same time. If they cross the bridge alone, the time required for n people is known, and if two people cross the bridge at the same time, it is the time required for the slower person to move alone. The question is, how to design a program that allows the N people to cross the bridge as quickly as possible.
-
-
Input
-
The
-
first line is an integer T (1<=t<=20) that represents the number of groups of test data
The first line of each set of test data is an integer n (1<=n<=1000) representing a total of n individuals to cross the river
The second row of each set of test data is n integer si, which indicates the time it takes the person to cross the river. (0<si<=100)
-
-
Output
-
-
the minimum time required for the output of all people crossing the river
-
-
Sample input
-
-
141 2 5 10
-
-
Sample output
-
-
17
A problem that feels more novel
Code:
#include <iostream> #include <algorithm>using namespace Std;int main () {int N,m,i,j,k;cin>>n;while ( n--) {cin>>m;int *a=new int[m];for (i=0;i<m;i++) cin>>a[i];sort (a,a+m); K=0;while (m>3) {if (2*a[1] <a[0]+a[m-2]) {k+=a[1];//a[0] and a[1] [River K+=a[0];//a[0] come back k+=a[m-1];//a[0] and a[n-1] River K+=a[1];//a[1] come back}else{k+=a[m-1] ;//a[0] and a[n-1] [river] k+=a[0];//a[0] come back k+=a[m-2];//a[0] and A[n-2] cross the river K+=a[0];//a[1] come back}m-=2;} if (3==m) K=k+a[0]+a[1]+a[2];else if (2==m) K=k+a[1];else if (1==m) k=k+a[0];cout<<k<<endl;delete[] A;} return 0;}
Nyoj 47 River Crossing problem