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. cocos run -p android报错 BUILD FAILED ..\ant\build.xml:892

    使用编译指令生成apk文件时,出现这个错误,是因为重复引用了..\YourGame\cocos2d\cocos\platform\android\java\bin\classes.jar文件. 为什么 ...

  2. sql中with as的用法练习

    在工作中经常看到有人使用with as,查询很顺畅,也很快,很好奇,在网上也有不少资料,看了大神的文章,也练习一下. 首先给出两位位大神文章的链接,介绍十分详细:http://www.cnblogs. ...

  3. python Django 学习笔记(六)—— 写一个简单blog做增删改练手

    简单效果图 1,创建一个项目myblog 可参考这里 myblog/ manage.py myblog/ __init__.py settings.py urls.py wsgi.py 2,创建blo ...

  4. CentOS 7 + nginx + uwsgi + web2py (502 bad gateway nginx)

    Web2py开发包中自带的setup-web2py-nginx-uwsgi-centos64.sh脚本, 只能运行在CentOS 6.4中使用, 如果直接在CentOS 7 中使用该脚本布署后, 访问 ...

  5. poj 1789 Truck History

    题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...

  6. Swift TabeleViewCell dequeueReusableCellWithIdentifier 使用的新的细节,原来现在可以这样

    今天在看官方的TableView Guide,突然想起来最近写的一个代码中实现tableViewCell复用的时候有点问题: var cell = UITableViewCell(style: UIT ...

  7. 1 通过JNI混合使用Java和C++ -----> 操作字符串

    JNI(Java Native Interface)是Java语言的一部分,可以访问非Java语言编写的程序,也可以用于在C++程序中执行Java代码. 步骤: 1>  编写带有native声明 ...

  8. Linux 编辑器

    vim编辑器 vi作为Unix上的一个编辑器,一直广受欢迎.之后GUN将其移植到开源世界中,经过开发人员对其进行了改善,被称为vi improved,就是现在的vim.为了方便使用,几乎所有的Linu ...

  9. Oracle Insert 多行(转)

    1.一般的insert 操作. 使用语法insert into table_name[(column[,column...])] values (value[,value…])的insert语句,每条 ...

  10. [vsftp服务]——ftp虚拟用户、权限设置等的实验

    搭建ftp服务器,满足以下要求: 1.允许匿名用户登录服务器并下载文件,下载速度设置为最高2MB/s 2.不允许本地用户登录ftp服务器 3.在服务器添加虚拟用户vuser01.vuser02.vus ...