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. Delphi7中编译提示“Unsafe type 'PChar'”的原因及处理办法

    delphi7中加入了对.net的支持 在.net中是没有指针的(托管环境中),所以指针都是不安全的,不符合.net规范 所以d7里有警告,可以不管它 DELPHI7已经考虑到了移植到点NET的问题, ...

  2. WCF全面解析第一章 WCF 简介

    1.WCF中的 "A","B","C" 介绍 我们先看个生活中的例子,某一天,公司的领导让你去送一份合同文件,送文件的过程你可以选择的交通方 ...

  3. Mongodb初学习--安装、试用

    MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. 在MongoDB中数据被分组存储在数据集中,被称为一个集合(Collection ...

  4. C 基于UDP实现一个简易的聊天室

    引言 本文是围绕Linux udp api 构建一个简易的多人聊天室.重点看思路,帮助我们加深 对udp开发中一些api了解.相对而言udp socket开发相比tcp socket开发注意的细节要少 ...

  5. L2-015. 互评成绩

    学生互评作业的简单规则是这样定的:每个人的作业会被k个同学评审,得到k个成绩.系统需要去掉一个最高分和一个最低分,将剩下的分数取平均,就得到这个学生的最后成绩.本题就要求你编写这个互评系统的算分模块. ...

  6. XMLCDataSection

    XmlCDataSection类描述XML数据中的CDATA节.CDATA节在XML数据中的作用是为文本内容定义引号和转义符,即XML解析器不解析CDATA中的任何字符. XmlCDataSectio ...

  7. 九度oj 1523 从上往下打印二叉树

    原题链接:http://ac.jobdu.com/problem.php?pid=1523 建树,再层次遍历bfs.为了找根方便些,加了father指针... 如下: #include<algo ...

  8. ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解

        通过终端安装程序sudo apt-get install xxx时出错:   E: Could not get lock /var/lib/dpkg/lock - open (11: Reso ...

  9. UIImageView swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  10. Java Day 09

    子父类的构造函数 在子类的构造函数中,第一行有一个默认的隐式语句:super() 子类的实例化过程:子类中所有的构造函数默认都会访问父类中的空参数的构造函数. 为什么子类实例化的时候要访问父类中的构造 ...