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. hdu1455 拼木棍(经典dfs)

    给定木棍序列,求解能将木棍拼成相同长度的数根长木棍的情况下长木棍长度的最小值. /*hdu1455dfs */ #include<bits/stdc++.h> using namespac ...

  2. 洛谷 P1438 无聊的数列 题解

    原题链接 首先,我们考虑用差分解决问题. 用 \(x_i\) 表示原数列,\(a_i = x_i - x_{i-1}\) 那么,先普及一下差分: 如果我们只需要维护区间加值,单点求值的话,你会发现两个 ...

  3. CF1324F Maximum White Subtree 题解

    原题链接 简要题意: 给定一棵树,每个点有黑白两种颜色:对每个节点,求出包含当前节点的连通图,使得白点数与黑点数差最小.输出这些值. F题也这么简单,咳咳,要是我也熬夜打上那么一场...可惜没时间打啊 ...

  4. 一、【Docker笔记】进入Docker世界

    ​ 我们平时判断一个电脑的性能主要看什么?磁盘读写?CPU的主频高低?还是内存的大小?可是作为个人使用者来说,这些参数高一些足够我们去使用了,可是对于一个大型系统甚至是超大型系统,当前的硬件是远远达不 ...

  5. 添加windows开机自启动项

    windows系统下我们最常用的是禁用启动项,但如果程序不在自启动列表里面,如何添加程序启动呢. 其实也很简单,首先找到windows启动路径C:\Users\NL\AppData\Roaming\M ...

  6. java 中的字符串处理--正则表达式

    最近在做一些支付报文处理工作,需要从各种各样的报文中提取需要的信息比如(金额,订单号...),每个渠道报文各式各样,想要写一个通用的提取逻辑,于是就回顾java正则表达式的用法.当然我们可以自己写一些 ...

  7. python学习第四节 迭代器 生成器

    1:什么是迭代 可以直接作用于for循环的对象统称为可迭代对象(Iterable). 可以被next()函数调用并不断返回下一个值的对象称为迭代器(Iterator). 所有的Iterable均可以通 ...

  8. 教你用Cobra开发类似docker的命令行

    目录 前言 一.安装 二.初始化应用 gomod初始化 创建入口文件cmd/root.go 创建主程序main.go 三.生成Command 创建hello子命令 创建version子命令 四.如何设 ...

  9. CentOS8中安装maven

    下载maven,具体目录可根据实际情况而定 # wget http://mirrors.cnnic.cn/apache/maven/maven-3/3.3.9/binaries/apache-mave ...

  10. 数据挖掘-K-近邻算法

    数据挖掘-K-近邻算法 目录 数据挖掘-K-近邻算法 1. K-近邻算法概述 1.1 K-近邻算法介绍 1.1.1 KNN算法作用 1.1.2 KNN 算法思想 1.1.3 KNN算法特点 1.2 K ...