快速过河(贪心)

[问题描述]

A group of n people wish to cross a bridge at night.

n个人的队伍想在晚上通过一座大桥。

At most two people may cross at any time, and each group must have a flashlight.

任何时间最多有2人通过,每组必须有一个手电筒。

Only one flashlight is available among the n people, so some sort of shuttle arrangement must be arranged in order to return the flashlight so that more people may cross.

很可怜,这n个人只有一个手电筒可用,因此必须合理地安排,让手电筒能回到另一端,这样才能让更多人通过大桥。

Each person has a different crossing speed; the speed of a group is determined by the speed of the slower member.

每个人有不同的速度,编组后,一个组的速度等于慢的那个人的速度。

Your job is to determine a strategy that gets all n people across the bridge in the minimum time.

你的任务是实现一种策略,让所有人在最短时间内通过。

[输入]

The input begins with a single positive integer on a line by itself indicating the number of test cases, followed by a blank line. There is also a blank line between each two consecutive inputs.

第一行是一个单独的数字,代表测试案例的个数;随后是一个空行。后面每两个输入之间都有一个空行。

The first line of each case contains n, followed by n lines giving the crossing times for each of the people. There are not more than 1,000 people and nobody takes more than 100 seconds to cross the bridge.

每个测试案例的第一行是n的值,随后n行是每个人单独通过大桥的时间。人数≤1000,时间≤100s

【输出】

For each test case, the first line of output must report the total number of seconds required for all n people to cross the bridge.

对每个案例,首行是n个人通过的时间。

Subsequent lines give a strategy for achieving this time. Each line contains either one or two integers, indicating which person or people form the next group to cross.

随后是你的通过和返回次序,每一行是一个或者两个整数,对应着人(通过时间代表人)。返回也要占一行。

Each person is indicated by the crossing time specified in the input.Although many people may have the same crossing time, this ambiguity is of no consequence.

虽然不同的人可能有相同的速度,但这没有影响(看做一种人就好了)。

Note that the crossings alternate directions, as it is necessary to return the flashlight so that more may cross.

注意方向,意思就是说返回情况也要占一行。

If more than one strategy yields the minimal time, any one will do.

多种方案都是最短时间的,任选其一皆可。

The output of two consecutive cases must be separated by a blank line.

案例之间空行分界。

【样例输入】

1

4

1

2

5

10

【样例输出】

17

1 2

1

5 10

2

1 2

【解释】

17 秒

1 2 2秒

1 1秒

5 10 10秒

2 2秒

1 2 2秒
思路:如果不要求输出方案的话,可以用dp处理,状态转移方程为dp[i]=min(dp[i-1]+a[i]+a[0],dp[i-2]+a[0]+2*a[1]+a[n])。但是本题需要输出方案,所以采用贪心处理。思路都在注释中。

#include <stdio.h>
#include <stdlib.h>
int a[1005],m,n,sum;
int kk(const void *c,const void *b)
{
	return *(int *)c-*(int *)b;
}
int main()
{                            //1:我们需要跑腿的送手电筒 所以最优的情况肯定是先让最快的两个人过河 这样才能让我们花费在送手电筒的时间最少 (即:最快的两个人必须有一个在对岸) 
	scanf("%d",&m);         //2:顺理成章的 我们会想到 让最快的那个人将手电筒送回来  然后我们让最慢的两个人一起走 (故 我们让这两个人为一组来考虑故 n-2) 
						   //   这样我们节省出一个第二慢的时间 让对面最快(第二快)的将手电筒送回来并将最快的那个人接走(为什么要把最快的接走呢?其实这里就回到了1所述的状态) 
	printf("
");         //3:第一种情况下 是不是有可能会出现这种情况:第二快比最快的那个人慢很多 而第二慢来送手电筒消耗的时间很多,那么我们让最快的和最慢的一块走 
	while(m--)           //    即:a[0]+a[n-1]<a[1]+a[1]; (为什么呢? 因为第一次肯定是最快的将手电筒送回来 而第一种情况耗时为a[n]+a[1]+a[1],第二种情况是a[n]+a[0]+a[n-1]) 
	{                   //此时我们让 n-1 (为什么这里是n-1而不是n-2,因为我们只让最慢的和a[0]一块走了,次慢的我们可以再和后面的用第一、第二种情况比较) 
		sum=0;          
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		qsort(a,n,sizeof(int ),kk);
		if(n==1)
			printf("%d",a[0]);
		else if(n==2)
			printf("%d",a[0]+a[1]);
		else if(n==3)
			printf("%d",a[1]+a[0]+a[2]);
		else
			{
				int i;
				sum+=a[1];
				for(i=n-1;i>=3;)
				{
					if(a[0]+a[n]+a[0]+a[i-1]<a[0]+a[n]+a[1]+a[1])   //第二种情况 
					{
						sum+=a[0]+a[i]+a[0]+a[i-1];
						i--; 
				 	} 
				 	else
				 	{
				 		sum+=a[0]+a[i]+a[1]+a[1];
				 		i-=2;
					 }
				}
				if(i==2)
					{
						sum+=a[0]+a[2];
					}
				printf("%d
%d %d
",sum,a[0],a[1]);
				for(i=n-1;i>=3;)
				{
					if(a[0]+a[i-1]<a[1]+a[1])
					{
						printf("%d
%d %d
%d
%d %d
",a[0],a[0],a[i],a[0],a[0],a[i-1]);
						i--;
				 	} 
				 	else
				 	{
				 		printf("%d
%d %d
%d
%d %d
",a[0],a[i-1],a[i],a[1],a[0],a[1]);
				 		i-=2;
					 }
					 if(i==2)
					{
						printf("%d
%d %d
",a[0],a[0],a[2]);
					}
				}
			 } 
	}
	return 0;
}
原文地址:https://www.cnblogs.com/fxzemmm/p/14847984.html