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. Python打包方法——Pyinstaller

    Python版本:Python3.5.2 一.安装Pyinstaller 1.安装pywin32 下载安装文件:查找到跟自己适用的python版本及window系统版本匹配的pywin32,下载后安装 ...

  2. CentOS中环境变量和配置文件

    什么是环境变量 bash shell用一个叫做 环境变量(environment variable) 的特性来存储有关shell会话和工作环境的信息.即允许在内存中存储数据,使得在程序或shell中运 ...

  3. 《Attention is All You Need》

    https://www.jianshu.com/p/25fc600de9fb 谷歌最近的一篇BERT取得了卓越的效果,为了研究BERT的论文,我先找出了<Attention is All You ...

  4. 微软BI 之SSRS 系列 - 如何让报表在一页显示,两种常用的技巧

    通常情况下,SSRS 报表在页面内容过多的时候会自动分页.但有的时候当页面内容不是很多,大概最多2页的情况下,或者客户要求所有内容必须在一页显示时,应该如何设置. 实际上,要考虑两种情况:第一种情况是 ...

  5. llvm pass

    https://polly.llvm.org/docs/Architecture.html#polly-in-the-llvm-pass-pipeline

  6. 《Unix&Linux大学教程》学习笔记三:Shell常识

    1:全局变量与局部变量 全局:可以从父进程传递给子进程的变量,如:环境变量. 局部:只能在特定的子Shell中使用的变量. 局部变量变全局:使用 “export 局部” 指令将创建的局部变量导出到环境 ...

  7. 〖Linux〗Kubuntu 14.04的Eclipse 崩溃解决方法总结

    1. 普通崩溃问题: eclipse/configuration/config.ini在后边添加 org.eclipse.swt.browser.DefaultType=mozilla 2. Kubu ...

  8. Docker 使用Docker知识简易部署一个LNMP平台

    1.自定义网络 docker network create lnmp 2.创建Mysql数据库容器(这里我们首先得创建一个mysql-vol数据卷) docker volume create mysq ...

  9. [k8s]jenkins配合kubernetes插件实现k8s集群构建的持续集成

    另一个结合harbor自动构建镜像的思路: 即code+baseimage一体的方案 - 程序员将代码提交到代码仓库gitlab - 钩子触发jenkins master启动一次构建 - jenkin ...

  10. 基于Openresty+的WEB安全防护系统架构--转

    随着时间的推移,我们在实践中也不断的演进我们的服务部署方案,希望WEB防护,不只是单独的云WAF来保护服务,而有其它的相关服务,对WAF进行增强加固的合理配合.我们使用Openresty+系统构建了W ...