#include<iostream>
#include<algorithm>
using namespace std;
int n,a[];
int main()
{
int t,i;
cin>>t;
while(t--)
{
int f=;
//每次f归0
cin>>n; for(i=;i<=n;i++)
cin>>a[i];
sort(a,a+n+);
while(n)
{
if(n==)
{
f+=a[];
break;
}
if(n==)
{
f+=a[];
break;
}
if(n==)
{
f+=a[]+a[]+a[];
break;
}
if(n>)
{
if(*a[]>(a[]+a[n-]))
f+=*a[]+a[n]+a[n-];
else
f+=*a[]+a[]+a[n];
n=n-;
//注意循环
}
}
cout<<f<<endl;
}
return ;
}

Description

A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has a different rowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. The first line of each case contains N, and the second line contains N integers giving the time for each people to cross the river. Each case is preceded by a blank line. There won't be more than 1000 people and nobody takes more than 100 seconds to cross.

Output

For each test case, print a line containing the total number of seconds required for all the N people to cross the river.

Sample Input

1
4
1 2 5 10

Sample Output

17

程序分析:

这是一个过河坐船问题,一共有两个策略
①最快和次快过去,最快回;最慢和次慢过去,次快回,最快的和次快的过去,t1=a[1]+a[0]+a[n-1]+a[1]+a[1]。
②最快和最慢过去,最快回;最快和次快过去,最快回,最快的和次慢的过去,t2=a[n-1]+a[0]+a[1]+a[0]+a[n-2]。
选择两者中用时较少的一个策略执行,判断t1与t2大小,只需要判断2a[1]是否大于a[0]+a[n-2],如此便将最慢和次慢送过河,对剩下n-2个人循环处理。注意当n=1、n=2、n=3时直接相加处理即可.

POJ1700(过河问题)的更多相关文章

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

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

  2. 过河问题(POJ1700)

    题目链接:http://poj.org/problem?id=1700 解题报告: 1.贪心算法,每次过两个速度最慢的人,抵消那个较慢的人的时间. #include <stdio.h> # ...

  3. POJ1700 【经典过河问题,贪心】

    题意: n个人过河, 船每次只能坐两个人, 然后船载每个人过河的所需时间不同, 问最快的过河时间. 思路: 仅仅启发一下思维: 我相信很多人一下子的想法就会有,每次最快和那些慢的过去,然后让最快一直来 ...

  4. POJ-1700 Crossing River---过河问题(贪心)

    题目链接: https://vjudge.net/problem/POJ-1700 题目大意: 有N个人要渡河,但是只有一艘船,船上每次最多只能载两个人,渡河的速度由两个人中较慢的那个决定,小船来回载 ...

  5. 贪心算法-过河问题 pojo1700

    过桥问题: 黑夜,只有一只手电筒 A过桥需要1s B过桥需要3s C过桥需要5s D过桥需要8s E过桥需要12s 求最小过桥时间 贪心算法: 从最大的开始过去,最小的两个做为辅助. 假如左岸人数为2 ...

  6. [LeetCode] Frog Jump 青蛙过河

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  7. [codevs1105][COJ0183][NOIP2005]过河

    [codevs1105][COJ0183][NOIP2005]过河 试题描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青 ...

  8. A*算法 -- 八数码问题和传教士过河问题的代码实现

    前段时间人工智能的课介绍到A*算法,于是便去了解了一下,然后试着用这个算法去解决经典的八数码问题,一开始写用了挺久时间的,后来试着把算法的框架抽离出来,编写成一个通用的算法模板,这样子如果以后需要用到 ...

  9. 袋鼠过河---DP

    题目:一只袋鼠要从河这边跳到河对岸,河很宽,但是河中间打了很多桩子,每隔一米就有一个,每个桩子上都有一个弹簧,袋鼠跳到弹簧上就可以跳的更远,每个弹簧力量不同,用一个数字代表它的力量,如果弹簧力量为5, ...

随机推荐

  1. android程序报错“error launching activity com.android.ddmlib.shellcommandunresponsiveexception”的解决方式

    今天在调试android程序的时候,因为是NDK开发,要先编译.so库再打包下载到android模拟器,所以花费的时间比較长.控制台就会报例如以下错误: error launching activit ...

  2. Oracle存储过程Procedure语法及案例

    create or replace procedure replace(desstr in varchar2, replacestr in varchar2, tablename in varchar ...

  3. 解决Jedis数据读取乱码问题

    现象 同一套代码,同一个数据源,不同的操作系统,在OSX上数据提取编码正常,而Ubuntu上拉取数据乱码,数据拉取代码如下. @Override public List<String> m ...

  4. ;(function($,window,undefined){})(jQuery,window)详细解析————借助B5教程解析自己整理了一下

    在jquery插件中我们经常看到以下这段代码 ;(function ( $, window, document, undefined ){ //函数体内具体代码 })(jQuery, window,d ...

  5. gets scanf以及缓冲区域的问题

    1:scanf scanf会忽略开头所有的空格,并以Space Enter Tab 结束输入, 不会舍弃最后的回车符(即回车符会残存在缓冲区域中) 2:getchar getchar以Enter结束, ...

  6. CButtonST的用法详解【转】

    在想使用CButtonST的工程中加入BtnST.h.BtnST.cpp.BCMenu.h.BCMenu.cpp4个文件.2个类. 1. 在按钮上加入Icon,使Icon和文字同时显示 假设按钮ID为 ...

  7. STL之map和multimap(关联容器)

    map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响.自动建立Key - value的对应,对于迭代器来说,可以修改实值,而不能修改key. ...

  8. YII2 使用js

    1.在 /backend/assets/ 中新建一个文件 CollectionAsset.php <?php /** * @link http://www.yiiframework.com/ * ...

  9. http://riddle.arthurluk.net walkthrough

    MSVFMyU4MCU4MWh0dHAlM0ElMkYlMkZyaWRkbGUuYXJ0aHVybHVrLm5ldCUyRnN0YWdlb25lLnBocCUwRCUwQTIlRTMlODAlODFo ...

  10. CQRS架构设计及其实现

    CQRS架构设计及其实现 一.为什么要实践领域驱动? 近一年时间我一直在思考一个问题:“如何设计一个松耦合.高伸缩性.易于维护的架构?”.之所以有这样的想法是因为我接触的不少项目都是以数据库脚本来实现 ...