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. Css 书写规范【转】

    1. 不同浏览器元素的默认属性有所不同,使用Reset可重置浏览器元素的一些默认属性,以达到浏览器的兼容. /** 清除内外边距 **/ body, h1, h2, h3, h4, h5, h6, h ...

  2. Keil(MDK-ARM)使用教程(二)_菜单

    Ⅰ.概述 接着上一篇来总结Keil(MDK-ARM)菜单相关的内容,详情请往下看. 关于Keil的下载.安装和新建工程我已将在前面做了详细的总结,不懂的可以参考我博客里面相关的文章.该文章是在新建好工 ...

  3. 关于EasyUI与富文本编辑器结合使用的问题(kindueditor与uueditor)

    最近使用easyui玩玩项目,在结合富文本编辑器时遇到了一些问题,很多人(在网上看到)集成富文本编辑器时常常不能显示, 第一次打开编辑的时候没有问题,但是第二次打开就出错了.为此我进行了一些调试研究. ...

  4. jruby中的异常

    先看看ruby中的异常知识: 异常处理raise 例子: raise raise "you lose" raise SyntaxError.new("invalid sy ...

  5. ios view的frame和bounds之区别(位置和大小)

    前言: 学习ios开发有一段时间了,项目也做了两个了,今天看视频,突然发现view的frame和bound两个属性,发现bound怎么也想不明白,好像饶你了死胡同里,经过一番尝试和思考,终于弄明白bo ...

  6. Android体系结构

    由图可知,android被分成4个层次,以linux为核心,针对手机进行专门的优化,提供了android操作系统最基本的功能,在此之上又分为android runtime和libraries.其中Da ...

  7. ED/EP系列6《扩展应用》

    包括:电子钱包复合应用:电子钱包灰锁应用. 1. 复合应用模式 Ø INITIALIZE FOR CAPP PURCHASE(复合应用消费初始化): Ø UPDATE CAPP DATA CACHE( ...

  8. 菜鸟学习Struts——bean标签库

    一.Struts标签库. Struts实际上包含了4个标签库:bean,logic,html,tiles bean:用来在属性范围中定义或取得属性的,同时可以读取资源文件信息 logic:替代JSTL ...

  9. .NET开源工作流RoadFlow-流程设计-流转条件设置(路由)

    当一个步骤后面有多个步骤时,可以设置为根据设置条件系统自动判断该流向哪些步骤,也叫路由. roadflow没有单独的路由步骤来设置条件,流程条件通过双击连线弹出条件设置框来设置. 1.sql条件 即通 ...

  10. 【javascript】html5中使用canvas编写头像上传截取功能

    [javascript]html5中使用canvas编写头像上传截取功能 本人对canvas很是喜欢,于是想仿照新浪微博头像上传功能(前端使用canvas) 本程序目前在谷歌浏览器和火狐浏览器测试可用 ...