题目描述

  几个人过河,每次过两人一人回,速度由慢者决定,问过河所需最短时间。

输入格式

  输入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的更多相关文章

  1. Crossing River

    Crossing River 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26251 题意: N个人希望去过 ...

  2. POJ 1700 Crossing River (贪心)

    Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9585 Accepted: 3622 Descri ...

  3. Crossing River(1700poj)

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9919   Accepted: 3752 De ...

  4. poj 1700 Crossing River 过河问题。贪心

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9887   Accepted: 3737 De ...

  5. poj1700--贪心--Crossing River

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12260   Accepted: 4641 D ...

  6. Crossing River 题解(贪心)

    题目链接 题目大意 t组数据(t<=20) 给你n个人(n<=1000)过河,每个人都有权值,一条船,每次船最多运2个人,每次的花费为两个人的较大花费 求所有人都过河需要的最小花费 题目思 ...

  7. POJ1700:Crossing River(过河问题)

    POJ1700 题目链接:http://poj.org/problem?id=1700 Time Limit:1000MS     Memory Limit:10000KB     64bit IO ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Ubuntu18.10 编译libevent出现错误: creating symbolic link XXXXXX : Operation not supported

    今天在VirtualBox虚拟机下的Ubuntu18.10编译libevent源代码时,按照github中使用cmake方式: $ mkdir build && cd build $ ...

  2. BZOJ 4003 (可并堆)

    题面 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池. 这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖, 其中 fi &l ...

  3. JS 富文本编码、解码

    第一种 escape()和unescape()方法 escape() 方法能够把 ASCII之外的所有字符转换为 %xx 或 %uxxxx(x表示十六进制的数字)的转义序列.从 \u000 到 \u0 ...

  4. 分支结构case 语句语法

  5. 一、Json

    一.Json遍历模糊查询 你没admin 所以这句话报错. //1.写法 dynamic query = from user in jsonObject where (user.ToString(). ...

  6. 三步完成Source Insight 4.0 破解安装(转)

    转自:https://blog.csdn.net/biubiuibiu/article/details/78044232 三步完成Source Insight 4.0 破解安装   下载地址有更新,之 ...

  7. 微信小程序 textarea的placeholder层级过高 在弹层之上 bug解决方法

    微信小程序textarea的placeholder的层级一直都是一个神坑, 我们是没有办法将我们的弹层加大层级去盖过placeholder的, 所以要解决这个问题只能从另外的角度找思路 我的思路是 : ...

  8. #2560异或和问题 jdfz集训—秦岳

    题目描述 N个数字,要求选择M次,每次从N个数中选出两个数(Ai,Aj)(但不能和之前某次选择相同),此次选择的得分为Ai xor Aj. 求最大得分. 输入格式 第一行包含两个整数N,M 接下来一行 ...

  9. \ HTML5开发项目实战:照片墙

    html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  10. Eigen ,MKL和 matlab 矩阵乘法速度比较

    Eigen 矩阵乘法的速度  < MKL矩阵乘法的速度,MKL矩阵乘法的速度与matlab矩阵乘法的速度相差不大,但matlab GPU版本的矩阵乘法速度是CUP的两倍,在采用float数据类型 ...