[MeetCoder] Crossing Bridge
Crossing Bridge
Description
N people wish to cross a bridge at night. It’s so dark around there that they have to cross the bridge under the help of a little lamp. Only one little lamp is available (Oh, Men...) and they have to have the little lamp walking with them. The bridge is of a width such that a maximum of 2 people may cross at a time.
Each person walks at his/her fixed speed. If two people cross the bridge together, they must walk at the pace of the slower one. How fast can you get all N people over the bridge?
Input
The input should be a list of N positive integers, where N is less than or equal to 1,000. And each integer of the list should be less than or equal to 100.
Output
The output should be the minimum time that all the people can cross the bridge.
Sample Input
1 2
Sample Output
2
HINT
Greedy
过桥问题!主要参考:http://blog.csdn.net/morewindows/article/details/7481851
class Solution {
private:
int _bridge(vector<int> &v, int n) {
if (n == ) return ;
if (n == ) return v[];
if (n == ) return v[];
if (n == ) return v[] + v[] + v[];
int res = ;
int a = v[], b = v[], x = v[n-], y = v[n-];
if ( * b > a + x) res += * a + x + y;
else res += a + * b + y;
return res + _bridge(v, n - );
}
public:
int bridge(vector<int> &v) {
sort(v.begin(), v.end());
return _bridge(v, v.size());
}
};
/**************************************************************
Problem: 45
User: easonliu
Language: C++
Result: Accepted
Time:1 ms
Memory:2708 kb
****************************************************************/
[MeetCoder] Crossing Bridge的更多相关文章
- Clock Crossing Adapter传输效率分析 (Latency增加,传输效率降低)
原创By DeeZeng [ Intel FPGA笔记 ] 在用Nios II测试 DDR3时候发现一个现象 (测试为:写全片,读全片+比对) 用单独的PLL产生时钟(200MHz)驱动 Nios I ...
- D - Bridge
n people wish to cross a bridge at night. A group of at most two people may cross at any time, and e ...
- poj-3404 Bridge over a rough river Ad Hoc
Bridge over a rough river POJ - 3404 Bridge over a rough river Time Limit: 1000MS Memory Limit: 65 ...
- 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 ...
- [LeetCode] Self Crossing 自交
You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to th ...
随机推荐
- C# 将一个string数组转换为int数组
int[] channelCIdArr = Array.ConvertAll(channelIdStr.Split(','),s=>int.Parse(s));
- OpenHaptics编程环境搭建
SensAble Technologies公司是3D可触摸(力反馈)解决方案和技术领域中的领先开发商,其解决方案和技术不仅使用户能够看到并听到屏幕计算机应用,还可以对该应用进行实际“感应”.该公司的P ...
- VTK中导入并显示STL、3DS文件
VTK(visualization toolkit)是一个开源的免费软件系统,主要用于三维计算机图形学.图像处理和科学计算可视化.VTK是在三维函数库OpenGL 的基础上采用面向对象的设计方法发展起 ...
- django之创建第8-1个项目-数据库之增删改查/数据库数据显示在html页面
1.为test.DB数据库预先创建下面数据 1 张三 16 2015-01-02 12 李四 17 2015-01-04 13 王五 14 ...
- java 增量运算符
//java 增量运算符 public class Test16{ public static void main(String args[]) { int x1=10; x1+=3; //x1=x1 ...
- 用Redis的zset防御Session Flood
zset (Sorted Set)是set的升级版本, 在set的基础上增加了一个顺序(或者权重)值属性, 属性在添加修改元素时候可以指定. 每次指定后zset会自动重新按新的值调整顺序. 可以理解为 ...
- 删除草稿信息之后 threads表的数据没有发生变化
现象描述: 无论是用_id来删除草稿信息,还是用thread_id和type来删除草稿,草稿都可以删除掉,但是threads表中的数据都没有发生变化. private void removeDraft ...
- web-app_2_5.xsd内容
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://w ...
- Easyui入门视频教程 第10集---Messager的使用
Easyui入门视频教程 第10集---Messager的使用 <script type="text/javascript"> function show(){ $.m ...
- errno.h - C Error Codes in Linux
All the Linux/C error codes are listed below. I occasionally google C error codes, but always end up ...