poj1700

题目大意:
过河
有N个人想要过河,但是只有一条船,它最多只能携带两个人,因此必须安排有人回来以便让所有的人都过河(要是有人不会来剩下的人就没有船可以过河了),每个人都有不同的划船速度,两个人的划船速度由慢的那个决定,你的工作就是安排怎么使用最少的时间过河。
题目分析:
可以看出来不可能每次都让速度最快的划船,那样太浪费时间,应该让两个速度差别不多的去划船,然后让速度比较快的划回来既可以了。
/////////////////////////////////////////////////////////////

 #include<stdio.h>

#include<math.h>
#include<algorithm>
using namespace std;

#define maxn 1005
#define INF -1000000000

int main()
{
    int T;

    scanf("%d", &T);

    while(T--)
    {
        int i, n, p[maxn]={0}, F, S, sum=0;

        scanf("%d", &n);

        for(i=0; i<n; i++)
            scanf("%d", &p[i]);
        sort(p, p+n);

        if(n==1)
            sum = p[0];
        else if(n == 2)
            sum = p[1];
        else
        {
            F = p[0], S=p[1];

            for(i=n-1; i>1; i--)
            {
                if(i==2)
                    sum += F+p[i];
                else if(S*2+F+p[i] <= p[i]+p[i-1]+2*F)
                    sum += S*2+F+p[i], i--;
                else
                    sum += p[i]+p[i-1]+2*F, i--;
            }

            sum += S;
        }

        printf("%d ", sum);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/liuxin13/p/4384020.html