您的位置:首页 > Web前端

UVa 10440 - Ferry Loading II

2016-04-05 19:00 1916 查看
问题:
一个渡口,有一艘渡船,运汽车到对面,一次能运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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: