Kattis -Safe Passage(撑伞问题)
Safe Passage
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(撑伞问题)的更多相关文章
- RainCup_No.1
Rain杯No.1 初见篇 本系列故事以及人名地名等纯属虚构,如有雷同,纯属巧合 在极东之地,有一个岛国,与岛国隔了一个海域有一个古老的国度,天朝.天朝T镇有个少年叫小S,故事从小S与少女Rain的相 ...
- 2017腾讯实习生Android客户端开发面试总结
欢迎访问我的个人博客转发请注明出处:http://wensibo.top/2017/04/13/2017Tencent_review/ 前言 先做个自我介绍,本人大三狗一枚,就读的是广州一个普通的一本 ...
- 日记整理---->2017-05-14
学习一下知识吧,好久没有写博客了.如果他总为别人撑伞,你又何苦非为他等在雨中. 学习的知识内容 一.关于base64的图片问题 byte[] decode = Base64.base64ToByteA ...
- Codeforces 988F Rain and Umbrellas(DP)
题目链接:http://codeforces.com/contest/988/problem/F 题目大意: 有三个整数a,n,m,a是终点坐标,给出n个范围(l,r)表示这块区域下雨,m把伞(p,w ...
- amazeui学习笔记--css(常用组件16)--文章页Article
amazeui学习笔记--css(常用组件16)--文章页Article 一.总结 1.基本使用:文章内容页的排版样式,包括标题.文章元信息.分隔线等样式. .am-article 文章内容容器 .a ...
- amazeui学习笔记--css(布局相关3)--辅助类Utility
amazeui学习笔记--css(布局相关3)--辅助类Utility 一.总结 1.元素清除浮动: 添加 am-cf 这个 class 即可 2.水平滚动: .am-scrollable-horiz ...
- NOIP退役记
10.10 想着自己再过一个月就要退役了,真叫人心酸.想到徐志摩的诗: "悄悄地,我走了,正如我悄悄的来,我挥一挥衣袖,不带走一片云彩." 学了这么久的OI,感觉真的就像诗里讲的一 ...
- N4复习考试总结
一つ(ひとつ) 半分(はんぶん) 煙草(たばこ)を吸う(すう) 玄関(げんかん) ナイフ(刀) 財布(さいふ) 浅い(あさい) 薄い(うすい) 牛乳(ぎゅうにゅう) 皿(さら) 七日(なのか) ...
- 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 ...
随机推荐
- 解决windows文件在linux系统中显示乱码的问题
问题: 在Windows下用matlab写的代码(.m)到Linux(centos)下,注释的中文全是乱码. 原因: Windows下默认使用的是GB2312编码,Linux默认使用的是UTF-8. ...
- 解决HTML select控件 设置属性 disabled 后无法向后台传值的方法
大家都知道有时候修改数据的时候我们希望有一些数据是不可以修改的,通常情况下我们会将input框设置为 readonly , 但是 select 控件没有这个属性,需要使用另一个属性 disabled ...
- Appendix B: Netsh Command Syntax for the Netsh Firewall Context
11 out of 19 rated this helpful - Rate this topic Published: December 17, 2004 The following Netsh c ...
- sqlserver日志文件太大解决方法
SQL Server 的事务日志意外增大或充满的处理方法 事务日志文件Transaction Log File是用来记录数据库更新情况的文件,扩展名为ldf. 在 SQL Server 7.0 和 S ...
- CodeForces - 274A - k-Multiple Free Set
先上题目 k-Multiple Free Set time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- ZEAL--可以查看所有软件API的软件
windows平台上最强大的可以查看所有API的软件,从此再也不用去各个网站上扒了,而且有时还有Greate Wall 点我下载
- 洛谷—— P1097 统计数字
https://www.luogu.org/problem/show?pid=1097 题目描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*10^9).已知不相同的数 ...
- Linux用户管理之使用/bin/false和/usr/sbin/nologin拒绝用户登录及其功能分析(转)
/bin/nologin,/bin/false的意思是禁止某个用户登录. 比较常用的用法: #添加一个不能登录的用户 useradd -d /usr/local/apache -g apache -s ...
- 【cl】sikuli下载安装
前提条件: 1.请确保你已经安装java 6 JRE 32位版本(如果是java 7 或者是64位JRE 那是不被支持的) 2.请确定你已经卸载的先前的sikuli版本(尤其是0.10.x版本) 3. ...
- dwr异常处理
配置异常转换器: dwr.xml: <!-- 异常转换 --> <convert match="java.lang.Exception" converter=&q ...