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

有两种过河方法

1.让最小的那个人一直来回载人过去

2.一群人过河,让最小的两个人过去,然后最小的人过来,还没过去的最大两个人过去,第二小的人在回来;一直重复;

最后比较两种方法那个更好

分情况讨论,小于4人可以直接找到方法;

要是大于4人,把四个人放在一组,然后比较两种方法运人哪个更快,然后选快的那种;

不可以所有人都用同一种方法如何在比较,这样还是太慢了

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define cl clear()
#define pb push_back
#define mm(a,b) memset((a),(b),sizeof(a))
#include<vector>
const double pi=acos(-1.0);
typedef __int64 ll;
typedef long double ld;
const ll mod=1e9+7;
using namespace std;
int main()
{
int re;
int a[1005];
cin>>re;
while(re--)
{
mm(a,0);
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
int sum=0;
int num=n/2;
while(n>3)
{
int sum1=0,sum2=0;
sum1=a[0]+2*a[1]+a[n-1];
sum2=a[0]*2+a[n-1]+a[n-2];
sum+=sum1<sum2?sum1:sum2;
n-=2;
}
if(n==1)
sum+=a[0];
else if(n==2)
sum+=a[1];
else if(n==3)
sum+=a[0]+a[1]+a[2];
pf("%d\n",sum);
}
return 0;
}

I - Crossing River的更多相关文章

  1. Crossing River

    Crossing River 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26251 题意: N个人希望去过 ...

  2. POJ 1700 Crossing River (贪心)

    Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9585 Accepted: 3622 Descri ...

  3. Crossing River(1700poj)

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9919   Accepted: 3752 De ...

  4. poj 1700 Crossing River 过河问题。贪心

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9887   Accepted: 3737 De ...

  5. poj1700--贪心--Crossing River

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12260   Accepted: 4641 D ...

  6. POJ1700:Crossing River(过河问题)

    POJ1700 题目链接:http://poj.org/problem?id=1700 Time Limit:1000MS     Memory Limit:10000KB     64bit IO ...

  7. 贪心Crossing river

    英文题目: A group of N people wishes to go across a river with only one boat, which can at most carry tw ...

  8. POJ 1700 - Crossing River

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13982   Accepted: 5349 Description A gr ...

  9. ACM学习历程——POJ 1700 Crossing River(贪心)

    Description A group of N people wishes to go across a river with only one boat, which can at most ca ...

随机推荐

  1. 3、Python的应用

    Python的应用 Google 实现Web爬虫和搜索引擎中的很多组件. Yahoo Yahoo使用它(包括其他技术)管理讨论组. NASA NASA在它的几个系统中既用了Python开发,又将其作为 ...

  2. pyqt QFileDialog

    from PyQt5 import QtWidgetsfrom PyQt5.QtWidgets import QFileDialog class MyWindow(QtWidgets.QWidget) ...

  3. Linux 中C/C++ search path(头文件搜索路径)

    https://blog.csdn.net/BjarneCpp/article/details/76135980 起因 我拿到了一套Linux下的C++代码,代码中有这个头文件#include < ...

  4. python3用BeautifulSoup用limit来获取指定数量的a标签

    # -*- coding:utf-8 -*- #python 2.7 #XiaoDeng #http://tieba.baidu.com/p/2460150866 #标签操作 from bs4 imp ...

  5. 10个优秀Objective-C和iOS开发在线视频教程

    如果你自己开发iOS应用,你肯定会发现网上有很多资源.学习编程的一个最好的方法就是自己写代码,而开始写代码的最快的方式就是看其他人怎么写.我们从海量视频和学习网站中整理出了我们认为对你学习Object ...

  6. js获取过滤条件中参数的快捷方式

    // window.location.href = "topupRecordController.do?exportExcel&" + encodeURI($(" ...

  7. 如何将Ubuntu Server 12.04 升级到 Ubuntu Server 14.04 LTS

    升级Ubuntu 12.04到Ubuntu 14.04方法如下: 步骤一:在终端中运行下面的命令,它将安装所有的升级包.$ sudo apt-get update && sudo ap ...

  8. VirtualBox 端口转发 SSH

    在服务器上安装VirtualBox并配置转发: VirtualBox网络连接方式,选择默认的NAT连接方式. 在下面有个 “端口转发” 按钮,点击配置主机到虚拟机的端口映射. ref: http:// ...

  9. 每日英语:How To Survive The Windows XPiration Date

    The default background for Microsoft's Windows XP operating system -- a perfect blue sky full of cot ...

  10. Xmpp获取离线消息

    文章只是选取了其中一段,无XMPP基础的人可能看起来有点复杂; 假设我们注册了一个用户,用户名叫shimiso,那么我们如何让shimiso这个用户一登陆就取到离线消息呢? PPConnection. ...