Crossing River
Time Limit: 1000MS | | Memory Limit: 10000K |
Total Submissions: 8215 | | Accepted: 3036 |
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
Source
题意就是我们以前听过的过桥的故事。但是这里时过河,一条船每次只能过两个人,问过去的最小路程
查阅相关资料,利用贪心算法解题,具体思路时这样的:
设i=人数;
i<3时,直接过去;
i=3时,设速度为ABC升序,那么A护送C过去,再回来,再送B过去最短,也就是t[0]+t[1]+t[2]
i>=4时,设最快的A,次快的B,次慢的C,最慢的D,
(1)如果用最小的来送:d+a+c+a+b=d+c+b+2a
(2)如果先让最小的两个过河,再让其中一个回来让最大的两个过河,再让前一步过去留下的那个回来,
再让最小的2个过河
也就是说小的两个过去2次,再单独回来一次.
2b+b+a+d=d+3b+a,【但是我们在处理(1)or(2)的时候因为在处理的时候是用递推的形式故只要处理送过去的和回来的就行了。。以后的步骤重复递推就OK了。。】千万要理解清楚去和返回算一次
比较两种方法,选小的;
我们分析每次的情况人数从N开始倒数回去,每次选取最优解,则得到的时最小值!
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
int m;
scanf("%d",&m);
while(m--)
{
int i,n,t[4001],max=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&t[i]);
//sort(t,t+n);这题不用sort也行AC,不过与他同胞的3404就不行了。。
for(i=n-1;i>2;i-=2)//这边是-2哦
{
if(2*t[0]+t[i]+t[i-1]<2*t[1]+t[0]+t[i])
max+=2*t[0]+t[i]+t[i-1];
else max+=2*t[1]+t[0]+t[i];
}
if(i==2)
max+=t[0]+t[1]+t[2];
else if(i==1)
max+=t[1];
else max+=t[0];
printf("%d\n",max);
}
return 0;
}
poj3404这题和上面那题是一样的加//的就是在poj1700的代码上修改的内容
Bridge over a rough river
Time Limit: 1000MS | | Memory Limit: 65536K |
Total Submissions: 3587 | | Accepted: 1474 |
Description
A group of N travelers (1 ≤ N ≤ 50) has approached an old and shabby bridge and wishes to cross the river as soon as possible. However, there can be no more than two persons on the bridge at a time. Besides it's necessary to light the way with a torch for safe crossing but the group has only one torch.
Each traveler needs ti seconds to cross the river on the bridge; i=1, ... , N (ti are integers from 1 to 100). If two travelers are crossing together their crossing time is the time of the slowest traveler.
The task is to determine minimal crossing time for the whole group.
Input
The input consists of two lines: the first line contains the value of N and the second one contains the values of ti (separated by one or several spaces).
Output
The output contains one line with the result.
Sample Input
4
6 7 6 5
Sample Output
29
Source
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
//int m;
//scanf("%d",&m);
//
while(m--)
//{
int i,n,t[4001],max=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&t[i]);
sort(t,t+n);
for(i=n-1;i>2;i-=2)
{
if(2*t[0]+t[i]+t[i-1]<2*t[1]+t[0]+t[i])
max+=2*t[0]+t[i]+t[i-1];
else max+=2*t[1]+t[0]+t[i];
}
if(i==2)
max+=t[0]+t[1]+t[2];
else if(i==1)
max+=t[1];
else max+=t[0];
printf("%d\n",max);
//
}
return 0;
}