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 ...
随机推荐
- springboot框架嵌入netty
1.pom.xml添加依赖 <dependency> <groupId>io.netty</groupId> <artifactId>netty-all ...
- Ubuntu Server下docker实战 02: docker进阶配置
在上一篇文章里<Ubuntu Server下docker实战 01: 安装docker>,我们已经把docker安装起来了,并运行了一个hello-world 这一篇,我们继续讲进阶配置. ...
- 08.Web服务器-1.浏览器访问服务器显示页面的网络模型步骤解析
- phpcms v9 邓士鹏(石家庄职业技术学院)
头部标题.关键词.描述调用: <title>{if isset($SEO['title']) && !empty($SEO['title'])}{$SEO['title'] ...
- python 对图片做垂直投影
Python 对图片做垂直投影 本文利用opencv对图片进行垂直投影,做出垂直投影图,大体思路:打开图片,灰度化,二值化,按列进行统计,新建一个大小和原图一样的图片,按列进行填充: cv2.cv.G ...
- 优化 RequireJS 项目(合并与压缩) 【已翻译100%】
英文原文:Optimize (Concatenate and Minify) RequireJS Projects 标签: RequireJS Node.js 参与翻译 (1人) : 裴宝亮 本文将演 ...
- 洛谷 P3178 BZOJ 4034 [HAOI2015]树上操作
题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a .操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 ...
- JAVA之StringBuffer测试样码
这类操作大同小异,但是,高手却能从String,StringBuilder,StringBuffer的应用场景和性能上,分析出其在JAVA编译和JVM上的实现过程差别. 我在CSDN上就看到一个高手分 ...
- 浅析IT系统监控方法和应用
浅析IT系统监控方法和应用 http://blog.csdn.net/zhangman117/article/details/35549363
- hdu 1011 树型dp
#include <cstdio> #include <iostream> #include <cstring> #include <vector> u ...