【Bridge】【UVA - 10037】(思维)

题目:

n people wish to cross a bridge at night. A group of at most two people may cross at any time, and each group must have a flashlight. 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.

      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.

Input

    The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

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

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

     The first line of output must contain the total number of seconds required for all n people to cross the bridge. The following 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 the 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.

Sample Input

1

 

4

1

2

5

10

Sample Output

17

1 2

1

5 10

2

1 2

 

解题报告:深夜过桥问题,一共有n个人,只有一个手电筒,必须要有手电筒才能够,且桥上只能同时容纳两个人前进(且前进按照团体最长时间算)。给出n个人的过桥耗时,问最小的时间能够让全部能通过。每次过桥之后都要等到手电筒传回来之后才能继续进行下一组。题目的突破口就是当人数小于等于4的时候,人数为3的时候最优的方式是最快的带最慢进去,然后最快的返回,然后继续带次快的,人数为2的时候,直接叫最快的带最慢的一次过去,当人数为1的时候,直接过去就可以。当人数大于等于4的时候,有两种解决方式1.最快带最慢,最快返回,最快带次慢,最快返回 2.最快带次快过去,然后最快返回,次慢带最慢过去,然后次快返回。其实之所以有这两种解决方式而没有更多是因为咱们始终是在选择最有解,为了避免最快和次慢大于次快的来回,每次操作都让n-2.然后一直小于4跳出,然后咱们重新输出这个过程就可以了。

ac代码:

#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long ll;

const int maxn =1100;

int num[maxn];

int main()
{
	int t,n;
	scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
		if(i!=1)
			printf("\n");
		int ans=0,m;
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			scanf("%d",&num[i]);
		sort(num,num+n);
		m=n;
		while(m>=4)
		{
			ans+=min(num[0]*2+num[m-1]+num[m-2],num[0]+num[1]*2+num[m-1]);
			m-=2;
		}
		if(m==3)
		{
			ans+=num[0]+num[1]+num[2];
		}
		else if(m==2)
		{
			ans+=num[1];
		}
		else
		{
			ans+=num[0];
		}
		printf("%d\n",ans);
		while(n>=4)
		{
			if(num[0]*2+num[n-1]+num[n-2]<num[0]+num[1]*2+num[n-1])
				printf("%d %d\n%d\n%d %d\n%d\n",num[0],num[n-1],num[0],num[0],num[n-2],num[0]);
			else
				printf("%d %d\n%d\n%d %d\n%d\n",num[0],num[1],num[0],num[n-2],num[n-1],num[1]);
			n-=2;
		}
		if(n==3)
		{
			printf("%d %d\n%d\n%d %d\n",num[0],num[1],num[0],num[0],num[2]);
		}
		else if(n==2)
		{
			printf("%d %d\n",num[0],num[1]);
		}
		else if(n==1)
		{
			printf("%d\n",num[0]);
		}
	}	
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值