POJ1700 【经典过河问题,贪心】

题意:
n个人过河,
船每次只能坐两个人,
然后船载每个人过河的所需时间不同,
问最快的过河时间。
思路:
仅仅启发一下思维:
我相信很多人一下子的想法就会有,每次最快和那些慢的过去,然后让最快一直来回,最后全部到对岸。
但是还有一种情况,我说说看,弱仅仅是想给大家一点思考,毕竟有意思。
你先让两个快的人从A过去至B,然后挑一个人回来至A,留下一个快的在B。OK,回来以后我让两个最慢的过去至B,然后让B那边的快的过来接A这边的快的。这样会不会节约一点啊。
OK.
我们假设只有四个人,tf(the fast最快),sf(second fast次快),sm(second man次慢),tm(the manst最慢)。OK.
第一种情况:
tf兜来兜去的case:
tf+tm–>
<–tf
tf+sm–>
<–tf
tf+sf–>
第二种情况:
tf+sf–>
<–sf
tm+sm–>
<–tf
tf+sf–>
然后各类的时间消掉,会得出两种方案的最终比较:2*sf和tf+sm的比较;
很好玩的东西吧,那么根据我们的想法,这样子的话,写法就是每次带走两个人直接比较那两个值就好了,然后小的按照小的方案走,最后会留下,肯定是在A留下的,3,2,还是1个。这个就自己考虑了。
code……………..

//#include<bits/stdc++.h>
#include<cstdio>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int eps=1e-9;
const int pi=acos(-1.0);
const int mod=1e8+7;
const int INF=0x3f3f3f3f;

const int N=1e3+10;
int a[N];

int main()
{
    int n,t,i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        int sum=0;
        for(i=n;i>3;i-=2){     //每次带两个人过去
            if(2*a[2]>a[1]+a[i-1]){
                sum+=2*a[1]+a[i]+a[i-1];
            }
            else
                sum+=2*a[2]+a[i]+a[1];
        }
        if(i==3) sum+=a[1]+a[2]+a[3];
        else if(i==2) sum+=a[2];
        else sum+=a[1];
        printf("%d
",sum);
    }
}
原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934397.html