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

Description

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

Source

 
每一趟的决策判断。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std; int f[];
bool cmp(int n1,int n2)
{
return n1>n2;
}
int main()
{
int T;
int i,k,n,Sum,Min;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&f[i]);
sort(f+,f++n,cmp);
if(n== || n==)
{
printf("%d\n",f[]);
continue;
}
if(n==)
{
printf("%d\n",f[]+f[]+f[]);
continue;
}
int min1=f[n];
int min2=f[n-];
if(n&) k=n-;
else k=n-;
for(i=,Sum=;i<=k;i=i+)
{
Min=min(min2+min1+f[i]+min2,f[i]+min1+f[i+]+min1);
Sum=Sum+Min;
}
if(n&) Sum=Sum+f[n-]+min1+min2;
else Sum=Sum+min2; printf("%d\n",Sum);
}
return ;
}

poj 1700 Crossing River 过河问题。贪心的更多相关文章

  1. POJ 1700 Crossing River (贪心)

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

  2. 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 ...

  3. poj 1700 Crossing River C++/Java

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

  4. POJ 1700 - Crossing River

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

  5. POJ 1700 cross river (数学模拟)

                                                                                                       ...

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

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

  7. 1700 Crossing River

    题目链接: http://poj.org/problem?id=1700 1. 当1个人时: 直接过河 t[0]. 2. 当2个人时: 时间为较慢的那个 t[1]. 3. 当3个人时: 时间为 t[0 ...

  8. POJ 1700 经典过河问题(贪心)

    POJ题目链接:http://poj.org/problem?id=1700 N个人过河,船每次最多只能坐两个人,船载每个人过河的所需时间不同,问最快的过河时间. 思路: 当n=1,2,3时所需要的最 ...

  9. Crossing River(1700poj)

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

随机推荐

  1. kali linux之拒绝服务攻击工具

    hping3 几乎可以定制发送任何TCP/IP数据包,用于测试FW,端口扫描,性能测试 -c - 计数包计数  -i - interval wait(uX表示X微秒,例如-i u1000)       ...

  2. CF223C【Partial Sums】(组合数学+乱搞)

    题面 传送门 题解 orz zzk 考虑这东西的组合意义 (图片来自zzk) \(a_i\)这个元素对\(k\)阶前缀和的第\(j\)个元素\(s_{k,j}\)的贡献就等于从\((0,i)\)走到\ ...

  3. [AIR] 检测移动设备运动

    Accelerometer 类根据由设备的运动传感器检测的活动调度事件.此数据表示设备的位置或沿三维轴的移动.当设备移动时,传感器会检测到此移动并返回加速数据.Accelerometer 类提供了各种 ...

  4. 使用beautifulsoup和pyquery爬小说

    # -*- coding:UTF-8 -*- from bs4 import BeautifulSoup #BeautifulSoup就是处理字符串的工具 import requests, sys & ...

  5. CentOS7-Minimal1708安装设置python3

    使用 python -V 命令查看一下是否安装Python然后使用命令 which python 查看一下Python可执行文件的位置可见执行文件在/usr/bin/ 目录下,切换到该目录下执行 ll ...

  6. [开源JVM] yvm - 自制Java虚拟机

    中文 | English | | | YVM是用C++写的一个Java虚拟机,现在支持Java大部分功能,以及一个基于标记清除算法的并发垃圾回收器. 不过还有很多bug等待修复. 感兴趣的朋友pull ...

  7. 斐讯K2P通过配置文件开启telnet的原理分析

    看过几篇教程之后我已经知道怎么备份固件了.但是现在有一个问题,我的本意是把K2P原机带的固件备份出来,用教程上的方法进行开启telnet.备份固件等操作是否会改变固件呢?下面我们来验证这个问题. Op ...

  8. 03. PNG,GIF,JPG的区别及如何选?

      3.PNG,GIF,JPG的区别及如何选? 照片用 JPG. 动画用 GIF. Logo.Icon 等小图用 PNG-8. 非特殊情况,尽量不要用 PNG-24 和 PNG-32.  

  9. 苹果Air A1466进入系统黑屏

    现象:苹果Air A1466笔记本安装Windows 7系统后,安装官网对应型号的bootcamp后,重启机器,在Windows滚动条完成后随即进入黑屏状态,安全模式能够进入,在安全模式下卸载删除显卡 ...

  10. python爬虫之urllib库(二)

    python爬虫之urllib库(二) urllib库 超时设置 网页长时间无法响应的,系统会判断网页超时,无法打开网页.对于爬虫而言,我们作为网页的访问者,不能一直等着服务器给我们返回错误信息,耗费 ...