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

D - Bridge的更多相关文章

  1. PHP设计模式(八)桥接模式(Bridge For PHP)

    一.概述 桥接模式:将两个原本不相关的类结合在一起,然后利用两个类中的方法和属性,输出一份新的结果. 二.案例 1.模拟毛笔(转) 需求:现在需要准备三种粗细(大中小),并且有五种颜色的比 如果使用蜡 ...

  2. Configure a bridge interface over a VLAN tagged bonded interface

    SOLUTION VERIFIED February 5 2014 KB340153 Environment Red Hat Enterprise Linux 6 (All Versions) Red ...

  3. Create a bridge using a tagged vlan (8021.q) interface

    SOLUTION VERIFIED April 27 2013 KB26727 Environment Red Hat Enterprise Linux 5 Red Hat Enterprise Li ...

  4. Configure bridge on a team interface using NetworkManager in RHEL 7

    SOLUTION IN PROGRESS February 29 2016 KB2181361 environment Red Hat Enterprise Linux 7 Teaming,Bridg ...

  5. 理解 neutron(15):Neutron linux-bridge-agent 创建 linux bridge 的简要过程

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  6. KVM 虚拟机联网方式:NAT 和 Bridge

    KVM 客户机网络连接有两种方式: 用户网络(User Networking):让虚拟机访问主机.互联网或本地网络上的资源的简单方法,但是不能从网络或其他的客户机访问客户机,性能上也需要大的调整.NA ...

  7. 桥接模式/bridge模式/对象结构型

    意图 将抽象部分与它的实现部分分离,使它们都可以独立的变化. 动机 当一个抽象类有多个实现时,通常用继承来协调它们.但是继承机制将抽象和实现固定,难以对抽象部分和实现部分独立地进行修改.扩充和重用. ...

  8. The network bridge on device VMnet0 is not running

    The network bridge on device VMnet0 is not running. The virtual machine will not be able to communic ...

  9. Net设计模式实例之桥接模式( Bridge Pattern)

    一.桥接模式简介(Brief Introduction) 桥接模式(Bridge Pattern),将抽象部分与它的实现部分分离,使的抽象和实现都可以独立地变化. Decouple an abstra ...

  10. Neutron 理解(14):Neutron ML2 + Linux bridge + VxLAN 组网

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

随机推荐

  1. 基于spring boot 2.x的websocket示例

    spring boot 2/spring 5自带了websocket,下面是最基本的示例(包括java服务端.java客户端以及js客户端) 一.pom依赖 <dependencies> ...

  2. .Net core下的配置设置(一)——Configuration

    ASP.NET Core 中提供了一个Configuration 包,用以应用配置基于配置提供程序建立的键值对.这里以json文件配置的方式,简单的介绍一下它的用法. 首先定义一个配置文件appset ...

  3. I2C总线协议图解

    原帖地址:https://www.cnblogs.com/aaronLinux/p/6218660.html

  4. JavaScript单独的模块中传递数据

    首先我们来看看这张图,让我们来思考一下! 下买我给出我的完整思路代码 html代码: <!DOCTYPE html> <html lang="zh-CN"> ...

  5. requirejs amd module load example

    person.js /** * This example make use of requireJS to provide a clean and simple way to split JavaSc ...

  6. instruments symbol name 不显示函数名!

    那是因为instruments找不到编译好的dSYM  其它的什么修改配置都没什么用 最好的办法就是直接删除资源文件APP名. 资源库 -> Developer -> Xcode -> ...

  7. 运维笔记10 (Linux软件的安装与管理(rpm,yum))

    概述:用rpm安装和管理软件(rpm解决依赖性),用yum安装与管理软件(yum解决依赖性). 1.linux的软件 linux能够说是一款改变时代的操作系统,可是一个操作系统再优秀假设没有好用的应用 ...

  8. CentOS7 安装FastDFS分布式文件系统

    CentOS7 安装FastDFS分布式文件系统 最近要用到fastDFS,所以自己研究了一下,在搭建FastDFS的过程中遇到过很多的问题,为了能帮忙到以后搭建FastDFS的同学,少走弯路,与大家 ...

  9. ubuntu install google-chrome-stable

    google-chrome-stable is available on a 3rd Party Repository: Google Chrome (for Stable). Follow the ...

  10. Xtrabackup的安装

    一.Installing Percona XtraBackup from Percona yum repository 添加源 yum install http://www.percona.com/d ...