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. easy-ui grid里的toobar按钮隐藏与显示

    //隐藏第一个按钮$('div.datagrid-toolbar a').eq(0).hide();//隐藏第一条分隔线$('div.datagrid-toolbar div').eq(0).hide ...

  2. gdb调试参数

    参考 这里 1)gdb 打印字符串的全部内容 set print element 0 2)用x命令查看内存(examine的缩写) (gdb) x/1ub 0x7ffff7fbf7a00x7ffff7 ...

  3. 2016级算法第五次上机-F.ModricWang的水系法术

    1066 ModricWang的水系法术 思路 比较典型的最大流问题,需要注意的是,题目已经暗示(明示)了这里的边是双向的,在建图的时候需要加上反向边的容量值. 解决最大流问题的基本思路就是不断在残量 ...

  4. tomcat 搭建以及发布配置

    身为开发人员, 一直干着开发的事情, 只干开发的事情, 却缺少了对于环境部署方面的必备技能的培养, 所以在公司安排的手头任务解决完的情况下, 自己抽空了解并且实践了一下tomcat的配置.写下通过网络 ...

  5. [HDU4089]Activation(概率DP)

    HDU4089 题意:有n个人排队等着在官网上激活游戏.Tomato排在第m个. 对于队列中的第一个人.有一下情况: 1.激活失败,留在队列中等待下一次激活(概率为p1) 2.失去连接,出队列,然后排 ...

  6. 洛谷 P4234 最小差值生成树(LCT)

    题面 luogu 题解 LCT 动态树Link-cut tree(LCT)总结 考虑先按边权排序,从小到大加边 如果构成一颗树了,就更新答案 当加入一条边,会形成环. 贪心地想,我们要最大边权-最小边 ...

  7. C#集合之队列

    队列是其元素按照先进先出(FIFO)的方式来处理的集合. 队列使用System.Collections.Generic名称空间中的泛型类Queue<T>实现.在内部,Queue<T& ...

  8. Python爬虫常用之登录(二) 浏览器模拟登录

    浏览器模拟登录的主要技术点在于: 1.如何使用python的浏览器操作工具selenium 2.简单看一下网页,找到帐号密码对应的框框,要知道python开启的浏览器如何定位到这些 一.使用selen ...

  9. python四则运算2.0

    github项目地址: https://github.com/kongkalong/python PSP 预估耗时(分钟) Planning .Estimate 48*60 Development . ...

  10. UML-6.2-用例-用例模型/用例/场景关系

    参与者:具有某些行为的人或事物.如上一章中的收银员. |_主要参与者:收银员. |_协助参与者:程序(自动付费.帮收银员验证输入要素) |_幕后参与者:政府等(电子签章取证找公证机构) 用例:一组相关 ...