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. latex输入希腊字母

    \alpha产生字符α;\beta产生字符β:\gamma产生字符γ:\delta产生字符δ;\epsilon产生字符ε; \zeta产生字符ζ:\eta产生字符η;\theta产生字符9; \iot ...

  2. Redhat Linux 性能 - 内置的 sar

    缺省 / 默认 Redhat Linux 会自动使用 sar 采集系统性能信息,并记录到 /var/log/sa 每 10分钟采集一次, 记录 CPU / Memory / Disk / Networ ...

  3. java 内部类1

    内部类: 常用的情景:一个类要经常访问另一个类的成员. 举个例子: class人 { 血,氧气... class 心脏{...} } 内部类的优势: 成员内类,作为外部类的成员,可以方便随意的访问外部 ...

  4. linux笔记:软件包管理-软件包简介

    软件包分类: 1.源码包: 2.二进制包(在centos里就是RPM包.和源码包的区别在于已经经过编译.) 源码包的优点和缺点: RPM包的优点和缺点:

  5. noip2016题解汇总

    [uoj#260]玩具谜题 传送门 http://uoj.ac/problem/260 分析 模拟. int n,m; int dir[N]; char nam[N][L]; int a[M],s[M ...

  6. AC自动机 & Fail树 专题练习

    Fail树就是AC自动机建出来的Fail指针构成的树. [bzoj3172][xsy1713]单词 题意 给定一些单词,求每个单词在所有单词里面的出现次数. 分析 构建Fail树,记录每个单词最后一个 ...

  7. jq--回到顶部

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. 为设计师准备的 20 个新的免费 PSD 模板

    设计师们每天的工作经常要用到各种模板,本文推荐 20 个新的免费 PSD 模板,可节省设计时间. Blueprint Notice PSD Wooden Drawer Icon Minimal Clo ...

  9. 工程目录 Java/Web/Maven

    jar包和war包的区别 war是一个web模块,其中需要包括WEB-INF,是可以直接运行的WEB模块.而jar一般只是包括一些class文件,在声明了Main_class之后是可以用java命令运 ...

  10. poj------(3468)A Simple Problem with Integers(区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 60745   ...