链接:http://poj.org/problem?id=1700
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
题意:有一个手电筒,N个人过桥,每次只能过两个人,需要手电筒,每次过桥时间为两个人过桥时间慢的那个人的时间,求所有过完桥所需的时间.
分情况讨论:
case 1:一个人时,所需时间为其本身的时间.
case 2:为慢的那个人的时间.
case 3:三个时间的总和.
case >3:
以每次送两个人为例
由于过桥的时间由去桥送人和回桥送手电筒两部分组成,显然如果回桥送手电筒始终为最快的那个人,虽然回桥送手电筒时间为最少,但去桥送人的过程慢了.此时时间为 2*a[0]+a[i]+a[i-1];(时间事先排好序)
可以考虑另外一种方案,第一步是最快的和次快的过去,用他们作为回桥送手电筒的人,第二步,两个最慢和次慢的过来,这样相对于第一步,省去了送次慢这个的时间.此时时间为a[0]+2*a[1]+a[i].
在送的过程中,互相比较,取最少的时间.
AC code;
#include<stdio.h>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1110],sum;
int min(int a,int b) { return a>b?b:a;}
int main()
{
int i,test,num;
scanf("%d",&test);
while(test--)
{
sum=0;
scanf("%d",&num);
memset(a,0,sizeof(a));
for(i=0;i<num;++i)
scanf("%d",&a[i]);
sort(a,a+num);
while(num>0)
{
if(num==1)
sum+=a[0];
else if(num==2)
sum+=a[1];
else if(num==3)
{
sum+=a[0]+a[1]+a[2];break;
}
else if(num>3)
{
if(a[0]+2*a[1]+a[num-1]>=2*a[0]+a[num-1]+a[num-2])
sum+=2*a[0]+a[num-1]+a[num-2];
else
sum+=a[0]+2*a[1]+a[num-1];
}
num-=2;
}
printf("%d\n",sum);
}
}