POJ 1700 Crossing River (贪心)

本文探讨了如何使用一种有效的策略,使一群人在只有一艘最多能载两人的小船的情况下,以最少的时间过河。通过分析不同人数情况下最优策略,实现所有人的快速过河。

Crossing River
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 9585Accepted: 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;
}

转载于:https://www.cnblogs.com/CKboss/p/3350996.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值