题目描述:

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>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
using namespace std;
#define PI 3.14159265358979323846264338327950 int T,n,a[],t;
int main()
{
scanf("%d",&T);
while(T--)
{
t=;
scanf("%d",&n);
memset(a,,sizeof(a));
for(int i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
if(n==)
t=a[];
else if(n==)
t=a[];
else if(n==)
t=a[]+a[]+a[];
else
{
for(int i=n-;i>;i=i-)
{
if(i>=)
{
if(a[]+a[i-]>=*a[])
t=t+a[]+*a[]+a[i];
else
t=t+*a[]+a[i]+a[i-];
}
if(i==)
t=t+a[]+a[]+a[];
if(i==)
t=t+a[]; }
}
printf("%d\n",t);
}
}

最新文章

  1. Javascript常用方法函数收集(一)
  2. Bilateral Filtering(双边滤波) for SSAO(转)
  3. JAVA的Proxy动态代理在自动化测试中的应用
  4. [JS]Javascript的this用法
  5. poj3233
  6. CentOS6.5下VNC Server远程桌面配置详解
  7. fullcalendar日历控件知识点集合
  8. &amp;quot;ScrollView can host only one direct child&amp;quot;问题解决了
  9. springmvc实现long-pulling技术
  10. 浅谈IOC
  11. css常用属性1
  12. Exceptionless 生产部署笔记
  13. windows下多tomcat部署
  14. Linux - 日志处理一
  15. iOS学习笔记之UITableViewController&amp;UITableView
  16. 廖雪峰网站:学习python基础知识—循环(四)
  17. SpringBoot企业级博客开发
  18. ubantu 与Windows 资源共享
  19. PHP开发——常见问题
  20. httpd.conf .htaccess apache 服务器配置

热门文章

  1. Python 踩坑之旅进程篇其四一次性踩透 uid euid suid gid egid sgid的坑坑洼洼
  2. jQuery:如何给动态生成的元素绑定事件?
  3. nuxt.js实战踩坑记录
  4. Php&mdash;AJAX跨域问题
  5. JAVA基础之Properties类、序列化流及打印流、commons-IO
  6. java es 骤合操作
  7. Android笔记--BroadcastReceiver
  8. Layer:如何调用layer.open打开的的iframe窗口中的JS?
  9. 基于 Ubuntu + nextCloud 搭建自己的私人网盘
  10. python_103_属性方法例子