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

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

141 2 5 10

Sample Output

17

Source

当人数等于1,2,3的时候:答案很容易得出;
当人数大于等于4时:

若设过桥速度最快的那个人过桥时间为a,第二快为b;过桥第二慢的那个人过桥时间为y,最慢为z;
此时有两种过桥方案:
一.最快和次快的人先过,然后最快的回来,然后最慢与次慢的人再过,次快的回来;
二.最快的和最慢的过,快的回来,在和次慢的过,快的再回来;

第一种方法时间为b*2+a+z
第二种方法时间为y+z+2*a

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <deque>
using namespace std;
int a[1111];
int main()
{
    int T;
    cin>>T;
while(T--)
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a;
    }
    sort(a,a+n);
    deque<int> dq;
    for(int i=0;i<n;i++)
        dq.push_back(a);
    int ans=0;
    while(!dq.empty())
    {
        if(dq.size()==1)
        {
            ans+=dq.front();
            break;
        }
        else if(dq.size()==2)
        {
            ans+=dq.back();
            break;
        }
        else if(dq.size()==3)
        {
            int a=dq.front();
            dq.pop_front();
            int b=dq.front();
            dq.pop_front();
            int c=dq.front();
            ans+=(a+b+c);
            break;
        }
        else if(dq.size()>=4)
        {
            int a,b,c,d;
            a=dq.front();
            dq.pop_front();
            b=dq.front();
            dq.pop_front();
            d=dq.back();
            dq.pop_back();
            c=dq.back();
            dq.pop_back();
            int t1=a+d+2*b;
            int t2=c+d+2*a;
            ans+=min(t1,t2);
            dq.push_front(b);
            dq.push_front(a);
        }
    }
    printf("%d\n",ans);
}
    return 0;
}

POJ 1700 Crossing River (贪心)的更多相关文章

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

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

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

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

  7. Crossing River(1700poj)

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

  8. Crossing River

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

  9. POJ 3190 Stall Reservations贪心

    POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...

随机推荐

  1. IOS开发之后台处理

    1 前言IOS4 之后提供了后台处理,在后台运行应用程序,在一些情形下甚至可以在用户按下Home按钮之后在后台运行. 2 详述IOS可以在用户按下Home按钮后将应用程序添加到暂停状态.这种暂停执行的 ...

  2. [terry笔记]更改oracle用户名

    更改oracle的用户名 之前有个需求,整理一个schema的表.索引等规划到一个表空间里,利用expdp/impdp然后remap就完成了,但是整理好的用户名remap变更了,应用又不想修改其连接信 ...

  3. 扯一扯前端css的整体架构设计:(2)base基础类的那些事儿

    周一下午在实验室写了第一篇博文,有几个人捧场,那咱就得接着下去啊.然后我觉得现在写的内容更多的偏向于谈一下我对于前端css架构的理解和前端经验的一个小总结,所以就把标题里原来的[项目总结]给删掉了.但 ...

  4. shell括号操作符

    以下以bash环境下做解说 一.单小括号() 二.双小括号(()) 可作数值条件操作,也可作数值运算使用(近似于 let 命令) 如 C 语言语法一样,支持运算符:<<.<<= ...

  5. UVA 10970 第一次比赛 D题 (后面才补的)

    Mohammad has recently visited Switzerland. As heloves his friends very much, he decided to buy somec ...

  6. MIFARE系列8《D8M1.exe》

    软件名:D8M1.exe 更新时间:2014.06.28 操作系统:windowAll 外部设备:D8读卡器 D8M1可以对MIFARE块读写操作,支持1K,4K.检验KEY后返回SAK,QTAQ,U ...

  7. MIFARE系列5《存储结构》

    Mifare S50把1K字节的容量分为16个扇区(Sector0-Sector15),每个扇区包括4个数据块(Block0-Block3),我们也将16个扇区的64个块按绝对地址编号为0~63,每个 ...

  8. ORACLE-用户常用数据字典的查询使用方法

    一.用户 查看当前用户的缺省表空间 SQL> select username,default_tablespace from user_users; USERNAME DEFAULT_TABLE ...

  9. Android--获取标题栏,状态栏,屏幕高度

    获取状态栏高度 Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int ...

  10. 新安装Ubuntu加载时提示“为/检查磁盘时发生严重错误”的解决方法

    本文部分内容转载自: http://jingyan.baidu.com/article/0aa22375bbffbe88cc0d6419.html http://www.aichengxu.com/v ...