Safe Passage

Photo by Ian Burt

A group of friends snuck away from their school campus, but now they must return from the main campus gate to their dorm while remaining undetected by the many teachers who patrol the campus. Fortunately, they have an invisibility cloak, but it is only large enough to cover two people at a time. They will take turns as individuals or pairs traveling across campus under the cloak (and by necessity, returning the cloak to the gate if others remain). Each student has a maximum pace at which he or she is able to travel, yet if a pair of students are walking under the cloak together, they will have to travel at the pace of the slower of the two. Their goal is to have everyone back at the dorm as quickly as possible.

As an example, assume that there are four people in the group, with person A able to make the trip in $1$ minute, person B able to travel in $2$ minutes, person C able to travel in $7$ minutes, and person D able to travel in $10$ minutes. It is possible to get everyone to the dorm in $17$ minutes with the following plan:

– A and B go from the gate to the dorm together

(taking $2$ minutes)

– A returns with the cloak to the gate

(taking $1$ minute)

– C and D go from the gate to the dorm together

(taking $10$ minutes)

– B returns with the cloak to the gate

(taking $2$ minutes)

– A and B go from the gate to the dorm together

(taking $2$ minutes)

Input

The input is a single line beginning with an integer, $2 \leq N \leq 15$. Following that are $N$ positive integers that respectively represent the minimum time in which each person is able to cross the campus if alone; these times are measured in minutes, with each being at most $5\, 000$. (It is a very large campus!)

Output

Output the minimum possible time it takes to get the entire group from the gate to the dorm.

Sample Input 1 Sample Output 1
2 15 5
15
Sample Input 2 Sample Output 2
4 1 2 7 10
17
Sample Input 3 Sample Output 3
5 12 1 3 8 6
29

题意

这是一道很经典的撑伞问题吧,有n个人,每个人带一个时间,为这个人走起点到终点的时间,然后斗篷每次只能最多两个人撑,斗篷只有一件,过去的时候时间算慢那个人的,过去了需要有人送回来,

求所有人都到终点的最少时间

思路

按照样例解释,我们很容易想到,第一次肯定是最快的两个人先过去,然后,每次的选择都有两种策略,最快那个回来,把最慢的和次慢依次送过去,或者次快的回来,让最慢的和次慢的过去,我们要取这两种策略中最少时间的,最后剩下两个人的时候,就是最快的回来,第二第三快的过去,第二快的回来。

代码

#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
int a[];
scanf("%d", &n);
for(int i = ; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
int i, cnt = ;
for(i = n - ; i > ; i -= )
cnt += min(a[] + a[] + a[] + a[i], a[] + a[] + a[i] + a[i - ]);
if(i == )
cnt += a[] + a[] + a[];
else if(i == )
cnt += a[];
else
cnt += a[];
printf("%d\n", cnt);
}

Kattis -Safe Passage(撑伞问题)的更多相关文章

  1. RainCup_No.1

    Rain杯No.1 初见篇 本系列故事以及人名地名等纯属虚构,如有雷同,纯属巧合 在极东之地,有一个岛国,与岛国隔了一个海域有一个古老的国度,天朝.天朝T镇有个少年叫小S,故事从小S与少女Rain的相 ...

  2. 2017腾讯实习生Android客户端开发面试总结

    欢迎访问我的个人博客转发请注明出处:http://wensibo.top/2017/04/13/2017Tencent_review/ 前言 先做个自我介绍,本人大三狗一枚,就读的是广州一个普通的一本 ...

  3. 日记整理---->2017-05-14

    学习一下知识吧,好久没有写博客了.如果他总为别人撑伞,你又何苦非为他等在雨中. 学习的知识内容 一.关于base64的图片问题 byte[] decode = Base64.base64ToByteA ...

  4. Codeforces 988F Rain and Umbrellas(DP)

    题目链接:http://codeforces.com/contest/988/problem/F 题目大意: 有三个整数a,n,m,a是终点坐标,给出n个范围(l,r)表示这块区域下雨,m把伞(p,w ...

  5. amazeui学习笔记--css(常用组件16)--文章页Article

    amazeui学习笔记--css(常用组件16)--文章页Article 一.总结 1.基本使用:文章内容页的排版样式,包括标题.文章元信息.分隔线等样式. .am-article 文章内容容器 .a ...

  6. amazeui学习笔记--css(布局相关3)--辅助类Utility

    amazeui学习笔记--css(布局相关3)--辅助类Utility 一.总结 1.元素清除浮动: 添加 am-cf 这个 class 即可 2.水平滚动: .am-scrollable-horiz ...

  7. NOIP退役记

    10.10 想着自己再过一个月就要退役了,真叫人心酸.想到徐志摩的诗: "悄悄地,我走了,正如我悄悄的来,我挥一挥衣袖,不带走一片云彩." 学了这么久的OI,感觉真的就像诗里讲的一 ...

  8. N4复习考试总结

    一つ(ひとつ) 半分(はんぶん) 煙草(たばこ)を吸う(すう) 玄関(げんかん) ナイフ(刀)     財布(さいふ) 浅い(あさい) 薄い(うすい) 牛乳(ぎゅうにゅう) 皿(さら) 七日(なのか) ...

  9. RFC 8684---TCP Extensions for Multipath Operation with Multiple Addresses

    https://datatracker.ietf.org/doc/rfc8684/?include_text=1 TCP Extensions for Multipath Operation with ...

随机推荐

  1. Intro.js 分步向导插件使用方法

    简介 为您的网站和项目提供一步一步的.更好的介绍 Intro.js 目前兼容 Firefox.Chrome 和 IE8,不兼容 IE6 和 IE7,后续版本将会提供更好的兼容. 在线演示及下载 在线演 ...

  2. 9.在idea中创建Maven项目

    1.新建maven WEB项目 打开-File-New-Project 点击NEXT 2.选择项目组,并给项目命名 3.选择maven路径 添加的配置为 archetypeCatalog=intern ...

  3. js给对象onclick事件赋值

    1)当方法没有参数时,赋值可以直接用onclick = 方法名 window.onload = function() { $('btnTest').onclick = test; } function ...

  4. POJ 1703 Find them, Catch them(并查集高级应用)

    手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...

  5. netsh wlan set hostednetwork 之后如何删除掉 配置 及终端无法获取IP的解决方法

    来源 微软 技术支持网站 仅仅禁用hostednetwork 是不行的,配置其实还在,彻底删除 配置可以按照如下操作进行: net stop wlansvcGet-ItemProperty " ...

  6. [GraphQL] Apollo React Mutation Component

    In this lesson I refactor a React component that utilizes a higher-order component for mutations to ...

  7. emacs 搭建racket开发环境

    emacs 搭建racket开发环境 emacs下搭建开发racket的环境,笔者之前用过下面两种模式:geiser和racket-mode.相对而言,后一种方式要显得简单.本文主要介绍后一种方式环境 ...

  8. BNU 34986 Football on Table

    "Bored? Let's play table football!" The table football is played on a rectangular table, u ...

  9. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第1章节--SharePoint 2013 介绍 SharePoint 2013 平台

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第1章节--SharePoint 2013 介绍 SharePoint 2013 平台         SharePoin ...

  10. struts1——静态ActionForm与动态ActionForm

    在struts1中,我们能够使用ActionForm来获取从client端提交上来的数据.并通过action配置中的name属性.将某个ActionForm配置到某次请求应答的Action中.作为本次 ...