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. 【管用】 使用VMtools实现主机Windows与虚拟机Linux文件共享

    实现windows主机与linux虚拟机文件共享,有很多方法,包括使用samba文件服务器等,本文介绍通过vmware虚拟机软件中的vmtools工具来实现文件共享. 一.环境 1.主机:Window ...

  2. 遭遇ASP.NET的Request is not available in this context

    如果ASP.NET程序以IIS集成模式运行,在Global.asax的Application_Start()中,只要访问Context.Request,比如下面的代码 var request = Co ...

  3. 开启win10下Ubuntu子系统的SSH服务 并设置为开机启动

    Win10中安装Ubuntu子系统后默认是没有开启SSH服务的,需要手动配置开启, 1.先通过 bash 进入子系统修改配置 vi /etc/ssh/sshd_config  备注 输入i 表示键入, ...

  4. Java操作Linuxshell并且获取返回值

    /** * */package com.king.weixin.util;import java.io.BufferedReader;import java.io.InputStream;/*** @ ...

  5. CAShapeLayer(UIBezierPath)、CAGradientLayer绘制动态小车

    看到一个大神写的代码,引用过来让大家看看! //  1.CAShapeLayer是一种特殊的层,可以在上面渲染图形. //  2.CAShapeLayer继承自CALayer,可使用CALayer的所 ...

  6. Delphi如何处理在进行大量循环时,导致的应用程序没有响应的情况

    一般用在比较费时的循环中,往往导致应用程序没有响应,此时在比较费时的程序体中加入Application.ProcessMessages即可解决,该语句的作用是检查并先处理消息队列中的其他消息. 例如, ...

  7. Python中的__name__和类

    1.   python中if __name__ == '__main__': 的解析 经常会在代码的最下面看到if __name__ == '__main__':,现在就来介 绍一下它的作用. 模块是 ...

  8. QLabel设置文字大小和颜色

    https://blog.csdn.net/fm0517/article/details/4805462 ui.label是QLabel ui.label_4->setText("so ...

  9. mac os下不同工具go env下gopath显示不同

    设置 vim ~/.zshrc 设置 vim ~/.bash_profile

  10. Linux下printf、fprintf、sprintf的区别

    (1)fprintf() int fprintf( FILE *stream, const char *format, ... );   用于文件操作,根据指定的format(格式)发送信息(参数)到 ...