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

 一个非常巧妙的贪心问题,刚开始的时候以为只要保证让船回来的时候时间最短就好了,即让最小的那一个一直跟着船走,但并不是这样的。。
题解: 分两种过程,第一种就是上述过程,第二种情况是我们先让最快的跟次快的过河,然后让最快的回来,再让最慢的跟次慢的过河,再让次快的回来,这两种情况去较小的,前提是至少要有大于等于4个人,如果小于4个人的话,模拟就可以
还要注意奇数的情况 ,如果是奇数,那么最后会剩下3个人,即最快 次快 次次快,如果是偶数的话,由于我们每次减少两个,最后只剩下最快于次快

AC代码
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1E5+;
int arr[N];
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=;i<=n;i++) scanf("%d",&arr[i]);
sort(arr+,arr++n);
int ans=;
if(n==) ans=arr[];
else if(n==) ans=arr[];
else if(n==) {
ans+=arr[]+arr[]+arr[];
}
else {
int sum1,sum2;
for(int i=n;i>=;i-=){
sum1=arr[i]+arr[i-]+arr[]+arr[];
sum2=arr[]+arr[]+arr[i]+arr[];
ans+=min(sum1,sum2);
}
if(n&)
ans+=arr[]+arr[]+arr[];
else ans+=arr[];
}
cout<<ans<<endl;
}
return ;
}

这篇博文讲的贼好。

https://blog.csdn.net/Cworld2017/article/details/81503102

Crossing River POJ过河问题的更多相关文章

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

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

  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. Crossing River

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

  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. poj 1700 Crossing River C++/Java

    http://poj.org/problem?id=1700 题目大意: 有n个人要过坐船过河,每一个人划船有个时间a[i],每次最多两个人坐一条船过河.且过河时间为两个人中速度慢的,求n个人过河的最 ...

  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. Chromium EC框架探索 1.1 开发环境搭建

    1.1 开发环境搭建 本节参考官方文档getting started building ec images quickly编写本节介绍搭建ec开发环境的两种方式,后一种对于绝大多数人而言是不必要的. ...

  2. java——构造器理解

    构造器理解 什么是构造器 构造器也叫构造方法:用于对象的初始化: 写构造器注意事项 构造器名与类名一致:有返回值但是不能定义返回类型(返回值类型是本类,可以加一个空的return): 构造器的调用 通 ...

  3. Mysql性能优化:为什么你的count(*)这么慢?

    导读 在开发中一定会用到统计一张表的行数,比如一个交易系统,老板会让你每天生成一个报表,这些统计信息少不了 sql 中的count函数. 但是随着记录越来越多,查询的速度会越来越慢,为什么会这样呢?M ...

  4. SVN: Cleanup failed update报错 文件被锁定lock办法,cleanup 失效报错。

    按照如下办法即可解决: 下载文件链接: https://pan.baidu.com/s/1Ump1BFihbX8izyAA4by5dA 提取码: ftsd 复制这段内容后打开百度网盘手机App,操作更 ...

  5. html-css:浮动_清除浮动

    1.浮动 清除浮动之前我们首先需要了解为什么要清除浮动 1. 假设我们有一个父盒子,不设置高度,其高度有内部子盒子的大小自动撑开,这样是完全可行的,因为有时候我们并不想直接固定死父盒子的大小,而是根据 ...

  6. Chrome 经典插件

    记录几个很喜欢的 Chrome 插件,怕之后找不到了. 1. Dark Theme 很喜欢的一个黑色主题! 2. Volume Booster 能把音量提高2倍的小插件!好用! 3. Looper f ...

  7. Java递归练习201908091049

    package org.jimmy.autofactory.test; public class TestRecursive20190809 { public static void main(Str ...

  8. 前端之jQuery基础篇02-事件

    什么是事件: 在元素上移动鼠标. 选取单选按钮 点击元素 常见的DOM事件: 鼠标事件:click() 当鼠标单击发生click事件 : <!DOCTYPE html> <html& ...

  9. B 雷诺与奴隶主

    时间限制 : 5000 MS   空间限制 : 524288 KB 问题描述 自从周文武老师在班上吹嘘了"录试"这款游戏之后,PHD同学沉迷于其中无法自拔. "录试&qu ...

  10. JavaScript简单使用

    本文参考廖雪峰老师网站:https://www.liaoxuefeng.com/wiki/1022910821149312 JavaScript是一种运行在浏览器中的解释型的编程语言,在Web世界里, ...