【题解】Crossing River
题目描述
几个人过河,每次过两人一人回,速度由慢者决定,问过河所需最短时间。
输入格式
输入t组数据,每组数据第1行输入n,第2行输入n个数,表示每个人过河的时间。
输出格式
输出t行数据,每行1个数,表示每组过河最少时间。
输入样例
1
4
1 2 5 10
输出样例
17
题解
容易想到,我们要尽可能利用速度快的人,且尽可能先把慢的人送走。
据此可以想到贪心策略:设$n$个人的时间升序排列为$a_{1}$到$a_{n}$,那么我们就让第$n$个人把第$1$个人运到对岸,再让第$1$个人自己回来,然后$n = n - 1$,继续上述过程。
但是当我们运了第$(n - 1)$和第$n$个人时,时间为$a_{1} + a_{1} + a_{n - 1} + a_{n}$,此时还有一种策略,即先让第$2$个人运第$1$个人过去,第$1$个人回来,再让第$n$个人运第$(n-1)$个人过去,然后第$2$个人回来,这样总时间就为$a_{1} + a_{2} + a_{2} + a_{n}$。
当$a_{1} + a_{n - 1} > a_{2} + a_{2}$时,实际第二种方案好过第一种方案,每次去最优方案即可。
#include <iostream>
#include <algorithm> #define MAX_N (100 + 5) using namespace std; int T;
int n;
int a[MAX_N];
int ans; int main()
{
cin >> T;
while(T--)
{
ans = ;
cin >> n;
for(register int i = ; i <= n; ++i)
{
cin >> a[i];
}
sort(a + , a + n + );
while(n >= )
{
ans += min(a[] + a[] + a[n - ] + a[n], a[] + a[] + a[] + a[n]);
n -= ;
}
if(n <= ) ans += a[n];
else ans += a[] + a[] + a[];
cout << ans << "\n";
}
return ;
参考程序
【题解】Crossing River的更多相关文章
- Crossing River
Crossing River 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26251 题意: N个人希望去过 ...
- POJ 1700 Crossing River (贪心)
Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9585 Accepted: 3622 Descri ...
- Crossing River(1700poj)
Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9919 Accepted: 3752 De ...
- poj 1700 Crossing River 过河问题。贪心
Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9887 Accepted: 3737 De ...
- poj1700--贪心--Crossing River
Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12260 Accepted: 4641 D ...
- Crossing River 题解(贪心)
题目链接 题目大意 t组数据(t<=20) 给你n个人(n<=1000)过河,每个人都有权值,一条船,每次船最多运2个人,每次的花费为两个人的较大花费 求所有人都过河需要的最小花费 题目思 ...
- POJ1700:Crossing River(过河问题)
POJ1700 题目链接:http://poj.org/problem?id=1700 Time Limit:1000MS Memory Limit:10000KB 64bit IO ...
- poj-1700 crossing river(贪心题)
题目描述: A group of N people wishes to go across a river with only one boat, which can at most carry tw ...
- Crossing River POJ过河问题
A group of N people wishes to go across a river with only one boat, which can at most carry two pers ...
随机推荐
- 爬取拉勾网python工程师的岗位信息并生成csv文件
转载自:https://www.cnblogs.com/sui776265233/p/11146969.html 代码写得很好,但是目前只看得懂前一部分 一.爬取和分析相关依赖包 Python版本: ...
- HDU 2815 Mod Tree (扩展 Baby Step Giant Step )
Mod Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- Git忽略已经跟踪的文件 转摘:http://blog.csdn.net/huguohuan/article/details/7380349
某工程project用Git管理代码,但是在他的根目录下有个配置文件,比如project.iws是不需要git每次跟踪它的修改记录的. 一般做法是在.gitignore文件中添加一行 project. ...
- 【目录】sql server 架构篇系列
随笔分类 - sql server 架构篇系列 sql server 高可用镜像 摘要: 一.什么是数据库镜像 基本软件的高可用性解决方案 快速的故障转移恢复(3秒转移),低硬件成本 基于数据库级别的 ...
- Java调用DB的存储过程
2015/12/7 使用数据库存储过程的java代码: try { con = (Connection) DBProxy.getConnection(null); ...
- hdu 5435 A serious math problem
A serious math problem Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 【问题解决方案】git/github本地和远程仓库的重命名
参考: CSDN博文:在Github上重命名仓库 背景: 偶然终于看到一条规范里写着: "通常(注意是通常,尤其是 Web 相关的项目) repo 的命名用小写英文,多个字母之间用连字符(比 ...
- 蛋疼的 403 Forbidden You don’t have permission to access / on this server.
参考博文: a.http://www.linuxidc.com/Linux/2016-09/134827.htm 这个解释挺好 昨天配置新服务器:以为自己老手 就一步到位结果一直出现 403 For ...
- CentOS7 配置163 yum源(详细步骤)
CentOS7 配置163 yum源 1)下载repo文件 wget http://mirrors.163.com/.help/CentOS7-Base-163.repo 2)备份并替换系统的r ...
- BZOJ 2238: Mst DFS序+KDtree
明明可以用二维数点来做啊,网上为什么都是树剖+线段树呢 ? code: #include <cstdio> #include <cstring> #include <al ...