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;
}
D - Bridge的更多相关文章
- PHP设计模式(八)桥接模式(Bridge For PHP)
一.概述 桥接模式:将两个原本不相关的类结合在一起,然后利用两个类中的方法和属性,输出一份新的结果. 二.案例 1.模拟毛笔(转) 需求:现在需要准备三种粗细(大中小),并且有五种颜色的比 如果使用蜡 ...
- 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 ...
- 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 ...
- 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 ...
- 理解 neutron(15):Neutron linux-bridge-agent 创建 linux bridge 的简要过程
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- KVM 虚拟机联网方式:NAT 和 Bridge
KVM 客户机网络连接有两种方式: 用户网络(User Networking):让虚拟机访问主机.互联网或本地网络上的资源的简单方法,但是不能从网络或其他的客户机访问客户机,性能上也需要大的调整.NA ...
- 桥接模式/bridge模式/对象结构型
意图 将抽象部分与它的实现部分分离,使它们都可以独立的变化. 动机 当一个抽象类有多个实现时,通常用继承来协调它们.但是继承机制将抽象和实现固定,难以对抽象部分和实现部分独立地进行修改.扩充和重用. ...
- 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 ...
- Net设计模式实例之桥接模式( Bridge Pattern)
一.桥接模式简介(Brief Introduction) 桥接模式(Bridge Pattern),将抽象部分与它的实现部分分离,使的抽象和实现都可以独立地变化. Decouple an abstra ...
- Neutron 理解(14):Neutron ML2 + Linux bridge + VxLAN 组网
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
随机推荐
- delphi 响应鼠标进入控件消息
procedure MSGEnter(var msg:TMessage);message CM_MOUSEENTER;//响应进入的消息 procedure MSGLeave(var msg: TMe ...
- 咏南APP(手机)开发框架
咏南APP(手机)开发框架 有意者可向咏南索取DEMO. 基于DELPHI官方的FIREMONKEY类库构建,不使用任何三方控件. 原生手机框架,支持各种手机硬件操作. 主界面 聊天 照相并分享 短信 ...
- SharePoint每日小贴士Web部件
SharePoint每日小贴士Web部件 项目描写叙述 此Web部件从指定SP自己定义列表或一个选定的 RSS源选择一个随机项目.并显示一张图片.标题和一个Tip. 适 ...
- UART简介及与COM口的区别
原帖地址:https://blog.csdn.net/jirryzhang/article/details/70084743 https://www.cnblogs.com/smartjourneys ...
- 使用AShot进行网页全页截图
import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.Chrom ...
- sqlite "insert or replace" 和 "insert or ignore" 用法
insert or replace:如果不存在就插入,存在就更新insert or ignore:如果不存在就插入,存在就忽略只对UNIQUE约束的字段起作用.举例:建表:CREATE TABLE T ...
- laravel实战化项目之三板斧
laravel实战化项目之三板斧 spring mvc 实战化项目之三板斧 asp.net mvc 实战化项目之三板斧 laravel是我工作10多年来见到的真正能称得上让phper从面条一样杂乱的代 ...
- iOS实现图片裁剪功能,基于TKImageView完善与讲解
1.功能需求:需要实现图片区域裁剪功能. 2.效果图: 3.实现原理:本来想自己实现的,刚好看到一个比较好的库:TKImageView,下载好研究了下,发现基本都能满足我的需求,而且封装的也比 ...
- numpy累积
numpy累积有两类函数:np.cumxxxxx和np.ufunc.accumulate() import numpy as np a = np.arange(1, 5) print(np.cumpr ...
- Unity应用架构设计(2)——使用中介者模式解耦ViewModel之间通信
当你开发一个客户端应用程序的时候,往往一个单页会包含很多子模块,在不同的平台下,这些子模块又被叫成子View(视图),或者子Component(组件).越是复杂的页面,被切割出来的子模块就越多,子模块 ...