#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. jdbc操作步骤和preparedStatment相比Statment的好处

    java操纵数据库封装了一组API,通过这组API可以透明的操作各种数据库,一般来讲,操纵数据库的步骤是: 一. try{ 1.加载数据库驱动 Class.forName("数据库驱动类&q ...

  2. HDU 4716 A Computer Graphics Problem

    A Computer Graphics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  3. BFG

    "/"应用程序中的服务器错误. 配置错误 说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误消息: 提供程序集合中不 ...

  4. c 计算Fibonacci数列:1,1,2,3,5,8,13……这题也是很经典。

    输出数字序列2/,/,/,/,/,/...,输出个数由键盘输入.注意输入使用scanf输入 比如: 输入 3输出为 / / / 输入 输出为 / / / / #include<stdio.h&g ...

  5. debian6 更新python版本到python3.3

    1.下载python3.3安装包 #wget wget --no-cookie --no-check-certificate --header "Cookie:gpw_e24=http%3A ...

  6. werkzeug中服务器处理请求的实现

    当成功建立好服务器后,接下来就是等待请求并处理请求通过路由分配给相应的视图函数了,以下是函数调用过程 -> self._handle_request_noblock() /usr/lib/pyt ...

  7. struts文件上传和下载

    文件上传 jsp中 <a href="/file/new.action">文件上传案例</a> fileaction中 @Override public S ...

  8. Hadoop HDFS分布式文件系统设计要点与架构

      Hadoop HDFS分布式文件系统设计要点与架构     Hadoop简介:一个分布式系统基础架构,由Apache基金会开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群 ...

  9. css网页自适应-1

    一."自适应网页设计"的概念 2010年,Ethan Marcotte提出了"自适应网页设计"(Responsive Web Design)这个名词,指可以自动 ...

  10. RGB,CMYK,HSB各种颜色表示的转换 C#语言

    Introduction Why an article on "colors"? It's the same question I asked myself before writ ...