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. spring中的Log4jConfigListener作用和webapp.root的设置

    转:http://blog.sina.com.cn/s/blog_7bbf356c01016wld.html 使用spring中的Log4jConfigListener有如如下好处:     1. 动 ...

  2. oracle中between

    oracle中between and包含边界值,也就是所谓的闭区间. 如 between 1 and 100,则表示包含1和100及以内的一切数值. 如以下语句: 1 2 3 4 5 6 7 8 9 ...

  3. 转:不是技术牛人,如何拿到国内IT巨头的Offer

    转自:http://blog.csdn.net/lsldd/article/details/13506263 不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛 ...

  4. JAVA调用C语言写的SO文件

    JAVA调用C语言写的SO文件 因为工作需要写一份SO文件,作为手机硬件IC读卡和APK交互的桥梁,也就是中间件,看了网上有说到JNI接口技术实现,这里转载了一个实例 // 用JNI实现 // 实例: ...

  5. Django中如何配置Database缓存?

    BACKEND: django.core.cache.backends.db.DatabaseCache LOCATION: 数据库表名 示例: CACHES = { 'default': { 'BA ...

  6. Linkclump – 批量打开多个链接[Chrome]

    我的收藏 | 登录 首页 » Chrome » Linkclump – 批量打开多个链接[Chrome] 收藏 2  (1 votes) 青小蛙 on 2016.04.06. Linkclump 是一 ...

  7. 2D客户端+微端技术总结

    本人于2013年9月23号加入一个页游项目组, 并作为项目组的客户端小组的主程, 带领一个4个人(峰值)的前端小组, 进行微端的开发.微端项目于2014年8月底大体完成, 历时11个月.9月份之后微端 ...

  8. Mybatis学习(壹)

    一.Mybatis的引言 1.Mybatis框架概念:是数据库持久层的框架,对数据库的访问和操作.Mybatis对JDBC的封装,Mybatis替换JDBC开发,解决DAO中的通用问题. 2.JDBC ...

  9. 纯css代码写旋转动画

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

  10. swf2pdf转swf时字符集问题【转】

    今天转了一个的pdf是出现字符集问题,并转换的swf为乱码.出现的错误如下. 错误的原因是缺少中文字符集GBK-EUC-H.解决方法使用xpdf增加缺少的字符集.解决步骤如下: (一) 下载相关的xp ...