Crossing River
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11365 Accepted: 4296
Description
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
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11365 Accepted: 4296
Description
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
开始做的时候,想到了一种情况,用时间最短的人依次将每一个人带到对岸去,算测试案例,不过。想到了第二种方法,先将用时最短的两个人到河边,回来次短的那个人,用时最长的两个人再过河,最短的人自己回来,这时候把用时最长的两个人已经度过去了;如果还剩3个人,就让用时最短的人依次带着另外两个人过河(也就是第一种情况),如果剩了2个人,过去就行,时间你懂得;如果还有很多人等着过去,这时候就按照第二种情况,用同样的方法把他们都过去就行,就相当于把时用时最长(也是已经在对岸了)的两个人排除掉,用剩下的人作为新的过河问题来求解。测试案例过了。
但是,
我、、两个方法都用了,第一个没过,就以为第一种方法不行,就采用第二种情况来计算,结果总是WA,后来总结了一下原因,第一种方法,虽然测试案例没过,但是不代表其他情况不行,没有经过验证就把第一种情况排除掉了,结果就浪费时间了,这道题是将两个方法在每一种情况都比较,哪个好,就用那个。
<pre name="code" class="cpp">#include <istream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1005];
int sum;
void work(int m)
{
if(m==3) sum+=a[1]+a[2]+a[3];
else if(m==2) sum+=a[2];
else if(m==1) sum+=a[1];
else
{
if(2*a[2]<a[1]+a[m-1])
sum+=2*a[2]+a[1]+a[m];
else
sum+=2*a[1]+a[m]+a[m-1];
m-=2;
work(m);
}
}
int main(void)
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
sum=0;
int m=n;
work(m);
printf("%d\n",sum);
}
return 0;
}