题目描述

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

输入格式

  输入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. 阿里巴巴高级Java面试题(首发,70道)

    整理的70道阿里的Java面试题,都来挑战一下,看看自己有多厉害.下面题目都带超详细的解答,详情见底部. 1.java事件机制包括哪三个部分?分别介绍. 2.为什么要使用线程池? 3.线程池有什么作用 ...

  2. 删除文件时提示“找不到该项目”,怎么解决? 转摘自:http://jingyan.baidu.com/article/e4d08ffdf5ab470fd2f60df4.html

    故障现象:在使用Windows系统删除文件或者文件夹的时候,有时会出现“找不到该项目”的错误提示,可能再次“重试”也无济于事. 那么,接下来T库小编就为库友们简单概括一下出现该问题的原因. 故障原因: ...

  3. 【目录】sql server 进阶篇系列

    随笔分类 - sql server 进阶篇系列 sql server 下载安装标记 摘要: SQL Server 2017 的各版本和支持的功能 https://docs.microsoft.com/ ...

  4. 页面跳转到Area区域连接

    @Html.ActionLink("主页", "Index", new { controller = "Test", Action = &q ...

  5. 【学习总结】Python-3-风格各异的数值类型实例

    菜鸟教程-Python3-基本数据类型 可能是考点的各种形态的数值类型 int型:正数负数,八进制0开头,十六进制0x开头 float型:小数点的前后都可以没有数字,自动补全 complex型:虚部的 ...

  6. LeetCode Array Easy 283. Move Zeroes

    Description Given an array nums, write a function to move all 0's to the end of it while maintaining ...

  7. 二、kubernetes搭建集群

    一.准备工作 主要内容 .环境准备(2主机) .安装流程 .问题分析 .总结 环境配置(2主机) 系统:CentOS 7.3 x64 网络:局域网(VPC) 主机: master:172.16.0.1 ...

  8. Matplotlib_key_point

    Matplotlib官方入门教程: http://www.labri.fr/perso/nrougier/teaching/matplotlib/ 本文参考教程: http://codingpy.co ...

  9. 简单递归____Fibonacci数列

    #include <stdio.h> int fun(int x) { ||x==) ; else return fun(x-1)+fun(x-2); } int main() { int ...

  10. IronPython C#与Python相互调用

    ironphy  microsoft.scripting dll using System;using System.Collections.Generic;using System.Linq;usi ...