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. 数位dp题集

    题集见大佬博客 不要62 入门题,检验刚才自己有没有看懂 注意一些细节. 的确挺套路的 #include<bits/stdc++.h> #define REP(i, a, b) for(r ...

  2. 洛谷 P2813 母舰

    题目描述 在小A的星际大战游戏中,一艘强力的母舰往往决定了一场战争的胜负.一艘母舰的攻击力是普通的MA(Mobile Armor)无法比较的. 对于一艘母舰而言,它是由若干个攻击系统和若干个防御系统组 ...

  3. fzu 2132

    #include<stdio.h> double h; double tt; void s(long long m,long long n) { long long i,j,sum; j= ...

  4. MySQL架构优化实战系列3:定时计划任务与表分区

    原创 2016-07-08 汤抗 DBAplus社群 一 定时计划任务 1.概论   mysql计划任务可以定时更新数据库表或者做大文件的汇总表.   2.配置 开启计划任务 SHOW VARIABL ...

  5. [jQuery]jQuery获取URL参数

    // jQuery url get parameters function [获取URL的GET参数值]// <code>// var GET = $.urlGet(); //获取URL的 ...

  6. OpenStack开发基础-oslo.config

    The cfg Module cfg Module来自于OpenStack中的重要的基础组件oslo.config,通过cfg Module能够用来通过命令行或者是配置文件来配置一些options,对 ...

  7. sikuli类、函数使用参考java doc

    sikuli类.函数使用可以参考java  dochttp://doc.sikuli.org/javadoc/ http://stackoverflow.com/questions/9568612/s ...

  8. 刚開始学习的人非常有用:struts2中将jsp数据传到action的几种方式

    先给上struts.xml代码: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE strut ...

  9. CentOS安装、配置APR和tomcat-native

    APR:Apache Portable Run-time libraries,Apache可移植执行库 在早期的Apache版本号中.应用程序本身必须可以处理各种详细操作系统平台的细节,并针对不同的平 ...

  10. POJ2228 Naptime 环形DP

    题目大意:牛在第i个小时睡觉能够恢复U[i]点体力.睡觉时第一小时不恢复体力.一天的N小时连着下一天的1小时.求能够恢复体力的和的最大值. 定义DP[i][j][0]为前i个小时休息了j个小时,i小时 ...