zoukankan      html  css  js  c++  java
  • D

    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
    ");
    			return 0;
    		}else if(n==1)
    		{
    			pf("%d
    %d
    ",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
    ",sum);
    		rep(i,0,v.size())//输出 
    		{
    			if(v[i]==1||v[i]==0)
    			{
    				pf("%d %d
    %d
    %d %d
    %d
    ",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
    ",a[k],a[k+1]);
    			}else if(v[i]==3)
    			{
    				pf("%d %d
    %d
    %d %d
    ",a[k],a[k+1],a[k+2],a[k+3],a[k+4]);
    			}
    		}
    	return 0;
    }
    
  • 相关阅读:
    IDirect3DDevice9::Clear
    Width vs Pitch
    5- vue django restful framework 打造生鲜超市 -完成商品列表页(上)
    4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍
    3- vue django restful framework 打造生鲜超市
    2- vue django restful framework 打造生鲜超市 -环境搭建
    1- vue django restful framework 打造生鲜超市
    Scrapy分布式爬虫打造搜索引擎- (二)伯乐在线爬取所有文章
    windows10上安装mysql
    博客开通第一天
  • 原文地址:https://www.cnblogs.com/wzl19981116/p/9477763.html
Copyright © 2011-2022 走看看