码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 10440 Ferry Loading II

时间:2015-05-10 01:00:52      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

问题:

一个渡口,有一艘渡船,运汽车到对面,一次能运n辆车,到达对面要t分钟,返回要t分钟,一共来m辆车。

给出m个数据,是到达渡口的时间。求最短把全部汽车运到对面的最短时间是多少,次数是多少(时间从0算)。

思路:贪心。

考虑三种情况(也可以说是两种)

一:n>m.最短时间为最后一辆车到达渡口的时间+t,次数为1;

二:n<=m,m恰好整除n。

三:n<=m,m除以n有余数。

详细思路代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

#define MAXN 1500

int Max(int a,int b){
    if (a>b)
        return a;
    else
        return b;
}

class Ferry{
    private:
        int n;
        int m;
        int t;
        int cars[MAXN];
    public:
        void process();
};

void Ferry::process(){
    int cases;//测试样例个数
    int ansTime,ansNum;//结果时间,次数


    cin >>cases;
    while(cases--){
        ansTime = 0;
        ansNum = 0;
        cin>>n>>t>>m;
        for(int i = 0;i < m;i++){
            cin>>cars[i];
        }
        
        if(n > m){//来的车辆比每次能运的还要少
            ansTime = cars[m-1] + t;
            ansNum = 1;
        }
        else if(m % n == 0){
            for(int i = n - 1;i < m;i += n){
                ansTime = Max(ansTime,cars[i]);/*比较渡船每次返回(起始也算)渡口的时间和车辆来到渡口的时间,哪个最晚选哪个*/
                ansTime += 2 * t;
            }
            ansTime = ansTime - t;//最后一次不必返航
            ansNum = m/n;
        }
        else{
            int remainder;//求出总车辆和一次能运车辆的余数
            remainder = m%n;
            ansTime = cars[remainder - 1]+2*t;//先运余数数目的车辆
            for(int i = remainder + n - 1;i < m;i = i + n){/*剩下就是每次装满的情况*/
                ansTime = Max(ansTime ,cars[i] );
                ansTime += 2 * t;
            }
            ansTime -=  t;
            ansNum = m/n + 1;
        }

        cout<<ansTime<<" "<<ansNum<<endl;
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("D:\\acm.txt","r",stdin);
    #endif // ONLINE_JUDGE
    Ferry ferry;
    ferry.process();
    return 0;
}

 

UVa 10440 Ferry Loading II

标签:

原文地址:http://www.cnblogs.com/ohxiaobai/p/4490660.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!