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

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
  freopen("in.txt","r",stdin);
  int speed[1000];

  int t,n,time;
  cin>>t;
  int i,j;
  for(i=0;i<t;i++)
  {
    cin>>n;
    memset(speed,0,sizeof(speed));
    for(j=0;j<n;j++)cin>>speed[j];
      sort(speed,speed+n); //从小到大排序
    time=0;
    while(n>3)
    {
      if(speed[1]*2 > speed[0]+speed[n-2])
      time=time+speed[0]+speed[0]+speed[n-1]+speed[n-2];
      else time=time+speed[1]+speed[0]+speed[n-1]+speed[1];
      n=n-2;
    }
    if(n==3)
    time=time+speed[0]+speed[1]+speed[2];
    else if(n==2)
    time=time+speed[1];
    else if(n==1)
    time=time+speed[0];
    cout<<time<<endl;
    }
return 0;
}

POJ 1700 F(Contest #3)的更多相关文章

  1. poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)

    /* poj 2187 Beauty Contest 凸包:寻找每两点之间距离的最大值 这个最大值一定是在凸包的边缘上的! 求凸包的算法: Andrew算法! */ #include<iostr ...

  2. POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包)

    POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包) Description N (1 ≤ N ...

  3. POJ 1719 Shooting Contest(二分图匹配)

    POJ 1719 Shooting Contest id=1719" target="_blank" style="">题目链接 题意:给定一个 ...

  4. POJ 2187 Beauty Contest【凸包周长】

    题目: http://poj.org/problem?id=1113 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  5. POJ - 3660 Cow Contest 传递闭包floyed算法

    Cow Contest POJ - 3660 :http://poj.org/problem?id=3660   参考:https://www.cnblogs.com/kuangbin/p/31408 ...

  6. poj 1700

    http://poj.org/problem?id=1700 题目大意就是一条船,有N个人需要过河,求N个人最短过河的时间 #include <stdio.h> int main() { ...

  7. poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)

    链接:http://poj.org/problem?id=2187 Description Bessie, Farmer John's prize cow, has just won first pl ...

  8. POJ 3660 Cow Contest

    题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  9. poj 1719 Shooting Contest

    http://poj.org/problem?id=1719 Shooting Contest Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

随机推荐

  1. Maven最佳实践:划分模块

    http://juvenshun.iteye.com/blog/305865 ************************************* "分天下为三十六郡,郡置守,尉,监& ...

  2. python 打包与部署

    环境:win10.eclipse-jee-mars.python2.7 打包在linux上进行安装测试 1.1 打包 项目目录结构如下: 打包对象:utils.reg 在P1项目的顶级目录,也就是ut ...

  3. ManyToMany关联方式----

    http://blog.csdn.net/sinat_18882775/article/details/51171427 好的博客 系列文章总结

  4. \r\n在php中有什么用

    \t \r \n 这是不是表现在html上的,你要看源代码才能看到效果,直接显示页面换行用<br />就可以了.\t \r \n 的话,你要用函数nl2br 才能在html页面上看到.写日 ...

  5. php正则:匹配(),{},[]小括号,大括号,中括号里面的内容

    php正则:匹配(),{},[]小括号,大括号,中括号里面的内容 比如有一段字符: $s='60c8 {"code":"200","message&q ...

  6. SQL Server Native Client 安装方法

    在 server 2008 r2 里面搜索到:  sqlncli.msi  安装即可

  7. 【转】Android设置虚线、圆角、渐变

      Android虚线圆角渐变 有图又真相,先上图再说. 点击效果: 设置虚线: <?xml version="1.0" encoding="utf-8" ...

  8. xcode6 framework missing submodule xxx 警告

    xcode6 framework missing submodule xxx 警告 从xcode6开始,iOS可以直接创建生成framework了 如: 创建 framework 项目,TFKit.f ...

  9. 华为面试题——一道关于指针方面的编程题(C/C++)

    int A[nSize],其中隐藏着若干0,其余非0整数,写一个函数int Func(int* A, int nSize),使A把0移至后面,非0整数移至数组前面并保持有序,返回值为原数据中第一个元素 ...

  10. 设置DIV最小高度以及高度自适应随着内容的变化而变化

    <!--退租账单--> <div id="bilsli" onmouseover="showBill(this)"> #bilsli{ ...