D - Bridge

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 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

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

4
1
2
5
10

Sample Output

17
1 2
1
5 10
2
1 2

和之前的过桥问题一样,只是增加了输出而已,用了贪心和分治的思想,两种过的方案,去更好的,把很多人慢慢变成2、或3个人就解决了

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h> 
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+100;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
const int N=1005;
int speed[N];
vector<int>v;
int a[N*5];
int main()
{
	int n;
	cin>>n;
		mm(a,0);v.clear();
		rep(i,0,n) sf("%d",&speed[i]);
		if(n==0)
		{
			pf("0\n");
			return 0;
		}else if(n==1)
		{
			pf("%d\n%d\n",speed[0],speed[0]);
			return 0;
		}
		sort(speed,speed+n);
		int sum=0,k=0;
		while(n>3)//一次载两个过去 
		{
			int x1=speed[0]+2*speed[1]+speed[n-1];
			int x2=2*speed[0]+speed[n-2]+speed[n-1];
			if(x1<x2)//判断哪种方法更好 
			{
				sum+=x1;
				v.push_back(1);
				a[k++]=speed[0];a[k++]=speed[1];
				a[k++]=speed[0];
				a[k++]=speed[n-2];a[k++]=speed[n-1];
				a[k++]=speed[1];
			}else
			{
				sum+=x2;
				v.push_back(0);
				a[k++]=speed[0];a[k++]=speed[n-1];
				a[k++]=speed[0];
				a[k++]=speed[0];a[k++]=speed[n-2];
				a[k++]=speed[0];
			}
			n-=2;
		}
		if(n==3)//最后剩下多少人 
		{
			sum+=speed[0]+speed[1]+speed[2];
			v.push_back(3);
			a[k++]=speed[0];a[k++]=speed[1];
			a[k++]=speed[0];
			a[k++]=speed[0];a[k++]=speed[2]; 
		}else if(n==2) 
		{
			sum+=speed[1];
			v.push_back(2);
			a[k++]=speed[0];a[k++]=speed[1]; 
		}
		k=0;pf("%d\n",sum);
		rep(i,0,v.size())//输出 
		{
			if(v[i]==1||v[i]==0)
			{
				pf("%d %d\n%d\n%d %d\n%d\n",a[k],a[k+1],a[k+2],a[k+3],a[k+4],a[k+5]);k+=6;
			}else if(v[i]==2)
			{
				pf("%d %d\n",a[k],a[k+1]);
			}else if(v[i]==3)
			{
				pf("%d %d\n%d\n%d %d\n",a[k],a[k+1],a[k+2],a[k+3],a[k+4]);
			}
		}
	return 0;
}
posted @ 2018-08-14 20:43  一无所知小白龙  阅读(290)  评论(0编辑  收藏  举报