题意:摆渡问题,一个船一次最多能运送n辆汽车。现有m辆汽车需要运送,运输一次的时间为t,求出运送完所有的汽车后的最小时间以及最小的运输次数。
贪心思想:如果m%n==0则运输m/n次 反之则运输 m+1次。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
void Greedy(int n,int t,int m){
int i,j;
int l=m%n;
int k=m/n;
int now_time;
int return_time=0;
int trans_time=k;
if(l){//
trans_time+=1;
while(l--){
scanf("%d",&now_time);
return_time=now_time+2*t;
}
}
for(i=0;i<k;i++){
for(j=0;j<n;j++)
scanf("%d",&now_time);//
if(now_time<=return_time)
return_time+=2*t;
else
return_time=now_time+2*t;
}
printf("%d %d\n",return_time-t,trans_time);
}
int main(){
int c;
scanf("%d",&c);
while(c--){
int n,t,m;
scanf("%d%d%d",&n,&t,&m);
Greedy(n,t,m);
}
return 0;
}