SDAU课程练习1014

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.

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

题目大意:

现有m个车准备过河,渡船一次装n辆车,渡船过河一次时间为t,下面是车辆到达渡口的时间
表。求最短用时。

思路:

开始是用模拟写的,考虑的情况有点多。。导致wa好多次。其实问题可以简化一下。如果m小于n,那么等最后一辆车到达开走即可。如果m是n的倍数。那么次数就为m/n次。剩下的那个情况就先开走一趟,次数为m/n 1。判断最短用时也比较好计算,就判断渡船装满时的时间和装入的最后一辆车到达的时间比较。最后一次过河不用回来。这个题目要求保证次数和时间同时最小,不能单方面考虑,一开始就是因为没考虑次数最少错误的。

感想:

错了很多次,错到最后就不愿意看了,这就很说明问题啊。以后在写代码之前,应该多思考,全都弄明白了,敲代码行云流水多嗨皮啊。这样就不用把时间花在调试上面了。
AC代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int n,t,m;
int time[10000];

void clear() {
    memset(time,0,sizeof(time));
}
int main() {
    int Case;
    scanf("%d",&Case);
    while(Case--) {
        scanf("%d %d %d",&n,&t,&m);
        clear();
        int r = m%n;
        int k = m/n;
        int ans1 = 0,ans2;
        if(r)
            ans2 = k + 1;
        else
            ans2 = k;
        for(int i=1;i<=m;i++)
            scanf("%d",&time[i]);
        if(r)
            ans1 = time[r] + t * 2;
        int j = 1;
        while(j <= k) {
            if(ans1 >= time[j * n + r]) {
                ans1 += t * 2;
                if(j == k)
                    ans1 -= t;
            }
            else {
                ans1 = time[j * n + r] + t * 2;
                if(j == k)
                    ans1 -= t;
            }
            j++;
        }
        printf("%d %d\n",ans1,ans2);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值