Problem O
2016-03-21 16:29
288 查看
Problem Description
Before bridges
were common, ferries were used to transport cars across rivers.
River ferries, unlike their larger cousins, run on a guide line and
are powered by the river's current. Cars drive onto the ferry from
one end, the ferry crosses the river, and the cars exit from the
other end of the ferry.
There is a ferry across the river that can take n cars across the river in t minutes and return in t minutes. m cars arrive at the ferry terminal by a given schedule. What is the earliest time that all the cars can be transported across the river? What is the minimum number of trips that the operator must make to deliver all cars by that time?
Input The first line of input contains c, the number of test cases. Each test case begins with n, t, m. m lines follow, each giving the arrival time for a car (in minutes since the beginning of the day). The operator can run the ferry whenever he or she wishes, but can take only the cars that have arrived up to that time.
Output For each test case, output a single line with two integers: the time, in minutes since the beginning of the day, when the last car is delivered to the other side of the river, and the minimum number of trips made by the ferry to carry the cars within that time. < br>< br>You may assume that 0 < n, t, m < 1440. The arrival times for each test case are in non-decreasing order.
Sample Input 2 2 10 10 0 10 20 30 40 50 60 70 80 90 2 10 3 10 30 40
Sample Output 100 5 50 2 题意:搬运车过河,但是并不是每一辆车都在一边等好的,每辆车到达岸边的时间有要求,一次最多搬运n辆车,需要时间t,并且回来也需要时间t,共有m辆车,求最少拌匀次数,且此时的用时。 解题思路:最少搬运次数不用想就是m%n?m/n+1:m/n;至于半时间如果m%n为零就就直接排好序之后每次搬n辆,要不的话就先把m%n搬走,让后面的车尽可能的少等; 感悟:做多了也没啥感悟了,最难的地方就是想贪心的条件; 代码(G++) #include
#include
#define maxn 1444
using namespace std;
int main()
{
//freopen("in.txt", "r", stdin);
int c,t,n,m,wait_time[maxn],times=0,time=0,lost_car=0;
scanf("%d",&c);
for(int i=0;i
{
time=times=0;
scanf("%d%d%d",&n,&t,&m);
//printf("n=%d t=%d m=%d\n",n,t,m);
for(int j=1;j<=m;j++)
scanf("%d",&wait_time[j]);
lost_car=m%n;
times=m%n?m/n+1:m/n;//最少运输的次数;
if(lost_car)
time=wait_time[lost_car]+t*2;
//printf("此时时间是%d\n",time);
for(int j=1;j<=m/n;j++)//总共运times次;
{
//printf("wait_time[j*n+lost_car]=%d\n",wait_time[j*n+lost_car]);
//printf("time=%d\n",time);
if(time
{
time+=2*t+(wait_time[j*n+lost_car]-time);
if(j==m/n)
time-=t;//最后一次不用回去了
}
else//现在有车了
{
time+=2*t;
if(j==m/n)
time-=t;//最后一次不用回去了
}
//printf("此时时间是%d\n",time);
}
printf("%d %d\n",time,times);
}
}
There is a ferry across the river that can take n cars across the river in t minutes and return in t minutes. m cars arrive at the ferry terminal by a given schedule. What is the earliest time that all the cars can be transported across the river? What is the minimum number of trips that the operator must make to deliver all cars by that time?
Input The first line of input contains c, the number of test cases. Each test case begins with n, t, m. m lines follow, each giving the arrival time for a car (in minutes since the beginning of the day). The operator can run the ferry whenever he or she wishes, but can take only the cars that have arrived up to that time.
Output For each test case, output a single line with two integers: the time, in minutes since the beginning of the day, when the last car is delivered to the other side of the river, and the minimum number of trips made by the ferry to carry the cars within that time. < br>< br>You may assume that 0 < n, t, m < 1440. The arrival times for each test case are in non-decreasing order.
Sample Input 2 2 10 10 0 10 20 30 40 50 60 70 80 90 2 10 3 10 30 40
Sample Output 100 5 50 2 题意:搬运车过河,但是并不是每一辆车都在一边等好的,每辆车到达岸边的时间有要求,一次最多搬运n辆车,需要时间t,并且回来也需要时间t,共有m辆车,求最少拌匀次数,且此时的用时。 解题思路:最少搬运次数不用想就是m%n?m/n+1:m/n;至于半时间如果m%n为零就就直接排好序之后每次搬n辆,要不的话就先把m%n搬走,让后面的车尽可能的少等; 感悟:做多了也没啥感悟了,最难的地方就是想贪心的条件; 代码(G++) #include
#include
#define maxn 1444
using namespace std;
int main()
{
//freopen("in.txt", "r", stdin);
int c,t,n,m,wait_time[maxn],times=0,time=0,lost_car=0;
scanf("%d",&c);
for(int i=0;i
{
time=times=0;
scanf("%d%d%d",&n,&t,&m);
//printf("n=%d t=%d m=%d\n",n,t,m);
for(int j=1;j<=m;j++)
scanf("%d",&wait_time[j]);
lost_car=m%n;
times=m%n?m/n+1:m/n;//最少运输的次数;
if(lost_car)
time=wait_time[lost_car]+t*2;
//printf("此时时间是%d\n",time);
for(int j=1;j<=m/n;j++)//总共运times次;
{
//printf("wait_time[j*n+lost_car]=%d\n",wait_time[j*n+lost_car]);
//printf("time=%d\n",time);
if(time
{
time+=2*t+(wait_time[j*n+lost_car]-time);
if(j==m/n)
time-=t;//最后一次不用回去了
}
else//现在有车了
{
time+=2*t;
if(j==m/n)
time-=t;//最后一次不用回去了
}
//printf("此时时间是%d\n",time);
}
printf("%d %d\n",time,times);
}
}
相关文章推荐
- 正则表达式
- ios手势识别——旋转、捏合、滑动、拖动、长按
- 寻找超过一半的数字(编程之美)
- SVN There are unfinished transactions detected
- MySQL参数--init_connect
- 执古之道,以御今之有
- 减少函数变量的方法
- ZZ: windows 批处理文件bat中获取当前目录
- 图的割边(有缺陷的算法)
- myeclipse tomcat的启动缓慢问题----一次会加载多个项目
- Linux/centos用户管理及权限管理
- Android内存优化大全
- 中断上下文、进程上下文
- Posterior probability
- linux系统上amr转为mp3的问题
- scala中如何编写自定义的流程控制结构
- 产品经理那些事儿学习整理笔记
- [ActionScript&Flex] FlashBuilder编译条件之如何屏蔽调试代码
- 触发器、存储过程和函数之拾遗
- java 实现md5