N - Crossing River

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

Title description:

There are N people crossing the river, but there is only one boat, up to 2 people at a time, there are many ways to cross the river, the boat must go back and forth. Everyone's rowing speed is different. When two people row a boat, the overall speed depends on the slower one. Find a plan that takes the least time for all N people to cross the river.

Problem-solving ideas:

When n<3:

  1. n=3, the fastest and second fastest crossing the river, then the fastest rowing back, and then the fastest and slowest crossing the river. sum = a[0] + a[1] + a[2];

  2. n=2, the fastest and slowest crossing the river. sum = a[1];

  3. n=1,sum = a[0];

When n>3:

Suppose the time required for n people to cross the river alone is stored in an array, and the array is sorted in ascending order.
Let the fastest and the second fastest cross the river first, then let one of them come back, then let the slowest and the second slow cross the river, and then let the other back.
which is:

  1. Cross the river the fastest and the second fastest, then row the boat back the fastest, cross the river again the slowest and the slowest, and then row the boat back the second fastest. a[0]+2*a[1]+a[n-1]

  2. The fastest and slowest cross the river, then the fastest rowing back, then the fastest and second slowest crossing the river, and then the fastest rowing back. 2*a[0]+a[n-2]+a[n-1]

The smaller of these two cases.
Sample explanation: 1 2 5 10

1.(1,2)先过河    时间是2" 
2.(2)回来        时间是2"  
3.(5,10)过河     时间10"
4.(1)回来        时间1"  
5.(1,2)过河      时间2"    
总时间17"

Code:

#include<stdio.h>
#include<algorithm>
using namespace std;
int a[10000];
int main()
{
    
    
	int t, n, m, i, j;
	scanf("%d", &t);
	while (t--)
	{
    
    
		int sum = 0,sum1,sum2;
		scanf("%d", &n);
		for (i = 0; i < n; i++)
			scanf("%d", &a[i]);
		sort(a, a + n);
		for (i = n - 1; i > 2; i -= 2)
		{
    
    
			sum1= 2 * a[0] + a[i - 1] + a[i];
			sum2= a[0] + 2 * a[1] + a[i];
			sum+=min(sum1,sum2); 
		}	
		if (i == 2)
			sum += a[0] + a[1] + a[2];
		else
			if (i == 1) 
				sum += a[1];
			else
				sum += a[0];
		printf("%d\n", sum);

	}
	return  0;
}

Guess you like

Origin blog.csdn.net/weixin_46703995/article/details/112530863