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. 控制WinForm中Tab键的跳转

    一,需求 在Winform中,默认情况下,按下Tab键,光标会按照我们设定的TabIndex值从小到大进行跳转. 但如果用户要求按下Tab键跳转到特定的控件,这种要求还是很合理的,比如用户只想输入几个 ...

  2. 异常处理汇总 ~ 修正果带着你的Code飞奔吧!

    异常处理汇总-运维系列 http://www.cnblogs.com/dunitian/p/4522983.html 异常处理汇总-开发工具  http://www.cnblogs.com/dunit ...

  3. iOS:给标签栏控制器的UITabbarItem添加点击动效

    一.介绍 现在很多app,附带很炫的点击效果,让用户享受到非常棒的体验,例如动画.渐变.音效等. 当然,市面上大多数app的标签栏点击还是挺中规中矩的,只是切换图片而已.然而,这个是可以优化的,附带点 ...

  4. CentOs7 HP找回root密码

    linuxman本人尝试了两种方式修改密码,只有一种成功.现展示如下第一种:成功1. 在启动界面选择  centos linux, with linux***.x86_642. 按 e 键进入编辑模式 ...

  5. &lt;climits&gt;头文件定义的符号常量

    <climits>头文件定义的符号常量 CHAR_MIN  char的最小值 SCHAR_MAX  signed char 最大值 SCHAR_MIN   signed char 最小值 ...

  6. golang:iconv

    最近在做邮件解析的工作,遇到需要转字符集编码的情况,go官方好像没有提供这样的库,于是从github上找了一下. https://github.com/qiniu/iconv 开发环境: linux ...

  7. Oracle中判断字段是否为数字

    在我们平常的开发中可能会遇到这样的问题,就是判断某一列是否全部由数字组成,我们都知道oracle并没有给我们提供这样一个现成的函数,那么根据我的经验我总结了两个行之有效的方法(列名:column,表名 ...

  8. Ubuntu 安装显卡驱动

    首先到英伟达的官网下载最新的驱动 然后,首先要卸载Ubuntu上已有的显卡驱动 $ sudo apt-get purge nvidia* 然后仅用Ubuntu自带的显卡驱动nouveau,不过我的16 ...

  9. 构建基于Suricata+Splunk的IDS入侵检测系统

    一.什么是IDS和IPS? IDS(Intrusion Detection Systems):入侵检测系统,是一种网络安全设备或应用软件,可以依照一定的安全策略,对网络.系统的运行状况进行监视,尽可能 ...

  10. MySQL 表中添加 时间戳 字段

    场景: 有张表的数据需要用同步工具同步至其他库,需要 update_time 时间戳字段 来做增量同步. 解决方法: alter table quant_stk_calc_d_wxcp add upd ...