快速过河(贪心)

[问题描述]

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("\n");         //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\n%d %d\n",sum,a[0],a[1]);
				for(i=n-1;i>=3;)
				{
					if(a[0]+a[i-1]<a[1]+a[1])
					{
						printf("%d\n%d %d\n%d\n%d %d\n",a[0],a[0],a[i],a[0],a[0],a[i-1]);
						i--;
				 	} 
				 	else
				 	{
				 		printf("%d\n%d %d\n%d\n%d %d\n",a[0],a[i-1],a[i],a[1],a[0],a[1]);
				 		i-=2;
					 }
					 if(i==2)
					{
						printf("%d\n%d %d\n",a[0],a[0],a[2]);
					}
				}
			 } 
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
小船过河问题是一个经典的贪心算法问题,其目标是找到一种最优的策略,使得在给定的条件下,将一定数量的人员或物品从一岸安全地运送到另一岸。 问题分析: 1. 条件:通常情况下,小船过河问题会给出以下条件: - 一条河流,分为两岸,每个岸上都有一些人员或物品需要过河。 - 小船的容量有限,每次只能携带一定数量的人员或物品过河。 - 某些人员或物品之间可能存在特定的限制条件,例如某些人员不能在没有其他特定人员的情况下留在某一岸上。 2. 目标:小船过河问题的目标是找到一种最优的策略,使得在满足给定条件的前提下,尽可能快地将所有人员或物品从一岸运送到另一岸。 解决思路: 贪心算法是解决小船过河问题的常用方法。其基本思路是每次选择当前最优的策略,即选择能够最大程度减少剩余问题规模的操作。 具体步骤如下: 1. 确定初始状态:将所有人员或物品都放在一岸,小船在一岸。 2. 根据给定条件,确定每次过河的最优策略: - 选择一定数量的人员或物品上船,使得满足限制条件。 - 将小船从一岸驶向另一岸。 - 在另一岸上船的人员或物品按照相应的限制条件进行筛选。 - 将小船从另一岸驶回一岸。 3. 重复步骤2,直到所有人员或物品都过河到达目标岸。 注意事项: 在实际应用中,需要根据具体问题的条件进行适当的调整和优化。同时,贪心算法并不保证能够找到全局最优解,因此在某些情况下可能需要结合其他算法进行求解。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值