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. Could not obtain connection metadata

    用hibernate连接数据库出现错误 2010-3-16 17:23:39, 093 [main] WARN [org.hibernate.cfg.SettingsFactory] - Could ...

  2. Matlab求齐次方程的解

    % 求Ax=0的解: r=rank(A): x=null(A,r) 求出来x的是归一化后的解.

  3. import package的问题

    在新建class的时候除了名字还可以选择包名: 新建2个包名,然后在不同的包里写2个同名的类, 程序中导入另外一个包 package com.hs;import com.hy.Father; 当直接使 ...

  4. (x&y) + ((x^y)>>1)即x和y的算数平均值

    (x&y) + ((x^y)>>1)相当于(x+y)/2 (x&y)+((x^y)>>1),把x和y里对应的每一位(指二进制位)都分成三类,每一类分别计算平均值 ...

  5. html页面,左边点击链接,右边显示内容参考代码。

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  6. mydbtest文档

    mydbtest是楼方鑫编写的一个数据库测试工具,有需要的话,请自取 http://pan.baidu.com/s/1mgJpukg#path=%252FOneSQL%252FDocument 找到& ...

  7. JAVA通过C3P0连接数据库

    配置文件: <?xml version="1.0" encoding="UTF-8"?> <c3p0-config>    <na ...

  8. Android 应用开发耗电量控制。。

    当程序启动手机越多的模块,那耗电就越快 当你的程序运行时只占用CPU的时候,这时候耗电量是最少的. 当然这时候如果cpu的运行速度很慢那是最好的.. 程序耗电量控制首要从下面3个方面抓起: 1.频繁的 ...

  9. Echart 商业级数据图表

    简介 最近工作上用到这个图表库,图表丰富,用起来也很方便.纯javascript,可以流畅得运行在PC和移动设备上,兼容大部分浏览器. 支持折线图(区域图).柱状图(条状图).散点图(气泡图).K线图 ...

  10. CentOS 7 /RHEL 7: How To Change The System Locale

    The system localeare used to control the language setting of system services and the UI before the u ...