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 ...
随机推荐
- tornado服务器运行django应用
在jumpserver项目中看到的 def main(): from django.core.wsgi import get_wsgi_application import tornado.wsgi ...
- Find the Clones Trie Tree
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8306 Accepted: 3130 Description Doubl ...
- 一个简单的ant应用
<pre name="code" class="html"><?xml version="1.0" encoding=&q ...
- WPF带cookie get/post请求网页,下载文件,图片,可保持会话状态
直接写成啦一个MyNet.cs类方便使用 get/post方法请求 //get请求 MyNet.SendRequest("http://www.baidu.com"); //pos ...
- 【Cocos2dx】资源目录,播放背景音乐,导入外部库
在Cocos2dx中播放背景音乐是一件非常easy的事情,就一行代码,可是首先要导入Cocos2dx的音频引擎cocosDenshion. cocosDenshion对cocos2dproject提供 ...
- iOS模仿微信的那个视频眼睛动画
咳咳,费死老劲把这个动画搞出来了. 欢迎批评指正 github URL: https://git.oschina.net/momochao/WeChatEyeDemo
- GET,POST,PUT,DELETE的区别 和 用法
Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而HTTP ...
- Head First 设计模式 —— 工厂模式与工厂方法
1. 实例化对象的方法 制造对象的方法不只 new 操作符一种.且实例化这个动作不应该总是公开地进行,还有初始化常常造成耦合问题.由此提出的工厂模式以进一步封装实例化的活动,且避免对象初始化时的可能产 ...
- JavaScript Simple
ylbtech-JavaScript: 1.返回顶部 1. 2. 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http: ...
- html页面、canvas导出图片
背景:项目现场提出将一个html做的图形页面导出为一张图片的需求,在网上搜了一下,发现都不是很全面,所以综合了很多大神的帖子,自己再次封装,以适用项目需求. 所需js库:html2canvas.js( ...