#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. 算法导论 6.5.9 堆实现K路归并问题

    问题: 设计一个时间复杂度为O(NlogK)的算法,它能够将K个有序链表合并为一个有序链表,这里的N为所有输入链表包含的总的元素个数 分析: 该问题为经典的利用堆完成K路归并的问题: 当K个序列满足一 ...

  2. PHP中用到的一些字符串函数

    /*常用的字符串输出函数   *   * echo()   输出字符串   * print()  输出一个或多个字符串   * die()  输出一条信息,并退出当前脚本   * printf()  ...

  3. WinRT Toolkit 介绍--Control篇

    WinRT toolkit是组针对Windows Runtime XAML开发的一系列Control,extension和helper类.它和Windows Phone Toolkit一样,也是由很多 ...

  4. HTML之框架(frameest、ifram)

    1.frameset 最外层的去掉body用frameset <frameset rows="100,*"> 上下排版拆开,第一行高度占100,第二行*占剩下的 :co ...

  5. BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )

    考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...

  6. php的迭代器

    接口Iterator 主要需要实现的方法: abstract public mixed current ( void ) abstract public scalar key ( void ) abs ...

  7. 调用 sphinx-build生成HTML文件

    安装 Sphinx $ easy_install sphinx Searching for sphinx Reading http://pypi.python.org/simple/sphinx/ R ...

  8. 转: 用css把图片转为灰色图

    小tip: 使用CSS将图片转换成黑白(灰色.置灰) by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.co ...

  9. CMake学习小结

    假定有vegagis工程,工程的目录结构如下: #--vegagis#  |--src 源文件目录#     |--gui 界面工程,输出类型:dll,依赖于QT的QtCore.QtGui.QtXml ...

  10. 将HDC保存为BMP文件

    HDC在MSDN中的全称为:The handle of device context.通常,我们都是用来做相应的显示操作.        熟悉WIN32的朋友对于其应该不会陌生,经常采用GetDC,G ...